Skip to content
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

Update Yugabyte wait strategy #7599

Closed
wants to merge 17 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public YugabyteDBYSQLContainer(final DockerImageName imageName) {
super(imageName);
imageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
withExposedPorts(YSQL_PORT, MASTER_DASHBOARD_PORT, TSERVER_DASHBOARD_PORT);
waitingFor(new YugabyteDBYSQLWaitStrategy(this).withStartupTimeout(Duration.ofSeconds(60)));
waitingFor(new YugabyteDBYSQLWaitStrategy().withStartupTimeout(Duration.ofSeconds(60)));
withCommand(ENTRYPOINT);
}

Expand Down Expand Up @@ -154,4 +154,9 @@ public YugabyteDBYSQLContainer withPassword(final String password) {
this.password = password;
return this;
}

@Override
protected void waitUntilContainerStarted() {
getWaitStrategy().waitUntilReady(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,20 @@ public void execute(
boolean continueOnError,
boolean ignoreFailedDrops
) {
final String containerInterfaceIP = container
.getContainerInfo()
.getNetworkSettings()
.getNetworks()
.entrySet()
.stream()
.findFirst()
.get()
.getValue()
.getIpAddress();
try {
ExecResult result = container.execInContainer(
BIN_PATH,
containerInterfaceIP,
"-u",
container.getUsername(),
"-p",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ public final class YugabyteDBYCQLWaitStrategy extends AbstractWaitStrategy {
public void waitUntilReady(WaitStrategyTarget target) {
YugabyteDBYCQLContainer container = (YugabyteDBYCQLContainer) target;
AtomicBoolean status = new AtomicBoolean(true);
final String containerInterfaceIP = container
.getContainerInfo()
.getNetworkSettings()
.getNetworks()
.entrySet()
.stream()
.findFirst()
.get()
.getValue()
.getIpAddress();
retryUntilSuccess(
(int) startupTimeout.getSeconds(),
TimeUnit.SECONDS,
Expand All @@ -46,6 +56,7 @@ public void waitUntilReady(WaitStrategyTarget target) {
try {
ExecResult result = container.execInContainer(
BIN_PATH,
containerInterfaceIP,
"-u",
container.getUsername(),
"-p",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import lombok.extern.slf4j.Slf4j;
import org.testcontainers.containers.YugabyteDBYSQLContainer;
import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;
import org.testcontainers.containers.wait.strategy.WaitStrategyTarget;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;

import static org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess;
Expand All @@ -27,32 +27,26 @@
@Slf4j
public final class YugabyteDBYSQLWaitStrategy extends AbstractWaitStrategy {

private static final String YSQL_TEST_QUERY = "SELECT 1";

private final WaitStrategyTarget target;
private static final String YSQL_EXTENDED_PROBE =
"CREATE TEMP TABLE IF NOT EXISTS YB_SAMPLE(k int, v int, primary key(k, v))";

@Override
public void waitUntilReady(WaitStrategyTarget target) {
YugabyteDBYSQLContainer container = (YugabyteDBYSQLContainer) target;
public void waitUntilReady() {
YugabyteDBYSQLContainer container = (YugabyteDBYSQLContainer) waitStrategyTarget;
retryUntilSuccess(
(int) startupTimeout.getSeconds(),
TimeUnit.SECONDS,
() -> {
getRateLimiter()
.doWhenReady(() -> {
try (Connection con = container.createConnection(container.getJdbcUrl())) {
con.createStatement().execute(YSQL_TEST_QUERY);
try (Connection con = container.createConnection(""); Statement stmt = con.createStatement()) {
stmt.execute(YSQL_EXTENDED_PROBE);
} catch (SQLException ex) {
log.error("Error connecting to the database", ex);
throw new RuntimeException(ex);
}
});
return true;
}
);
}

@Override
public void waitUntilReady() {
waitUntilReady(target);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class YugabyteDBYCQLTest {

private static final String IMAGE_NAME = "yugabytedb/yugabyte:2.14.4.0-b26";

private static final String IMAGE_NAME_2_18 = "yugabytedb/yugabyte:2.18.3.0-b75";

private static final DockerImageName YBDB_TEST_IMAGE = DockerImageName.parse(IMAGE_NAME);

@Test
Expand Down Expand Up @@ -106,6 +108,14 @@ public void testInitScript() {
}
}

@Test
public void shouldStartWhenContainerIpIsUsedInWaitStrategy() {
try (final YugabyteDBYCQLContainer ycqlContainer = new YugabyteDBYCQLContainer(IMAGE_NAME_2_18)) {
ycqlContainer.start();
assertThat(performQuery(ycqlContainer, "SELECT release_version FROM system.local").wasApplied()).isTrue();
}
}

private ResultSet performQuery(YugabyteDBYCQLContainer ycqlContainer, String cql) {
try (
CqlSession session = CqlSession
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,20 @@ public void testWithCustomRole() throws SQLException {
.isEqualTo(1);
}
}

@Test
public void testWaitStrategy() throws SQLException {
try (final YugabyteDBYSQLContainer ysqlContainer = new YugabyteDBYSQLContainer(YBDB_TEST_IMAGE)) {
ysqlContainer.start();
assertThat(performQuery(ysqlContainer, "SELECT 1").getInt(1))
.as("A sample test query succeeds")
.isEqualTo(1);
assertThat(
performQuery(ysqlContainer, "SELECT EXISTS (SELECT FROM pg_tables WHERE tablename = 'YB_SAMPLE')")
.getBoolean(1)
)
.as("yb_sample table does not exists")
.isEqualTo(false);
}
}
}
Loading