Index: src/test/java/org/apache/hadoop/hbase/regionserver/TestSplitTransactionOnCluster.java =================================================================== --- src/test/java/org/apache/hadoop/hbase/regionserver/TestSplitTransactionOnCluster.java (revision 1422354) +++ src/test/java/org/apache/hadoop/hbase/regionserver/TestSplitTransactionOnCluster.java (working copy) @@ -26,6 +26,7 @@ import static org.junit.Assert.assertFalse; import java.io.IOException; +import java.io.InterruptedIOException; import java.util.List; import java.util.NavigableMap; import java.util.concurrent.CountDownLatch; @@ -832,6 +833,57 @@ } } } + + @Test + public void testBasicSplit() throws Exception { + + final String STRING_TABLE_NAME = "randTable"; + final byte[] TEST_FAM = Bytes.toBytes("c"); + + // Create a table + HBaseAdmin admin = TESTING_UTIL.getHBaseAdmin(); + final long startTime = System.currentTimeMillis(); + final String localTableNameAsString = STRING_TABLE_NAME + startTime; + final byte[] localTableName = Bytes.toBytes(localTableNameAsString); + HTable original = TESTING_UTIL.createTable( + Bytes.toBytes(localTableNameAsString), TEST_FAM); + TESTING_UTIL.loadTable(original, TEST_FAM); + final int loadedTableCount = TESTING_UTIL.countRows(original); + LOG.info("Original table has: " + loadedTableCount + " rows"); + + // Verify that region information is the same pre-split + List originalTableHRegions = admin.getTableRegions(Bytes + .toBytes(localTableNameAsString)); + LOG.info("Original table has " + originalTableHRegions.size() + + " regions. "); + final int originalRegionCount = originalTableHRegions.size(); + + original.clearRegionCache(); + + admin.split(originalTableHRegions.get(0).getRegionName()); + waitOnSplit(localTableName, originalRegionCount); + } + + private void waitOnSplit(byte[] tableName, int originalCount) + throws Exception { + + List regions = cluster.getRegions(tableName); + + for (int i = 0; i < 150; i++) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + throw new InterruptedIOException(e.getMessage()); + } + + regions = cluster.getRegions(tableName); + if (regions.size() > originalCount) { + return; + } + } + throw new Exception("Split did not increase the number of regions"); + } + public static class MockedSplitTransaction extends SplitTransaction { private HRegion currentRegion; @@ -1020,5 +1072,4 @@ LOG.debug("Customised master executed."); } } -} - +} \ No newline at end of file Index: src/main/java/org/apache/hadoop/hbase/io/hfile/HFileBlockIndex.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/io/hfile/HFileBlockIndex.java (revision 1422354) +++ src/main/java/org/apache/hadoop/hbase/io/hfile/HFileBlockIndex.java (working copy) @@ -342,7 +342,7 @@ midKey = Arrays.copyOfRange(b.array(), keyOffset, keyOffset + keyLen); } else { // The middle of the root-level index. - midKey = blockKeys[(rootCount - 1) / 2]; + midKey = blockKeys[rootCount / 2]; } this.midKey.set(midKey); @@ -1429,5 +1429,4 @@ public static int getMaxChunkSize(Configuration conf) { return conf.getInt(MAX_CHUNK_SIZE_KEY, DEFAULT_MAX_CHUNK_SIZE); } - }