Search in sources :

Example 26 with RequestHandlerTestHarness

use of com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness in project java-driver by datastax.

the class ContinuousCqlRequestHandlerNodeTargetingTest method should_fail_if_targeted_node_not_available.

@Test
@UseDataProvider(value = "allDseProtocolVersions", location = DseTestDataProviders.class)
public void should_fail_if_targeted_node_not_available(DseProtocolVersion version) {
    try (RequestHandlerTestHarness harness = continuousHarnessBuilder().withResponse(node1, defaultFrameOf(DseTestFixtures.singleDseRow())).withResponse(node2, defaultFrameOf(DseTestFixtures.singleDseRow())).withEmptyPool(node3).withProtocolVersion(version).build()) {
        LoadBalancingPolicyWrapper loadBalancingPolicy = harness.getContext().getLoadBalancingPolicyWrapper();
        InOrder invocations = Mockito.inOrder(loadBalancingPolicy);
        // target node3, which should be unavailable
        CompletionStage<ContinuousAsyncResultSet> resultSetFuture = new ContinuousCqlRequestHandler(UNDEFINED_IDEMPOTENCE_STATEMENT.setNode(node3), harness.getSession(), harness.getContext(), "target node 3, unavailable").handle();
        assertThatStage(resultSetFuture).isFailed(error -> {
            assertThat(error).isInstanceOf(AllNodesFailedException.class);
            Map<Node, List<Throwable>> errors = ((AllNodesFailedException) error).getAllErrors();
            assertThat(errors).hasSize(1);
            List<Throwable> nodeErrors = errors.values().iterator().next();
            assertThat(nodeErrors).singleElement().isInstanceOf(NodeUnavailableException.class);
            invocations.verify(loadBalancingPolicy, never()).newQueryPlan(any(Request.class), anyString(), any(Session.class));
        });
        resultSetFuture = new ContinuousCqlRequestHandler(UNDEFINED_IDEMPOTENCE_STATEMENT, harness.getSession(), harness.getContext(), "no node targeting, should use node 1").handle();
        assertThatStage(resultSetFuture).isSuccess(resultSet -> {
            assertThat(resultSet.getExecutionInfo().getCoordinator()).isEqualTo(node1);
            invocations.verify(loadBalancingPolicy).newQueryPlan(UNDEFINED_IDEMPOTENCE_STATEMENT, DriverExecutionProfile.DEFAULT_NAME, harness.getSession());
        });
        resultSetFuture = new ContinuousCqlRequestHandler(UNDEFINED_IDEMPOTENCE_STATEMENT, harness.getSession(), harness.getContext(), "no node targeting, should use node 2").handle();
        assertThatStage(resultSetFuture).isSuccess(resultSet -> {
            assertThat(resultSet.getExecutionInfo().getCoordinator()).isEqualTo(node2);
            invocations.verify(loadBalancingPolicy).newQueryPlan(UNDEFINED_IDEMPOTENCE_STATEMENT, DriverExecutionProfile.DEFAULT_NAME, harness.getSession());
        });
    }
}
Also used : InOrder(org.mockito.InOrder) AllNodesFailedException(com.datastax.oss.driver.api.core.AllNodesFailedException) Node(com.datastax.oss.driver.api.core.metadata.Node) Request(com.datastax.oss.driver.api.core.session.Request) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) LoadBalancingPolicyWrapper(com.datastax.oss.driver.internal.core.metadata.LoadBalancingPolicyWrapper) List(java.util.List) Session(com.datastax.oss.driver.api.core.session.Session) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 27 with RequestHandlerTestHarness

use of com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness in project java-driver by datastax.

the class ContinuousCqlRequestHandlerNodeTargetingTest method should_target_node.

@Test
@UseDataProvider(value = "allDseProtocolVersions", location = DseTestDataProviders.class)
public void should_target_node(DseProtocolVersion version) {
    try (RequestHandlerTestHarness harness = continuousHarnessBuilder().withResponse(node1, defaultFrameOf(DseTestFixtures.singleDseRow())).withResponse(node2, defaultFrameOf(DseTestFixtures.singleDseRow())).withResponse(node3, defaultFrameOf(DseTestFixtures.singleDseRow())).withProtocolVersion(version).build()) {
        LoadBalancingPolicyWrapper loadBalancingPolicy = harness.getContext().getLoadBalancingPolicyWrapper();
        InOrder invocations = Mockito.inOrder(loadBalancingPolicy);
        CompletionStage<ContinuousAsyncResultSet> resultSetFuture = new ContinuousCqlRequestHandler(UNDEFINED_IDEMPOTENCE_STATEMENT.setNode(node3), harness.getSession(), harness.getContext(), "target node 3").handle();
        assertThatStage(resultSetFuture).isSuccess(resultSet -> {
            assertThat(resultSet.getExecutionInfo().getCoordinator()).isEqualTo(node3);
            invocations.verify(loadBalancingPolicy, never()).newQueryPlan(any(Request.class), anyString(), any(Session.class));
        });
        resultSetFuture = new ContinuousCqlRequestHandler(UNDEFINED_IDEMPOTENCE_STATEMENT, harness.getSession(), harness.getContext(), "no node targeting").handle();
        assertThatStage(resultSetFuture).isSuccess(resultSet -> {
            assertThat(resultSet.getExecutionInfo().getCoordinator()).isEqualTo(node1);
            invocations.verify(loadBalancingPolicy).newQueryPlan(UNDEFINED_IDEMPOTENCE_STATEMENT, DriverExecutionProfile.DEFAULT_NAME, harness.getSession());
        });
    }
}
Also used : InOrder(org.mockito.InOrder) Request(com.datastax.oss.driver.api.core.session.Request) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) LoadBalancingPolicyWrapper(com.datastax.oss.driver.internal.core.metadata.LoadBalancingPolicyWrapper) Session(com.datastax.oss.driver.api.core.session.Session) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 28 with RequestHandlerTestHarness

