Skip to content

Commit

Permalink
add missing docs and reformat lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Tosca committed May 19, 2024
1 parent dcecf20 commit c503909
Showing 1 changed file with 42 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,27 @@
import java.nio.file.Path;

/**
* Constructs a single in memory <a href="https://www.foundationdb.org/">FoundationDB</a> database for testing
* transactions.
* Constructs a single in memory <a href="https://www.foundationdb.org/">FoundationDB</a> database
* for testing transactions.
*
* <p>Tested on a FoundationDB version 7.1.61 (the default version if not specified).</p>
* <p>Uses foundationdb:7.1.61 by default</p>
*
* <p>Other docker images can be used from
* <a href="https://hub.docker.com/r/foundationdb/foundationdb">foundationdb docker hub</a>. The FoundationDB
* API version must be aligned with the docker version used (eg. for 7.1.61 use api version 710).</p>
* <a href="https://hub.docker.com/r/foundationdb/foundationdb">foundationdb docker hub</a>.
* The FoundationDB API version must be aligned with the docker version used
* (e.g., for 7.1.61 use api version 710).</p>
*
* <p>FDB requires the native client libraries be installed separately from the java bindings. Install the libraries
* before using the java FDB client. Also, it might have issues working on newer macOS with the java bindings, try using
* java 8 and `export DYLD_LIBRARY_PATH=/usr/local/lib` in environment variables after installing FDB clients locally.
* <p>FDB requires the native client libraries be installed separately from the java bindings.
* Install the libraries before using the java FDB client. Also, it might have issues working
* on macOS with the java bindings, try using `export DYLD_LIBRARY_PATH=/usr/local/lib` in
* environment variables after installing FDB clients locally.
* </p>
*/
@Slf4j
public class FoundationDBContainer extends GenericContainer<FoundationDBContainer> {

private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("foundationdb/foundationdb");
private static final DockerImageName DEFAULT_IMAGE_NAME =
DockerImageName.parse("foundationdb/foundationdb");

private static final String DEFAULT_TAG = "7.1.61";

Expand Down Expand Up @@ -107,20 +110,26 @@ public String getClusterFilePath() {
@SneakyThrows
@Override
public void doStart() {
// FDB server port and the port seen by FDB clients must match, otherwise the FDB server will crash with
// errors like "Assertion pkt.canonicalRemotePort == peerAddress.port failed..."
// FDB server port and the port seen by FDB clients must match,
// otherwise the FDB server will crash with errors like
// "Assertion pkt.canonicalRemotePort == peerAddress.port failed..."
proxy =
new SocatContainer()
.withNetwork(getNetwork())
// the real port we want to proxy to FDB, will get the binding port after socat proxy container is starting
// the real port we want to proxy to FDB,
// will get the binding port after socat proxy container is starting
.withExposedPorts(INTERNAL_PORT)
// unused port, needed for the container to start before mapping the real port to the bind port of FDB
// cannot use this directly as the mapped port will be known after proxy container starts
// unused port, needed for the container to start before mapping the real port
// to the bind port of FDB
// cannot use this directly as the mapped port will be known after
// the proxy container starts
.withTarget(INTERNAL_PORT + 1, networkAlias)
.withReuse(this.isShouldBeReused());
proxy.setWaitStrategy(null); // set so it does not wait for the check on the INTERNAL_PORT also
// set so it does not wait for the check on the INTERNAL_PORT also
proxy.setWaitStrategy(null);
proxy.start();
// now socat proxy has started, will use the mapped port as the FDB port, so it will match with the
// now socat proxy has started, will use the mapped port as the FDB port,
// so it will match with the
// socat exposed port which is "seen" by FDB clients
bindPort = proxy.getMappedPort(INTERNAL_PORT);
// FDB server bind port can be set on env before starting the FDB container
Expand All @@ -137,7 +146,11 @@ public void doStart() {
private void proxyBindPort(final int listenPort, final int mappedPort) {
final ExecCreateCmdResponse createCmdResponse = dockerClient
.execCreateCmd(proxy.getContainerId())
.withCmd("socat", "TCP-LISTEN:" + listenPort + ",fork,reuseaddr", "TCP:" + networkAlias + ":" + mappedPort)
.withCmd(
"socat",
"TCP-LISTEN:" + listenPort + ",fork,reuseaddr",
"TCP:" + networkAlias + ":" + mappedPort
)
.exec();

final ToStringConsumer stdOutConsumer = new ToStringConsumer();
Expand All @@ -154,15 +167,17 @@ private void proxyBindPort(final int listenPort, final int mappedPort) {
}
final String stdErr = stdErrConsumer.toString(StandardCharsets.UTF_8);
if (!stdErr.isEmpty()) {
final String errorMessage = String.format("Error when attempting to bind port with socat: %s", stdErr);
final String errorMessage = String
.format("Error when attempting to bind port with socat: %s", stdErr);
log.error("{}", errorMessage);
throw new ProxyInitializationException(errorMessage);
}
}

@Override
protected void containerIsStarted(final InspectContainerResponse containerInfo) {
// is faster when not checking if the database is initialized, and this is only necessary if reusing container
// is faster when not checking if the database is initialized,
// and this is only necessary if reusing container
if (isShouldBeReused()) {
if (!isDatabaseInitialized()) {
initDatabaseSingleInMemory();
Expand Down Expand Up @@ -193,7 +208,8 @@ private void initDatabaseSingleInMemory() {
final String output = runCliExecOutput("configure new single memory");
if (!output.contains("Database created")) {
final String errorMessage = String.format(
"Database not created when attempting to initialize a new single memory database. Output was: %s",
"Database not created when attempting to initialize " +
"a new single memory database. Output was: %s",
output
);
log.error(errorMessage);
Expand All @@ -219,15 +235,19 @@ private String runCliExecOutput(final String command) {
return execResult.getStdout();
}

/**
* Exception thrown when there is an issue initializing the database.
*/
public static class DatabaseInitializationException extends RuntimeException {

DatabaseInitializationException(final String errorMessage) {
super(errorMessage);
}
}

/**
* Exception thrown when there is an issue initializing the proxy to the database.
*/
public static class ProxyInitializationException extends RuntimeException {

ProxyInitializationException(final String errorMessage) {
super(errorMessage);
}
Expand Down

0 comments on commit c503909

Please sign in to comment.