Search in sources :

Example 1 with AllNodesFailedException

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

the class MapBasedConfigLoaderIT method should_create_policies_per_profile.

/**
 * Checks that profiles that have specific policy options will get their own policy instance.
 */
@Test
public void should_create_policies_per_profile() {
    // Given
    // a query that throws UNAVAILABLE
    String mockQuery = "mock query";
    SIMULACRON_RULE.cluster().prime(when(mockQuery).then(unavailable(QUORUM, 3, 2)));
    // a default profile that uses the default retry policy, and an alternate profile that uses a
    // policy that ignores all errors
    OptionsMap optionsMap = OptionsMap.driverDefaults();
    String alternateProfile = "profile1";
    optionsMap.put(alternateProfile, TypedDriverOption.RETRY_POLICY_CLASS, IgnoreAllPolicy.class.getName());
    try (CqlSession session = CqlSession.builder().addContactEndPoints(SIMULACRON_RULE.getContactPoints()).withLocalDatacenter("dc1").withConfigLoader(DriverConfigLoader.fromMap(optionsMap)).build()) {
        // When
        // executing the query for the default profile
        SimpleStatement defaultProfileStatement = SimpleStatement.newInstance(mockQuery);
        assertThatThrownBy(() -> session.execute(defaultProfileStatement)).satisfies(t -> {
            // Then
            // the UNAVAILABLE error is surfaced
            assertThat(t).isInstanceOf(AllNodesFailedException.class);
            AllNodesFailedException anfe = (AllNodesFailedException) t;
            assertThat(anfe.getAllErrors()).hasSize(1);
            List<Throwable> nodeErrors = anfe.getAllErrors().values().iterator().next();
            assertThat(nodeErrors).hasSize(1);
            assertThat(nodeErrors.get(0)).isInstanceOf(UnavailableException.class);
        });
        // When
        // executing the query for the alternate profile
        SimpleStatement alternateProfileStatement = SimpleStatement.newInstance(mockQuery).setExecutionProfileName(alternateProfile);
        ResultSet rs = session.execute(alternateProfileStatement);
        // Then
        // the error is ignored
        assertThat(rs.one()).isNull();
    }
}
Also used : OptionsMap(com.datastax.oss.driver.api.core.config.OptionsMap) AllNodesFailedException(com.datastax.oss.driver.api.core.AllNodesFailedException) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Example 2 with AllNodesFailedException

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

the class LifecycleListenerIT method should_not_notify_listener_when_init_fails.

@Test
public void should_not_notify_listener_when_init_fails() {
    TestLifecycleListener listener = new TestLifecycleListener();
    assertThat(listener.ready).isFalse();
    assertThat(listener.closed).isFalse();
    SIMULACRON_RULE.cluster().rejectConnections(0, RejectScope.STOP);
    try (CqlSession session = newSession(listener)) {
        fail("Expected AllNodesFailedException");
    } catch (AllNodesFailedException ignored) {
    } finally {
        SIMULACRON_RULE.cluster().acceptConnections();
    }
    assertThat(listener.ready).isFalse();
    await().atMost(1, TimeUnit.SECONDS).until(() -> listener.closed);
}
Also used : AllNodesFailedException(com.datastax.oss.driver.api.core.AllNodesFailedException) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Example 3 with AllNodesFailedException

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

the class NodeTargetingIT method should_fail_if_node_fails_query.

@Test
public void should_fail_if_node_fails_query() {
    String query = "mock";
    SIMULACRON_RULE.cluster().node(3).prime(when(query).then(unavailable(ConsistencyLevel.ALL, 1, 0)));
    // given a statement with a node configured to fail the given query.
    Node node3 = getNode(3);
    Statement statement = SimpleStatement.newInstance(query).setNode(node3);
    // when statement is executed an error should be raised.
    try {
        SESSION_RULE.session().execute(statement);
        fail("Should have thrown AllNodesFailedException");
    } catch (AllNodesFailedException e) {
        assertThat(e.getAllErrors().size()).isEqualTo(1);
        assertThat(e.getAllErrors().get(node3).get(0)).isInstanceOf(UnavailableException.class);
    }
}
Also used : AllNodesFailedException(com.datastax.oss.driver.api.core.AllNodesFailedException) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) Node(com.datastax.oss.driver.api.core.metadata.Node) BoundNode(com.datastax.oss.simulacron.server.BoundNode) UnavailableException(com.datastax.oss.driver.api.core.servererrors.UnavailableException) Test(org.junit.Test)

Example 4 with AllNodesFailedException

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

the class CqlRequestHandlerSpeculativeExecutionTest method should_fail_if_no_more_nodes_and_initial_execution_is_last.

@Test
@UseDataProvider("idempotentConfig")
public void should_fail_if_no_more_nodes_and_initial_execution_is_last(boolean defaultIdempotence, Statement<?> statement) throws Exception {
    RequestHandlerTestHarness.Builder harnessBuilder = RequestHandlerTestHarness.builder().withDefaultIdempotence(defaultIdempotence);
    PoolBehavior node1Behavior = harnessBuilder.customBehavior(node1);
    harnessBuilder.withResponse(node2, defaultFrameOf(new Error(ProtocolConstants.ErrorCode.IS_BOOTSTRAPPING, "mock message")));
    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 yet
        // Discard the timeout task
        harness.nextScheduledTimeout();
        // Run the next scheduled task to start the speculative execution. node2 will reply with a
        // BOOTSTRAPPING error, causing a RETRY_NEXT; but the query plan is now empty so the
        // speculative execution stops.
        // 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 the same response, that triggers a RETRY_NEXT
        node1Behavior.setResponseSuccess(defaultFrameOf(new Error(ProtocolConstants.ErrorCode.IS_BOOTSTRAPPING, "mock message")));
        // But again the query plan is empty so that should fail the request
        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 : AllNodesFailedException(com.datastax.oss.driver.api.core.AllNodesFailedException) 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) 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 5 with AllNodesFailedException

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

the class CqlRequestHandlerSpeculativeExecutionTest method should_fail_if_no_more_nodes_and_speculative_execution_is_last.

@Test
@UseDataProvider("idempotentConfig")
public void should_fail_if_no_more_nodes_and_speculative_execution_is_last(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);
        CompletionStage<AsyncResultSet> resultSetFuture = new CqlRequestHandler(statement, harness.getSession(), harness.getContext(), "test").handle();
        node1Behavior.verifyWrite();
        node1Behavior.setWriteSuccess();
        // do not simulate a response from node1 yet
        // 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);
        // 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(defaultFrameOf(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(defaultFrameOf(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 : AllNodesFailedException(com.datastax.oss.driver.api.core.AllNodesFailedException) 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) 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)

Aggregations

AllNodesFailedException (com.datastax.oss.driver.api.core.AllNodesFailedException)16 Test (org.junit.Test)16 CqlSession (com.datastax.oss.driver.api.core.CqlSession)8 Node (com.datastax.oss.driver.api.core.metadata.Node)8 List (java.util.List)6 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)5 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)5 SpeculativeExecutionPolicy (com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy)4 CapturedTimeout (com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout)4 Error (com.datastax.oss.protocol.internal.response.Error)4 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)3 AsyncGraphResultSet (com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet)2 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)2 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)2 UnsupportedProtocolVersionException (com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException)2 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)2 Statement (com.datastax.oss.driver.api.core.cql.Statement)2 CassandraRequirement (com.datastax.oss.driver.api.testinfra.CassandraRequirement)2 PoolBehavior (com.datastax.oss.driver.internal.core.cql.PoolBehavior)2 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)2