Search in sources :

Example 11 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_nodes.

@Test
@UseDataProvider(location = DseTestDataProviders.class, value = "idempotentGraphConfig")
public void should_fail_if_no_nodes(boolean defaultIdempotence, GraphStatement<?> statement) {
    GraphRequestHandlerTestHarness.Builder harnessBuilder = GraphRequestHandlerTestHarness.builder().withDefaultIdempotence(defaultIdempotence);
    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();
        assertThatStage(resultSetFuture).isFailed(error -> assertThat(error).isInstanceOf(NoNodeAvailableException.class));
    }
}
Also used : 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) SpeculativeExecutionPolicy(com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy) NoNodeAvailableException(com.datastax.oss.driver.api.core.NoNodeAvailableException) 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 12 with AsyncGraphResultSet

use of com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet in project java-driver by datastax.

the class ContinuousGraphRequestHandlerSpeculativeExecutionTest method should_not_start_execution_if_result_complete.

@Test
@UseDataProvider(location = DseTestDataProviders.class, value = "idempotentGraphConfig")
public void should_not_start_execution_if_result_complete(boolean defaultIdempotence, GraphStatement<?> statement) throws Exception {
    GraphRequestHandlerTestHarness.Builder harnessBuilder = GraphRequestHandlerTestHarness.builder().withGraphTimeout(Duration.ofSeconds(10)).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());
        ContinuousGraphRequestHandler requestHandler = new ContinuousGraphRequestHandler(statement, harness.getSession(), harness.getContext(), "test", module, graphSupportChecker);
        CompletionStage<AsyncGraphResultSet> resultSetFuture = requestHandler.handle();
        node1Behavior.verifyWrite();
        node1Behavior.setWriteSuccess();
        // The first timeout scheduled should be the global timeout
        CapturedTimeout globalTimeout = harness.nextScheduledTimeout();
        assertThat(globalTimeout.getDelay(TimeUnit.SECONDS)).isEqualTo(10);
        // Check that the first execution was scheduled but don't run it yet
        CapturedTimeout speculativeExecution1 = harness.nextScheduledTimeout();
        assertThat(speculativeExecution1.getDelay(TimeUnit.MILLISECONDS)).isEqualTo(firstExecutionDelay);
        // Complete the request from the initial execution
        node1Behavior.setResponseSuccess(defaultDseFrameOf(singleGraphRow(GraphProtocol.GRAPH_BINARY_1_0, module)));
        assertThatStage(resultSetFuture).isSuccess();
        // Pending speculative executions should have been cancelled. However we don't check
        // firstExecutionTask directly because the request handler's onResponse can sometimes be
        // invoked before operationComplete (this is very unlikely in practice, but happens in our
        // Travis CI build). When that happens, the speculative execution is not recorded yet when
        // cancelScheduledTasks runs.
        // The fact that we missed the speculative execution is not a problem; even if it starts, it
        // will eventually find out that the result is already complete and cancel itself:
        speculativeExecution1.task().run(speculativeExecution1);
        node2Behavior.verifyNoWrite();
        verify(nodeMetricUpdater1).updateTimer(eq(DseNodeMetric.GRAPH_MESSAGES), eq(DriverExecutionProfile.DEFAULT_NAME), anyLong(), eq(TimeUnit.NANOSECONDS));
        verifyNoMoreInteractions(nodeMetricUpdater1);
    }
}
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) 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 13 with AsyncGraphResultSet

use of com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet in project java-driver by datastax.

the class ContinuousGraphRequestHandlerSpeculativeExecutionTest method should_retry_in_speculative_executions.

@Test
@UseDataProvider(location = DseTestDataProviders.class, value = "idempotentGraphConfig")
public void should_retry_in_speculative_executions(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();
        // do not simulate a response from node1. The request will stay hanging for the rest of this
        // test
        // 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();
        // node2 replies with a response that triggers a RETRY_NEXT
        node2Behavior.setResponseSuccess(defaultDseFrameOf(new Error(ProtocolConstants.ErrorCode.IS_BOOTSTRAPPING, "mock message")));
        node3Behavior.setResponseSuccess(defaultDseFrameOf(singleGraphRow(GraphProtocol.GRAPH_BINARY_1_0, module)));
        // The second execution should move to node3 and complete the request
        assertThatStage(resultSetFuture).isSuccess();
        // The request to node1 was still in flight, it should have been cancelled
        node1Behavior.verifyCancellation();
    }
}
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 14 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_multiple_pages.

