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

Changed YSQL wait strategy in YugabyteDB module #7784

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -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 @@ -8,6 +8,7 @@

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,10 +28,13 @@
@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 TABLE IF NOT EXISTS YB_SAMPLE(k int, v int, primary key(k, v))";

private static final String YSQL_EXTENDED_PROBE_DROP_TABLE = "DROP TABLE IF EXISTS YB_SAMPLE";
HarshDaryani896 marked this conversation as resolved.
Show resolved Hide resolved

@Override
public void waitUntilReady(WaitStrategyTarget target) {
YugabyteDBYSQLContainer container = (YugabyteDBYSQLContainer) target;
Expand All @@ -40,10 +44,11 @@ public void waitUntilReady(WaitStrategyTarget target) {
() -> {
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);
stmt.execute(YSQL_EXTENDED_PROBE_DROP_TABLE);
} catch (SQLException ex) {
log.error("Error connecting to the database", ex);
throw new RuntimeException(ex);
}
});
return true;
Expand Down
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