Search in sources :

Example 6 with AsyncGraphResultSet

use of com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet in project java-driver by datastax.

the class ContinuousGraphRequestHandlerTest method should_return_paged_results.

@Test
@UseDataProvider(location = DseTestDataProviders.class, value = "supportedGraphProtocols")
public void should_return_paged_results(GraphProtocol graphProtocol) throws IOException {
    String profileName = "test-graph";
    when(nodeMetricUpdater1.isEnabled(DseNodeMetric.GRAPH_MESSAGES, profileName)).thenReturn(true);
    GraphBinaryModule module = createGraphBinaryModule(mockContext);
    GraphRequestHandlerTestHarness.Builder builder = GraphRequestHandlerTestHarness.builder().withGraphProtocolForTestConfig(graphProtocol);
    PoolBehavior node1Behavior = builder.customBehavior(node);
    try (RequestHandlerTestHarness harness = builder.build()) {
        GraphStatement<?> graphStatement = ScriptGraphStatement.newInstance("mockQuery").setExecutionProfileName(profileName);
        ContinuousGraphRequestHandler handler = new ContinuousGraphRequestHandler(graphStatement, harness.getSession(), harness.getContext(), "test", module, new GraphSupportChecker());
        // send the initial request
        CompletionStage<AsyncGraphResultSet> page1Future = handler.handle();
        node1Behavior.setResponseSuccess(defaultDseFrameOf(tenGraphRows(graphProtocol, module, 1, false)));
        assertThatStage(page1Future).isSuccess(page1 -> {
            assertThat(page1.hasMorePages()).isTrue();
            assertThat(page1.currentPage()).hasSize(10).allMatch(GraphNode::isVertex);
            ExecutionInfo executionInfo = page1.getRequestExecutionInfo();
            assertThat(executionInfo.getCoordinator()).isEqualTo(node);
            assertThat(executionInfo.getErrors()).isEmpty();
            assertThat(executionInfo.getIncomingPayload()).isEmpty();
            assertThat(executionInfo.getSpeculativeExecutionCount()).isEqualTo(0);
            assertThat(executionInfo.getSuccessfulExecutionIndex()).isEqualTo(0);
            assertThat(executionInfo.getWarnings()).isEmpty();
        });
        AsyncGraphResultSet page1 = CompletableFutures.getCompleted(page1Future);
        CompletionStage<AsyncGraphResultSet> page2Future = page1.fetchNextPage();
        node1Behavior.setResponseSuccess(defaultDseFrameOf(tenGraphRows(graphProtocol, module, 2, true)));
        assertThatStage(page2Future).isSuccess(page2 -> {
            assertThat(page2.hasMorePages()).isFalse();
            assertThat(page2.currentPage()).hasSize(10).allMatch(GraphNode::isVertex);
            ExecutionInfo executionInfo = page2.getRequestExecutionInfo();
            assertThat(executionInfo.getCoordinator()).isEqualTo(node);
            assertThat(executionInfo.getErrors()).isEmpty();
            assertThat(executionInfo.getIncomingPayload()).isEmpty();
            assertThat(executionInfo.getSpeculativeExecutionCount()).isEqualTo(0);
            assertThat(executionInfo.getSuccessfulExecutionIndex()).isEqualTo(0);
            assertThat(executionInfo.getWarnings()).isEmpty();
        });
        validateMetrics(profileName, harness);
    }
}
Also used : PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) 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) AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 7 with AsyncGraphResultSet

use of com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet in project java-driver by datastax.

the class ContinuousGraphRequestHandlerTest method should_honor_default_timeout.

@Test
public void should_honor_default_timeout() throws Exception {
    // given
    GraphBinaryModule binaryModule = createGraphBinaryModule(mockContext);
    Duration defaultTimeout = Duration.ofSeconds(1);
    RequestHandlerTestHarness.Builder builder = GraphRequestHandlerTestHarness.builder().withGraphTimeout(defaultTimeout);
    PoolBehavior node1Behavior = builder.customBehavior(node);
    try (RequestHandlerTestHarness harness = builder.build()) {
        DriverExecutionProfile profile = harness.getContext().getConfig().getDefaultProfile();
        when(profile.isDefined(DseDriverOption.GRAPH_SUB_PROTOCOL)).thenReturn(true);
        when(profile.getString(DseDriverOption.GRAPH_SUB_PROTOCOL)).thenReturn(GraphProtocol.GRAPH_BINARY_1_0.toInternalCode());
        GraphStatement<?> graphStatement = ScriptGraphStatement.newInstance("mockQuery");
        // when
        ContinuousGraphRequestHandler handler = new ContinuousGraphRequestHandler(graphStatement, harness.getSession(), harness.getContext(), "test", binaryModule, new GraphSupportChecker());
        // send the initial request
        CompletionStage<AsyncGraphResultSet> page1Future = handler.handle();
        // acknowledge the write, will set the global timeout
        node1Behavior.verifyWrite();
        node1Behavior.setWriteSuccess();
        CapturedTimeout globalTimeout = harness.nextScheduledTimeout();
        assertThat(globalTimeout.getDelay(TimeUnit.NANOSECONDS)).isEqualTo(defaultTimeout.toNanos());
        // will trigger the global timeout and complete it exceptionally
        globalTimeout.task().run(globalTimeout);
        assertThat(page1Future.toCompletableFuture()).isCompletedExceptionally();
        assertThatThrownBy(() -> page1Future.toCompletableFuture().get()).hasRootCauseExactlyInstanceOf(DriverTimeoutException.class).hasMessageContaining("Query timed out after " + defaultTimeout);
    }
}
Also used : PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) DriverTimeoutException(com.datastax.oss.driver.api.core.DriverTimeoutException) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) Duration(java.time.Duration) GraphTestUtils.createGraphBinaryModule(com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule) GraphBinaryModule(com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule) AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet) CapturedTimeout(com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout) Test(org.junit.Test)

