Skip to content

Commit c3d9aca

Browse files
nickkkcccdkasimovskiy
authored andcommitted
Improve testcontainers-java-tarantool
Closes #98.
1 parent deb1892 commit c3d9aca

File tree

5 files changed

+81
-32
lines changed

5 files changed

+81
-32
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,11 @@
22
target/
33
.idea/
44
*.iml
5+
.classpath
6+
.project
7+
.settings/
8+
*.swp
9+
workbench.xmi
10+
511

612
src/test/resources/cartridge/tmp/*

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## [Unreleased]
44

5+
- Bump testcontainers to 1.19.3
6+
57
## [1.0.2] - 2023-11-10
68

79
- Make a separate step for building the cartridge ([#94](https://github.com/tarantool/testcontainers-java-tarantool/issues/94))

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
</scm>
5454

5555
<properties>
56-
<testcontainers.version>1.18.0</testcontainers.version>
56+
<testcontainers.version>1.19.3</testcontainers.version>
5757
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5858
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
5959
<logging.config>${project.basedir}/src/test/resources/logback-test.xml</logging.config>

src/main/java/org/testcontainers/containers/TarantoolCartridgeContainer.java

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public class TarantoolCartridgeContainer extends GenericContainer<TarantoolCartr
120120
private String directoryResourcePath = SCRIPT_RESOURCE_DIRECTORY;
121121
private String instanceDir = INSTANCE_DIR;
122122
private String topologyConfigurationFile;
123+
private String instancesFile;
123124
private SslContext sslContext;
124125

125126
/**
@@ -205,6 +206,7 @@ private TarantoolCartridgeContainer(ImageFromDockerfile image, String instancesF
205206
if (topologyConfigurationFile == null || topologyConfigurationFile.isEmpty()) {
206207
throw new IllegalArgumentException("Topology configuration file must not be null or empty");
207208
}
209+
this.instancesFile = instancesFile;
208210
this.topologyConfigurationFile = topologyConfigurationFile;
209211
this.instanceFileParser = new CartridgeConfigParser(instancesFile);
210212
this.clientHelper = new TarantoolContainerClientHelper(this);
@@ -494,17 +496,23 @@ protected void containerIsStarting(InspectContainerResponse containerInfo) {
494496
}
495497

496498
private boolean setupTopology() {
497-
String fileType = topologyConfigurationFile.substring(topologyConfigurationFile.lastIndexOf('.') + 1);
498-
499+
String fileType = topologyConfigurationFile
500+
.substring(topologyConfigurationFile.lastIndexOf('.') + 1);
499501
if (fileType.equals("yml")) {
500502
String replicasetsFileName = topologyConfigurationFile
501-
.substring(topologyConfigurationFile.lastIndexOf('/') + 1);
502-
503+
.substring(topologyConfigurationFile.lastIndexOf('/') + 1);
504+
String instancesFileName = instancesFile
505+
.substring(instancesFile.lastIndexOf('/') + 1);
503506
try {
504-
ExecResult result = execInContainer("cartridge",
505-
"replicasets",
506-
"--run-dir=" + TARANTOOL_RUN_DIR,
507-
"--file=" + replicasetsFileName, "setup", "--bootstrap-vshard");
507+
ExecResult result = execInContainer(
508+
"cartridge",
509+
"replicasets",
510+
"--run-dir=" + TARANTOOL_RUN_DIR,
511+
"--file=" + replicasetsFileName,
512+
"--cfg=" + instancesFileName,
513+
"setup",
514+
"--bootstrap-vshard"
515+
);
508516
if (result.getExitCode() != 0) {
509517
throw new CartridgeTopologyException("Failed to change the app topology via cartridge CLI: "
510518
+ result.getStdout());
@@ -540,7 +548,7 @@ private void retryingSetupTopology() {
540548
if (!setupTopology()) {
541549
try {
542550
logger().info("Retrying setup topology in 10 seconds");
543-
Thread.sleep(10000);
551+
Thread.sleep(1_000);
544552
} catch (InterruptedException e) {
545553
throw new RuntimeException(e);
546554
}
@@ -563,10 +571,10 @@ private void bootstrapVshard() {
563571
protected void containerIsStarted(InspectContainerResponse containerInfo, boolean reused) {
564572
super.containerIsStarted(containerInfo, reused);
565573

566-
waitUntilRouterIsUp(60);
574+
waitUntilRouterIsUp(120);
567575
retryingSetupTopology();
568576
// wait until Roles are configured
569-
waitUntilCartridgeIsHealthy(10);
577+
waitUntilCartridgeIsHealthy(120);
570578
bootstrapVshard();
571579

572580
logger().info("Tarantool Cartridge cluster is started");
@@ -575,49 +583,79 @@ protected void containerIsStarted(InspectContainerResponse containerInfo, boolea
575583
}
576584

577585
private void waitUntilRouterIsUp(int secondsToWait) {
578-
waitUntilTrue(secondsToWait, this::routerIsUp);
586+
if(!waitUntilTrue(secondsToWait, this::routerIsUp)) {
587+
throw new RuntimeException("Timeout exceeded during router upping stage." +
588+
" See the specific error in logs.");
589+
}
579590
}
580591

581592
private void waitUntilCartridgeIsHealthy(int secondsToWait) {
582-
waitUntilTrue(secondsToWait, this::isCartridgeHealthy);
593+
if(!waitUntilTrue(secondsToWait, this::isCartridgeHealthy)) {
594+
throw new RuntimeException("Timeout exceeded during cartridge topology applying stage." +
595+
" See the specific error in logs.");
596+
}
583597
}
584598

585-
private void waitUntilTrue(int secondsToWait, Supplier<Boolean> waitFunc) {
599+
private boolean waitUntilTrue(int secondsToWait, Supplier<Boolean> waitFunc) {
586600
int secondsPassed = 0;
587601
boolean result = waitFunc.get();
588602
while (!result && secondsPassed < secondsToWait) {
589603
result = waitFunc.get();
590604
try {
591-
Thread.sleep(1000);
605+
Thread.sleep(1_000);
606+
secondsPassed++;
592607
} catch (InterruptedException e) {
593608
break;
594609
}
595610
}
596-
if (!result) {
597-
throw new RuntimeException("Failed to change the app topology after retry");
598-
}
611+
return result;
599612
}
600613

601614
private boolean routerIsUp() {
602-
String healthyCmd = " local cartridge = package.loaded['cartridge']" +
603-
" return cartridge ~= nil";
615+
// String healthyCmd = " local cartridge = package.loaded['cartridge']" +
616+
// " return cartridge ~= nil";
617+
String healthyCmd = "return require('cartridge').is_healthy()";
618+
ExecResult result;
619+
604620
try {
605-
List<?> result = executeCommandDecoded(healthyCmd);
606-
return result.get(0).getClass() == Boolean.class && (Boolean) result.get(0);
607-
} catch (Exception e) {
608-
logger().warn("Error while waiting for router instance to be up: " + e.getMessage());
621+
result = executeCommand(healthyCmd);
622+
if (result.getExitCode() != 0 && result.getStderr().contains("Connection refused") &&
623+
result.getStdout().isEmpty()) {
624+
return false;
625+
} else if (result.getExitCode() != 0) {
626+
logger().error("exit code: {}, stdout: {}, stderr: {}",result.getExitCode(), result.getStdout(),
627+
result.getStderr());
628+
return false;
629+
} else {
630+
return true;
631+
}
632+
}catch (Exception e) {
633+
logger().error(e.getMessage());
609634
return false;
610635
}
636+
611637
}
612638

613639
private boolean isCartridgeHealthy() {
614-
String healthyCmd = " local cartridge = package.loaded['cartridge']" +
615-
" return cartridge ~= nil and cartridge.is_healthy()";
640+
String healthyCmd = " return require('cartridge').is_healthy()";
641+
ExecResult result;
616642
try {
617-
List<?> result = executeCommandDecoded(healthyCmd);
618-
return result.get(0).getClass() == Boolean.class && (Boolean) result.get(0);
643+
result = executeCommand(healthyCmd);
644+
if (result.getExitCode() != 0) {
645+
logger().error("exitCode: {}, stdout: {}, stderr: {}", result.getExitCode(), result.getStdout(),
646+
result.getStderr());
647+
return false;
648+
} else if (result.getStdout().startsWith("---\n- null\n")){
649+
return false;
650+
} else if (result.getStdout().contains("true")) {
651+
return true;
652+
} else {
653+
logger().warn("exitCode: {}, stdout: {}, stderr: {}", result.getExitCode(), result.getStdout(),
654+
result.getStderr());
655+
return false;
656+
}
619657
} catch (Exception e) {
620-
logger().warn("Error while waiting for cartridge healthy state: " + e.getMessage());
658+
logger().error("Error while waiting for cartridge healthy state: " + e.getMessage());
621659
return false;
622660
}
623661
}

src/main/resources/Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ ARG TARANTOOL_DATADIR="/tmp/data"
2323
ARG TARANTOOL_LOGDIR="/tmp/log"
2424
ARG TARANTOOL_INSTANCES_FILE="./instances.yml"
2525
ARG TARANTOOL_CLUSTER_COOKIE="testapp-cluster-cookie"
26+
ARG START_DELAY="5s"
27+
ENV START_DELAY=$START_DELAY
2628
ENV TARANTOOL_WORKDIR=$TARANTOOL_WORKDIR
2729
ENV TARANTOOL_RUNDIR=$TARANTOOL_RUNDIR
2830
ENV TARANTOOL_DATADIR=$TARANTOOL_DATADIR
@@ -31,6 +33,7 @@ ENV TARANTOOL_INSTANCES_FILE=$TARANTOOL_INSTANCES_FILE
3133
ENV TARANTOOL_CLUSTER_COOKIE=$TARANTOOL_CLUSTER_COOKIE
3234
COPY $CARTRIDGE_SRC_DIR $TARANTOOL_WORKDIR
3335
WORKDIR $TARANTOOL_WORKDIR
34-
RUN cartridge build
35-
CMD cartridge start --run-dir=$TARANTOOL_RUNDIR --data-dir=$TARANTOOL_DATADIR \
36+
37+
RUN rm -rf .rocks && cartridge build --verbose
38+
CMD sleep $START_DELAY && cartridge start --run-dir=$TARANTOOL_RUNDIR --data-dir=$TARANTOOL_DATADIR \
3639
--log-dir=$TARANTOOL_LOGDIR --cfg=$TARANTOOL_INSTANCES_FILE

0 commit comments

Comments
 (0)