HDFS-9849. DiskBalancer: reduce lock path in shutdown code. Contributed by Yuanbo Liu.

This commit is contained in:
Anu Engineer 2016-09-08 20:00:42 -07:00
parent 35c5943b8b
commit baab48922a

View File

@ -121,17 +121,23 @@ public DiskBalancer(String dataNodeUUID,
*/
public void shutdown() {
lock.lock();
boolean needShutdown = false;
try {
this.isDiskBalancerEnabled = false;
this.currentResult = Result.NO_PLAN;
if ((this.future != null) && (!this.future.isDone())) {
this.currentResult = Result.PLAN_CANCELLED;
this.blockMover.setExitFlag();
shutdownExecutor();
scheduler.shutdown();
needShutdown = true;
}
} finally {
lock.unlock();
}
// no need to hold lock while shutting down executor.
if (needShutdown) {
shutdownExecutor();
}
}
/**
@ -139,7 +145,6 @@ public void shutdown() {
*/
private void shutdownExecutor() {
final int secondsTowait = 10;
scheduler.shutdown();
try {
if (!scheduler.awaitTermination(secondsTowait, TimeUnit.SECONDS)) {
scheduler.shutdownNow();
@ -228,6 +233,7 @@ public DiskBalancerWorkStatus queryWorkStatus() throws DiskBalancerException {
*/
public void cancelPlan(String planID) throws DiskBalancerException {
lock.lock();
boolean needShutdown = false;
try {
checkDiskBalancerEnabled();
if (this.planID == null ||
@ -239,13 +245,18 @@ public void cancelPlan(String planID) throws DiskBalancerException {
DiskBalancerException.Result.NO_SUCH_PLAN);
}
if (!this.future.isDone()) {
this.blockMover.setExitFlag();
shutdownExecutor();
this.currentResult = Result.PLAN_CANCELLED;
this.blockMover.setExitFlag();
scheduler.shutdown();
needShutdown = true;
}
} finally {
lock.unlock();
}
// no need to hold lock while shutting down executor.
if (needShutdown) {
shutdownExecutor();
}
}
/**