@Override public CompletableFuture<Void> close() { CompletableFuture<Void> future = new CompletableFuture<>(); lock.lock() .thenCompose(v -> balancer.replace(server.server().cluster())) .whenComplete( (r1, e1) -> { balancer.close(); lock.unlock() .whenComplete( (r2, e2) -> { super.close() .whenComplete( (r3, e3) -> { server .close() .whenComplete( (r4, e4) -> { if (e4 == null) { future.complete(null); } else { future.completeExceptionally(e4); } }); }); }); }); return future; }
/** Balances the cluster. */ private void balance() { if (lock != null && !locking && server.server().cluster().member().equals(server.server().cluster().leader())) { locking = true; lock.lock() .thenCompose(v -> balancer.balance(server.server().cluster())) .whenComplete((r1, e1) -> lock.unlock().whenComplete((r2, e2) -> locking = false)); } }