Skip to content

Commit

Permalink
improve logging in MsSQLTestDatabase
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane-airbyte committed Feb 25, 2024
1 parent 6102af1 commit 6cfd1be
Showing 1 changed file with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ static public MsSQLTestDatabase in(final BaseImage imageName, final ContainerMod

public MsSQLTestDatabase(final MSSQLServerContainer<?> container) {
super(container);
LOGGER.info("SGX creating new database. databaseId=" + this.databaseId + ", databaseName=" + getDatabaseName());
}

public MsSQLTestDatabase withCdc() {
Expand Down Expand Up @@ -104,39 +105,39 @@ public MsSQLTestDatabase withShortenedCapturePollingInterval() {

private void waitForAgentState(final boolean running) {
final String expectedValue = running ? "Running." : "Stopped.";
LOGGER.debug("Waiting for SQLServerAgent state to change to '{}'.", expectedValue);
LOGGER.info(formatLogLine("Waiting for SQLServerAgent state to change to '{}'."), expectedValue);
for (int i = 0; i < MAX_RETRIES; i++) {
try {
final var r = query(ctx -> ctx.fetch("EXEC master.dbo.xp_servicecontrol 'QueryState', N'SQLServerAGENT';").get(0));
if (expectedValue.equalsIgnoreCase(r.getValue(0).toString())) {
LOGGER.debug("SQLServerAgent state is '{}', as expected.", expectedValue);
LOGGER.info(formatLogLine("SQLServerAgent state is '{}', as expected."), expectedValue);
return;
}
LOGGER.debug("Retrying, SQLServerAgent state {} does not match expected '{}'.", r, expectedValue);
LOGGER.info(formatLogLine("Retrying, SQLServerAgent state {} does not match expected '{}'."), r, expectedValue);
} catch (final SQLException e) {
LOGGER.debug("Retrying agent state query after catching exception {}.", e.getMessage());
LOGGER.info(formatLogLine("Retrying agent state query after catching exception {}."), e.getMessage());
}
try {
Thread.sleep(1_000); // Wait one second between retries.
} catch (final InterruptedException e) {
throw new RuntimeException(e);
}
}
throw new RuntimeException("Exhausted retry attempts while polling for agent state");
throw new RuntimeException(formatLogLine("Exhausted retry attempts while polling for agent state"));
}

public MsSQLTestDatabase withWaitUntilMaxLsnAvailable() {
LOGGER.debug("Waiting for max LSN to become available for database {}.", getDatabaseName());
LOGGER.info(formatLogLine("Waiting for max LSN to become available for database {}."), getDatabaseName());
for (int i = 0; i < MAX_RETRIES; i++) {
try {
final var maxLSN = query(ctx -> ctx.fetch("SELECT sys.fn_cdc_get_max_lsn();").get(0).get(0, byte[].class));
if (maxLSN != null) {
LOGGER.debug("Max LSN available for database {}: {}", getDatabaseName(), Lsn.valueOf(maxLSN));
LOGGER.info(formatLogLine("Max LSN available for database {}: {}"), getDatabaseName(), Lsn.valueOf(maxLSN));
return self();
}
LOGGER.debug("Retrying, max LSN still not available for database {}.", getDatabaseName());
LOGGER.info(formatLogLine("Retrying, max LSN still not available for database {}."), getDatabaseName());
} catch (final SQLException e) {
LOGGER.warn("Retrying max LSN query after catching exception {}", e.getMessage());
LOGGER.info(formatLogLine("Retrying max LSN query after catching exception {}"), e.getMessage());
}
try {
Thread.sleep(1_000); // Wait one second between retries.
Expand Down

0 comments on commit 6cfd1be

Please sign in to comment.