HBASE-5613 ThriftServer getTableRegions does not return serverName and port (Scott Chen)

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1304602 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Zhihong Yu 2012-03-23 21:15:50 +00:00
parent 929113dfab
commit 26b2950bc7
2 changed files with 51 additions and 34 deletions

View File

@ -27,6 +27,7 @@ import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -50,6 +51,8 @@ import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.TableNotFoundException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
@ -584,22 +587,29 @@ public class ThriftServerRunner implements Runnable {
public List<TRegionInfo> getTableRegions(ByteBuffer tableName)
throws IOError {
try {
List<HRegionInfo> hris =
this.getHBaseAdmin().getTableRegions(getBytes(tableName));
List<TRegionInfo> regions = new ArrayList<TRegionInfo>();
if (hris != null) {
for (HRegionInfo regionInfo : hris){
HTable table = getTable(tableName);
Map<HRegionInfo, ServerName> regionLocations =
table.getRegionLocations();
List<TRegionInfo> results = new ArrayList<TRegionInfo>();
for (Map.Entry<HRegionInfo, ServerName> entry :
regionLocations.entrySet()) {
HRegionInfo info = entry.getKey();
ServerName serverName = entry.getValue();
TRegionInfo region = new TRegionInfo();
region.startKey = ByteBuffer.wrap(regionInfo.getStartKey());
region.endKey = ByteBuffer.wrap(regionInfo.getEndKey());
region.id = regionInfo.getRegionId();
region.name = ByteBuffer.wrap(regionInfo.getRegionName());
region.version = regionInfo.getVersion();
regions.add(region);
region.serverName = ByteBuffer.wrap(
Bytes.toBytes(serverName.getHostname()));
region.port = serverName.getPort();
region.startKey = ByteBuffer.wrap(info.getStartKey());
region.endKey = ByteBuffer.wrap(info.getEndKey());
region.id = info.getRegionId();
region.name = ByteBuffer.wrap(info.getRegionName());
region.version = info.getVersion();
results.add(region);
}
}
return regions;
return results;
} catch (TableNotFoundException e) {
// Return empty list for non-existing table
return Collections.emptyList();
} catch (IOException e){
LOG.warn(e.getMessage(), e);
throw new IOError(e.getMessage());

View File

@ -29,6 +29,8 @@ import java.util.ArrayList;
import java.util.List;
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;
@ -60,6 +62,7 @@ import org.junit.experimental.categories.Category;
@Category(MediumTests.class)
public class TestThriftServer {
private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
private static final Log LOG = LogFactory.getLog(TestThriftServer.class);
protected static final int MAXVERSIONS = 3;
private static ByteBuffer asByteBuffer(String i) {
@ -434,10 +437,14 @@ public class TestThriftServer {
public static void doTestGetTableRegions(Hbase.Iface handler)
throws Exception {
assertEquals(handler.getTableNames().size(), 0);
handler.createTable(tableAname, getColumnDescriptors());
int regionCount = handler.getTableRegions(tableAname).size();
assertEquals(handler.getTableNames().size(), 1);
List<TRegionInfo> regions = handler.getTableRegions(tableAname);
int regionCount = regions.size();
assertEquals("empty table should have only 1 region, " +
"but found " + regionCount, regionCount, 1);
LOG.info("Region found:" + regions.get(0));
handler.disableTable(tableAname);
handler.deleteTable(tableAname);
regionCount = handler.getTableRegions(tableAname).size();