Skip to content

Commit

Permalink
Resolve #1211 | Thread-safe version
Browse files Browse the repository at this point in the history
  • Loading branch information
milena.krawczyk committed Jun 10, 2020
1 parent 23ebfaa commit d54d18a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,10 @@ private HealthCheckResult doHealthCheck(ZookeeperClient zookeeperClient) {
}

private void updateMode(List<HealthCheckResult> healthCheckResults) {
/* ReadOnly set by admin can be changed to ReadWrite only by admin */
if (!modeService.isReadOnlySetByAdmin()) {
if (healthCheckResults.contains(HealthCheckResult.UNHEALTHY)) {
modeService.setMode(ModeService.ManagementMode.READ_ONLY);
} else {
modeService.setMode(ModeService.ManagementMode.READ_WRITE);
}
if (healthCheckResults.contains(HealthCheckResult.UNHEALTHY)) {
modeService.compareAndSwapMode(ModeService.ManagementMode.READ_WRITE, ModeService.ManagementMode.READ_ONLY);
} else {
modeService.compareAndSwapMode(ModeService.ManagementMode.READ_ONLY, ModeService.ManagementMode.READ_WRITE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ public ManagementMode getMode() {
return mode;
}

public void setMode(ManagementMode mode) {
public synchronized void setMode(ManagementMode mode) {
this.mode = mode;
}

public boolean isReadOnlyEnabled() {
return mode == ManagementMode.READ_ONLY || mode == ManagementMode.READ_ONLY_ADMIN;
}

public boolean isReadOnlySetByAdmin() {
return mode == ManagementMode.READ_ONLY_ADMIN;
public synchronized void compareAndSwapMode(ManagementMode expectedMode, ManagementMode newMode) {
if (mode.equals(expectedMode)) {
setMode(newMode);
}
}
}

0 comments on commit d54d18a

Please sign in to comment.