@Test
public void should_create_result_set_from_multiple_pages() {
    // Given
    AsyncGraphResultSet page1 = mockPage(true, 0, 1, 2);
    AsyncGraphResultSet page2 = mockPage(true, 3, 4, 5);
    AsyncGraphResultSet page3 = mockPage(false, 6, 7, 8);
    complete(page1.fetchNextPage(), page2);
    complete(page2.fetchNextPage(), page3);
    // When
    GraphResultSet resultSet = GraphResultSets.toSync(page1);
    // Then
    assertThat(resultSet.iterator().hasNext()).isTrue();
    assertThat(resultSet.getRequestExecutionInfo()).isSameAs(page1.getRequestExecutionInfo());
    assertThat(((MultiPageGraphResultSet) resultSet).getRequestExecutionInfos()).containsExactly(page1.getRequestExecutionInfo());
    Iterator<GraphNode> iterator = resultSet.iterator();
    assertNextRow(iterator, 0);
    assertNextRow(iterator, 1);
    assertNextRow(iterator, 2);
    assertThat(iterator.hasNext()).isTrue();
    // This should have triggered the fetch of page2
    assertThat(resultSet.getRequestExecutionInfo()).isEqualTo(page2.getRequestExecutionInfo());
    assertThat(((MultiPageGraphResultSet) resultSet).getRequestExecutionInfos()).containsExactly(page1.getRequestExecutionInfo(), page2.getRequestExecutionInfo());
    assertNextRow(iterator, 3);
    assertNextRow(iterator, 4);
    assertNextRow(iterator, 5);
    assertThat(iterator.hasNext()).isTrue();
    // This should have triggered the fetch of page3
    assertThat(resultSet.getRequestExecutionInfo()).isEqualTo(page3.getRequestExecutionInfo());
    assertThat(((MultiPageGraphResultSet) resultSet).getRequestExecutionInfos()).containsExactly(page1.getRequestExecutionInfo(), page2.getRequestExecutionInfo(), page3.getRequestExecutionInfo());
    assertNextRow(iterator, 6);
    assertNextRow(iterator, 7);
    assertNextRow(iterator, 8);
}
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)

Example 15 with AsyncGraphResultSet

use of com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet in project java-driver by datastax.

the class ContinuousGraphRequestHandlerTest method should_honor_statement_timeout.

@Test
public void should_honor_statement_timeout() throws Exception {
    // given
    GraphBinaryModule binaryModule = createGraphBinaryModule(mockContext);
    Duration defaultTimeout = Duration.ofSeconds(1);
    Duration statementTimeout = Duration.ofSeconds(2);
    RequestHandlerTestHarness.Builder builder = GraphRequestHandlerTestHarness.builder().withGraphTimeout(defaultTimeout);
    PoolBehavior node1Behavior = builder.customBehavior(node);
    try (RequestHandlerTestHarness harness = builder.build()) {
        DriverExecutionProfile profile = harness.getContext().getConfig().getDefaultProfile();
        when(profile.isDefined(DseDriverOption.GRAPH_SUB_PROTOCOL)).thenReturn(true);
        when(profile.getString(DseDriverOption.GRAPH_SUB_PROTOCOL)).thenReturn(GraphProtocol.GRAPH_BINARY_1_0.toInternalCode());
        GraphStatement<?> graphStatement = ScriptGraphStatement.newInstance("mockQuery").setTimeout(statementTimeout);
        // when
        ContinuousGraphRequestHandler handler = new ContinuousGraphRequestHandler(graphStatement, harness.getSession(), harness.getContext(), "test", binaryModule, new GraphSupportChecker());
        // send the initial request
        CompletionStage<AsyncGraphResultSet> page1Future = handler.handle();
        // acknowledge the write, will set the global timeout
        node1Behavior.verifyWrite();
        node1Behavior.setWriteSuccess();
        CapturedTimeout globalTimeout = harness.nextScheduledTimeout();
        assertThat(globalTimeout.getDelay(TimeUnit.NANOSECONDS)).isEqualTo(statementTimeout.toNanos());
        // will trigger the global timeout and complete it exceptionally
        globalTimeout.task().run(globalTimeout);
        assertThat(page1Future.toCompletableFuture()).isCompletedExceptionally();
        assertThatThrownBy(() -> page1Future.toCompletableFuture().get()).hasRootCauseExactlyInstanceOf(DriverTimeoutException.class).hasMessageContaining("Query timed out after " + statementTimeout);
    }
}
Also used : PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) DriverTimeoutException(com.datastax.oss.driver.api.core.DriverTimeoutException) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) Duration(java.time.Duration) 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) CapturedTimeout(com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout) 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