use of com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness in project java-driver by datastax.

the class ContinuousCqlRequestHandlerRetryTest method should_always_try_next_node_if_bootstrapping.

@Test
@UseDataProvider("allIdempotenceConfigs")
public void should_always_try_next_node_if_bootstrapping(boolean defaultIdempotence, Statement<?> statement, DseProtocolVersion version) {
    try (RequestHandlerTestHarness harness = continuousHarnessBuilder().withProtocolVersion(version).withDefaultIdempotence(defaultIdempotence).withResponse(node1, defaultFrameOf(new Error(ProtocolConstants.ErrorCode.IS_BOOTSTRAPPING, "mock message"))).withResponse(node2, defaultFrameOf(DseTestFixtures.singleDseRow())).build()) {
        ContinuousCqlRequestHandler handler = new ContinuousCqlRequestHandler(statement, harness.getSession(), harness.getContext(), "test");
        CompletionStage<ContinuousAsyncResultSet> resultSetFuture = handler.handle();
        assertThat(handler.getState()).isEqualTo(-1);
        assertThatStage(resultSetFuture).isSuccess(resultSet -> {
            Iterator<Row> rows = resultSet.currentPage().iterator();
            assertThat(rows.hasNext()).isTrue();
            assertThat(rows.next().getString("message")).isEqualTo("hello, world");
            ExecutionInfo executionInfo = resultSet.getExecutionInfo();
            assertThat(executionInfo.getCoordinator()).isEqualTo(node2);
            assertThat(executionInfo.getErrors()).hasSize(1);
            assertThat(executionInfo.getErrors().get(0).getKey()).isEqualTo(node1);
            assertThat(executionInfo.getErrors().get(0).getValue()).isInstanceOf(BootstrappingException.class);
            assertThat(executionInfo.getIncomingPayload()).isEmpty();
            assertThat(executionInfo.getPagingState()).isNull();
            assertThat(executionInfo.getSpeculativeExecutionCount()).isEqualTo(0);
            assertThat(executionInfo.getSuccessfulExecutionIndex()).isEqualTo(0);
            assertThat(executionInfo.getWarnings()).isEmpty();
            Mockito.verifyNoMoreInteractions(harness.getContext().getRetryPolicy(anyString()));
        });
    }
}
Also used : RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) Error(com.datastax.oss.protocol.internal.response.Error) ServerError(com.datastax.oss.driver.api.core.servererrors.ServerError) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) Row(com.datastax.oss.driver.api.core.cql.Row) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 29 with RequestHandlerTestHarness

use of com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness in project java-driver by datastax.

the class ContinuousCqlRequestHandlerRetryTest method should_try_same_node_if_idempotent_and_retry_policy_decides_so.

