001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.List;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.HColumnDescriptor;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.HRegionInfo;
031import org.apache.hadoop.hbase.HTableDescriptor;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.regionserver.compactions.CompactionConfiguration;
034import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequestImpl;
035import org.apache.hadoop.hbase.regionserver.compactions.RatioBasedCompactionPolicy;
036import org.apache.hadoop.hbase.regionserver.wal.FSHLog;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.apache.hadoop.hbase.util.CommonFSUtils;
039import org.junit.After;
040import org.junit.Assert;
041import org.junit.Before;
042import org.slf4j.Logger;
043import org.slf4j.LoggerFactory;
044
045import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
046
047public class TestCompactionPolicy {
048  private final static Logger LOG = LoggerFactory.getLogger(TestCompactionPolicy.class);
049  protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
050
051  protected Configuration conf;
052  protected HStore store;
053  private static final String DIR =
054    TEST_UTIL.getDataTestDir(TestCompactionPolicy.class.getSimpleName()).toString();
055  protected static Path TEST_FILE;
056  protected static final int minFiles = 3;
057  protected static final int maxFiles = 5;
058
059  protected static final long minSize = 10;
060  protected static final long maxSize = 2100;
061
062  private FSHLog hlog;
063  private HRegion region;
064
065  @Before
066  public void setUp() throws Exception {
067    config();
068    initialize();
069  }
070
071  /**
072   * setup config values necessary for store
073   */
074  protected void config() {
075    this.conf = TEST_UTIL.getConfiguration();
076    this.conf.setLong(HConstants.MAJOR_COMPACTION_PERIOD, 0);
077    this.conf.setInt(CompactionConfiguration.HBASE_HSTORE_COMPACTION_MIN_KEY, minFiles);
078    this.conf.setInt(CompactionConfiguration.HBASE_HSTORE_COMPACTION_MAX_KEY, maxFiles);
079    this.conf.setLong(CompactionConfiguration.HBASE_HSTORE_COMPACTION_MIN_SIZE_KEY, minSize);
080    this.conf.setLong(CompactionConfiguration.HBASE_HSTORE_COMPACTION_MAX_SIZE_KEY, maxSize);
081    this.conf.setFloat(CompactionConfiguration.HBASE_HSTORE_COMPACTION_RATIO_KEY, 1.0F);
082  }
083
084  /**
085   * Setting up a Store
086   * @throws IOException with error
087   */
088  protected void initialize() throws IOException {
089    Path basedir = new Path(DIR);
090    String logName = "logs";
091    Path logdir = new Path(DIR, logName);
092    HColumnDescriptor hcd = new HColumnDescriptor(Bytes.toBytes("family"));
093    FileSystem fs = FileSystem.get(conf);
094
095    fs.delete(logdir, true);
096
097    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(Bytes.toBytes("table")));
098    htd.addFamily(hcd);
099    HRegionInfo info = new HRegionInfo(htd.getTableName(), null, null, false);
100
101    hlog = new FSHLog(fs, basedir, logName, conf);
102    ChunkCreator.initialize(MemStoreLAB.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null,
103      MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT);
104    region = HRegion.createHRegion(info, basedir, conf, htd, hlog);
105    region.close();
106    Path tableDir = CommonFSUtils.getTableDir(basedir, htd.getTableName());
107    region = new HRegion(tableDir, hlog, fs, conf, info, htd, null);
108    store = new HStore(region, hcd, conf, false);
109
110    TEST_FILE = region.getRegionFileSystem().createTempName();
111    fs.createNewFile(TEST_FILE);
112  }
113
114  @After
115  public void tearDown() throws IOException {
116    IOException ex = null;
117    try {
118      region.close();
119    } catch (IOException e) {
120      LOG.warn("Caught Exception", e);
121      ex = e;
122    }
123    try {
124      hlog.close();
125    } catch (IOException e) {
126      LOG.warn("Caught Exception", e);
127      ex = e;
128    }
129    if (ex != null) {
130      throw ex;
131    }
132  }
133
134  ArrayList<Long> toArrayList(long... numbers) {
135    ArrayList<Long> result = new ArrayList<>();
136    for (long i : numbers) {
137      result.add(i);
138    }
139    return result;
140  }
141
142  List<HStoreFile> sfCreate(long... sizes) throws IOException {
143    ArrayList<Long> ageInDisk = new ArrayList<>();
144    for (int i = 0; i < sizes.length; i++) {
145      ageInDisk.add(0L);
146    }
147    return sfCreate(toArrayList(sizes), ageInDisk);
148  }
149
150  List<HStoreFile> sfCreate(ArrayList<Long> sizes, ArrayList<Long> ageInDisk) throws IOException {
151    return sfCreate(false, sizes, ageInDisk);
152  }
153
154  List<HStoreFile> sfCreate(boolean isReference, long... sizes) throws IOException {
155    ArrayList<Long> ageInDisk = new ArrayList<>(sizes.length);
156    for (int i = 0; i < sizes.length; i++) {
157      ageInDisk.add(0L);
158    }
159    return sfCreate(isReference, toArrayList(sizes), ageInDisk);
160  }
161
162  List<HStoreFile> sfCreate(boolean isReference, ArrayList<Long> sizes, ArrayList<Long> ageInDisk)
163    throws IOException {
164    List<HStoreFile> ret = Lists.newArrayList();
165    for (int i = 0; i < sizes.size(); i++) {
166      ret.add(
167        new MockHStoreFile(TEST_UTIL, TEST_FILE, sizes.get(i), ageInDisk.get(i), isReference, i));
168    }
169    return ret;
170  }
171
172  long[] getSizes(List<HStoreFile> sfList) {
173    long[] aNums = new long[sfList.size()];
174    for (int i = 0; i < sfList.size(); ++i) {
175      aNums[i] = sfList.get(i).getReader().length();
176    }
177    return aNums;
178  }
179
180  void compactEquals(List<HStoreFile> candidates, long... expected) throws IOException {
181    compactEquals(candidates, false, false, expected);
182  }
183
184  void compactEquals(List<HStoreFile> candidates, boolean forcemajor, long... expected)
185    throws IOException {
186    compactEquals(candidates, forcemajor, false, expected);
187  }
188
189  void compactEquals(List<HStoreFile> candidates, boolean forcemajor, boolean isOffPeak,
190    long... expected) throws IOException {
191    store.forceMajor = forcemajor;
192    // Test Default compactions
193    CompactionRequestImpl result =
194      ((RatioBasedCompactionPolicy) store.storeEngine.getCompactionPolicy())
195        .selectCompaction(candidates, new ArrayList<>(), false, isOffPeak, forcemajor);
196    List<HStoreFile> actual = new ArrayList<>(result.getFiles());
197    if (isOffPeak && !forcemajor) {
198      Assert.assertTrue(result.isOffPeak());
199    }
200    Assert.assertEquals(Arrays.toString(expected), Arrays.toString(getSizes(actual)));
201    store.forceMajor = false;
202  }
203}