use of com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet in project java-driver by datastax.
the class GraphRequestSyncProcessor method process.
@Override
public GraphResultSet process(GraphStatement<?> request, DefaultSession session, InternalDriverContext context, String sessionLogPrefix) {
BlockingOperation.checkNotDriverThread();
AsyncGraphResultSet firstPage = CompletableFutures.getUninterruptibly(asyncProcessor.process(request, session, context, sessionLogPrefix));
return GraphResultSets.toSync(firstPage);
}
use of com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet in project java-driver by datastax.
the class ContinuousGraphRequestHandlerSpeculativeExecutionTest method should_fail_if_no_more_nodes_and_speculative_execution_is_last.
@Test
@UseDataProvider(location = DseTestDataProviders.class, value = "idempotentGraphConfig")
public void should_fail_if_no_more_nodes_and_speculative_execution_is_last(boolean defaultIdempotence, GraphStatement<?> statement) throws Exception {
GraphRequestHandlerTestHarness.Builder harnessBuilder = GraphRequestHandlerTestHarness.builder().withDefaultIdempotence(defaultIdempotence);
PoolBehavior node1Behavior = harnessBuilder.customBehavior(node1);
PoolBehavior node2Behavior = harnessBuilder.customBehavior(node2);
try (GraphRequestHandlerTestHarness harness = harnessBuilder.build()) {
SpeculativeExecutionPolicy speculativeExecutionPolicy = harness.getContext().getSpeculativeExecutionPolicy(DriverExecutionProfile.DEFAULT_NAME);
long firstExecutionDelay = 100L;
when(speculativeExecutionPolicy.nextExecution(any(Node.class), eq(null), eq(statement), eq(1))).thenReturn(firstExecutionDelay);
GraphBinaryModule module = createGraphBinaryModule(harness.getContext());
CompletionStage<AsyncGraphResultSet> resultSetFuture = new ContinuousGraphRequestHandler(statement, harness.getSession(), harness.getContext(), "test", module, graphSupportChecker).handle();
node1Behavior.verifyWrite();
node1Behavior.setWriteSuccess();
// do not simulate a response from node1 yet
// next scheduled timeout should be the first speculative execution. Get it and run it.
CapturedTimeout speculativeExecution1 = harness.nextScheduledTimeout();
assertThat(speculativeExecution1.getDelay(TimeUnit.MILLISECONDS)).isEqualTo(firstExecutionDelay);
speculativeExecution1.task().run(speculativeExecution1);
// node1 now replies with a BOOTSTRAPPING error that triggers a RETRY_NEXT
// but the query plan is empty so the initial execution stops
node1Behavior.setResponseSuccess(defaultDseFrameOf(new Error(ProtocolConstants.ErrorCode.IS_BOOTSTRAPPING, "mock message")));
// Same thing with node2, so the speculative execution should reach the end of the query plan
// and fail the request
node2Behavior.setResponseSuccess(defaultDseFrameOf(new Error(ProtocolConstants.ErrorCode.IS_BOOTSTRAPPING, "mock message")));
assertThatStage(resultSetFuture).isFailed(error -> {
assertThat(error).isInstanceOf(AllNodesFailedException.class);
Map<Node, List<Throwable>> nodeErrors = ((AllNodesFailedException) error).getAllErrors();
assertThat(nodeErrors).containsOnlyKeys(node1, node2);
assertThat(nodeErrors.get(node1).get(0)).isInstanceOf(BootstrappingException.class);
assertThat(nodeErrors.get(node2).get(0)).isInstanceOf(BootstrappingException.class);
});
}
}
use of com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet in project java-driver by datastax.
the class ContinuousGraphRequestHandlerSpeculativeExecutionTest method should_stop_retrying_other_executions_if_result_complete.
@Test
@UseDataProvider(location = DseTestDataProviders.class, value = "idempotentGraphConfig")
public void should_stop_retrying_other_executions_if_result_complete(boolean defaultIdempotence, GraphStatement<?> statement) throws Exception {
GraphRequestHandlerTestHarness.Builder harnessBuilder = GraphRequestHandlerTestHarness.builder().withDefaultIdempotence(defaultIdempotence);
PoolBehavior node1Behavior = harnessBuilder.customBehavior(node1);
PoolBehavior node2Behavior = harnessBuilder.customBehavior(node2);
PoolBehavior node3Behavior = harnessBuilder.customBehavior(node3);
try (GraphRequestHandlerTestHarness harness = harnessBuilder.build()) {
SpeculativeExecutionPolicy speculativeExecutionPolicy = harness.getContext().getSpeculativeExecutionPolicy(DriverExecutionProfile.DEFAULT_NAME);
long firstExecutionDelay = 100L;
when(speculativeExecutionPolicy.nextExecution(any(Node.class), eq(null), eq(statement), eq(1))).thenReturn(firstExecutionDelay);
GraphBinaryModule module = createGraphBinaryModule(harness.getContext());
CompletionStage<AsyncGraphResultSet> resultSetFuture = new ContinuousGraphRequestHandler(statement, harness.getSession(), harness.getContext(), "test", module, graphSupportChecker).handle();
node1Behavior.verifyWrite();
node1Behavior.setWriteSuccess();
// next scheduled timeout should be the first speculative execution. Get it and run it.
CapturedTimeout speculativeExecution1 = harness.nextScheduledTimeout();
assertThat(speculativeExecution1.getDelay(TimeUnit.MILLISECONDS)).isEqualTo(firstExecutionDelay);
speculativeExecution1.task().run(speculativeExecution1);
node2Behavior.verifyWrite();
node2Behavior.setWriteSuccess();
// Complete the request from the initial execution
node1Behavior.setResponseSuccess(defaultDseFrameOf(singleGraphRow(GraphProtocol.GRAPH_BINARY_1_0, module)));
assertThatStage(resultSetFuture).isSuccess();
// node2 replies with a response that would trigger a RETRY_NEXT if the request was still
// running
node2Behavior.setResponseSuccess(defaultDseFrameOf(new Error(ProtocolConstants.ErrorCode.IS_BOOTSTRAPPING, "mock message")));
// The speculative execution should not move to node3 because it is stopped
node3Behavior.verifyNoWrite();
}
}
use of com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet in project java-driver by datastax.
the class GraphResultSetTestBase method mockPage.
/**
* Mocks an async result set where column 0 has type INT, with rows with the provided data.
*/
protected AsyncGraphResultSet mockPage(boolean nextPage, Integer... data) {
AsyncGraphResultSet page = mock(AsyncGraphResultSet.class);
ExecutionInfo executionInfo = mock(ExecutionInfo.class);
when(page.getRequestExecutionInfo()).thenReturn(executionInfo);
if (nextPage) {
when(page.hasMorePages()).thenReturn(true);
when(page.fetchNextPage()).thenReturn(spy(new CompletableFuture<>()));
} else {
when(page.hasMorePages()).thenReturn(false);
when(page.fetchNextPage()).thenThrow(new IllegalStateException());
}
// Emulate DefaultAsyncResultSet's internals (this is a bit sketchy, maybe it would be better
// to use real DefaultAsyncResultSet instances)
Queue<Integer> queue = Lists.newLinkedList(Arrays.asList(data));
CountingIterator<GraphNode> iterator = new CountingIterator<GraphNode>(queue.size()) {
@Override
protected GraphNode computeNext() {
Integer index = queue.poll();
return (index == null) ? endOfData() : mockRow(index);
}
};
when(page.currentPage()).thenReturn(() -> iterator);
when(page.remaining()).thenAnswer(invocation -> iterator.remaining());
return page;
}
use of com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet in project java-driver by datastax.
the class GraphResultSetsTest method should_create_result_set_from_single_page.
@Test
public void should_create_result_set_from_single_page() {
// Given
AsyncGraphResultSet page1 = mockPage(false, 0, 1, 2);
// When
GraphResultSet resultSet = GraphResultSets.toSync(page1);
// Then
assertThat(resultSet.getRequestExecutionInfo()).isSameAs(page1.getRequestExecutionInfo());
Iterator<GraphNode> iterator = resultSet.iterator();
assertNextRow(iterator, 0);
assertNextRow(iterator, 1);
assertNextRow(iterator, 2);
assertThat(iterator.hasNext()).isFalse();
}
Aggregations