@Test
@UseDataProvider("failureAndIdempotent")
public void should_try_same_node_if_idempotent_and_retry_policy_decides_so(FailureScenario failureScenario, boolean defaultIdempotence, Statement<?> statement, DseProtocolVersion version) {
    RequestHandlerTestHarness.Builder harnessBuilder = continuousHarnessBuilder().withProtocolVersion(version).withDefaultIdempotence(defaultIdempotence);
    failureScenario.mockRequestError(harnessBuilder, node1);
    harnessBuilder.withResponse(node1, defaultFrameOf(DseTestFixtures.singleDseRow()));
    try (RequestHandlerTestHarness harness = harnessBuilder.build()) {
        failureScenario.mockRetryPolicyVerdict(harness.getContext().getRetryPolicy(anyString()), RetryVerdict.RETRY_SAME);
        ContinuousCqlRequestHandler handler = new ContinuousCqlRequestHandler(statement, harness.getSession(), harness.getContext(), "test");
        CompletionStage<ContinuousAsyncResultSet> resultSetFuture = handler.handle();
        assertThat(handler.getState()).isEqualTo(-1);
        assertThatStage(resultSetFuture).isSuccess(resultSet -> {
            Iterator<Row> rows = resultSet.currentPage().iterator();
            assertThat(rows.hasNext()).isTrue();
            assertThat(rows.next().getString("message")).isEqualTo("hello, world");
            ExecutionInfo executionInfo = resultSet.getExecutionInfo();
            assertThat(executionInfo.getCoordinator()).isEqualTo(node1);
            assertThat(executionInfo.getErrors()).hasSize(1);
            assertThat(executionInfo.getErrors().get(0).getKey()).isEqualTo(node1);
            Mockito.verify(nodeMetricUpdater1).incrementCounter(eq(failureScenario.errorMetric), anyString());
            Mockito.verify(nodeMetricUpdater1).incrementCounter(eq(DefaultNodeMetric.RETRIES), anyString());
            Mockito.verify(nodeMetricUpdater1).incrementCounter(eq(failureScenario.retryMetric), anyString());
            Mockito.verify(nodeMetricUpdater1, atMost(2)).updateTimer(eq(DefaultNodeMetric.CQL_MESSAGES), anyString(), anyLong(), eq(TimeUnit.NANOSECONDS));
            Mockito.verifyNoMoreInteractions(nodeMetricUpdater1);
        });
    }
}
Also used : RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) Row(com.datastax.oss.driver.api.core.cql.Row) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 30 with RequestHandlerTestHarness

use of com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness in project java-driver by datastax.

the class ContinuousCqlRequestHandlerRetryTest method should_rethrow_error_if_not_idempotent_and_error_unsafe_or_policy_rethrows.

@Test
@UseDataProvider("failureAndNotIdempotent")
public void should_rethrow_error_if_not_idempotent_and_error_unsafe_or_policy_rethrows(FailureScenario failureScenario, boolean defaultIdempotence, Statement<?> statement, DseProtocolVersion version) {
    // For two of the possible exceptions, the retry policy is called even if the statement is not
    // idempotent
    boolean shouldCallRetryPolicy = (failureScenario.expectedExceptionClass.equals(UnavailableException.class) || failureScenario.expectedExceptionClass.equals(ReadTimeoutException.class));
    RequestHandlerTestHarness.Builder harnessBuilder = continuousHarnessBuilder().withProtocolVersion(version).withDefaultIdempotence(defaultIdempotence);
    failureScenario.mockRequestError(harnessBuilder, node1);
    harnessBuilder.withResponse(node2, defaultFrameOf(DseTestFixtures.singleDseRow()));
    try (RequestHandlerTestHarness harness = harnessBuilder.build()) {
        if (shouldCallRetryPolicy) {
            failureScenario.mockRetryPolicyVerdict(harness.getContext().getRetryPolicy(anyString()), RetryVerdict.RETHROW);
        }
        ContinuousCqlRequestHandler handler = new ContinuousCqlRequestHandler(statement, harness.getSession(), harness.getContext(), "test");
        CompletionStage<ContinuousAsyncResultSet> resultSetFuture = handler.handle();
        assertThat(handler.getState()).isEqualTo(-2);
        assertThatStage(resultSetFuture).isFailed(error -> {
            assertThat(error).isInstanceOf(failureScenario.expectedExceptionClass);
            // When non idempotent, the policy is bypassed completely:
            if (!shouldCallRetryPolicy) {
                Mockito.verifyNoMoreInteractions(harness.getContext().getRetryPolicy(anyString()));
            }
            Mockito.verify(nodeMetricUpdater1).incrementCounter(eq(failureScenario.errorMetric), anyString());
            Mockito.verify(nodeMetricUpdater1, atMost(1)).updateTimer(eq(DefaultNodeMetric.CQL_MESSAGES), anyString(), anyLong(), eq(TimeUnit.NANOSECONDS));
            Mockito.verifyNoMoreInteractions(nodeMetricUpdater1);
        });
    }
}
Also used : RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

RequestHandlerTestHarness (com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness)35 Test (org.junit.Test)34 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)27 ContinuousAsyncResultSet (com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet)23 PoolBehavior (com.datastax.oss.driver.internal.core.cql.PoolBehavior)14 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)12 Row (com.datastax.oss.driver.api.core.cql.Row)7 ReactiveRow (com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow)4 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)4 ColumnDefinitions (com.datastax.oss.driver.api.core.cql.ColumnDefinitions)4 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)4 DefaultSession (com.datastax.oss.driver.internal.core.session.DefaultSession)4 CapturedTimeout (com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout)4 AsyncGraphResultSet (com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet)3 ContinuousCqlRequestAsyncProcessor (com.datastax.dse.driver.internal.core.cql.continuous.ContinuousCqlRequestAsyncProcessor)3 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)3 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)3 CqlRequestAsyncProcessor (com.datastax.oss.driver.internal.core.cql.CqlRequestAsyncProcessor)3 Prepare (com.datastax.oss.protocol.internal.request.Prepare)3 Query (com.datastax.oss.protocol.internal.request.Query)3