Search in sources :

Example 11 with SpeculativeExecutionPolicy

use of com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy 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 12 with SpeculativeExecutionPolicy

use of com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy 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 13 with SpeculativeExecutionPolicy

use of com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy in project java-driver by datastax.

the class CqlRequestHandlerSpeculativeExecutionTest method should_retry_in_speculative_executions.

@Test
@UseDataProvider("idempotentConfig")
public void should_retry_in_speculative_executions(boolean defaultIdempotence, Statement<?> statement) throws Exception {
    RequestHandlerTestHarness.Builder harnessBuilder = RequestHandlerTestHarness.builder().withDefaultIdempotence(defaultIdempotence);
    PoolBehavior node1Behavior = harnessBuilder.customBehavior(node1);
    PoolBehavior node2Behavior = harnessBuilder.customBehavior(node2);
    harnessBuilder.withResponse(node3, defaultFrameOf(singleRow()));
    try (RequestHandlerTestHarness 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);
        CompletionStage<AsyncResultSet> resultSetFuture = new CqlRequestHandler(statement, harness.getSession(), harness.getContext(), "test").handle();
        node1Behavior.verifyWrite();
        node1Behavior.setWriteSuccess();
        // do not simulate a response from node1. The request will stay hanging for the rest of this
        // test
        // Discard the timeout task
        harness.nextScheduledTimeout();
        // 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(defaultFrameOf(new Error(ProtocolConstants.ErrorCode.IS_BOOTSTRAPPING, "mock message")));
        // 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 : AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) Node(com.datastax.oss.driver.api.core.metadata.Node) 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) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 14 with SpeculativeExecutionPolicy

use of com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy in project java-driver by datastax.

the class CqlRequestHandlerSpeculativeExecutionTest method should_not_schedule_speculative_executions_if_not_idempotent.

@Test
@UseDataProvider("nonIdempotentConfig")
public void should_not_schedule_speculative_executions_if_not_idempotent(boolean defaultIdempotence, Statement<?> statement) {
    RequestHandlerTestHarness.Builder harnessBuilder = RequestHandlerTestHarness.builder().withDefaultIdempotence(defaultIdempotence);
    PoolBehavior node1Behavior = harnessBuilder.customBehavior(node1);
    try (RequestHandlerTestHarness harness = harnessBuilder.build()) {
        SpeculativeExecutionPolicy speculativeExecutionPolicy = harness.getContext().getSpeculativeExecutionPolicy(DriverExecutionProfile.DEFAULT_NAME);
        new CqlRequestHandler(statement, harness.getSession(), harness.getContext(), "test").handle();
        node1Behavior.verifyWrite();
        // Discard the timeout task
        assertThat(harness.nextScheduledTimeout()).isNotNull();
        assertThat(harness.nextScheduledTimeout()).isNull();
        verifyNoMoreInteractions(speculativeExecutionPolicy);
        verifyNoMoreInteractions(nodeMetricUpdater1);
    }
}
Also used : SpeculativeExecutionPolicy(com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 15 with SpeculativeExecutionPolicy

use of com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy in project java-driver by datastax.

the class CqlRequestHandlerSpeculativeExecutionTest method should_not_start_execution_if_result_complete.

@Test
@UseDataProvider("idempotentConfig")
public void should_not_start_execution_if_result_complete(boolean defaultIdempotence, Statement<?> statement) throws Exception {
    RequestHandlerTestHarness.Builder harnessBuilder = RequestHandlerTestHarness.builder().withDefaultIdempotence(defaultIdempotence);
    PoolBehavior node1Behavior = harnessBuilder.customBehavior(node1);
    PoolBehavior node2Behavior = harnessBuilder.customBehavior(node2);
    try (RequestHandlerTestHarness 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);
        CqlRequestHandler requestHandler = new CqlRequestHandler(statement, harness.getSession(), harness.getContext(), "test");
        CompletionStage<AsyncResultSet> resultSetFuture = requestHandler.handle();
        node1Behavior.verifyWrite();
        node1Behavior.setWriteSuccess();
        // Discard the timeout task
        harness.nextScheduledTimeout();
        // 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(defaultFrameOf(singleRow()));
        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.
        // So check the timeout future instead, since it's cancelled in the same method.
        assertThat(requestHandler.scheduledTimeout.isCancelled()).isTrue();
        // 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).isEnabled(DefaultNodeMetric.CQL_MESSAGES, DriverExecutionProfile.DEFAULT_NAME);
        verify(nodeMetricUpdater1).updateTimer(eq(DefaultNodeMetric.CQL_MESSAGES), eq(DriverExecutionProfile.DEFAULT_NAME), anyLong(), eq(TimeUnit.NANOSECONDS));
        verifyNoMoreInteractions(nodeMetricUpdater1);
    }
}
Also used : AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) Node(com.datastax.oss.driver.api.core.metadata.Node) SpeculativeExecutionPolicy(com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy) CapturedTimeout(com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

SpeculativeExecutionPolicy (com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy)18 Test (org.junit.Test)17 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)16 Node (com.datastax.oss.driver.api.core.metadata.Node)14 CapturedTimeout (com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout)12 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)8 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)8 Error (com.datastax.oss.protocol.internal.response.Error)8 PoolBehavior (com.datastax.oss.driver.internal.core.cql.PoolBehavior)7 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)7 AsyncGraphResultSet (com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet)6 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)6 AllNodesFailedException (com.datastax.oss.driver.api.core.AllNodesFailedException)4 List (java.util.List)4 NoNodeAvailableException (com.datastax.oss.driver.api.core.NoNodeAvailableException)2 ConstantSpeculativeExecutionPolicy (com.datastax.oss.driver.internal.core.specex.ConstantSpeculativeExecutionPolicy)2 NoSpeculativeExecutionPolicy (com.datastax.oss.driver.internal.core.specex.NoSpeculativeExecutionPolicy)2 CqlSession (com.datastax.oss.driver.api.core.CqlSession)1 DriverConfig (com.datastax.oss.driver.api.core.config.DriverConfig)1 ProgrammaticDriverConfigLoaderBuilder (com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder)1