Skip to content

Configure cluster from yaml #40

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

Merged
merged 13 commits into from
Apr 27, 2022
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,4 @@
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,18 @@ public class TarantoolCartridgeContainer extends GenericContainer<TarantoolCartr
private static final String ENV_TARANTOOL_RUNDIR = "TARANTOOL_RUNDIR";
private static final String ENV_TARANTOOL_DATADIR = "TARANTOOL_DATADIR";
private static final String ENV_TARANTOOL_INSTANCES_FILE = "TARANTOOL_INSTANCES_FILE";
private final CartridgeConfigParser instanceFileParser;
private final TarantoolContainerClientHelper clientHelper;
private boolean useFixedPorts = false;

private String routerHost = ROUTER_HOST;
private int routerPort = ROUTER_PORT;
private int apiPort = API_PORT;
private String routerUsername = CARTRIDGE_USERNAME;
private String routerPassword = CARTRIDGE_PASSWORD;
private String directoryResourcePath = SCRIPT_RESOURCE_DIRECTORY;
private String instanceDir = INSTANCE_DIR;
private final CartridgeConfigParser instanceFileParser;
private final String topologyConfigurationFile;
private final TarantoolContainerClientHelper clientHelper;
private String topologyConfigurationFile;
private String replicasetsFileName;

