Search in sources :

Example 21 with Error

use of com.datastax.oss.protocol.internal.response.Error in project java-driver by datastax.

the class CqlRequestHandlerSpeculativeExecutionTest method should_stop_retrying_other_executions_if_result_complete.

@Test
@UseDataProvider("idempotentConfig")
public void should_stop_retrying_other_executions_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);
    PoolBehavior node3Behavior = harnessBuilder.customBehavior(node3);
    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();
        // 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();
        // Complete the request from the initial execution
        node1Behavior.setResponseSuccess(defaultFrameOf(singleRow()));
        assertThatStage(resultSetFuture).isSuccess();
        // node2 replies with a response that would trigger a RETRY_NEXT if the request was still
        // running
        node2Behavior.setResponseSuccess(defaultFrameOf(new Error(ProtocolConstants.ErrorCode.IS_BOOTSTRAPPING, "mock message")));
        // The speculative execution should not move to node3 because it is stopped
        node3Behavior.verifyNoWrite();
    }
}
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 22 with Error

use of com.datastax.oss.protocol.internal.response.Error in project java-driver by datastax.

the class InFlightHandlerTest method should_hold_stream_id_for_multi_response_callback.

@Test
public void should_hold_stream_id_for_multi_response_callback() {
    // Given
    addToPipeline();
    when(streamIds.acquire()).thenReturn(42);
    MockResponseCallback responseCallback = new MockResponseCallback(frame -> frame.message instanceof Error);
    // When
    channel.writeAndFlush(new DriverChannel.RequestMessage(QUERY, false, Frame.NO_PAYLOAD, responseCallback)).awaitUninterruptibly();
    // Then
    // notify callback of stream id
    assertThat(responseCallback.streamId).isEqualTo(42);
    Frame requestFrame = readOutboundFrame();
    for (int i = 0; i < 5; i++) {
        // When
        // completing pending request
        Frame responseFrame = buildInboundFrame(requestFrame, Void.INSTANCE);
        writeInboundFrame(responseFrame);
        // Then
        assertThat(responseCallback.getLastResponse()).isSameAs(responseFrame);
        // Stream id not released, callback can receive more responses
        verify(streamIds, never()).release(42);
    }
    // When
    // a terminal response comes in
    Frame responseFrame = buildInboundFrame(requestFrame, new Error(0, "test"));
    writeInboundFrame(responseFrame);
    // Then
    verify(streamIds).release(42);
    assertThat(responseCallback.getLastResponse()).isSameAs(responseFrame);
    // When
    // more responses come in
    writeInboundFrame(requestFrame, Void.INSTANCE);
    // Then
    // the callback does not get them anymore (this could only be responses to a new request that
    // reused the id)
    assertThat(responseCallback.getLastResponse()).isNull();
}
Also used : Frame(com.datastax.oss.protocol.internal.Frame) Error(com.datastax.oss.protocol.internal.response.Error) Test(org.junit.Test)

Example 23 with Error

use of com.datastax.oss.protocol.internal.response.Error in project java-driver by datastax.

the class InFlightHandlerTest method should_release_stream_id_when_orphaned_multi_response_callback_receives_last_response.

@Test
public void should_release_stream_id_when_orphaned_multi_response_callback_receives_last_response() {
    // Given
    addToPipeline();
    when(streamIds.acquire()).thenReturn(42);
    MockResponseCallback responseCallback = new MockResponseCallback(frame -> frame.message instanceof Error);
    channel.writeAndFlush(new DriverChannel.RequestMessage(QUERY, false, Frame.NO_PAYLOAD, responseCallback)).awaitUninterruptibly();
    Frame requestFrame = readOutboundFrame();
    for (int i = 0; i < 5; i++) {
        Frame responseFrame = buildInboundFrame(requestFrame, Void.INSTANCE);
        writeInboundFrame(responseFrame);
        assertThat(responseCallback.getLastResponse()).isSameAs(responseFrame);
        verify(streamIds, never()).release(42);
    }
    // When
    // cancelled mid-flight
    channel.writeAndFlush(responseCallback);
    // Then
    // subsequent non-final responses are not propagated (we assume the callback completed itself
    // already), but do not release the stream id
    writeInboundFrame(requestFrame, Void.INSTANCE);
    assertThat(responseCallback.getLastResponse()).isNull();
    verify(streamIds, never()).release(42);
    // When
    // the terminal response arrives
    writeInboundFrame(requestFrame, new Error(0, "test"));
    // Then
    // still not propagated but the id is released
    assertThat(responseCallback.getLastResponse()).isNull();
    verify(streamIds).release(42);
}
Also used : Frame(com.datastax.oss.protocol.internal.Frame) Error(com.datastax.oss.protocol.internal.response.Error) Test(org.junit.Test)

Example 24 with Error

use of com.datastax.oss.protocol.internal.response.Error in project java-driver by datastax.

the class ChannelFactoryProtocolNegotiationTest method should_fail_if_version_specified_and_considered_beta_by_server.

