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

Resolve #1211 | Read only mode persistence #1214

Merged
merged 4 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
import static pl.allegro.tech.hermes.management.domain.mode.ModeService.ManagementMode.READ_ONLY;
import static pl.allegro.tech.hermes.management.domain.mode.ModeService.ManagementMode.READ_WRITE;
import static pl.allegro.tech.hermes.management.domain.mode.ModeService.ManagementMode.READ_ONLY_ADMIN;

@Component
@Path("/mode")
Expand Down Expand Up @@ -48,10 +48,11 @@ public Response setMode(@QueryParam("mode") String mode) {
}
switch (mode) {
case ModeService.READ_WRITE:
modeService.setMode(READ_WRITE);
modeService.setModeByAdmin(READ_WRITE);
break;
case ModeService.READ_ONLY:
modeService.setMode(READ_ONLY);
case ModeService.READ_ONLY_ADMIN:
modeService.setModeByAdmin(READ_ONLY_ADMIN);
break;
default:
return Response.status(Response.Status.BAD_REQUEST).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ public class ModeService {

public static final String READ_WRITE = "readWrite";
public static final String READ_ONLY = "readOnly";
public static final String READ_ONLY_ADMIN = "readOnlyAdmin";

public enum ManagementMode {
READ_WRITE(ModeService.READ_WRITE),
READ_ONLY(ModeService.READ_ONLY);
READ_ONLY(ModeService.READ_ONLY),
READ_ONLY_ADMIN(ModeService.READ_ONLY_ADMIN);

private final String text;

Expand All @@ -30,11 +32,18 @@ public ManagementMode getMode() {
return mode;
}

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

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

public synchronized void setMode(ManagementMode newMode) {
/* READ_ONLY_ADMIN is a flag that can be changed only by admin */
if (!mode.equals(ManagementMode.READ_ONLY_ADMIN)) {
this.mode = newMode;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,37 @@ class HealthCheckTaskTest extends MultiZookeeperIntegrationTest {
successfulCounter.count() == 3
}

def "should not change status to READ_WRITE if READ_ONLY is set by admin"() {
given:
modeService.setModeByAdmin(ModeService.ManagementMode.READ_ONLY_ADMIN)

when:
healthCheckTask.run()

then:
modeService.readOnlyEnabled

and:
successfulCounter.count() == 2
}

def "should change status from READ_ONLY_ADMIN to READ_WRITE only by admin"() {
given:
modeService.setModeByAdmin(ModeService.ManagementMode.READ_ONLY_ADMIN)

when:
healthCheckTask.run()

then:
modeService.readOnlyEnabled

when:
modeService.setModeByAdmin(ModeService.ManagementMode.READ_WRITE)

then:
!modeService.readOnlyEnabled
}

static setupZookeeperPath(ZookeeperClient zookeeperClient, String path) {
def healthCheckPathExists = zookeeperClient.curatorFramework
.checkExists()
Expand Down