From a2dca1c61d44724ef4de1bb40ceace472a7886a6 Mon Sep 17 00:00:00 2001 From: Alex Newman Date: Thu, 5 Apr 2012 10:12:18 -0700 Subject: [PATCH 2/2] HBASE-5217 --- .../hadoop/hbase/thrift/ThriftServerRunner.java | 57 +++++++++-- .../hadoop/hbase/thrift/TestThriftServer.java | 106 ++++++++++++-------- 2 files changed, 113 insertions(+), 50 deletions(-) diff --git a/src/main/java/org/apache/hadoop/hbase/thrift/ThriftServerRunner.java b/src/main/java/org/apache/hadoop/hbase/thrift/ThriftServerRunner.java index a66dbf3..50fe400 100644 --- a/src/main/java/org/apache/hadoop/hbase/thrift/ThriftServerRunner.java +++ b/src/main/java/org/apache/hadoop/hbase/thrift/ThriftServerRunner.java @@ -66,7 +66,6 @@ import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.ParseFilter; import org.apache.hadoop.hbase.filter.PrefixFilter; import org.apache.hadoop.hbase.filter.WhileMatchFilter; -import org.apache.hadoop.hbase.migration.HRegionInfo090x; import org.apache.hadoop.hbase.security.User; import org.apache.hadoop.hbase.thrift.CallQueue.Call; import org.apache.hadoop.hbase.thrift.generated.AlreadyExists; @@ -1131,7 +1130,7 @@ public class ThriftServerRunner implements Runnable { @Override public List scannerGet(int id) throws IllegalArgument, IOError { - return scannerGetList(id,1); + return scannerGetList(id, 1); } public int scannerOpenWithScan(ByteBuffer tableName, TScan tScan, @@ -1331,16 +1330,54 @@ public class ThriftServerRunner implements Runnable { } } - /*** - * Given a row in the meta table return its regioninfo - * XXX Handle splits properly - * @param searchRow The region in meta - * @return The regioninfo associated with that region - * @throws IOError We will IOError if the region can not be found in meta. - */ @Override public TRegionInfo getRegionInfo(ByteBuffer searchRow) throws IOError { - return null; + try { + HTable table = getTable(HConstants.META_TABLE_NAME); + byte[] row = getBytes(searchRow); + + Scan scan = new Scan(); + scan.addColumn(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER); + byte[] b = new byte[searchRow.remaining()]; + searchRow.get(b, 0, b.length); + scan.setStartRow(b); + ResultScanner scanner = table.getScanner(scan); + Result startRowResult = scanner.next(); + if (startRowResult == null) { + throw new IOException("Cannot find row in .META., row=" + + Bytes.toStringBinary(row)); + } + + // find region start and end keys + byte[] value = startRowResult.getValue(HConstants.CATALOG_FAMILY, + HConstants.REGIONINFO_QUALIFIER); + if (value == null || value.length == 0) { + throw new IOException("HRegionInfo REGIONINFO was null or " + + " empty in Meta for row=" + + Bytes.toStringBinary(row)); + } + HRegionInfo regionInfo = Writables.getHRegionInfo(value); + TRegionInfo region = new TRegionInfo(); + region.setStartKey(regionInfo.getStartKey()); + region.setEndKey(regionInfo.getEndKey()); + region.id = regionInfo.getRegionId(); + region.setName(regionInfo.getRegionName()); + region.version = regionInfo.getVersion(); + + // find region assignment to server + value = startRowResult.getValue(HConstants.CATALOG_FAMILY, + HConstants.SERVER_QUALIFIER); + if (value != null && value.length > 0) { + String hostAndPort = Bytes.toString(value); + region.setServerName(Bytes.toBytes( + Addressing.parseHostname(hostAndPort))); + region.port = Addressing.parsePort(hostAndPort); + } + return region; + } catch (IOException e) { + LOG.warn(e.getMessage(), e); + throw new IOError(e.getMessage()); + } } private void initMetrics(ThriftMetrics metrics) { diff --git a/src/test/java/org/apache/hadoop/hbase/thrift/TestThriftServer.java b/src/test/java/org/apache/hadoop/hbase/thrift/TestThriftServer.java index 444d6d5..e9ab10d 100644 --- a/src/test/java/org/apache/hadoop/hbase/thrift/TestThriftServer.java +++ b/src/test/java/org/apache/hadoop/hbase/thrift/TestThriftServer.java @@ -19,10 +19,6 @@ */ package org.apache.hadoop.hbase.thrift; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -32,10 +28,7 @@ import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.hbase.HBaseTestingUtility; -import org.apache.hadoop.hbase.HConstants; -import org.apache.hadoop.hbase.HRegionInfo; -import org.apache.hadoop.hbase.MediumTests; +import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.filter.ParseFilter; import org.apache.hadoop.hbase.thrift.generated.BatchMutation; import org.apache.hadoop.hbase.thrift.generated.ColumnDescriptor; @@ -55,6 +48,8 @@ import org.junit.BeforeClass; import org.junit.Test; import org.junit.experimental.categories.Category; +import static org.junit.Assert.*; + /** * Unit testing for ThriftServerRunner.HBaseHandler, a part of the * org.apache.hadoop.hbase.thrift package. @@ -91,28 +86,7 @@ public class TestThriftServer { UTIL.shutdownMiniCluster(); } - /** - * Runs all of the tests under a single JUnit test method. We - * consolidate all testing to one method because HBaseClusterTestCase - * is prone to OutOfMemoryExceptions when there are three or more - * JUnit test methods. - * - * @throws Exception - */ - @Test - public void testAll() throws Exception { - // Run all tests - doTestTableCreateDrop(); - doTestThriftMetrics(); - doTestTableMutations(); - doTestTableTimestampsAndColumns(); - doTestTableScanners(); - doTestGetTableRegions(); - doTestFilterRegistration(); - doTestGetRegionInfo(); - } - - /** + /** * Tests for creating, enabling, disabling, and deleting tables. Also * tests that creating a table with an invalid column name yields an * IllegalArgument exception. @@ -208,6 +182,7 @@ public class TestThriftServer { * * @throws Exception */ + @Test public void doTestTableMutations() throws Exception { // Setup ThriftServerRunner.HBaseHandler handler = @@ -221,11 +196,11 @@ public class TestThriftServer { // Assert that the changes were made assertEquals(valueAname, - handler.get(tableAname, rowAname, columnAname, null).get(0).value); + handler.get(tableAname, rowAname, columnAname, null).get(0).value); TRowResult rowResult1 = handler.getRow(tableAname, rowAname, null).get(0); assertEquals(rowAname, rowResult1.row); assertEquals(valueBname, - rowResult1.columns.get(columnBname).value); + rowResult1.columns.get(columnBname).value); // Apply a few BatchMutations for rowA and rowB // rowAmutations.add(new Mutation(true, columnAname, null)); @@ -281,6 +256,7 @@ public class TestThriftServer { * * @throws Exception */ + @Test public void doTestTableTimestampsAndColumns() throws Exception { // Setup ThriftServerRunner.HBaseHandler handler = @@ -360,6 +336,7 @@ public class TestThriftServer { * * @throws Exception */ + @Test public void doTestTableScanners() throws Exception { // Setup ThriftServerRunner.HBaseHandler handler = @@ -429,6 +406,7 @@ public class TestThriftServer { * * @throws Exception */ + @Test public void doTestGetTableRegions() throws Exception { ThriftServerRunner.HBaseHandler handler = new ThriftServerRunner.HBaseHandler(UTIL.getConfiguration()); @@ -464,19 +442,15 @@ public class TestThriftServer { assertEquals("filterclass", registeredFilters.get("MyFilter")); } - public void doTestGetRegionInfo() throws Exception { - ThriftServerRunner.HBaseHandler handler = - new ThriftServerRunner.HBaseHandler(UTIL.getConfiguration()); - doTestGetRegionInfo(handler); - } - public static void doTestGetRegionInfo(Hbase.Iface handler) throws Exception { // Create tableA and add two columns to rowA handler.createTable(tableAname, getColumnDescriptors()); try { handler.mutateRow(tableAname, rowAname, getMutations(), null); - byte[] searchRow = HRegionInfo.createRegionName( - tableAname.array(), rowAname.array(), HConstants.NINES, false); + byte[] searchRow = HRegionInfo.createRegionName(tableAname.array(), + rowAname.array(), + Long.parseLong(HConstants.NINES), + false); TRegionInfo regionInfo = handler.getRegionInfo(ByteBuffer.wrap(searchRow)); assertTrue(Bytes.toStringBinary(regionInfo.getName()).startsWith( Bytes.toStringBinary(tableAname))); @@ -575,6 +549,58 @@ public class TestThriftServer { handler.scannerClose(scannerId); } + + /** + * Tests for creating, enabling, disabling, and deleting tables. Also + * tests that creating a table with an invalid column name yields an + * IllegalArgument exception. + * + * @throws Exception + */ + @Test + public void doTestGetRegionInfo() throws Exception { + ThriftServerRunner.HBaseHandler handler = + new ThriftServerRunner.HBaseHandler(UTIL.getConfiguration()); + + createTestTables(handler); + + ByteBuffer searchRow; + byte[] startRow; + byte[] tableName; + TRegionInfo regionInfo; + + startRow = HTableDescriptor.getStartRow(tableAname.array(), + "".getBytes()); + searchRow = ByteBuffer.wrap(startRow); + regionInfo = handler.getRegionInfo(searchRow); + tableName = HRegionInfo.parseRegionName(regionInfo.getName())[0]; + assertArrayEquals(tableAname.array(), tableName); + + startRow = HTableDescriptor.getStartRow(tableAname.array(), + "zzzzz".getBytes()); + searchRow = ByteBuffer.wrap(startRow); + regionInfo = handler.getRegionInfo(searchRow); + tableName = HRegionInfo.parseRegionName(regionInfo.getName())[0]; + assertArrayEquals(tableAname.array(), tableName); + + + startRow = HTableDescriptor.getStartRow(tableBname.array(), + "".getBytes()); + searchRow = ByteBuffer.wrap(startRow); + regionInfo = handler.getRegionInfo(searchRow); + tableName = HRegionInfo.parseRegionName(regionInfo.getName())[0]; + assertArrayEquals(tableBname.array(), tableName); + + startRow = HTableDescriptor.getStartRow(tableBname.array(), + "zzzzz".getBytes()); + searchRow = ByteBuffer.wrap(startRow); + regionInfo = handler.getRegionInfo(searchRow); + tableName = HRegionInfo.parseRegionName(regionInfo.getName())[0]; + assertArrayEquals(tableBname.array(), tableName); + + dropTestTables(handler); + } + @org.junit.Rule public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu = new org.apache.hadoop.hbase.ResourceCheckerJUnitRule(); -- 1.7.9.4