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.client;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertTrue;
022import static org.junit.Assert.fail;
023
024import java.util.List;
025import java.util.concurrent.ExecutionException;
026import java.util.concurrent.Future;
027import java.util.concurrent.TimeUnit;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseTestingUtility;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.Waiter.ExplainingPredicate;
032import org.apache.hadoop.hbase.testclassification.ClientTests;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.apache.hadoop.hbase.util.Threads;
036import org.junit.AfterClass;
037import org.junit.BeforeClass;
038import org.junit.ClassRule;
039import org.junit.Rule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042import org.junit.rules.TestName;
043
044@Category({ MediumTests.class, ClientTests.class })
045public class TestSplitOrMergeAtTableLevel {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049      HBaseClassTestRule.forClass(TestSplitOrMergeAtTableLevel.class);
050
051  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
052  private static byte[] FAMILY = Bytes.toBytes("testFamily");
053
054  @Rule
055  public TestName name = new TestName();
056  private static Admin admin;
057
058  @BeforeClass
059  public static void setUpBeforeClass() throws Exception {
060    TEST_UTIL.startMiniCluster(2);
061    admin = TEST_UTIL.getAdmin();
062  }
063
064  @AfterClass
065  public static void tearDownAfterClass() throws Exception {
066    TEST_UTIL.shutdownMiniCluster();
067  }
068
069  @Test
070  public void testTableSplitSwitch() throws Exception {
071    final TableName tableName = TableName.valueOf(name.getMethodName());
072    TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tableName)
073        .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY))
074        .setSplitEnabled(false).build();
075
076    // create a table with split disabled
077    Table t = TEST_UTIL.createTable(tableDesc, null);
078    TEST_UTIL.waitTableAvailable(tableName);
079
080    // load data into the table
081    TEST_UTIL.loadTable(t, FAMILY, false);
082
083    assertTrue(admin.getRegions(tableName).size() == 1);
084
085    // check that we have split disabled
086    assertFalse(admin.getDescriptor(tableName).isSplitEnabled());
087    trySplitAndEnsureItFails(tableName);
088    enableTableSplit(tableName);
089    trySplitAndEnsureItIsSuccess(tableName);
090  }
091
092  @Test
093  public void testTableSplitSwitchForPreSplittedTable() throws Exception {
094    final TableName tableName = TableName.valueOf(name.getMethodName());
095
096    // create a table with split disabled
097    TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tableName)
098        .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY))
099        .setSplitEnabled(false)
100        .build();
101    Table t = TEST_UTIL.createTable(tableDesc, new byte[][] { Bytes.toBytes(10) });
102    TEST_UTIL.waitTableAvailable(tableName);
103
104    // load data into the table
105    TEST_UTIL.loadTable(t, FAMILY, false);
106
107    assertTrue(admin.getRegions(tableName).size() == 2);
108
109    // check that we have split disabled
110    assertFalse(admin.getDescriptor(tableName).isSplitEnabled());
111    trySplitAndEnsureItFails(tableName);
112    enableTableSplit(tableName);
113    trySplitAndEnsureItIsSuccess(tableName);
114  }
115
116  @Test
117  public void testTableMergeSwitch() throws Exception {
118    final TableName tableName = TableName.valueOf(name.getMethodName());
119
120    TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tableName)
121        .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY))
122        .setMergeEnabled(false)
123        .build();
124
125    Table t = TEST_UTIL.createTable(tableDesc, null);
126    TEST_UTIL.waitTableAvailable(tableName);
127    TEST_UTIL.loadTable(t, FAMILY, false);
128
129    // check merge is disabled for the table
130    assertFalse(admin.getDescriptor(tableName).isMergeEnabled());
131
132    trySplitAndEnsureItIsSuccess(tableName);
133    Threads.sleep(10000);
134    tryMergeAndEnsureItFails(tableName);
135    admin.disableTable(tableName);
136    enableTableMerge(tableName);
137    admin.enableTable(tableName);
138    tryMergeAndEnsureItIsSuccess(tableName);
139  }
140
141  @Test
142  public void testTableMergeSwitchForPreSplittedTable() throws Exception {
143    final TableName tableName = TableName.valueOf(name.getMethodName());
144
145    TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tableName)
146        .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY))
147        .setMergeEnabled(false)
148        .build();
149
150    Table t = TEST_UTIL.createTable(tableDesc, new byte[][] { Bytes.toBytes(10) });
151    TEST_UTIL.waitTableAvailable(tableName);
152    TEST_UTIL.loadTable(t, FAMILY, false);
153
154    // check merge is disabled for the table
155    assertFalse(admin.getDescriptor(tableName).isMergeEnabled());
156    assertTrue(admin.getRegions(tableName).size() == 2);
157    tryMergeAndEnsureItFails(tableName);
158    enableTableMerge(tableName);
159    tryMergeAndEnsureItIsSuccess(tableName);
160  }
161
162  private void trySplitAndEnsureItFails(final TableName tableName) throws Exception {
163    // get the original table region count
164    List<RegionInfo> regions = admin.getRegions(tableName);
165    int originalCount = regions.size();
166
167    // split the table and make sure region count does not increase
168    Future<?> f = admin.splitRegionAsync(regions.get(0).getEncodedNameAsBytes(), Bytes.toBytes(2));
169    try {
170      f.get(10, TimeUnit.SECONDS);
171      fail("Should not get here.");
172    } catch (ExecutionException ee) {
173      // expected to reach here
174      // check and ensure that table does not get splitted
175      assertTrue(admin.getRegions(tableName).size() == originalCount);
176    }
177  }
178
179  /**
180   * Method to enable split for the passed table and validate this modification.
181   * @param tableName name of the table
182   */
183  private void enableTableSplit(final TableName tableName) throws Exception {
184    // Get the original table descriptor
185    TableDescriptor originalTableDesc = admin.getDescriptor(tableName);
186    TableDescriptor modifiedTableDesc = TableDescriptorBuilder.newBuilder(originalTableDesc)
187        .setSplitEnabled(true)
188        .build();
189
190    // Now modify the table descriptor and enable split for it
191    admin.modifyTable(modifiedTableDesc);
192
193    // Verify that split is enabled
194    assertTrue(admin.getDescriptor(tableName).isSplitEnabled());
195  }
196
197  private void trySplitAndEnsureItIsSuccess(final TableName tableName)
198      throws Exception {
199    // get the original table region count
200    List<RegionInfo> regions = admin.getRegions(tableName);
201    int originalCount = regions.size();
202
203    // split the table and wait until region count increases
204    admin.split(tableName, Bytes.toBytes(3));
205    TEST_UTIL.waitFor(30000, new ExplainingPredicate<Exception>() {
206
207      @Override
208      public boolean evaluate() throws Exception {
209        return admin.getRegions(tableName).size() > originalCount;
210      }
211
212      @Override
213      public String explainFailure() throws Exception {
214        return "Split has not finished yet";
215      }
216    });
217  }
218
219  private void tryMergeAndEnsureItFails(final TableName tableName) throws Exception {
220    // assert we have at least 2 regions in the table
221    List<RegionInfo> regions = admin.getRegions(tableName);
222    int originalCount = regions.size();
223    assertTrue(originalCount >= 2);
224
225    byte[] nameOfRegionA = regions.get(0).getEncodedNameAsBytes();
226    byte[] nameOfRegionB = regions.get(1).getEncodedNameAsBytes();
227
228    // check and ensure that region do not get merged
229    Future<?> f = admin.mergeRegionsAsync(nameOfRegionA, nameOfRegionB, true);
230    try {
231      f.get(10, TimeUnit.SECONDS);
232      fail("Should not get here.");
233    } catch (ExecutionException ee) {
234      // expected to reach here
235      // check and ensure that region do not get merged
236      assertTrue(admin.getRegions(tableName).size() == originalCount);
237    }
238  }
239
240
241  /**
242   * Method to enable merge for the passed table and validate this modification.
243   * @param tableName name of the table
244   */
245  private void enableTableMerge(final TableName tableName) throws Exception {
246    // Get the original table descriptor
247    TableDescriptor originalTableDesc = admin.getDescriptor(tableName);
248    TableDescriptor modifiedTableDesc = TableDescriptorBuilder.newBuilder(originalTableDesc)
249        .setMergeEnabled(true)
250        .build();
251
252    // Now modify the table descriptor and enable merge for it
253    admin.modifyTable(modifiedTableDesc);
254
255    // Verify that merge is enabled
256    assertTrue(admin.getDescriptor(tableName).isMergeEnabled());
257  }
258
259  private void tryMergeAndEnsureItIsSuccess(final TableName tableName) throws Exception {
260    // assert we have at least 2 regions in the table
261    List<RegionInfo> regions = admin.getRegions(tableName);
262    int originalCount = regions.size();
263    assertTrue(originalCount >= 2);
264
265    byte[] nameOfRegionA = regions.get(0).getEncodedNameAsBytes();
266    byte[] nameOfRegionB = regions.get(1).getEncodedNameAsBytes();
267
268    // merge the table regions and wait until region count decreases
269    admin.mergeRegionsAsync(nameOfRegionA, nameOfRegionB, true);
270    TEST_UTIL.waitFor(30000, new ExplainingPredicate<Exception>() {
271
272      @Override
273      public boolean evaluate() throws Exception {
274        return admin.getRegions(tableName).size() < originalCount;
275      }
276
277      @Override
278      public String explainFailure() throws Exception {
279        return "Merge has not finished yet";
280      }
281    });
282  }
283}