@Test
public void should_fail_if_version_specified_and_considered_beta_by_server() {
    // Given
    when(defaultProfile.isDefined(DefaultDriverOption.PROTOCOL_VERSION)).thenReturn(true);
    when(defaultProfile.getString(DefaultDriverOption.PROTOCOL_VERSION)).thenReturn("V5");
    when(protocolVersionRegistry.fromName("V5")).thenReturn(DefaultProtocolVersion.V5);
    ChannelFactory factory = newChannelFactory();
    // When
    CompletionStage<DriverChannel> channelFuture = factory.connect(SERVER_ADDRESS, DriverChannelOptions.DEFAULT, NoopNodeMetricUpdater.INSTANCE);
    Frame requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Options.class);
    writeInboundFrame(requestFrame, TestResponses.supportedResponse("mock_key", "mock_value"));
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.protocolVersion).isEqualTo(DefaultProtocolVersion.V5.getCode());
    // Server considers v5 beta, e.g. C* 3.10 or 3.11
    writeInboundFrame(requestFrame, new Error(ProtocolConstants.ErrorCode.PROTOCOL_ERROR, "Beta version of the protocol used (5/v5-beta), but USE_BETA flag is unset"));
    // Then
    assertThatStage(channelFuture).isFailed(e -> {
        assertThat(e).isInstanceOf(UnsupportedProtocolVersionException.class).hasMessageContaining("Host does not support protocol version V5");
        assertThat(((UnsupportedProtocolVersionException) e).getAttemptedVersions()).containsExactly(DefaultProtocolVersion.V5);
    });
}
Also used : Frame(com.datastax.oss.protocol.internal.Frame) Error(com.datastax.oss.protocol.internal.response.Error) UnsupportedProtocolVersionException(com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException) Test(org.junit.Test)

Example 25 with Error

use of com.datastax.oss.protocol.internal.response.Error in project java-driver by datastax.

the class ChannelFactoryProtocolNegotiationTest method should_fail_if_negotiation_finds_no_matching_version.

@Test
@UseDataProvider("unsupportedProtocolCodes")
public void should_fail_if_negotiation_finds_no_matching_version(int errorCode) {
    // Given
    when(defaultProfile.isDefined(DefaultDriverOption.PROTOCOL_VERSION)).thenReturn(false);
    when(protocolVersionRegistry.highestNonBeta()).thenReturn(DefaultProtocolVersion.V4);
    when(protocolVersionRegistry.downgrade(DefaultProtocolVersion.V4)).thenReturn(Optional.of(DefaultProtocolVersion.V3));
    when(protocolVersionRegistry.downgrade(DefaultProtocolVersion.V3)).thenReturn(Optional.empty());
    ChannelFactory factory = newChannelFactory();
    // When
    CompletionStage<DriverChannel> channelFuture = factory.connect(SERVER_ADDRESS, DriverChannelOptions.DEFAULT, NoopNodeMetricUpdater.INSTANCE);
    Frame requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Options.class);
    writeInboundFrame(requestFrame, TestResponses.supportedResponse("mock_key", "mock_value"));
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.protocolVersion).isEqualTo(DefaultProtocolVersion.V4.getCode());
    // Server does not support v4
    writeInboundFrame(requestFrame, new Error(errorCode, "Invalid or unsupported protocol version"));
    // Client retries with v3
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Options.class);
    writeInboundFrame(requestFrame, TestResponses.supportedResponse("mock_key", "mock_value"));
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.protocolVersion).isEqualTo(DefaultProtocolVersion.V3.getCode());
    // Server does not support v3
    writeInboundFrame(requestFrame, new Error(errorCode, "Invalid or unsupported protocol version"));
    // Then
    assertThatStage(channelFuture).isFailed(e -> {
        assertThat(e).isInstanceOf(UnsupportedProtocolVersionException.class).hasMessageContaining("Protocol negotiation failed: could not find a common version " + "(attempted: [V4, V3])");
        assertThat(((UnsupportedProtocolVersionException) e).getAttemptedVersions()).containsExactly(DefaultProtocolVersion.V4, DefaultProtocolVersion.V3);
    });
}
Also used : Frame(com.datastax.oss.protocol.internal.Frame) Error(com.datastax.oss.protocol.internal.response.Error) UnsupportedProtocolVersionException(com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

Error (com.datastax.oss.protocol.internal.response.Error)27 Test (org.junit.Test)24 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)15 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)8 Node (com.datastax.oss.driver.api.core.metadata.Node)8 SpeculativeExecutionPolicy (com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy)8 CapturedTimeout (com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout)8 Frame (com.datastax.oss.protocol.internal.Frame)8 AsyncGraphResultSet (com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet)4 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)4 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)4 AllNodesFailedException (com.datastax.oss.driver.api.core.AllNodesFailedException)4 ServerError (com.datastax.oss.driver.api.core.servererrors.ServerError)4 PoolBehavior (com.datastax.oss.driver.internal.core.cql.PoolBehavior)4 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)4 List (java.util.List)4 UnsupportedProtocolVersionException (com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException)3 InetSocketAddress (java.net.InetSocketAddress)3 ContinuousAsyncResultSet (com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet)2 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)2