Example 8 with AsyncGraphResultSet

use of com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet in project java-driver by datastax.

the class GraphTraversalITBase method should_handle_asynchronous_execution_graph_binary.

@Test
public void should_handle_asynchronous_execution_graph_binary() {
    Assumptions.assumeThat(isGraphBinary()).isTrue();
    StringBuilder names = new StringBuilder();
    CompletionStage<AsyncGraphResultSet> future = session().executeAsync(FluentGraphStatement.newInstance(graphTraversalSource().V().hasLabel("person")));
    try {
        // dumb processing to make sure the completable future works correctly and correct results are
        // returned
        Iterable<GraphNode> results = future.thenApply(AsyncGraphResultSet::currentPage).toCompletableFuture().get();
        for (GraphNode gn : results) {
            names.append(gn.asVertex().id());
        }
    } catch (InterruptedException | ExecutionException e) {
        fail("Shouldn't have thrown an exception waiting for the result to complete");
    }
    assertThat(names.toString()).contains("peter", "marko", "vadas", "josh");
}
Also used : AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet) GraphNode(com.datastax.dse.driver.api.core.graph.GraphNode) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 9 with AsyncGraphResultSet

use of com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet in project java-driver by datastax.

the class GraphTraversalITBase method should_handle_asynchronous_execution_graphson.

@Test
public void should_handle_asynchronous_execution_graphson() {
    Assumptions.assumeThat(isGraphBinary()).isFalse();
    StringBuilder names = new StringBuilder();
    CompletionStage<AsyncGraphResultSet> future = session().executeAsync(FluentGraphStatement.newInstance(graphTraversalSource().V().hasLabel("person")));
    try {
        // dumb processing to make sure the completable future works correctly and correct results are
        // returned
        Iterable<GraphNode> results = future.thenApply(AsyncGraphResultSet::currentPage).toCompletableFuture().get();
        for (GraphNode gn : results) {
            names.append(gn.asVertex().property("name").value());
        }
    } catch (InterruptedException | ExecutionException e) {
        fail("Shouldn't have thrown an exception waiting for the result to complete");
    }
    assertThat(names.toString()).contains("peter", "marko", "vadas", "josh");
}
Also used : AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet) GraphNode(com.datastax.dse.driver.api.core.graph.GraphNode) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 10 with AsyncGraphResultSet

use of com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet in project java-driver by datastax.

the class ContinuousGraphRequestHandlerSpeculativeExecutionTest method should_fail_if_no_more_nodes_and_initial_execution_is_last.

@Test
@UseDataProvider(location = DseTestDataProviders.class, value = "idempotentGraphConfig")
public void should_fail_if_no_more_nodes_and_initial_execution_is_last(boolean defaultIdempotence, GraphStatement<?> statement) throws Exception {
    GraphRequestHandlerTestHarness.Builder harnessBuilder = GraphRequestHandlerTestHarness.builder().withDefaultIdempotence(defaultIdempotence);
    PoolBehavior node1Behavior = harnessBuilder.customBehavior(node1);
    harnessBuilder.withResponse(node2, defaultDseFrameOf(new Error(ProtocolConstants.ErrorCode.IS_BOOTSTRAPPING, "mock message")));
    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 yet
        // 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(defaultDseFrameOf(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 : PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) AllNodesFailedException(com.datastax.oss.driver.api.core.AllNodesFailedException) Node(com.datastax.oss.driver.api.core.metadata.Node) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) Error(com.datastax.oss.protocol.internal.response.Error) SpeculativeExecutionPolicy(com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy) GraphTestUtils.createGraphBinaryModule(com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule) GraphBinaryModule(com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule) AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet) 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

AsyncGraphResultSet (com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet)15 Test (org.junit.Test)13 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)9 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)9 PoolBehavior (com.datastax.oss.driver.internal.core.cql.PoolBehavior)8 CapturedTimeout (com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout)7 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)7 GraphNode (com.datastax.dse.driver.api.core.graph.GraphNode)6 Node (com.datastax.oss.driver.api.core.metadata.Node)6 SpeculativeExecutionPolicy (com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy)6 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)6 Error (com.datastax.oss.protocol.internal.response.Error)4 RequestHandlerTestHarness (com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness)3 GraphResultSet (com.datastax.dse.driver.api.core.graph.GraphResultSet)2 AllNodesFailedException (com.datastax.oss.driver.api.core.AllNodesFailedException)2 DriverTimeoutException (com.datastax.oss.driver.api.core.DriverTimeoutException)2 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)2 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)2 Duration (java.time.Duration)2 List (java.util.List)2