Skip to content

Commit

Permalink
Prevent bookie shutdown due to rest api when bookie prohibits readOnl…
Browse files Browse the repository at this point in the history
…yMode (apache#3972)
  • Loading branch information
wenbingshen authored and Anup Ghatage committed Jul 12, 2024
1 parent cc32363 commit 3d1e888
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ public boolean isForceReadOnly(){
return forceReadOnly.get();
}

@Override
public boolean isReadOnlyModeEnabled() {
return conf.isReadOnlyModeEnabled();
}

@Override
public boolean isAvailableForHighPriorityWrites() {
return availableForHighPriorityWrites;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public interface StateManager extends AutoCloseable {
*/
boolean isForceReadOnly();

/**
* Check is readOnlyModeEnabled.
*/
boolean isReadOnlyModeEnabled();

/**
* Check is Running.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
}
stateManager.transitionToWritableMode().get();
} else if (!stateManager.isReadOnly() && inState.isReadOnly()) {
if (!stateManager.isReadOnlyModeEnabled()) {
response.setCode(HttpServer.StatusCode.BAD_REQUEST);
response.setBody("Bookie is disabled ReadOnly mode, cannot transit to readOnly mode");
return response;
}
stateManager.transitionToReadOnlyMode().get();
}
} else if (!HttpServer.Method.GET.equals(request.getMethod())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1023,11 +1023,43 @@ public void testBookieReadOnlyState() throws Exception {
assertFalse(readOnlyState.isReadOnly());

//forceReadonly to writable
baseConf.setForceReadOnlyBookie(true);
baseConf.setReadOnlyModeEnabled(true);
restartBookies();
MetadataBookieDriver metadataDriver = BookieResources.createMetadataDriver(
baseConf, NullStatsLogger.INSTANCE);
restartBookies(c -> {
c.setForceReadOnlyBookie(true);
c.setReadOnlyModeEnabled(true);
return c;
});
// the old bkHttpServiceProvider has an old bookie instance who has been shutdown
// so we need create a new bkHttpServiceProvider2 to contains a new bookie which has created by restart.
BKHttpServiceProvider bkHttpServiceProvider2 = new BKHttpServiceProvider.Builder()
.setBookieServer(serverByIndex(numberOfBookies - 1))
.setServerConfiguration(baseConf)
.setLedgerManagerFactory(metadataDriver.getLedgerManagerFactory())
.build();
HttpEndpointService bookieReadOnlyService2 = bkHttpServiceProvider2
.provideHttpEndpointService(HttpServer.ApiType.BOOKIE_STATE_READONLY);

request = new HttpServiceRequest(JsonUtil.toJson(new ReadOnlyState(false)), HttpServer.Method.PUT, null);
response = bookieReadOnlyService.handle(request);
response = bookieReadOnlyService2.handle(request);
assertEquals(400, response.getStatusCode());

// disable readOnly mode
restartBookies(c -> {
c.setForceReadOnlyBookie(false);
c.setReadOnlyModeEnabled(false);
return c;
});
bkHttpServiceProvider2 = new BKHttpServiceProvider.Builder()
.setBookieServer(serverByIndex(numberOfBookies - 1))
.setServerConfiguration(baseConf)
.setLedgerManagerFactory(metadataDriver.getLedgerManagerFactory())
.build();
bookieReadOnlyService2 = bkHttpServiceProvider2
.provideHttpEndpointService(HttpServer.ApiType.BOOKIE_STATE_READONLY);

request = new HttpServiceRequest(JsonUtil.toJson(new ReadOnlyState(true)), HttpServer.Method.PUT, null);
response = bookieReadOnlyService2.handle(request);
assertEquals(400, response.getStatusCode());
}

Expand Down

0 comments on commit 3d1e888

Please sign in to comment.