下面列出了org.apache.hadoop.hbase.client.Admin#majorCompactRegion ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void startCompaction(Admin admin, TableName table, RegionInfo region, byte[] cf)
throws IOException, InterruptedException {
LOG.info("Started major compaction: table={} cf={} region={}", table,
Bytes.toString(cf), region.getRegionNameAsString());
admin.majorCompactRegion(region.getRegionName(), cf);
// Wait until it really starts
// but with finite timeout
long waitTime = 300000; // 5 min
long startTime = EnvironmentEdgeManager.currentTime();
while (admin.getCompactionStateForRegion(region.getRegionName()) == CompactionState.NONE) {
// Is 1 second too aggressive?
Thread.sleep(1000);
if (EnvironmentEdgeManager.currentTime() - startTime > waitTime) {
LOG.warn("Waited for {} ms to start major MOB compaction on table={} cf={} region={}."+
" Stopped waiting for request confirmation. This is not an ERROR, continue next region."
, waitTime, table.getNameAsString(), Bytes.toString(cf),region.getRegionNameAsString());
break;
}
}
}
public static void compactAndBlockUntilDone(Admin admin, HRegionServer rs, byte[] regionName)
throws IOException, InterruptedException {
log("Compacting region: " + Bytes.toStringBinary(regionName));
// Wait till its online before we do compact else it comes back with NoServerForRegionException
try {
TEST_UTIL.waitFor(10000, new Waiter.Predicate<Exception>() {
@Override public boolean evaluate() throws Exception {
return rs.getServerName().equals(MetaTableAccessor.
getRegionLocation(admin.getConnection(), regionName).getServerName());
}
});
} catch (Exception e) {
throw new IOException(e);
}
admin.majorCompactRegion(regionName);
log("blocking until compaction is complete: " + Bytes.toStringBinary(regionName));
Threads.sleepWithoutInterrupt(500);
outer: for (;;) {
for (Store store : rs.getOnlineRegion(regionName).getStores()) {
if (store.getStorefilesCount() > 1) {
Threads.sleep(50);
continue outer;
}
}
break;
}
}
@Override
public void perform() throws Exception {
HBaseTestingUtility util = context.getHBaseIntegrationTestingUtility();
Admin admin = util.getAdmin();
boolean major = RandomUtils.nextInt(0, 100) < majorRatio;
getLogger().info("Performing action: Compact random region of table "
+ tableName + ", major=" + major);
List<RegionInfo> regions = admin.getRegions(tableName);
if (regions == null || regions.isEmpty()) {
getLogger().info("Table " + tableName + " doesn't have regions to compact");
return;
}
RegionInfo region = PolicyBasedChaosMonkey.selectRandomItem(
regions.toArray(new RegionInfo[0]));
try {
if (major) {
getLogger().debug("Major compacting region " + region.getRegionNameAsString());
admin.majorCompactRegion(region.getRegionName());
} else {
getLogger().debug("Compacting region " + region.getRegionNameAsString());
admin.compactRegion(region.getRegionName());
}
} catch (Exception ex) {
getLogger().warn("Compaction failed, might be caused by other chaos: " + ex.getMessage());
}
if (sleepTime > 0) {
Thread.sleep(sleepTime);
}
}
private void compactRegionOnServer(MajorCompactionRequest request, Admin admin, String store)
throws IOException {
admin.majorCompactRegion(request.getRegion().getEncodedNameAsBytes(),
Bytes.toBytes(store));
}