Search in sources :

Example 6 with PoolBehavior

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

the class GraphRequestHandlerTest method should_return_results_for_statements.

@Test
@UseDataProvider("supportedGraphProtocolsWithDseVersions")
public void should_return_results_for_statements(GraphProtocol graphProtocol, Version dseVersion) throws IOException {
    GraphRequestHandlerTestHarness.Builder builder = GraphRequestHandlerTestHarness.builder().withGraphProtocolForTestConfig(graphProtocol).withDseVersionInMetadata(dseVersion);
    PoolBehavior node1Behavior = builder.customBehavior(node);
    GraphRequestHandlerTestHarness harness = builder.build();
    GraphBinaryModule module = createGraphBinaryModule(harness.getContext());
    // ideally we would be able to provide a function here to
    // produce results instead of a static predefined response.
    // Function to which we would pass the harness instance or a (mocked)DriverContext.
    // Since that's not possible in the RequestHandlerTestHarness API at the moment, we
    // have to use another DseDriverContext and GraphBinaryModule here,
    // instead of reusing the one in the harness' DriverContext
    node1Behavior.setResponseSuccess(defaultDseFrameOf(singleGraphRow(graphProtocol, module)));
    GraphSupportChecker graphSupportChecker = mock(GraphSupportChecker.class);
    when(graphSupportChecker.isPagingEnabled(any(), any())).thenReturn(false);
    when(graphSupportChecker.inferGraphProtocol(any(), any(), any())).thenReturn(graphProtocol);
    GraphRequestAsyncProcessor p = Mockito.spy(new GraphRequestAsyncProcessor(harness.getContext(), graphSupportChecker));
    when(p.getGraphBinaryModule()).thenReturn(module);
    GraphStatement<?> graphStatement = ScriptGraphStatement.newInstance("mockQuery").setExecutionProfileName("test-graph");
    GraphResultSet grs = new GraphRequestSyncProcessor(p).process(graphStatement, harness.getSession(), harness.getContext(), "test-graph");
    List<GraphNode> nodes = grs.all();
    assertThat(nodes.size()).isEqualTo(1);
    GraphNode graphNode = nodes.get(0);
    assertThat(graphNode.isVertex()).isTrue();
    Vertex vRead = graphNode.asVertex();
    assertThat(vRead.label()).isEqualTo("person");
    assertThat(vRead.id()).isEqualTo(1);
    if (!graphProtocol.isGraphBinary()) {
        // GraphBinary does not encode properties regardless of whether they are present in the
        // parent element or not :/
        assertThat(vRead.property("name").id()).isEqualTo(11);
        assertThat(vRead.property("name").value()).isEqualTo("marko");
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) GraphResultSet(com.datastax.dse.driver.api.core.graph.GraphResultSet) GraphNode(com.datastax.dse.driver.api.core.graph.GraphNode) 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 7 with PoolBehavior

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

the class GraphRequestHandlerTest method should_invoke_request_tracker_and_update_metrics.

@Test
@UseDataProvider("dseVersionsWithDefaultGraphProtocol")
public void should_invoke_request_tracker_and_update_metrics(GraphProtocol graphProtocol, Version dseVersion) throws IOException {
    when(nodeMetricUpdater1.isEnabled(DseNodeMetric.GRAPH_MESSAGES, DriverExecutionProfile.DEFAULT_NAME)).thenReturn(true);
    GraphRequestHandlerTestHarness.Builder builder = GraphRequestHandlerTestHarness.builder().withGraphProtocolForTestConfig(graphProtocol).withDseVersionInMetadata(dseVersion);
    PoolBehavior node1Behavior = builder.customBehavior(node);
    GraphRequestHandlerTestHarness harness = builder.build();
    GraphBinaryModule module = createGraphBinaryModule(harness.getContext());
    GraphSupportChecker graphSupportChecker = mock(GraphSupportChecker.class);
    when(graphSupportChecker.isPagingEnabled(any(), any())).thenReturn(false);
    when(graphSupportChecker.inferGraphProtocol(any(), any(), any())).thenReturn(graphProtocol);
    GraphRequestAsyncProcessor p = Mockito.spy(new GraphRequestAsyncProcessor(harness.getContext(), graphSupportChecker));
    when(p.getGraphBinaryModule()).thenReturn(module);
    RequestTracker requestTracker = mock(RequestTracker.class);
    when(harness.getContext().getRequestTracker()).thenReturn(requestTracker);
    GraphStatement<?> graphStatement = ScriptGraphStatement.newInstance("mockQuery");
    node1Behavior.setResponseSuccess(defaultDseFrameOf(singleGraphRow(graphProtocol, module)));
    GraphResultSet grs = new GraphRequestSyncProcessor(new GraphRequestAsyncProcessor(harness.getContext(), graphSupportChecker)).process(graphStatement, harness.getSession(), harness.getContext(), "test-graph");
    List<GraphNode> nodes = grs.all();
    assertThat(nodes.size()).isEqualTo(1);
    GraphNode graphNode = nodes.get(0);
    assertThat(graphNode.isVertex()).isTrue();
    Vertex actual = graphNode.asVertex();
    assertThat(actual.label()).isEqualTo("person");
    assertThat(actual.id()).isEqualTo(1);
    if (!graphProtocol.isGraphBinary()) {
        // GraphBinary does not encode properties regardless of whether they are present in the
        // parent element or not :/
        assertThat(actual.property("name").id()).isEqualTo(11);
        assertThat(actual.property("name").value()).isEqualTo("marko");
    }
    verify(requestTracker).onSuccess(eq(graphStatement), anyLong(), any(DriverExecutionProfile.class), eq(node), matches(LOG_PREFIX_PER_REQUEST));
    verify(requestTracker).onNodeSuccess(eq(graphStatement), anyLong(), any(DriverExecutionProfile.class), eq(node), matches(LOG_PREFIX_PER_REQUEST));
    verifyNoMoreInteractions(requestTracker);
    verify(nodeMetricUpdater1).isEnabled(DseNodeMetric.GRAPH_MESSAGES, DriverExecutionProfile.DEFAULT_NAME);
    verify(nodeMetricUpdater1).updateTimer(eq(DseNodeMetric.GRAPH_MESSAGES), eq(DriverExecutionProfile.DEFAULT_NAME), anyLong(), eq(TimeUnit.NANOSECONDS));
    verifyNoMoreInteractions(nodeMetricUpdater1);
    verify(harness.getSession().getMetricUpdater()).isEnabled(DseSessionMetric.GRAPH_REQUESTS, DriverExecutionProfile.DEFAULT_NAME);
    verify(harness.getSession().getMetricUpdater()).updateTimer(eq(DseSessionMetric.GRAPH_REQUESTS), eq(DriverExecutionProfile.DEFAULT_NAME), anyLong(), eq(TimeUnit.NANOSECONDS));
    verifyNoMoreInteractions(harness.getSession().getMetricUpdater());
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) GraphNode(com.datastax.dse.driver.api.core.graph.GraphNode) RequestTracker(com.datastax.oss.driver.api.core.tracker.RequestTracker) GraphTestUtils.createGraphBinaryModule(com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule) GraphBinaryModule(com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule) GraphResultSet(com.datastax.dse.driver.api.core.graph.GraphResultSet) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 8 with PoolBehavior

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

the class ContinuousCqlRequestHandlerTest method should_time_out_if_other_page_takes_too_long.

@Test
@UseDataProvider(value = "allDseProtocolVersions", location = DseTestDataProviders.class)
public void should_time_out_if_other_page_takes_too_long(DseProtocolVersion version) throws Exception {
    RequestHandlerTestHarness.Builder builder = continuousHarnessBuilder().withProtocolVersion(version);
    PoolBehavior node1Behavior = builder.customBehavior(node1);
    try (RequestHandlerTestHarness harness = builder.build()) {
        CompletionStage<ContinuousAsyncResultSet> page1Future = new ContinuousCqlRequestHandler(UNDEFINED_IDEMPOTENCE_STATEMENT, harness.getSession(), harness.getContext(), "test").handle();
        // mark the initial request as successful, which should schedule a timeout for the first page
        node1Behavior.setWriteSuccess();
        CapturedTimeout page1Timeout = harness.nextScheduledTimeout();
        assertThat(page1Timeout.getDelay(TimeUnit.NANOSECONDS)).isEqualTo(TIMEOUT_FIRST_PAGE.toNanos());
        // the server replies with page 1, the corresponding timeout should be cancelled
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(1, false)));
        assertThat(page1Timeout.isCancelled()).isTrue();
        // request page 2, the queue is empty so this should request more pages and schedule another
        // timeout
        ContinuousAsyncResultSet page1 = CompletableFutures.getUninterruptibly(page1Future);
        CompletionStage<ContinuousAsyncResultSet> page2Future = page1.fetchNextPage();
        CapturedTimeout page2Timeout = harness.nextScheduledTimeout();
        assertThat(page2Timeout.getDelay(TimeUnit.NANOSECONDS)).isEqualTo(TIMEOUT_OTHER_PAGES.toNanos());
        page2Timeout.task().run(page2Timeout);
        assertThatStage(page2Future).isFailed(t -> assertThat(t).isInstanceOf(DriverTimeoutException.class).hasMessageContaining("Timed out waiting for page 2"));
    }
}
Also used : PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) CapturedTimeout(com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 9 with PoolBehavior

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

the class ContinuousCqlRequestHandlerTest method should_time_out_if_first_page_takes_too_long.

@Test
@UseDataProvider(value = "allDseProtocolVersions", location = DseTestDataProviders.class)
public void should_time_out_if_first_page_takes_too_long(DseProtocolVersion version) throws Exception {
    RequestHandlerTestHarness.Builder builder = continuousHarnessBuilder().withProtocolVersion(version);
    PoolBehavior node1Behavior = builder.customBehavior(node1);
    try (RequestHandlerTestHarness harness = builder.build()) {
        CompletionStage<ContinuousAsyncResultSet> resultSetFuture = new ContinuousCqlRequestHandler(UNDEFINED_IDEMPOTENCE_STATEMENT, harness.getSession(), harness.getContext(), "test").handle();
        // mark the initial request as successful, which should schedule a timeout for the first page
        node1Behavior.setWriteSuccess();
        CapturedTimeout page1Timeout = harness.nextScheduledTimeout();
        assertThat(page1Timeout.getDelay(TimeUnit.NANOSECONDS)).isEqualTo(TIMEOUT_FIRST_PAGE.toNanos());
        page1Timeout.task().run(page1Timeout);
        assertThatStage(resultSetFuture).isFailed(t -> assertThat(t).isInstanceOf(DriverTimeoutException.class).hasMessageContaining("Timed out waiting for page 1"));
    }
}
Also used : PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) CapturedTimeout(com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 10 with PoolBehavior

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

the class ContinuousCqlRequestHandlerTest method should_send_backpressure_request_if_dse_v2.

@Test
public void should_send_backpressure_request_if_dse_v2() {
    RequestHandlerTestHarness.Builder builder = continuousHarnessBuilder().withProtocolVersion(DSE_V2);
    PoolBehavior node1Behavior = builder.customBehavior(node1);
    try (RequestHandlerTestHarness harness = builder.build()) {
        CompletionStage<ContinuousAsyncResultSet> page1Future = new ContinuousCqlRequestHandler(UNDEFINED_IDEMPOTENCE_STATEMENT, harness.getSession(), harness.getContext(), "test").handle();
        // simulate the arrival of 4 pages, the first one will complete page1 future above,
        // the following 3 will be enqueued
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(1, false)));
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(2, false)));
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(3, false)));
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(4, false)));
        // simulate the retrieval of 2 pages, this should dequeue page 2
        // and trigger a backpressure request as the queue is now half empty (2/4)
        ContinuousAsyncResultSet page1 = CompletableFutures.getCompleted(page1Future);
        CompletableFutures.getCompleted(page1.fetchNextPage());
        verify(node1Behavior.getChannel()).write(argThat(this::isBackpressureRequest), anyBoolean(), anyMap(), any());
        // should not mess with autoread in dse v2
        verify(node1Behavior.getChannel().config(), never()).setAutoRead(anyBoolean());
    }
}
Also used : PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) Test(org.junit.Test)

Aggregations

PoolBehavior (com.datastax.oss.driver.internal.core.cql.PoolBehavior)25 Test (org.junit.Test)25 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)20 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)14 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)14 RequestHandlerTestHarness (com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness)14 CapturedTimeout (com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout)10 ContinuousAsyncResultSet (com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet)9 AsyncGraphResultSet (com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet)8 SpeculativeExecutionPolicy (com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy)7 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)6 Node (com.datastax.oss.driver.api.core.metadata.Node)6 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)6 DefaultSession (com.datastax.oss.driver.internal.core.session.DefaultSession)4 Error (com.datastax.oss.protocol.internal.response.Error)4 GraphNode (com.datastax.dse.driver.api.core.graph.GraphNode)3 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)3 ReactiveRow (com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow)2 GraphResultSet (com.datastax.dse.driver.api.core.graph.GraphResultSet)2 ReactiveGraphNode (com.datastax.dse.driver.api.core.graph.reactive.ReactiveGraphNode)2