@Test(timeout = 180000)
public void testColocationAfterRegionsMerge() throws Exception {
MiniHBaseCluster cluster = UTIL.getHBaseCluster();
HMaster master = cluster.getMaster();
RegionStates regionStates = master.getAssignmentManager().getRegionStates();
// Table names to make use of the
TableName tableName = TableName.valueOf("testColocationAfterRegionsMerge");
TableName indexTableName = TableName.valueOf("testColocationAfterRegionsMerge_index");
createUserAndIndexTable(tableName, indexTableName);
ServerName server = cluster.getRegionServer(0).getServerName();
List<HRegionInfo> regionsOfUserTable = regionStates.getRegionsOfTable(tableName);
Pair<HRegionInfo, HRegionInfo> regionsToMerge = new Pair<HRegionInfo, HRegionInfo>();
byte[] startKey1 = { (byte) 'C' };
byte[] startKey2 = { (byte) 'D' };
for (HRegionInfo region : regionsOfUserTable) {
if (Bytes.compareTo(startKey1, region.getStartKey()) == 0) {
regionsToMerge.setFirst(region);
} else if (Bytes.compareTo(startKey2, region.getStartKey()) == 0) {
regionsToMerge.setSecond(region);
}
}
admin.move(regionsToMerge.getFirst().getEncodedNameAsBytes(), Bytes.toBytes(server
.toString()));
admin.move(regionsToMerge.getSecond().getEncodedNameAsBytes(), Bytes.toBytes(server
.toString()));
List<HRegionInfo> regionsOfIndexTable = regionStates.getRegionsOfTable(indexTableName);
Pair<HRegionInfo, HRegionInfo> indexRegionsToMerge = new Pair<HRegionInfo, HRegionInfo>();
for (HRegionInfo region : regionsOfIndexTable) {
if (Bytes.compareTo(startKey1, region.getStartKey()) == 0) {
indexRegionsToMerge.setFirst(region);
} else if (Bytes.compareTo(startKey2, region.getStartKey()) == 0) {
indexRegionsToMerge.setSecond(region);
}
}
admin.move(indexRegionsToMerge.getFirst().getEncodedNameAsBytes(), Bytes.toBytes(server
.toString()));
admin.move(indexRegionsToMerge.getSecond().getEncodedNameAsBytes(), Bytes.toBytes(server
.toString()));
while (!regionStates.getRegionServerOfRegion(regionsToMerge.getFirst()).equals(server)
|| !regionStates.getRegionServerOfRegion(regionsToMerge.getSecond()).equals(server)
|| !regionStates.getRegionServerOfRegion(indexRegionsToMerge.getFirst()).equals(
server)
|| !regionStates.getRegionServerOfRegion(indexRegionsToMerge.getSecond()).equals(
server)) {
Threads.sleep(1000);
}
admin.mergeRegions(regionsToMerge.getFirst().getEncodedNameAsBytes(), regionsToMerge
.getSecond().getEncodedNameAsBytes(), true);
admin.mergeRegions(indexRegionsToMerge.getFirst().getEncodedNameAsBytes(),
indexRegionsToMerge.getSecond().getEncodedNameAsBytes(), true);
while (regionsOfUserTable.size() != 20 || regionsOfIndexTable.size() != 20) {
Thread.sleep(100);
regionsOfUserTable = regionStates.getRegionsOfTable(tableName);
regionsOfIndexTable = regionStates.getRegionsOfTable(indexTableName);
}
boolean isRegionColocated =
checkForColocation(master, tableName.getNameAsString(), indexTableName
.getNameAsString());
assertTrue("User regions and index regions should colocate.", isRegionColocated);