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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.hadoop
.hbase
;
20 import static org
.junit
.Assert
.assertEquals
;
21 import static org
.junit
.Assert
.assertNotNull
;
22 import static org
.junit
.Assert
.assertTrue
;
24 import java
.util
.List
;
25 import java
.util
.concurrent
.TimeUnit
;
26 import org
.apache
.hadoop
.hbase
.Waiter
.ExplainingPredicate
;
27 import org
.apache
.hadoop
.hbase
.client
.AsyncConnection
;
28 import org
.apache
.hadoop
.hbase
.client
.ColumnFamilyDescriptorBuilder
;
29 import org
.apache
.hadoop
.hbase
.client
.ConnectionFactory
;
30 import org
.apache
.hadoop
.hbase
.client
.RegionInfo
;
31 import org
.apache
.hadoop
.hbase
.client
.TableDescriptor
;
32 import org
.apache
.hadoop
.hbase
.client
.TableDescriptorBuilder
;
33 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
34 import org
.apache
.hadoop
.hbase
.testclassification
.MiscTests
;
35 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
36 import org
.junit
.AfterClass
;
37 import org
.junit
.BeforeClass
;
38 import org
.junit
.ClassRule
;
39 import org
.junit
.Test
;
40 import org
.junit
.experimental
.categories
.Category
;
42 @Category({ MiscTests
.class, MediumTests
.class })
43 public class TestSplitMerge
{
46 public static final HBaseClassTestRule CLASS_RULE
=
47 HBaseClassTestRule
.forClass(TestSplitMerge
.class);
49 private static final HBaseTestingUtil UTIL
= new HBaseTestingUtil();
52 public static void setUp() throws Exception
{
53 UTIL
.getConfiguration().setInt(HConstants
.HBASE_CLIENT_META_OPERATION_TIMEOUT
, 1000);
54 UTIL
.getConfiguration().setInt(HConstants
.HBASE_CLIENT_RETRIES_NUMBER
, 2);
55 UTIL
.startMiniCluster(1);
59 public static void tearDown() throws Exception
{
60 UTIL
.shutdownMiniCluster();
64 public void test() throws Exception
{
65 TableName tableName
= TableName
.valueOf("SplitMerge");
66 byte[] family
= Bytes
.toBytes("CF");
67 TableDescriptor td
= TableDescriptorBuilder
.newBuilder(tableName
)
68 .setColumnFamily(ColumnFamilyDescriptorBuilder
.of(family
)).build();
69 UTIL
.getAdmin().createTable(td
, new byte[][] { Bytes
.toBytes(1) });
70 UTIL
.waitTableAvailable(tableName
);
71 UTIL
.getAdmin().split(tableName
, Bytes
.toBytes(2));
72 UTIL
.waitFor(30000, new ExplainingPredicate
<Exception
>() {
75 public boolean evaluate() throws Exception
{
76 return UTIL
.getMiniHBaseCluster().getRegions(tableName
).size() == 3;
80 public String
explainFailure() throws Exception
{
81 return "Split has not finished yet";
84 UTIL
.waitUntilNoRegionsInTransition();
85 RegionInfo regionA
= null;
86 RegionInfo regionB
= null;
87 for (RegionInfo region
: UTIL
.getAdmin().getRegions(tableName
)) {
88 if (region
.getStartKey().length
== 0) {
90 } else if (Bytes
.equals(region
.getStartKey(), Bytes
.toBytes(1))) {
94 assertNotNull(regionA
);
95 assertNotNull(regionB
);
96 UTIL
.getAdmin().mergeRegionsAsync(regionA
.getRegionName(), regionB
.getRegionName(), false)
97 .get(30, TimeUnit
.SECONDS
);
98 assertEquals(2, UTIL
.getAdmin().getRegions(tableName
).size());
100 ServerName expected
= UTIL
.getMiniHBaseCluster().getRegionServer(0).getServerName();
101 assertEquals(expected
, UTIL
.getConnection().getRegionLocator(tableName
)
102 .getRegionLocation(Bytes
.toBytes(1), true).getServerName());
103 try (AsyncConnection asyncConn
=
104 ConnectionFactory
.createAsyncConnection(UTIL
.getConfiguration()).get()) {
105 assertEquals(expected
, asyncConn
.getRegionLocator(tableName
)
106 .getRegionLocation(Bytes
.toBytes(1), true).get().getServerName());
111 public void testMergeRegionOrder() throws Exception
{
114 TableName tableName
= TableName
.valueOf("MergeRegionOrder");
115 byte[] family
= Bytes
.toBytes("CF");
116 TableDescriptor td
= TableDescriptorBuilder
.newBuilder(tableName
)
117 .setColumnFamily(ColumnFamilyDescriptorBuilder
.of(family
)).build();
119 byte[][] splitKeys
= new byte[regionCount
-1][];
121 for (int c
= 0; c
< regionCount
-1; c
++) {
122 splitKeys
[c
] = Bytes
.toBytes(c
+1 * 1000);
125 UTIL
.getAdmin().createTable(td
, splitKeys
);
126 UTIL
.waitTableAvailable(tableName
);
128 List
<RegionInfo
> regions
= UTIL
.getAdmin().getRegions(tableName
);
130 byte[][] regionNames
= new byte[regionCount
][];
131 for (int c
= 0; c
< regionCount
; c
++) {
132 regionNames
[c
] = regions
.get(c
).getRegionName();
135 UTIL
.getAdmin().mergeRegionsAsync(regionNames
, false).get(60, TimeUnit
.SECONDS
);
137 List
<RegionInfo
> mergedRegions
=
138 MetaTableAccessor
.getTableRegions(UTIL
.getConnection(), tableName
);
140 assertEquals(1, mergedRegions
.size());
142 RegionInfo mergedRegion
= mergedRegions
.get(0);
144 List
<RegionInfo
> mergeParentRegions
= UTIL
.getMiniHBaseCluster().getMaster()
145 .getAssignmentManager().getRegionStateStore().getMergeRegions(mergedRegion
);
147 assertEquals(mergeParentRegions
.size(), regionCount
);
149 for (int c
= 0; c
< regionCount
- 1; c
++) {
150 assertTrue(Bytes
.compareTo(mergeParentRegions
.get(c
).getStartKey(),
151 mergeParentRegions
.get(c
+ 1).getStartKey()) < 0);