下面列出了org.apache.hadoop.hbase.client.HBaseAdmin#move ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void doMove(HBaseAdmin admin, TableName tableName) throws IOException {
LOG.trace("Preparing region move");
Collection<ServerName> servers = admin.getClusterStatus().getServers();
try (Connection connection = ConnectionFactory.createConnection(HConfiguration.unwrapDelegate())) {
List<HRegionLocation> locations = connection.getRegionLocator(tableName).getAllRegionLocations();
int r = new Random().nextInt(locations.size());
HRegionLocation location = locations.get(r);
location.getServerName().getServerName();
ServerName pick = null;
for (ServerName sn : servers) {
if (!sn.equals(location.getServerName())) {
pick = sn;
break;
}
}
if (pick != null) {
LOG.trace("Moving region");
admin.move(location.getRegionInfo().getEncodedNameAsBytes(), Bytes.toBytes(pick.getServerName()));
}
}
}
@VisibleForTesting
static void move(Args args, HBaseAdmin admin, String tableName, String targetServerName, String encodedRegionName,
boolean asynchronous)
throws IOException, InterruptedException {
int i;
for (i = 0; i < Constant.TRY_MAX; i++) {
try {
admin.move(encodedRegionName.getBytes(), targetServerName.getBytes());
} catch (java.lang.reflect.UndeclaredThrowableException ignore) {
}
if (asynchronous)
return;
if (CommandAdapter.isMetaTable(tableName))
return;
if (!isTableEnabled(args, admin, tableName))
throw new IllegalStateException(Constant.MESSAGE_DISABLED_OR_NOT_FOUND_TABLE);
if (Util.isMoved(admin, tableName, encodedRegionName, targetServerName)) {
return;
}
Thread.sleep(Constant.WAIT_INTERVAL_MS);
// assign region again
if (i >= Constant.TRY_MAX / 2)
admin.assign(encodedRegionName.getBytes());
}
if (i >= Constant.TRY_MAX)
throw new IllegalStateException(Constant.MESSAGE_CANNOT_MOVE + " - "
+ encodedRegionName + " to " + targetServerName);
}
@VisibleForTesting
static void move(Args args, HBaseAdmin admin, String tableName, String targetServerName, String encodedRegionName,
boolean asynchronous)
throws IOException, InterruptedException {
int i;
for (i = 0; i < Constant.TRY_MAX; i++) {
try {
admin.move(encodedRegionName.getBytes(), targetServerName.getBytes());
} catch (java.lang.reflect.UndeclaredThrowableException ignore) {
}
if (asynchronous)
return;
if (CommandAdapter.isMetaTable(tableName))
return;
if (!isTableEnabled(args, admin, tableName))
throw new IllegalStateException(Constant.MESSAGE_DISABLED_OR_NOT_FOUND_TABLE);
if (Util.isMoved(admin, tableName, encodedRegionName, targetServerName)) {
return;
}
Thread.sleep(Constant.WAIT_INTERVAL_MS);
// assign region again
if (i >= Constant.TRY_MAX / 2)
admin.assign(encodedRegionName.getBytes());
}
if (i >= Constant.TRY_MAX)
throw new IllegalStateException(Constant.MESSAGE_CANNOT_MOVE + " - "
+ encodedRegionName + " to " + targetServerName);
}
@VisibleForTesting
static void move(Args args, HBaseAdmin admin, String tableName, String targetServerName, String encodedRegionName,
boolean asynchronous)
throws IOException, InterruptedException {
int i;
for (i = 0; i < Constant.TRY_MAX; i++) {
try {
admin.move(encodedRegionName.getBytes(), targetServerName.getBytes());
} catch (java.lang.reflect.UndeclaredThrowableException ignore) {
}
if (asynchronous)
return;
if (CommandAdapter.isMetaTable(tableName))
return;
if (!isTableEnabled(args, admin, tableName))
throw new IllegalStateException(Constant.MESSAGE_DISABLED_OR_NOT_FOUND_TABLE);
if (Util.isMoved(admin, tableName, encodedRegionName, targetServerName)) {
return;
}
Thread.sleep(Constant.WAIT_INTERVAL_MS);
// assign region again
if (i >= Constant.TRY_MAX / 2)
admin.assign(encodedRegionName.getBytes());
}
if (i >= Constant.TRY_MAX)
throw new IllegalStateException(Constant.MESSAGE_CANNOT_MOVE + " - "
+ encodedRegionName + " to " + targetServerName);
}
@VisibleForTesting
static void move(Args args, HBaseAdmin admin, String tableName, String targetServerName, String encodedRegionName,
boolean asynchronous)
throws IOException, InterruptedException {
int i;
for (i = 0; i < Constant.TRY_MAX; i++) {
try {
admin.move(encodedRegionName.getBytes(), targetServerName.getBytes());
} catch (java.lang.reflect.UndeclaredThrowableException ignore) {
}
if (asynchronous)
return;
if (CommandAdapter.isMetaTable(tableName))
return;
if (!isTableEnabled(args, admin, tableName))
throw new IllegalStateException(Constant.MESSAGE_DISABLED_OR_NOT_FOUND_TABLE);
if (Util.isMoved(admin, tableName, encodedRegionName, targetServerName)) {
return;
}
Thread.sleep(Constant.WAIT_INTERVAL_MS);
// assign region again
if (i >= Constant.TRY_MAX / 2)
admin.assign(encodedRegionName.getBytes());
}
if (i >= Constant.TRY_MAX)
throw new IllegalStateException(Constant.MESSAGE_CANNOT_MOVE + " - "
+ encodedRegionName + " to " + targetServerName);
}
@VisibleForTesting
static void move(Args args, HBaseAdmin admin, String tableName, String targetServerName, String encodedRegionName,
boolean asynchronous)
throws IOException, InterruptedException {
int i;
for (i = 0; i < Constant.TRY_MAX; i++) {
try {
admin.move(encodedRegionName.getBytes(), targetServerName.getBytes());
} catch (java.lang.reflect.UndeclaredThrowableException ignore) {
}
if (asynchronous)
return;
if (CommandAdapter.isMetaTable(tableName))
return;
if (!isTableEnabled(args, admin, tableName))
throw new IllegalStateException(Constant.MESSAGE_DISABLED_OR_NOT_FOUND_TABLE);
if (Util.isMoved(admin, tableName, encodedRegionName, targetServerName)) {
return;
}
Thread.sleep(Constant.WAIT_INTERVAL_MS);
// assign region again
if (i >= Constant.TRY_MAX / 2)
admin.assign(encodedRegionName.getBytes());
}
if (i >= Constant.TRY_MAX)
throw new IllegalStateException(Constant.MESSAGE_CANNOT_MOVE + " - "
+ encodedRegionName + " to " + targetServerName);
}
@Test(timeout=300000)
public void testWriteFailureWithRegionServerDown() throws Exception {
String query;
ResultSet rs;
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
Connection conn = driver.connect(url, props);
conn.setAutoCommit(false);
conn.createStatement().execute(
"CREATE TABLE " + DATA_TABLE_FULL_NAME + " (k VARCHAR NOT NULL PRIMARY KEY, v1 VARCHAR, v2 VARCHAR)");
query = "SELECT * FROM " + DATA_TABLE_FULL_NAME;
rs = conn.createStatement().executeQuery(query);
assertFalse(rs.next());
conn.createStatement().execute(
"CREATE INDEX " + INDEX_TABLE_NAME + " ON " + DATA_TABLE_FULL_NAME + " (v1) INCLUDE (v2)");
query = "SELECT * FROM " + INDEX_TABLE_FULL_NAME;
rs = conn.createStatement().executeQuery(query);
assertFalse(rs.next());
// Verify the metadata for index is correct.
rs = conn.getMetaData().getTables(null, StringUtil.escapeLike(SCHEMA_NAME), INDEX_TABLE_NAME,
new String[] { PTableType.INDEX.toString() });
assertTrue(rs.next());
assertEquals(INDEX_TABLE_NAME, rs.getString(3));
assertEquals(PIndexState.ACTIVE.toString(), rs.getString("INDEX_STATE"));
assertFalse(rs.next());
PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + DATA_TABLE_FULL_NAME + " VALUES(?,?,?)");
stmt.setString(1, "a");
stmt.setString(2, "x");
stmt.setString(3, "1");
stmt.execute();
conn.commit();
// find a RS which doesn't has CATALOG table
TableName catalogTable = TableName.valueOf("SYSTEM.CATALOG");
TableName indexTable = TableName.valueOf(INDEX_TABLE_FULL_NAME);
final HBaseCluster cluster = this.util.getHBaseCluster();
Collection<ServerName> rss = cluster.getClusterStatus().getServers();
HBaseAdmin admin = this.util.getHBaseAdmin();
List<HRegionInfo> regions = admin.getTableRegions(catalogTable);
ServerName catalogRS = cluster.getServerHoldingRegion(regions.get(0).getTable(),
regions.get(0).getRegionName());
ServerName metaRS = cluster.getServerHoldingMeta();
ServerName rsToBeKilled = null;
// find first RS isn't holding META or CATALOG table
for(ServerName curRS : rss) {
if(!curRS.equals(catalogRS) && !metaRS.equals(curRS)) {
rsToBeKilled = curRS;
break;
}
}
assertTrue(rsToBeKilled != null);
regions = admin.getTableRegions(indexTable);
final HRegionInfo indexRegion = regions.get(0);
final ServerName dstRS = rsToBeKilled;
admin.move(indexRegion.getEncodedNameAsBytes(), Bytes.toBytes(rsToBeKilled.getServerName()));
this.util.waitFor(30000, 200, new Waiter.Predicate<Exception>() {
@Override
public boolean evaluate() throws Exception {
ServerName sn = cluster.getServerHoldingRegion(indexRegion.getTable(),
indexRegion.getRegionName());
return (sn != null && sn.equals(dstRS));
}
});
// use timer sending updates in every 10ms
this.scheduleTimer = new Timer(true);
this.scheduleTimer.schedule(new SendingUpdatesScheduleTask(conn), 0, 10);
// let timer sending some updates
Thread.sleep(100);
// kill RS hosting index table
this.util.getHBaseCluster().killRegionServer(rsToBeKilled);
// wait for index table completes recovery
this.util.waitUntilAllRegionsAssigned(indexTable);
// Verify the metadata for index is correct.
do {
Thread.sleep(15 * 1000); // sleep 15 secs
rs = conn.getMetaData().getTables(null, StringUtil.escapeLike(SCHEMA_NAME), INDEX_TABLE_NAME,
new String[] { PTableType.INDEX.toString() });
assertTrue(rs.next());
if(PIndexState.ACTIVE.toString().equals(rs.getString("INDEX_STATE"))){
break;
}
} while(true);
this.scheduleTimer.cancel();
assertEquals(cluster.getClusterStatus().getDeadServers(), 1);
}