HBASE-6643 Accept encoded region name in compacting/spliting region from shell

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1377862 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
jxiang 2012-08-27 21:05:38 +00:00
parent 37f39c40c3
commit 275f0453c5
2 changed files with 91 additions and 41 deletions

View File

@ -28,6 +28,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -100,7 +101,6 @@ import org.apache.hadoop.hbase.regionserver.wal.FailedLogCloseException;
import org.apache.hadoop.hbase.util.Addressing; import org.apache.hadoop.hbase.util.Addressing;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.Pair; import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.hbase.util.Writables;
import org.apache.hadoop.ipc.RemoteException; import org.apache.hadoop.ipc.RemoteException;
import org.apache.hadoop.util.StringUtils; import org.apache.hadoop.util.StringUtils;
import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.KeeperException;
@ -409,11 +409,13 @@ public class HBaseAdmin implements Abortable, Closeable {
MetaScannerVisitor visitor = new MetaScannerVisitorBase() { MetaScannerVisitor visitor = new MetaScannerVisitorBase() {
@Override @Override
public boolean processRow(Result rowResult) throws IOException { public boolean processRow(Result rowResult) throws IOException {
HRegionInfo info = Writables.getHRegionInfoOrNull( if (rowResult == null || rowResult.size() <= 0) {
rowResult.getValue(HConstants.CATALOG_FAMILY, return true;
HConstants.REGIONINFO_QUALIFIER)); }
//If regioninfo is null, skip this row HRegionInfo info = MetaReader.parseHRegionInfoFromCatalogResult(
if (null == info) { rowResult, HConstants.REGIONINFO_QUALIFIER);
if (info == null) {
LOG.warn("No serialized HRegionInfo in " + rowResult);
return true; return true;
} }
if (!(Bytes.equals(info.getTableName(), desc.getName()))) { if (!(Bytes.equals(info.getTableName(), desc.getName()))) {
@ -1228,16 +1230,15 @@ public class HBaseAdmin implements Abortable, Closeable {
public void flush(final byte [] tableNameOrRegionName) public void flush(final byte [] tableNameOrRegionName)
throws IOException, InterruptedException { throws IOException, InterruptedException {
CatalogTracker ct = getCatalogTracker(); CatalogTracker ct = getCatalogTracker();
boolean isRegionName = isRegionName(tableNameOrRegionName, ct);
try { try {
if (isRegionName) { Pair<HRegionInfo, ServerName> regionServerPair
Pair<HRegionInfo, ServerName> pair = = getRegion(tableNameOrRegionName, ct);
MetaReader.getRegion(ct, tableNameOrRegionName); if (regionServerPair != null) {
if (pair == null || pair.getSecond() == null) { if (regionServerPair.getSecond() == null) {
LOG.info("No server in .META. for " + LOG.info("No server in .META. for " +
Bytes.toStringBinary(tableNameOrRegionName) + "; pair=" + pair); Bytes.toStringBinary(tableNameOrRegionName) + "; pair=" + regionServerPair);
} else { } else {
flush(pair.getSecond(), pair.getFirst()); flush(regionServerPair.getSecond(), regionServerPair.getFirst());
} }
} else { } else {
final String tableName = tableNameString(tableNameOrRegionName, ct); final String tableName = tableNameString(tableNameOrRegionName, ct);
@ -1340,14 +1341,14 @@ public class HBaseAdmin implements Abortable, Closeable {
throws IOException, InterruptedException { throws IOException, InterruptedException {
CatalogTracker ct = getCatalogTracker(); CatalogTracker ct = getCatalogTracker();
try { try {
if (isRegionName(tableNameOrRegionName, ct)) { Pair<HRegionInfo, ServerName> regionServerPair
Pair<HRegionInfo, ServerName> pair = = getRegion(tableNameOrRegionName, ct);
MetaReader.getRegion(ct, tableNameOrRegionName); if (regionServerPair != null) {
if (pair == null || pair.getSecond() == null) { if (regionServerPair.getSecond() == null) {
LOG.info("No server in .META. for " + LOG.info("No server in .META. for " +
Bytes.toStringBinary(tableNameOrRegionName) + "; pair=" + pair); Bytes.toStringBinary(tableNameOrRegionName) + "; pair=" + regionServerPair);
} else { } else {
compact(pair.getSecond(), pair.getFirst(), major); compact(regionServerPair.getSecond(), regionServerPair.getFirst(), major);
} }
} else { } else {
final String tableName = tableNameString(tableNameOrRegionName, ct); final String tableName = tableNameString(tableNameOrRegionName, ct);
@ -1622,15 +1623,14 @@ public class HBaseAdmin implements Abortable, Closeable {
final byte [] splitPoint) throws IOException, InterruptedException { final byte [] splitPoint) throws IOException, InterruptedException {
CatalogTracker ct = getCatalogTracker(); CatalogTracker ct = getCatalogTracker();
try { try {
if (isRegionName(tableNameOrRegionName, ct)) { Pair<HRegionInfo, ServerName> regionServerPair
// Its a possible region name. = getRegion(tableNameOrRegionName, ct);
Pair<HRegionInfo, ServerName> pair = if (regionServerPair != null) {
MetaReader.getRegion(ct, tableNameOrRegionName); if (regionServerPair.getSecond() == null) {
if (pair == null || pair.getSecond() == null) {
LOG.info("No server in .META. for " + LOG.info("No server in .META. for " +
Bytes.toStringBinary(tableNameOrRegionName) + "; pair=" + pair); Bytes.toStringBinary(tableNameOrRegionName) + "; pair=" + regionServerPair);
} else { } else {
split(pair.getSecond(), pair.getFirst(), splitPoint); split(regionServerPair.getSecond(), regionServerPair.getFirst(), splitPoint);
} }
} else { } else {
final String tableName = tableNameString(tableNameOrRegionName, ct); final String tableName = tableNameString(tableNameOrRegionName, ct);
@ -1685,19 +1685,45 @@ public class HBaseAdmin implements Abortable, Closeable {
/** /**
* @param tableNameOrRegionName Name of a table or name of a region. * @param tableNameOrRegionName Name of a table or name of a region.
* @param ct A {@link CatalogTracker} instance (caller of this method usually has one). * @param ct A {@link CatalogTracker} instance (caller of this method usually has one).
* @return True if <code>tableNameOrRegionName</code> is a verified region * @return a pair of HRegionInfo and ServerName if <code>tableNameOrRegionName</code> is
* name (we call {@link MetaReader#getRegion( CatalogTracker, byte[])} * a verified region name (we call {@link MetaReader#getRegion( CatalogTracker, byte[])}
* else false. * else null.
* Throw an exception if <code>tableNameOrRegionName</code> is null. * Throw an exception if <code>tableNameOrRegionName</code> is null.
* @throws IOException * @throws IOException
*/ */
private boolean isRegionName(final byte[] tableNameOrRegionName, Pair<HRegionInfo, ServerName> getRegion(final byte[] tableNameOrRegionName,
CatalogTracker ct) final CatalogTracker ct) throws IOException {
throws IOException {
if (tableNameOrRegionName == null) { if (tableNameOrRegionName == null) {
throw new IllegalArgumentException("Pass a table name or region name"); throw new IllegalArgumentException("Pass a table name or region name");
} }
return (MetaReader.getRegion(ct, tableNameOrRegionName) != null); Pair<HRegionInfo, ServerName> pair = MetaReader.getRegion(ct, tableNameOrRegionName);
if (pair == null) {
final AtomicReference<Pair<HRegionInfo, ServerName>> result =
new AtomicReference<Pair<HRegionInfo, ServerName>>(null);
final String encodedName = Bytes.toString(tableNameOrRegionName);
MetaScannerVisitor visitor = new MetaScannerVisitorBase() {
@Override
public boolean processRow(Result data) throws IOException {
if (data == null || data.size() <= 0) {
return true;
}
HRegionInfo info = MetaReader.parseHRegionInfoFromCatalogResult(
data, HConstants.REGIONINFO_QUALIFIER);
if (info == null) {
LOG.warn("No serialized HRegionInfo in " + data);
return true;
}
if (!encodedName.equals(info.getEncodedName())) return true;
ServerName sn = MetaReader.getServerNameFromCatalogResult(data);
result.set(new Pair<HRegionInfo, ServerName>(info, sn));
return false; // found the region, stop
}
};
MetaScanner.metaScan(conf, visitor);
pair = result.get();
}
return pair;
} }
/** /**
@ -1962,18 +1988,18 @@ public class HBaseAdmin implements Abortable, Closeable {
CompactionState state = CompactionState.NONE; CompactionState state = CompactionState.NONE;
CatalogTracker ct = getCatalogTracker(); CatalogTracker ct = getCatalogTracker();
try { try {
if (isRegionName(tableNameOrRegionName, ct)) { Pair<HRegionInfo, ServerName> regionServerPair
Pair<HRegionInfo, ServerName> pair = = getRegion(tableNameOrRegionName, ct);
MetaReader.getRegion(ct, tableNameOrRegionName); if (regionServerPair != null) {
if (pair == null || pair.getSecond() == null) { if (regionServerPair.getSecond() == null) {
LOG.info("No server in .META. for " + LOG.info("No server in .META. for " +
Bytes.toStringBinary(tableNameOrRegionName) + "; pair=" + pair); Bytes.toStringBinary(tableNameOrRegionName) + "; pair=" + regionServerPair);
} else { } else {
ServerName sn = pair.getSecond(); ServerName sn = regionServerPair.getSecond();
AdminProtocol admin = AdminProtocol admin =
this.connection.getAdmin(sn.getHostname(), sn.getPort()); this.connection.getAdmin(sn.getHostname(), sn.getPort());
GetRegionInfoRequest request = RequestConverter.buildGetRegionInfoRequest( GetRegionInfoRequest request = RequestConverter.buildGetRegionInfoRequest(
pair.getFirst().getRegionName(), true); regionServerPair.getFirst().getRegionName(), true);
GetRegionInfoResponse response = admin.getRegionInfo(null, request); GetRegionInfoResponse response = admin.getRegionInfo(null, request);
return response.getCompactionState(); return response.getCompactionState();
} }

View File

@ -40,6 +40,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.catalog.CatalogTracker;
import org.apache.hadoop.hbase.executor.EventHandler; import org.apache.hadoop.hbase.executor.EventHandler;
import org.apache.hadoop.hbase.executor.EventHandler.EventType; import org.apache.hadoop.hbase.executor.EventHandler.EventType;
import org.apache.hadoop.hbase.executor.ExecutorService; import org.apache.hadoop.hbase.executor.ExecutorService;
@ -52,6 +53,7 @@ import org.apache.hadoop.hbase.regionserver.HRegionServer;
import org.apache.hadoop.hbase.regionserver.wal.HLogUtilsForTests; import org.apache.hadoop.hbase.regionserver.wal.HLogUtilsForTests;
import org.apache.hadoop.hbase.InvalidFamilyOperationException; import org.apache.hadoop.hbase.InvalidFamilyOperationException;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.hbase.zookeeper.ZKTable; import org.apache.hadoop.hbase.zookeeper.ZKTable;
import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher; import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
import org.junit.*; import org.junit.*;
@ -1610,7 +1612,29 @@ public class TestAdmin {
htd.addFamily(hcd); htd.addFamily(hcd);
TEST_UTIL.getHBaseAdmin().createTable(htd); TEST_UTIL.getHBaseAdmin().createTable(htd);
} }
@Test
public void testGetRegion() throws Exception {
final String name = "testGetRegion";
LOG.info("Started " + name);
final byte [] nameBytes = Bytes.toBytes(name);
HTable t = TEST_UTIL.createTable(nameBytes, HConstants.CATALOG_FAMILY);
TEST_UTIL.createMultiRegions(t, HConstants.CATALOG_FAMILY);
CatalogTracker ct = new CatalogTracker(TEST_UTIL.getConfiguration());
ct.start();
try {
HRegionLocation regionLocation = t.getRegionLocation("mmm");
HRegionInfo region = regionLocation.getRegionInfo();
byte[] regionName = region.getRegionName();
Pair<HRegionInfo, ServerName> pair = admin.getRegion(regionName, ct);
assertTrue(Bytes.equals(regionName, pair.getFirst().getRegionName()));
pair = admin.getRegion(region.getEncodedNameAsBytes(), ct);
assertTrue(Bytes.equals(regionName, pair.getFirst().getRegionName()));
} finally {
ct.stop();
}
}
@org.junit.Rule @org.junit.Rule
public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu = public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
new org.apache.hadoop.hbase.ResourceCheckerJUnitRule(); new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();