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.HBaseTestingUtil;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
030import org.apache.hadoop.hbase.client.Connection;
031import org.apache.hadoop.hbase.client.ConnectionFactory;
032import org.apache.hadoop.hbase.client.TableDescriptor;
033import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
034import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.apache.hadoop.hbase.util.CommonFSUtils;
038import org.junit.After;
039import org.junit.Assert;
040import org.junit.Before;
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 HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
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  @Before
067  public 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 cleanupTest() throws Exception {
095    try {
096      TEST_UTIL.shutdownMiniCluster();
097    } catch (Exception e) {
098      // NOOP;
099    }
100  }
101
102  @Test
103  public void testThreadPoolSizeTuning() throws Exception {
104    Configuration conf = TEST_UTIL.getConfiguration();
105    Connection conn = ConnectionFactory.createConnection(conf);
106    try {
107      TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
108        .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).setCompactionEnabled(false)
109        .build();
110      TEST_UTIL.getAdmin().createTable(tableDescriptor);
111      TEST_UTIL.waitTableAvailable(tableName);
112      HRegionServer regionServer = TEST_UTIL.getRSForFirstRegionInTable(tableName);
113
114      // check initial configuration of thread pool sizes
115      assertEquals(3, regionServer.getCompactSplitThread().getLargeCompactionThreadNum());
116      assertEquals(4, regionServer.getCompactSplitThread().getSmallCompactionThreadNum());
117      assertEquals(5, regionServer.getCompactSplitThread().getSplitThreadNum());
118
119      // change bigger configurations and do online update
120      conf.setInt(CompactSplit.LARGE_COMPACTION_THREADS, 4);
121      conf.setInt(CompactSplit.SMALL_COMPACTION_THREADS, 5);
122      conf.setInt(CompactSplit.SPLIT_THREADS, 6);
123      try {
124        regionServer.getCompactSplitThread().onConfigurationChange(conf);
125      } catch (IllegalArgumentException iae) {
126        Assert.fail("Update bigger configuration failed!");
127      }
128
129      // check again after online update
130      assertEquals(4, regionServer.getCompactSplitThread().getLargeCompactionThreadNum());
131      assertEquals(5, regionServer.getCompactSplitThread().getSmallCompactionThreadNum());
132      assertEquals(6, regionServer.getCompactSplitThread().getSplitThreadNum());
133
134      // change smaller configurations and do online update
135      conf.setInt(CompactSplit.LARGE_COMPACTION_THREADS, 2);
136      conf.setInt(CompactSplit.SMALL_COMPACTION_THREADS, 3);
137      conf.setInt(CompactSplit.SPLIT_THREADS, 4);
138      try {
139        regionServer.getCompactSplitThread().onConfigurationChange(conf);
140      } catch (IllegalArgumentException iae) {
141        Assert.fail("Update smaller configuration failed!");
142      }
143
144      // check again after online update
145      assertEquals(2, regionServer.getCompactSplitThread().getLargeCompactionThreadNum());
146      assertEquals(3, regionServer.getCompactSplitThread().getSmallCompactionThreadNum());
147      assertEquals(4, regionServer.getCompactSplitThread().getSplitThreadNum());
148    } finally {
149      conn.close();
150    }
151  }
152
153  @Test
154  public void testFlushWithTableCompactionDisabled() throws Exception {
155    TableDescriptor htd =
156      TableDescriptorBuilder.newBuilder(tableName).setCompactionEnabled(false).build();
157    TEST_UTIL.createTable(htd, new byte[][] { family }, null);
158
159    // load the table
160    for (int i = 0; i < blockingStoreFiles + 1; i++) {
161      TEST_UTIL.loadTable(TEST_UTIL.getConnection().getTable(tableName), family);
162      TEST_UTIL.flush(tableName);
163    }
164
165    // Make sure that store file number is greater than blockingStoreFiles + 1
166    Path tableDir = CommonFSUtils.getTableDir(rootDir, tableName);
167    Collection<String> hfiles = SnapshotTestingUtils.listHFileNames(fs, tableDir);
168    assert (hfiles.size() > blockingStoreFiles + 1);
169  }
170
171  @Test
172  public void testFlushWithRegionReplicas() throws Exception {
173    TableDescriptor htd =
174      TableDescriptorBuilder.newBuilder(tableName).setRegionReplication(2).build();
175    TEST_UTIL.createTable(htd, new byte[][] { family }, null);
176
177    // load the table
178    for (int i = 0; i < blockingStoreFiles + 1; i++) {
179      TEST_UTIL.loadTable(TEST_UTIL.getConnection().getTable(tableName), family);
180      TEST_UTIL.flush(tableName);
181    }
182
183    // One region split should have taken place, because the primary replica gets split, and not the
184    // secondary replica.
185    assertEquals(1, TEST_UTIL.getRSForFirstRegionInTable(tableName).getCompactSplitThread()
186      .getSubmittedSplitsCount());
187  }
188}