View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertTrue;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.testclassification.MediumTests;
32  import org.apache.hadoop.hbase.client.HTable;
33  import org.apache.hadoop.hbase.regionserver.compactions.CompactionConfiguration;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.junit.AfterClass;
36  import org.junit.BeforeClass;
37  import org.junit.Test;
38  import org.junit.experimental.categories.Category;
39  
40  import java.io.IOException;
41  
42  /**
43   * Verify that the Online config Changes on the HRegionServer side are actually
44   * happening. We should add tests for important configurations which will be
45   * changed online.
46   */
47  
48  @Category({MediumTests.class})
49  public class TestRegionServerOnlineConfigChange {
50    private static final Log LOG =
51            LogFactory.getLog(TestRegionServerOnlineConfigChange.class.getName());
52    private static HBaseTestingUtility hbaseTestingUtility = new HBaseTestingUtility();
53    private static Configuration conf = null;
54  
55    private static HTable t1 = null;
56    private static HRegionServer rs1 = null;
57    private static byte[] r1name = null;
58    private static Region r1 = null;
59  
60    private final static String table1Str = "table1";
61    private final static String columnFamily1Str = "columnFamily1";
62    private final static byte[] TABLE1 = Bytes.toBytes(table1Str);
63    private final static byte[] COLUMN_FAMILY1 = Bytes.toBytes(columnFamily1Str);
64  
65  
66    @BeforeClass
67    public static void setUp() throws Exception {
68      conf = hbaseTestingUtility.getConfiguration();
69      hbaseTestingUtility.startMiniCluster(1,1);
70      t1 = hbaseTestingUtility.createTable(TABLE1, COLUMN_FAMILY1);
71      @SuppressWarnings("deprecation")
72      HRegionInfo firstHRI = t1.getRegionLocations().keySet().iterator().next();
73      r1name = firstHRI.getRegionName();
74      rs1 = hbaseTestingUtility.getHBaseCluster().getRegionServer(
75          hbaseTestingUtility.getHBaseCluster().getServerWith(r1name));
76      r1 = rs1.getRegion(r1name);
77    }
78  
79    @AfterClass
80    public static void tearDown() throws Exception {
81      hbaseTestingUtility.shutdownMiniCluster();
82    }
83  
84    /**
85     * Check if the number of compaction threads changes online
86     * @throws IOException
87     */
88    @Test
89    public void testNumCompactionThreadsOnlineChange() throws IOException {
90      assertTrue(rs1.compactSplitThread != null);
91      int newNumSmallThreads =
92              rs1.compactSplitThread.getSmallCompactionThreadNum() + 1;
93      int newNumLargeThreads =
94              rs1.compactSplitThread.getLargeCompactionThreadNum() + 1;
95  
96      conf.setInt("hbase.regionserver.thread.compaction.small",
97              newNumSmallThreads);
98      conf.setInt("hbase.regionserver.thread.compaction.large",
99              newNumLargeThreads);
100     rs1.getConfigurationManager().notifyAllObservers(conf);
101 
102     assertEquals(newNumSmallThreads,
103                   rs1.compactSplitThread.getSmallCompactionThreadNum());
104     assertEquals(newNumLargeThreads,
105                   rs1.compactSplitThread.getLargeCompactionThreadNum());
106   }
107 
108   /**
109    * Test that the configurations in the CompactionConfiguration class change
110    * properly.
111    *
112    * @throws IOException
113    */
114   @Test
115   public void testCompactionConfigurationOnlineChange() throws IOException {
116     String strPrefix = "hbase.hstore.compaction.";
117     Store s = r1.getStore(COLUMN_FAMILY1);
118     if (!(s instanceof HStore)) {
119       LOG.error("Can't test the compaction configuration of HStore class. "
120           + "Got a different implementation other than HStore");
121       return;
122     }
123     HStore hstore = (HStore)s;
124 
125     // Set the new compaction ratio to a different value.
126     double newCompactionRatio =
127             hstore.getStoreEngine().getCompactionPolicy().getConf().getCompactionRatio() + 0.1;
128     conf.setFloat(strPrefix + "ratio", (float)newCompactionRatio);
129 
130     // Notify all the observers, which includes the Store object.
131     rs1.getConfigurationManager().notifyAllObservers(conf);
132 
133     // Check if the compaction ratio got updated in the Compaction Configuration
134     assertEquals(newCompactionRatio,
135                  hstore.getStoreEngine().getCompactionPolicy().getConf().getCompactionRatio(),
136                  0.00001);
137 
138     // Check if the off peak compaction ratio gets updated.
139     double newOffPeakCompactionRatio =
140         hstore.getStoreEngine().getCompactionPolicy().getConf().getCompactionRatioOffPeak() + 0.1;
141     conf.setFloat(strPrefix + "ratio.offpeak",
142             (float)newOffPeakCompactionRatio);
143     rs1.getConfigurationManager().notifyAllObservers(conf);
144     assertEquals(newOffPeakCompactionRatio,
145         hstore.getStoreEngine().getCompactionPolicy().getConf().getCompactionRatioOffPeak(),
146                  0.00001);
147 
148     // Check if the throttle point gets updated.
149     long newThrottlePoint =
150         hstore.getStoreEngine().getCompactionPolicy().getConf().getThrottlePoint() + 10;
151     conf.setLong("hbase.regionserver.thread.compaction.throttle",
152                   newThrottlePoint);
153     rs1.getConfigurationManager().notifyAllObservers(conf);
154     assertEquals(newThrottlePoint,
155         hstore.getStoreEngine().getCompactionPolicy().getConf().getThrottlePoint());
156 
157     // Check if the minFilesToCompact gets updated.
158     int newMinFilesToCompact =
159             hstore.getStoreEngine().getCompactionPolicy().getConf().getMinFilesToCompact() + 1;
160     conf.setLong(strPrefix + "min", newMinFilesToCompact);
161     rs1.getConfigurationManager().notifyAllObservers(conf);
162     assertEquals(newMinFilesToCompact,
163         hstore.getStoreEngine().getCompactionPolicy().getConf().getMinFilesToCompact());
164 
165     // Check if the maxFilesToCompact gets updated.
166     int newMaxFilesToCompact =
167             hstore.getStoreEngine().getCompactionPolicy().getConf().getMaxFilesToCompact() + 1;
168     conf.setLong(strPrefix + "max", newMaxFilesToCompact);
169     rs1.getConfigurationManager().notifyAllObservers(conf);
170     assertEquals(newMaxFilesToCompact,
171         hstore.getStoreEngine().getCompactionPolicy().getConf().getMaxFilesToCompact());
172 
173     // Check OffPeak hours is updated in an online fashion.
174     conf.setLong(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_START_HOUR, 6);
175     conf.setLong(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_END_HOUR, 7);
176     rs1.getConfigurationManager().notifyAllObservers(conf);
177     assertFalse(hstore.getOffPeakHours().isOffPeakHour(4));
178 
179     // Check if the minCompactSize gets updated.
180     long newMinCompactSize =
181             hstore.getStoreEngine().getCompactionPolicy().getConf().getMinCompactSize() + 1;
182     conf.setLong(strPrefix + "min.size", newMinCompactSize);
183     rs1.getConfigurationManager().notifyAllObservers(conf);
184     assertEquals(newMinCompactSize,
185                  hstore.getStoreEngine().getCompactionPolicy().getConf().getMinCompactSize());
186 
187     // Check if the maxCompactSize gets updated.
188     long newMaxCompactSize =
189             hstore.getStoreEngine().getCompactionPolicy().getConf().getMaxCompactSize() - 1;
190     conf.setLong(strPrefix + "max.size", newMaxCompactSize);
191     rs1.getConfigurationManager().notifyAllObservers(conf);
192     assertEquals(newMaxCompactSize,
193                  hstore.getStoreEngine().getCompactionPolicy().getConf().getMaxCompactSize());
194     // Check if the offPeakMaxCompactSize gets updated.
195     long newOffpeakMaxCompactSize =
196             hstore.getStoreEngine().getCompactionPolicy().getConf().getOffPeakMaxCompactSize() - 1;
197     conf.setLong(CompactionConfiguration.HBASE_HSTORE_COMPACTION_MAX_SIZE_OFFPEAK_KEY,
198       newOffpeakMaxCompactSize);
199     rs1.getConfigurationManager().notifyAllObservers(conf);
200     assertEquals(newOffpeakMaxCompactSize,
201                  hstore.getStoreEngine().getCompactionPolicy().getConf().getOffPeakMaxCompactSize());
202 
203     // Check if majorCompactionPeriod gets updated.
204     long newMajorCompactionPeriod =
205             hstore.getStoreEngine().getCompactionPolicy().getConf().getMajorCompactionPeriod() + 10;
206     conf.setLong(HConstants.MAJOR_COMPACTION_PERIOD, newMajorCompactionPeriod);
207     rs1.getConfigurationManager().notifyAllObservers(conf);
208     assertEquals(newMajorCompactionPeriod,
209             hstore.getStoreEngine().getCompactionPolicy().getConf().getMajorCompactionPeriod());
210 
211     // Check if majorCompactionJitter gets updated.
212     float newMajorCompactionJitter =
213         hstore.getStoreEngine().getCompactionPolicy().getConf().getMajorCompactionJitter() + 0.02F;
214     conf.setFloat("hbase.hregion.majorcompaction.jitter",
215                   newMajorCompactionJitter);
216     rs1.getConfigurationManager().notifyAllObservers(conf);
217     assertEquals(newMajorCompactionJitter,
218       hstore.getStoreEngine().getCompactionPolicy().getConf().getMajorCompactionJitter(), 0.00001);
219   }
220 }