/**
* Create a container with default image and specified instances file from the classpath resources. Assumes that
Expand Down Expand Up @@ -166,7 +166,6 @@ public TarantoolCartridgeContainer(String dockerFile, String buildImageName,
this(dockerFile, buildImageName, instancesFile, topologyConfigurationFile, Collections.emptyMap());
}


/**
* Create a container with specified image and specified instances file from the classpath resources. By providing
* the result Cartridge container image name, you can cache the image and avoid rebuilding on each test run (the
Expand All @@ -186,13 +185,22 @@ public TarantoolCartridgeContainer(String dockerFile, String buildImageName, Str
instancesFile, topologyConfigurationFile);
}


private TarantoolCartridgeContainer(Future<String> image, String instancesFile, String topologyConfigurationFile) {
super(image);
if (instancesFile == null || instancesFile.isEmpty()) {
throw new IllegalArgumentException("Instance file name must not be null or empty");
}
if (topologyConfigurationFile == null || topologyConfigurationFile.isEmpty()) {
throw new IllegalArgumentException("Topology configuration file must not be null or empty");
}
String fileType = topologyConfigurationFile.substring(topologyConfigurationFile.lastIndexOf('.') + 1);
if (fileType.equals("lua")) {
this.topologyConfigurationFile = topologyConfigurationFile;
}else{
this.replicasetsFileName = topologyConfigurationFile.substring(topologyConfigurationFile.lastIndexOf('/')+1);
}
this.instanceFileParser = new CartridgeConfigParser(instancesFile);
this.topologyConfigurationFile = topologyConfigurationFile;
this.clientHelper = new TarantoolContainerClientHelper(this);
}

Expand Down Expand Up @@ -435,7 +443,6 @@ protected void configure() {
if (!getDirectoryBinding().isEmpty()) {
withFileSystemBind(getDirectoryBinding(), getInstanceDir(), BindMode.READ_WRITE);
}

if (useFixedPorts) {
for (Integer port : instanceFileParser.getExposablePorts()) {
addFixedExposedPort(port, port);
Expand All @@ -451,21 +458,55 @@ protected void containerIsStarting(InspectContainerResponse containerInfo) {
}

private boolean setupTopology() {
try {
executeScript(topologyConfigurationFile).get();
// The client connection will be closed after that command
} catch (Exception e) {
if (e instanceof ExecutionException) {
if (e.getCause() instanceof TimeoutException) {
return true;
// Do nothing, the cluster is reloading
} else if (e.getCause() instanceof TarantoolConnectionException) {
// Probably cluster is not ready
logger().error("Failed to setup topology: {}", e.getMessage());
return false;
if (topologyConfigurationFile == null) {
String runDirPath = null;
try {
Container.ExecResult envVariablesContainer = this.execInContainer("env");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you are getting container env ?
see withArguments()

You can just use System.getenv() or default to ./tmp/run

Copy link
Contributor Author

@Voloodya Voloodya Apr 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just use System.getenv() or default to ./tmp/run

I try use System.getenv() - this doesn't work (haven't need env).
Default to ./tmp/run - would not like use hardcore

see withArguments()

We maybe don't pass "ENV_TARANTOOL_RUNDIR" in Argumets. This is env setup to Dockerfile.

String stdout = envVariablesContainer.getStdout();
int exitCode = envVariablesContainer.getExitCode();
if (exitCode != 0) {
logger().error("Failed to bootstrap replica sets topology: {}", stdout);
}
int startInd = stdout.lastIndexOf(ENV_TARANTOOL_RUNDIR + "=");
try {
runDirPath = stdout.substring(startInd,
stdout.indexOf('\n', startInd)).split("=")[1];
} catch (Exception e) {
logger().error("Missing dir-run environment variable: {}", e.getMessage());
}

} catch (Exception e) {
logger().error("Failed to get environment variables: {}", e.getMessage());
}

try {
Container.ExecResult lsResult = this.execInContainer("cartridge", "replicasets", "--run-dir=" + runDirPath,
"--file=" + this.replicasetsFileName, "setup", "--bootstrap-vshard");
String stdout = lsResult.getStdout();
int exitCode = lsResult.getExitCode();
if (exitCode != 0) {
logger().error("Failed to bootstrap replica sets topology: {}", stdout);
}
} catch (Exception e) {
logger().error("Failed to bootstrap replica sets topology: {}", e.getMessage());
}
} else {
try {
executeScript(topologyConfigurationFile).get();
// The client connection will be closed after that command
} catch (Exception e) {
if (e instanceof ExecutionException) {
if (e.getCause() instanceof TimeoutException) {
return true;
// Do nothing, the cluster is reloading
} else if (e.getCause() instanceof TarantoolConnectionException) {
// Probably cluster is not ready
logger().error("Failed to setup topology: {}", e.getMessage());
return false;
}
} else {
throw new RuntimeException("Failed to change the app topology", e);
}
} else {
throw new RuntimeException("Failed to change the app topology", e);
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.testcontainers.containers;

import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.MountableFile;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

public class TarantoolCartridgeContainerReplicasetsTest {

@Test
public void test_ClusterContainer_StartsSuccessfully_ifFilesAreCopiedUnderRoot() throws Exception {
Map<String, String> buildArgs = new HashMap<String, String>() {
{
put("TARANTOOL_SERVER_USER", "root");
put("TARANTOOL_SERVER_UID", "0");
put("TARANTOOL_SERVER_GROUP", "root");
put("TARANTOOL_SERVER_GID", "0");
}
};
TarantoolCartridgeContainer container =
new TarantoolCartridgeContainer(
"Dockerfile",
"testcontainers-java-tarantool:test",
"cartridge/instances.yml",
"cartridge/replicasets.yml",
buildArgs)
.withCopyFileToContainer(MountableFile.forClasspathResource("cartridge"), "/app")
.withStartupTimeout(Duration.ofSeconds(300))
.withLogConsumer(new Slf4jLogConsumer(
LoggerFactory.getLogger(TarantoolCartridgeContainerReplicasetsTest.class)));

container.start();
CartridgeContainerTestUtils.executeProfileReplaceSmokeTest(container);
if(container.isRunning())
container.stop();
}
}
18 changes: 18 additions & 0 deletions src/test/resources/cartridge/replicasets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
router:
instances:
- router
roles:
- failover-coordinator
- vshard-router
- app.roles.custom
- app.roles.api_router
all_rw: false
s-1:
instances:
- s1-master
roles:
- vshard-storage
- app.roles.api_storage
weight: 1
all_rw: false
vshard_group: default