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 static org.junit.Assert.assertEquals;
021
022import java.util.Collection;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.fs.FileSystem;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.HColumnDescriptor;
029import org.apache.hadoop.hbase.HTableDescriptor;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.Connection;
032import org.apache.hadoop.hbase.client.ConnectionFactory;
033import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.apache.hadoop.hbase.util.CommonFSUtils;
037import org.junit.After;
038import org.junit.AfterClass;
039import org.junit.Assert;
040import org.junit.BeforeClass;
041import org.junit.ClassRule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044import org.slf4j.Logger;
045import org.slf4j.LoggerFactory;
046
047@Category(MediumTests.class)
048public class TestCompactSplitThread {
049
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052    HBaseClassTestRule.forClass(TestCompactSplitThread.class);
053
054  private static final Logger LOG = LoggerFactory.getLogger(TestCompactSplitThread.class);
055  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
056  private final TableName tableName = TableName.valueOf(getClass().getSimpleName());
057  private final byte[] family = Bytes.toBytes("f");
058  private static final int NUM_RS = 1;
059  private static final int blockingStoreFiles = 3;
060  private static Path rootDir;
061  private static FileSystem fs;
062
063  /**
064   * Setup the config for the cluster
065   */
066  @BeforeClass
067  public static void setupCluster() throws Exception {
068    setupConf(TEST_UTIL.getConfiguration());
069    TEST_UTIL.startMiniCluster(NUM_RS);
070    fs = TEST_UTIL.getDFSCluster().getFileSystem();
071    rootDir = TEST_UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
072
073  }
074
075  private static void setupConf(Configuration conf) {
076    // disable the ui
077    conf.setInt("hbase.regionsever.info.port", -1);
078    // so make sure we get a compaction when doing a load, but keep around some
079    // files in the store
080    conf.setInt("hbase.hstore.compaction.min", 2);
081    conf.setInt("hbase.hstore.compactionThreshold", 5);
082    // change the flush size to a small amount, regulating number of store files
083    conf.setInt("hbase.hregion.memstore.flush.size", 25000);
084
085    // block writes if we get to blockingStoreFiles store files
086    conf.setInt("hbase.hstore.blockingStoreFiles", blockingStoreFiles);
087    // Ensure no extra cleaners on by default (e.g. TimeToLiveHFileCleaner)
088    conf.setInt(CompactSplit.LARGE_COMPACTION_THREADS, 3);
089    conf.setInt(CompactSplit.SMALL_COMPACTION_THREADS, 4);
090    conf.setInt(CompactSplit.SPLIT_THREADS, 5);
091  }
092
093  @After
094  public void tearDown() throws Exception {
095    TEST_UTIL.deleteTable(tableName);
096  }
097
098  @AfterClass
099  public static void cleanupTest() throws Exception {
100    try {
101      TEST_UTIL.shutdownMiniCluster();
102    } catch (Exception e) {
103      // NOOP;
104    }
105  }
106
107  @Test
108  public void testThreadPoolSizeTuning() throws Exception {
109    Configuration conf = TEST_UTIL.getConfiguration();
110    Connection conn = ConnectionFactory.createConnection(conf);
111    try {
112      HTableDescriptor htd = new HTableDescriptor(tableName);
113      htd.addFamily(new HColumnDescriptor(family));
114      htd.setCompactionEnabled(false);
115      TEST_UTIL.getAdmin().createTable(htd);
116      TEST_UTIL.waitTableAvailable(tableName);
117      HRegionServer regionServer = TEST_UTIL.getRSForFirstRegionInTable(tableName);
118
119      // check initial configuration of thread pool sizes
120      assertEquals(3, regionServer.getCompactSplitThread().getLargeCompactionThreadNum());
121      assertEquals(4, regionServer.getCompactSplitThread().getSmallCompactionThreadNum());
122      assertEquals(5, regionServer.getCompactSplitThread().getSplitThreadNum());
123
124      // change bigger configurations and do online update
125      conf.setInt(CompactSplit.LARGE_COMPACTION_THREADS, 4);
126      conf.setInt(CompactSplit.SMALL_COMPACTION_THREADS, 5);
127      conf.setInt(CompactSplit.SPLIT_THREADS, 6);
128      try {
129        regionServer.getCompactSplitThread().onConfigurationChange(conf);
130      } catch (IllegalArgumentException iae) {
131        Assert.fail("Update bigger configuration failed!");
132      }
133
134      // check again after online update
135      assertEquals(4, regionServer.getCompactSplitThread().getLargeCompactionThreadNum());
136      assertEquals(5, regionServer.getCompactSplitThread().getSmallCompactionThreadNum());
137      assertEquals(6, regionServer.getCompactSplitThread().getSplitThreadNum());
138
139      // change smaller configurations and do online update
140      conf.setInt(CompactSplit.LARGE_COMPACTION_THREADS, 2);
141      conf.setInt(CompactSplit.SMALL_COMPACTION_THREADS, 3);
142      conf.setInt(CompactSplit.SPLIT_THREADS, 4);
143      try {
144        regionServer.getCompactSplitThread().onConfigurationChange(conf);
145      } catch (IllegalArgumentException iae) {
146        Assert.fail("Update smaller configuration failed!");
147      }
148
149      // check again after online update
150      assertEquals(2, regionServer.getCompactSplitThread().getLargeCompactionThreadNum());
151      assertEquals(3, regionServer.getCompactSplitThread().getSmallCompactionThreadNum());
152      assertEquals(4, regionServer.getCompactSplitThread().getSplitThreadNum());
153    } finally {
154      conn.close();
155    }
156  }
157
158  @Test
159  public void testFlushWithTableCompactionDisabled() throws Exception {
160    HTableDescriptor htd = new HTableDescriptor(tableName);
161    htd.setCompactionEnabled(false);
162    TEST_UTIL.createTable(htd, new byte[][] { family }, null);
163
164    // load the table
165    for (int i = 0; i < blockingStoreFiles + 1; i++) {
166      TEST_UTIL.loadTable(TEST_UTIL.getConnection().getTable(tableName), family);
167      TEST_UTIL.flush(tableName);
168    }
169
170    // Make sure that store file number is greater than blockingStoreFiles + 1
171    Path tableDir = CommonFSUtils.getTableDir(rootDir, tableName);
172    Collection<String> hfiles = SnapshotTestingUtils.listHFileNames(fs, tableDir);
173    assert (hfiles.size() > blockingStoreFiles + 1);
174  }
175}