-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
YugabyteDBYCQLWaitStrategy.java
71 lines (61 loc) · 2.3 KB
/
YugabyteDBYCQLWaitStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package org.testcontainers.containers.strategy;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.testcontainers.containers.Container.ExecResult;
import org.testcontainers.containers.YugabyteDBYCQLContainer;
import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;
import org.testcontainers.containers.wait.strategy.WaitStrategyTarget;
import static org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess;
/**
* Custom wait strategy for YCQL API.
*
* <p>
* Though we can either use HTTP or PORT based wait strategy, when we create a custom
* keyspace/role, it gets executed asynchronously. As the wait on container.start() on a
* specific port wouldn't fully guarantee the custom object execution. It's better to
* check the DB status with this way with a smoke test query that uses the underlying
* custom objects and wait for the operation to complete.
* </p>
*
* @author srinivasa-vasu
*/
@RequiredArgsConstructor
@Slf4j
public final class YugabyteDBYCQLWaitStrategy extends AbstractWaitStrategy {
private static final String YCQL_TEST_QUERY = "SELECT release_version FROM system.local";
private static final String BIN_PATH = "/home/yugabyte/tserver/bin/ycqlsh";
private final WaitStrategyTarget target;
@Override
public void waitUntilReady(WaitStrategyTarget target) {
YugabyteDBYCQLContainer container = (YugabyteDBYCQLContainer) target;
AtomicBoolean status = new AtomicBoolean(true);
retryUntilSuccess((int) startupTimeout.getSeconds(), TimeUnit.SECONDS, () -> {
YugabyteDBYCQLWaitStrategy.this.getRateLimiter().doWhenReady(() -> {
try {
ExecResult result = container.execInContainer(BIN_PATH, "-u", container.getUsername(), "-p",
container.getPassword(), "-k", container.getKeyspace(), "-e", YCQL_TEST_QUERY);
if (result.getExitCode() != 0) {
status.set(false);
log.debug(result.getStderr());
}
}
catch (Exception e) {
status.set(false);
log.debug(e.getMessage(), e);
}
finally {
if (!status.getAndSet(true)) {
throw new RuntimeException("container hasn't come up yet");
}
}
});
return status;
});
}
@Override
public void waitUntilReady() {
waitUntilReady(target);
}
}