Search in sources :

Example 1 with AsyncGraphResultSet

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);
}
Also used : AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet)

Example 2 with AsyncGraphResultSet

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);
        });
    }
}
Also used : PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) AllNodesFailedException(com.datastax.oss.driver.api.core.AllNodesFailedException) Node(com.datastax.oss.driver.api.core.metadata.Node) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) Error(com.datastax.oss.protocol.internal.response.Error) SpeculativeExecutionPolicy(com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy) GraphTestUtils.createGraphBinaryModule(com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule) GraphBinaryModule(com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule) AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet) List(java.util.List) CapturedTimeout(com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 3 with AsyncGraphResultSet

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();
    }
}
Also used : PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) Node(com.datastax.oss.driver.api.core.metadata.Node) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet) Error(com.datastax.oss.protocol.internal.response.Error) SpeculativeExecutionPolicy(com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy) CapturedTimeout(com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout) GraphTestUtils.createGraphBinaryModule(com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule) GraphBinaryModule(com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 4 with AsyncGraphResultSet

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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CountingIterator(com.datastax.oss.driver.internal.core.util.CountingIterator) AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) GraphNode(com.datastax.dse.driver.api.core.graph.GraphNode)

Example 5 with AsyncGraphResultSet

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();
}
Also used : AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet) AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet) GraphResultSet(com.datastax.dse.driver.api.core.graph.GraphResultSet) GraphNode(com.datastax.dse.driver.api.core.graph.GraphNode) Test(org.junit.Test)

Aggregations

AsyncGraphResultSet (com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet)15 Test (org.junit.Test)13 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)9 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)9 PoolBehavior (com.datastax.oss.driver.internal.core.cql.PoolBehavior)8 CapturedTimeout (com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout)7 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)7 GraphNode (com.datastax.dse.driver.api.core.graph.GraphNode)6 Node (com.datastax.oss.driver.api.core.metadata.Node)6 SpeculativeExecutionPolicy (com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy)6 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)6 Error (com.datastax.oss.protocol.internal.response.Error)4 RequestHandlerTestHarness (com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness)3 GraphResultSet (com.datastax.dse.driver.api.core.graph.GraphResultSet)2 AllNodesFailedException (com.datastax.oss.driver.api.core.AllNodesFailedException)2 DriverTimeoutException (com.datastax.oss.driver.api.core.DriverTimeoutException)2 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)2 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)2 Duration (java.time.Duration)2 List (java.util.List)2