Skip to content
Open
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 @@ -36,6 +36,7 @@
import com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures;
import com.datastax.oss.driver.internal.core.util.concurrent.Debouncer;
import com.datastax.oss.driver.internal.core.util.concurrent.RunOrSchedule;
import com.datastax.oss.driver.internal.core.util.concurrent.UncaughtExceptions;
import com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down Expand Up @@ -436,6 +437,9 @@ private void startSchemaRequest(CompletableFuture<RefreshSchemaResult> refreshFu
(schemaInAgreement, agreementError) -> {
if (agreementError != null) {
refreshFuture.completeExceptionally(agreementError);
adminExecutor
.submit(this::finishSchemaRequest)
.addListener(UncaughtExceptions::log);
} else {
try {
schemaQueriesFactory
Expand All @@ -453,18 +457,16 @@ private void startSchemaRequest(CompletableFuture<RefreshSchemaResult> refreshFu

firstSchemaRefreshFuture.complete(null);

currentSchemaRefresh = null;
// If another refresh was enqueued during this one, run it now
if (queuedSchemaRefresh != null) {
CompletableFuture<RefreshSchemaResult> tmp =
this.queuedSchemaRefresh;
this.queuedSchemaRefresh = null;
startSchemaRequest(tmp);
}
adminExecutor
.submit(this::finishSchemaRequest)
.addListener(UncaughtExceptions::log);
});
} catch (Throwable t) {
LOG.debug("[{}] Exception getting new metadata", logPrefix, t);
refreshFuture.completeExceptionally(t);
adminExecutor
.submit(this::finishSchemaRequest)
.addListener(UncaughtExceptions::log);
}
}
});
Expand All @@ -476,6 +478,17 @@ private void startSchemaRequest(CompletableFuture<RefreshSchemaResult> refreshFu
}
}

private void finishSchemaRequest() {
assert adminExecutor.inEventLoop();
currentSchemaRefresh = null;
// If another refresh was enqueued during this one, run it now
if (queuedSchemaRefresh != null) {
CompletableFuture<RefreshSchemaResult> tmp = this.queuedSchemaRefresh;
this.queuedSchemaRefresh = null;
startSchemaRequest(tmp);
}
}

// To query schema tables, we need the control connection.
// Normally that the topology monitor has already initialized it to query node tables. But if a
// custom topology monitor is in place, it might not use the control connection at all.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@
import com.datastax.oss.driver.internal.core.context.InternalDriverContext;
import com.datastax.oss.driver.internal.core.context.NettyOptions;
import com.datastax.oss.driver.internal.core.control.ControlConnection;
import com.datastax.oss.driver.internal.core.metadata.schema.parsing.SchemaParser;
import com.datastax.oss.driver.internal.core.metadata.schema.parsing.SchemaParserFactory;
import com.datastax.oss.driver.internal.core.metadata.schema.queries.SchemaQueries;
import com.datastax.oss.driver.internal.core.metadata.schema.queries.SchemaQueriesFactory;
import com.datastax.oss.driver.internal.core.metadata.schema.queries.SchemaRows;
import com.datastax.oss.driver.internal.core.metadata.schema.refresh.SchemaRefresh;
import com.datastax.oss.driver.internal.core.metrics.MetricsFactory;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet;
Expand Down Expand Up @@ -313,6 +317,118 @@ public void refreshSchema_should_work() {
assertThatStage(result).isFailed(t -> assertThat(t).isEqualTo(expectedException));
}

@Test
public void should_run_queued_request_after_schema_queries_factory_throws() {
// Given
IllegalStateException expectedException =
new IllegalStateException("Control channel not available, aborting schema refresh");
CompletableFuture<Boolean> firstAgreement = new CompletableFuture<>();
SchemaQueries schemaQueries = stubSchemaQueriesAndParser();
when(schemaQueriesFactory.newInstance()).thenThrow(expectedException).thenReturn(schemaQueries);
when(topologyMonitor.checkSchemaAgreement())
.thenReturn(firstAgreement)
.thenReturn(CompletableFuture.completedFuture(Boolean.TRUE));
initNodesForSchemaRefresh();
CompletionStage<MetadataManager.RefreshSchemaResult> first =
metadataManager.refreshSchema("foo", true, true);
verify(topologyMonitor, timeout(500)).checkSchemaAgreement();
waitForPendingAdminTasks();
CompletionStage<MetadataManager.RefreshSchemaResult> second =
metadataManager.refreshSchema("foo", true, true);
waitForPendingAdminTasks();
assertThat(second.toCompletableFuture().isDone()).isFalse();

// When
firstAgreement.complete(Boolean.FALSE);

// Then
waitForPendingAdminTasks(() -> first.toCompletableFuture().isDone());
assertThatStage(first).isFailed(t -> assertThat(t).isEqualTo(expectedException));
waitForPendingAdminTasks(() -> second.toCompletableFuture().isDone());
assertThatStage(second).isSuccess();
assertThat(metadataManager.refreshes).hasSize(2);
}

