下面列出了org.apache.hadoop.hbase.client.Admin#assign ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* It always unassign first region of table.
* @param tableName move region of table.
* @throws IOException
*/
protected static void unassignRegionAsync(final String tableName) throws IOException {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
final Admin admin = utility.getAdmin();
final RegionInfo tableRegion =
admin.getRegions(TableName.valueOf(tableName)).get(0);
admin.unassign(tableRegion.getEncodedNameAsBytes(), false);
admin.assign(tableRegion.getEncodedNameAsBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.setDaemon(true);
thread.start();
}
@Test
public void testCleanupOnClose() throws Exception {
TableName tableName = TableName.valueOf("testCleanupOnClose");
String familyName = "f";
byte[] familyNameBytes = Bytes.toBytes(familyName);
util.createTable(tableName, familyName);
Admin hBaseAdmin = util.getAdmin();
Table table = util.getConnection().getTable(tableName);
HRegionServer rs = util.getRSForFirstRegionInTable(tableName);
Region region = rs.getRegions(tableName).get(0);
int refSFCount = 4;
for (int i = 0; i < refSFCount; i++) {
for (int j = 0; j < refSFCount; j++) {
Put put = new Put(Bytes.toBytes(j));
put.addColumn(familyNameBytes, Bytes.toBytes(i), Bytes.toBytes(j));
table.put(put);
}
util.flush(tableName);
}
assertEquals(refSFCount, region.getStoreFileList(new byte[][]{familyNameBytes}).size());
//add a delete, to test wether we end up with an inconsistency post region close
Delete delete = new Delete(Bytes.toBytes(refSFCount-1));
table.delete(delete);
util.flush(tableName);
assertFalse(table.exists(new Get(Bytes.toBytes(refSFCount-1))));
//Create a scanner and keep it open to add references to StoreFileReaders
Scan scan = new Scan();
scan.withStopRow(Bytes.toBytes(refSFCount-2));
scan.setCaching(1);
ResultScanner scanner = table.getScanner(scan);
Result res = scanner.next();
assertNotNull(res);
assertEquals(refSFCount, res.getFamilyMap(familyNameBytes).size());
//Verify the references
int count = 0;
for (HStoreFile sf : (Collection<HStoreFile>)region.getStore(familyNameBytes).getStorefiles()) {
synchronized (sf) {
if (count < refSFCount) {
assertTrue(sf.isReferencedInReads());
} else {
assertFalse(sf.isReferencedInReads());
}
}
count++;
}
//Major compact to produce compacted storefiles that need to be cleaned up
util.compact(tableName, true);
assertEquals(1, region.getStoreFileList(new byte[][]{familyNameBytes}).size());
assertEquals(refSFCount+1,
((HStore)region.getStore(familyNameBytes)).getStoreEngine().getStoreFileManager()
.getCompactedfiles().size());
//close then open the region to determine wether compacted storefiles get cleaned up on close
hBaseAdmin.unassign(region.getRegionInfo().getRegionName(), false);
hBaseAdmin.assign(region.getRegionInfo().getRegionName());
util.waitUntilNoRegionsInTransition(10000);
assertFalse("Deleted row should not exist",
table.exists(new Get(Bytes.toBytes(refSFCount-1))));
rs = util.getRSForFirstRegionInTable(tableName);
region = rs.getRegions(tableName).get(0);
assertEquals(1, region.getStoreFileList(new byte[][]{familyNameBytes}).size());
assertEquals(0,
((HStore)region.getStore(familyNameBytes)).getStoreEngine().getStoreFileManager()
.getCompactedfiles().size());
}
/**
* In 0.90, this forces an HRI offline by setting the RegionTransitionData
* in ZK to have HBCK_CODE_NAME as the server. This is a special case in
* the AssignmentManager that attempts an assign call by the master.
*
* This doesn't seem to work properly in the updated version of 0.92+'s hbck
* so we use assign to force the region into transition. This has the
* side-effect of requiring a RegionInfo that considers regionId (timestamp)
* in comparators that is addressed by HBASE-5563.
*/
private static void forceOfflineInZK(Admin admin, final RegionInfo region)
throws ZooKeeperConnectionException, KeeperException, IOException, InterruptedException {
admin.assign(region.getRegionName());
}
/**
* In 0.90, this forces an HRI offline by setting the RegionTransitionData
* in ZK to have HBCK_CODE_NAME as the server. This is a special case in
* the AssignmentManager that attempts an assign call by the master.
*
* This doesn't seem to work properly in the updated version of 0.92+'s hbck
* so we use assign to force the region into transition. This has the
* side-effect of requiring a RegionInfo that considers regionId (timestamp)
* in comparators that is addressed by HBASE-5563.
*/
private static void forceOfflineInZK(Admin admin, final RegionInfo region)
throws ZooKeeperConnectionException, KeeperException, IOException, InterruptedException {
admin.assign(region.getRegionName());
}