Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix updateStatus doesn't update replication when read-only mode #656

Merged
merged 8 commits into from
Dec 23, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -88,45 +88,49 @@ public CompletableFuture<ServerStatus> updateStatus(ServiceRequestContext ctx,
return rejectStatusPatch(patch);
}

// writable | replicating | result
//-------------------+---------------+-----------------------------------
// true -> true | true -> true | not_modified exception
// | true -> false | bad_request exception
//-------------------+---------------+-----------------------------------
// true -> false | true -> true | setWritable(false)
// | true -> false | setWritable(false) & stop executor.
//-------------------+---------------+-----------------------------------
// false -> true | true -> true | setWritable(true)
// | false -> true | setWritable(true) & start executor.
//-------------------+---------------+-----------------------------------
// false -> false | true -> false | Stop executor.
// | false -> true | Start executor.
final boolean writable = writableNode.asBoolean();
final boolean replicating = replicatingNode.asBoolean();
if (writable && !replicating) {
return HttpApiUtil.throwResponse(ctx, HttpStatus.BAD_REQUEST,
"'replicating' must be 'true' if 'writable' is 'true'.");
}
if (oldStatus.writable == writable && oldStatus.replicating == replicating) {
throw HttpStatusException.of(HttpStatus.NOT_MODIFIED);
}

if (oldStatus.writable) {
if (!writable) { // writable -> unwritable
executor().setWritable(false);
if (replicating) {
logger.warn("Entered read-only mode with replication enabled");
return CompletableFuture.completedFuture(status());
} else {
logger.warn("Entering read-only mode with replication disabled ..");
return executor().stop().handle((unused, cause) -> {
if (cause != null) {
logger.warn("Failed to stop the command executor:", cause);
} else {
logger.info("Entered read-only mode with replication disabled");
}
return status();
});
}
if (oldStatus.writable != writable) {
executor().setWritable(writable);
if (writable) {
logger.warn("Left read-only mode");
} else {
logger.warn("Entered read-only mode");
0x1306e6d marked this conversation as resolved.
Show resolved Hide resolved
}
} else if (writable) { // unwritable -> writable
logger.warn("Leaving read-only mode ..");
executor().setWritable(true);
return executor().start().handle((unused, cause) -> {
if (cause != null) {
logger.warn("Failed to leave read-only mode:", cause);
} else {
logger.info("Left read-only mode");
}
return status();
});
} else if (oldStatus.replicating) {
if (!replicating) { // replicating -> unreplicating
logger.warn("Disabling replication with read-only mode");
}

if (oldStatus.replicating != replicating) {
if (replicating) {
return executor().start().handle((unused, cause) -> {
if (cause != null) {
logger.warn("Failed to start the command executor:", cause);
} else {
logger.info("Enabled replication");
0x1306e6d marked this conversation as resolved.
Show resolved Hide resolved
}
return status();
});
} else {
return executor().stop().handle((unused, cause) -> {
if (cause != null) {
logger.warn("Failed to stop the command executor:", cause);
Expand All @@ -136,19 +140,9 @@ public CompletableFuture<ServerStatus> updateStatus(ServiceRequestContext ctx,
return status();
});
}
} else if (replicating) { // unreplicating -> replicating
logger.warn("Enabling replication with read-only mode");
return executor().start().handle((unused, cause) -> {
if (cause != null) {
logger.warn("Failed to start the command executor:", cause);
} else {
logger.info("Enabled replication");
}
return status();
});
}

throw HttpStatusException.of(HttpStatus.NOT_MODIFIED);
return CompletableFuture.completedFuture(status());
}

private static CompletableFuture<ServerStatus> rejectStatusPatch(JsonNode patch) {
Expand Down