@Test
public void should_run_next_request_after_schema_agreement_fails() {
// Given
IllegalStateException expectedException = new IllegalStateException("Agreement failed");
CompletableFuture<Boolean> failedAgreement = new CompletableFuture<>();
SchemaQueries schemaQueries = stubSchemaQueriesAndParser();
when(schemaQueriesFactory.newInstance()).thenReturn(schemaQueries);
when(topologyMonitor.checkSchemaAgreement())
.thenReturn(failedAgreement)
.thenReturn(CompletableFuture.completedFuture(Boolean.TRUE));
initNodesForSchemaRefresh();
CompletionStage<MetadataManager.RefreshSchemaResult> first =
metadataManager.refreshSchema("foo", true, true);
verify(topologyMonitor, timeout(500)).checkSchemaAgreement();
waitForPendingAdminTasks();

// When
failedAgreement.completeExceptionally(expectedException);
waitForPendingAdminTasks(() -> first.toCompletableFuture().isDone());
CompletionStage<MetadataManager.RefreshSchemaResult> second =
metadataManager.refreshSchema("foo", true, true);

// Then
assertThatStage(first).isFailed(t -> assertThat(t).isEqualTo(expectedException));
waitForPendingAdminTasks(() -> second.toCompletableFuture().isDone());
assertThatStage(second).isSuccess();
assertThat(metadataManager.refreshes).hasSize(2);
}

@Test
public void should_run_queued_request_after_schema_queries_fail() {
// Given
IllegalStateException expectedException = new IllegalStateException("Schema query failed");
CompletableFuture<SchemaRows> failingRows = new CompletableFuture<>();
SchemaQueries failingQueries = mock(SchemaQueries.class);
when(failingQueries.execute()).thenReturn(failingRows);
SchemaQueries schemaQueries = stubSchemaQueriesAndParser();
when(schemaQueriesFactory.newInstance()).thenReturn(failingQueries).thenReturn(schemaQueries);
when(topologyMonitor.checkSchemaAgreement())
.thenReturn(CompletableFuture.completedFuture(Boolean.TRUE));
initNodesForSchemaRefresh();
CompletionStage<MetadataManager.RefreshSchemaResult> first =
metadataManager.refreshSchema("foo", true, true);
verify(failingQueries, timeout(500)).execute();
waitForPendingAdminTasks();
CompletionStage<MetadataManager.RefreshSchemaResult> second =
metadataManager.refreshSchema("foo", true, true);
waitForPendingAdminTasks();
assertThat(second.toCompletableFuture().isDone()).isFalse();

// When
failingRows.completeExceptionally(expectedException);

// Then
waitForPendingAdminTasks(() -> first.toCompletableFuture().isDone());
assertThatStage(first).isFailed(t -> assertThat(t).isEqualTo(expectedException));
waitForPendingAdminTasks(() -> second.toCompletableFuture().isDone());
assertThatStage(second).isSuccess();
assertThat(metadataManager.refreshes).hasSize(2);
}

private SchemaQueries stubSchemaQueriesAndParser() {
SchemaQueries schemaQueries = mock(SchemaQueries.class);
SchemaRows schemaRows = mock(SchemaRows.class);
SchemaParser schemaParser = mock(SchemaParser.class);
when(schemaQueries.execute()).thenReturn(CompletableFuture.completedFuture(schemaRows));
when(schemaParserFactory.newInstance(schemaRows)).thenReturn(schemaParser);
when(schemaParser.parse()).thenReturn(mock(SchemaRefresh.class));
return schemaQueries;
}

private void initNodesForSchemaRefresh() {
when(topologyMonitor.refreshNodeList())
.thenReturn(CompletableFuture.completedFuture(ImmutableList.of(mock(NodeInfo.class))));
when(controlConnection.init(anyBoolean(), anyBoolean(), anyBoolean()))
.thenReturn(CompletableFuture.completedFuture(null));
metadataManager.refreshNodes();
waitForPendingAdminTasks(() -> metadataManager.refreshes.size() == 1);
}

private static class TestMetadataManager extends MetadataManager {

private List<MetadataRefresh> refreshes = new CopyOnWriteArrayList<>();
Expand Down Expand Up @@ -353,4 +469,10 @@ public void removeNode(InetSocketAddress broadcastRpcAddress) {
private void waitForPendingAdminTasks(Callable<Boolean> condition) {
await().atMost(500, TimeUnit.MILLISECONDS).until(condition);
}

private void waitForPendingAdminTasks() {
assertThat(
adminEventLoopGroup.submit(() -> null).awaitUninterruptibly(500, TimeUnit.MILLISECONDS))
.isTrue();
}
}
Loading