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; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertNotNull; 022 023import java.util.concurrent.TimeUnit; 024import org.apache.hadoop.hbase.Waiter.ExplainingPredicate; 025import org.apache.hadoop.hbase.client.AsyncConnection; 026import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 027import org.apache.hadoop.hbase.client.ConnectionFactory; 028import org.apache.hadoop.hbase.client.RegionInfo; 029import org.apache.hadoop.hbase.client.TableDescriptor; 030import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 031import org.apache.hadoop.hbase.testclassification.MediumTests; 032import org.apache.hadoop.hbase.testclassification.MiscTests; 033import org.apache.hadoop.hbase.util.Bytes; 034import org.junit.AfterClass; 035import org.junit.BeforeClass; 036import org.junit.ClassRule; 037import org.junit.Test; 038import org.junit.experimental.categories.Category; 039 040@Category({ MiscTests.class, MediumTests.class }) 041public class TestSplitMerge { 042 043 @ClassRule 044 public static final HBaseClassTestRule CLASS_RULE = 045 HBaseClassTestRule.forClass(TestSplitMerge.class); 046 047 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); 048 049 @BeforeClass 050 public static void setUp() throws Exception { 051 UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_META_OPERATION_TIMEOUT, 1000); 052 UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2); 053 UTIL.startMiniCluster(1); 054 } 055 056 @AfterClass 057 public static void tearDown() throws Exception { 058 UTIL.shutdownMiniCluster(); 059 } 060 061 @Test 062 public void test() throws Exception { 063 TableName tableName = TableName.valueOf("SplitMerge"); 064 byte[] family = Bytes.toBytes("CF"); 065 TableDescriptor td = TableDescriptorBuilder.newBuilder(tableName) 066 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build(); 067 UTIL.getAdmin().createTable(td, new byte[][] { Bytes.toBytes(1) }); 068 UTIL.waitTableAvailable(tableName); 069 UTIL.getAdmin().split(tableName, Bytes.toBytes(2)); 070 UTIL.waitFor(30000, new ExplainingPredicate<Exception>() { 071 072 @Override 073 public boolean evaluate() throws Exception { 074 return UTIL.getMiniHBaseCluster().getRegions(tableName).size() == 3; 075 } 076 077 @Override 078 public String explainFailure() throws Exception { 079 return "Split has not finished yet"; 080 } 081 }); 082 RegionInfo regionA = null; 083 RegionInfo regionB = null; 084 for (RegionInfo region : UTIL.getAdmin().getRegions(tableName)) { 085 if (region.getStartKey().length == 0) { 086 regionA = region; 087 } else if (Bytes.equals(region.getStartKey(), Bytes.toBytes(1))) { 088 regionB = region; 089 } 090 } 091 assertNotNull(regionA); 092 assertNotNull(regionB); 093 UTIL.getAdmin().mergeRegionsAsync(regionA.getRegionName(), regionB.getRegionName(), false) 094 .get(30, TimeUnit.SECONDS); 095 assertEquals(2, UTIL.getAdmin().getRegions(tableName).size()); 096 097 ServerName expected = UTIL.getMiniHBaseCluster().getRegionServer(0).getServerName(); 098 assertEquals(expected, UTIL.getConnection().getRegionLocator(tableName) 099 .getRegionLocation(Bytes.toBytes(1), true).getServerName()); 100 try (AsyncConnection asyncConn = 101 ConnectionFactory.createAsyncConnection(UTIL.getConfiguration()).get()) { 102 assertEquals(expected, asyncConn.getRegionLocator(tableName) 103 .getRegionLocation(Bytes.toBytes(1), true).get().getServerName()); 104 } 105 } 106}