Skip to content

Commit

Permalink
Zookeeper restarting
Browse files Browse the repository at this point in the history
  • Loading branch information
faderskd committed Nov 22, 2024
1 parent ed7b18c commit 41bcb7e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package pl.allegro.tech.hermes.test.helper.containers;

import static java.lang.String.format;
import static pl.allegro.tech.hermes.test.helper.containers.TestcontainersUtils.copyScriptToContainer;
import static pl.allegro.tech.hermes.test.helper.containers.TestcontainersUtils.readFileFromClasspath;

import com.github.dockerjava.api.command.InspectContainerResponse;
import java.io.IOException;
import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import org.rnorth.ducttape.unreliables.Unreliables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
Expand All @@ -16,6 +21,7 @@ public class ZookeeperContainer extends GenericContainer<ZookeeperContainer> {
private static final DockerImageName DEFAULT_ZOOKEEPER_IMAGE_NAME =
DockerImageName.parse("confluentinc/cp-zookeeper").withTag(ImageTags.confluentImagesTag());
private static final int DEFAULT_ZOOKEEPER_PORT = 2181;
private static final Duration ZOOKEEPER_RESTART_WAIT_TIMEOUT = Duration.ofMinutes(1);
private static final String START_STOP_SCRIPT = "/zookeeper_start_stop_wrapper.sh";
private Logger logger;

Expand Down Expand Up @@ -49,14 +55,14 @@ public void start() {
setupLogger();
}

// @Override
// protected void doStart() {
// withCommand(
// "sh",
// "-c",
// "while [ ! -f " + START_STOP_SCRIPT + " ]; do sleep 0.1; done; " + START_STOP_SCRIPT);
// super.doStart();
// }
@Override
protected void doStart() {
withCommand(
"sh",
"-c",
"while [ ! -f " + START_STOP_SCRIPT + " ]; do sleep 0.1; done; " + START_STOP_SCRIPT);
super.doStart();
}

private void setupLogger() {
if (this.logger != null) {
Expand All @@ -66,21 +72,52 @@ private void setupLogger() {
}

public String getConnectionString() {
return String.format("%s:%s", getHost(), getMappedPort(clientPort));
return format("%s:%s", getHost(), getMappedPort(clientPort));
}

@Override
protected void containerIsStarting(InspectContainerResponse containerInfo, boolean reused) {
try {
super.containerIsStarting(containerInfo, reused);
if (reused) {
return;
}
String wrapperScript =
readFileFromClasspath("testcontainers/zookeeper_start_stop_wrapper.sh");
copyScriptToContainer(wrapperScript, this, START_STOP_SCRIPT);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}

public void restart() throws IOException, InterruptedException {
logger.info(
"Restarting zookeeper process with exposed container port {}", getMappedPort(clientPort));
stopAndWaitUntilKilled();
startAndWaitUntilRunning();
}

private void startAndWaitUntilRunning() throws IOException, InterruptedException {
execInContainer("sh", "-c", "touch /tmp/start");
Unreliables.retryUntilTrue(
(int) ZOOKEEPER_RESTART_WAIT_TIMEOUT.toSeconds(), TimeUnit.SECONDS, this::isStarted);
}

private void stopAndWaitUntilKilled() throws IOException, InterruptedException {
execInContainer("sh", "-c", "touch /tmp/stop");
Unreliables.retryUntilTrue(
(int) ZOOKEEPER_RESTART_WAIT_TIMEOUT.toSeconds(), TimeUnit.SECONDS, this::isStopped);
}

public boolean isStopped() throws IOException, InterruptedException {
ExecResult result =
execInContainer("sh", "-c", format("echo srvr | nc localhost %s", DEFAULT_ZOOKEEPER_PORT));
return result.getExitCode() != 0;
}

public boolean isStarted() throws IOException, InterruptedException {
ExecResult result =
execInContainer("sh", "-c", format("echo srvr | nc localhost %s", DEFAULT_ZOOKEEPER_PORT));
return result.getExitCode() == 0;
}
//
// @Override
// protected void containerIsStarting(InspectContainerResponse containerInfo, boolean reused) {
// try {
// super.containerIsStarting(containerInfo, reused);
// if (reused) {
// return;
// }
// String wrapperScript =
// readFileFromClasspath("testcontainers/zookeeper_start_stop_wrapper.sh");
// copyScriptToContainer(wrapperScript, this, START_STOP_SCRIPT);
// } catch (Exception ex) {
// throw new RuntimeException(ex);
// }
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ public void restart() throws IOException, InterruptedException {
consumers.stop();
frontend.stop();

hermesZookeeper.restart();

management.start();
consumers.start();
frontend.start();
Expand Down

0 comments on commit 41bcb7e

Please sign in to comment.