Search in sources :

Example 1 with QueryTrace

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

the class QueryTraceIT method should_fetch_trace_when_tracing_enabled.

@Test
public void should_fetch_trace_when_tracing_enabled() {
    ExecutionInfo executionInfo = SESSION_RULE.session().execute(SimpleStatement.builder("SELECT release_version FROM system.local").setTracing().build()).getExecutionInfo();
    assertThat(executionInfo.getTracingId()).isNotNull();
    EndPoint contactPoint = CCM_RULE.getContactPoints().iterator().next();
    InetAddress nodeAddress = ((InetSocketAddress) contactPoint.resolve()).getAddress();
    boolean expectPorts = CCM_RULE.getCassandraVersion().nextStable().compareTo(Version.V4_0_0) >= 0 && !CCM_RULE.getDseVersion().isPresent();
    QueryTrace queryTrace = executionInfo.getQueryTrace();
    assertThat(queryTrace.getTracingId()).isEqualTo(executionInfo.getTracingId());
    assertThat(queryTrace.getRequestType()).isEqualTo("Execute CQL3 query");
    assertThat(queryTrace.getDurationMicros()).isPositive();
    assertThat(queryTrace.getCoordinatorAddress().getAddress()).isEqualTo(nodeAddress);
    if (expectPorts) {
        Row row = SESSION_RULE.session().execute("SELECT coordinator_port FROM system_traces.sessions WHERE session_id = " + queryTrace.getTracingId()).one();
        assertThat(row).isNotNull();
        int expectedPort = row.getInt(0);
        assertThat(queryTrace.getCoordinatorAddress().getPort()).isEqualTo(expectedPort);
    } else {
        assertThat(queryTrace.getCoordinatorAddress().getPort()).isEqualTo(0);
    }
    assertThat(queryTrace.getParameters()).containsEntry("consistency_level", "LOCAL_ONE").containsEntry("page_size", "5000").containsEntry("query", "SELECT release_version FROM system.local").containsEntry("serial_consistency_level", "SERIAL");
    assertThat(queryTrace.getStartedAt()).isPositive();
    // Don't want to get too deep into event testing because that could change across versions
    assertThat(queryTrace.getEvents()).isNotEmpty();
    InetSocketAddress sourceAddress0 = queryTrace.getEvents().get(0).getSourceAddress();
    assertThat(sourceAddress0).isNotNull();
    assertThat(sourceAddress0.getAddress()).isEqualTo(nodeAddress);
    if (expectPorts) {
        Row row = SESSION_RULE.session().execute("SELECT source_port FROM system_traces.events WHERE session_id = " + queryTrace.getTracingId() + " LIMIT 1").one();
        assertThat(row).isNotNull();
        int expectedPort = row.getInt(0);
        assertThat(sourceAddress0.getPort()).isEqualTo(expectedPort);
    } else {
        assertThat(sourceAddress0.getPort()).isEqualTo(0);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) Row(com.datastax.oss.driver.api.core.cql.Row) InetAddress(java.net.InetAddress) QueryTrace(com.datastax.oss.driver.api.core.cql.QueryTrace) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) Test(org.junit.Test)

Example 2 with QueryTrace

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

the class QueryTraceFetcherTest method should_fail_when_session_query_fails.

@Test
public void should_fail_when_session_query_fails() {
    // Given
    RuntimeException mockError = new RuntimeException("mock error");
    when(session.executeAsync(any(SimpleStatement.class))).thenReturn(CompletableFutures.failedFuture(mockError));
    // When
    QueryTraceFetcher fetcher = new QueryTraceFetcher(TRACING_ID, session, context, config);
    CompletionStage<QueryTrace> traceFuture = fetcher.fetch();
    // Then
    verify(session).executeAsync(statementCaptor.capture());
    SimpleStatement statement = statementCaptor.getValue();
    assertSessionQuery(statement);
    verifyNoMoreInteractions(session);
    assertThatStage(traceFuture).isFailed(error -> assertThat(error).isSameAs(mockError));
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) QueryTrace(com.datastax.oss.driver.api.core.cql.QueryTrace) Test(org.junit.Test)

Example 3 with QueryTrace

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

the class QueryTraceFetcherTest method should_retry_when_session_row_is_incomplete.

@Test
public void should_retry_when_session_row_is_incomplete() {
    // Given
    CompletionStage<AsyncResultSet> sessionRow1 = incompleteSessionRow();
    CompletionStage<AsyncResultSet> sessionRow2 = completeSessionRow();
    CompletionStage<AsyncResultSet> eventRows = singlePageEventRows();
    when(session.executeAsync(any(SimpleStatement.class))).thenAnswer(invocation -> sessionRow1).thenAnswer(invocation -> sessionRow2).thenAnswer(invocation -> eventRows);
    // When
    QueryTraceFetcher fetcher = new QueryTraceFetcher(TRACING_ID, session, context, config);
    CompletionStage<QueryTrace> traceFuture = fetcher.fetch();
    // Then
    verify(session, times(3)).executeAsync(statementCaptor.capture());
    List<SimpleStatement> statements = statementCaptor.getAllValues();
    assertSessionQuery(statements.get(0));
    assertSessionQuery(statements.get(1));
    assertEventsQuery(statements.get(2));
    verifyNoMoreInteractions(session);
    assertThatStage(traceFuture).isSuccess(trace -> {
        assertThat(trace.getTracingId()).isEqualTo(TRACING_ID);
        assertThat(trace.getRequestType()).isEqualTo("mock request");
        assertThat(trace.getDurationMicros()).isEqualTo(42);
        assertThat(trace.getCoordinatorAddress().getAddress()).isEqualTo(address);
        assertThat(trace.getCoordinatorAddress().getPort()).isEqualTo(PORT);
        assertThat(trace.getParameters()).hasSize(2).containsEntry("key1", "value1").containsEntry("key2", "value2");
        assertThat(trace.getStartedAt()).isEqualTo(0);
        List<TraceEvent> events = trace.getEvents();
        assertThat(events).hasSize(3);
        for (int i = 0; i < events.size(); i++) {
            TraceEvent event = events.get(i);
            assertThat(event.getActivity()).isEqualTo("mock activity " + i);
            assertThat(event.getTimestamp()).isEqualTo(i);
            assertThat(event.getSourceAddress()).isNotNull();
            assertThat(event.getSourceAddress().getAddress()).isEqualTo(address);
            assertThat(event.getSourceAddress().getPort()).isEqualTo(PORT);
            assertThat(event.getSourceElapsedMicros()).isEqualTo(i);
            assertThat(event.getThreadName()).isEqualTo("mock thread " + i);
        }
    });
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) EventExecutor(io.netty.util.concurrent.EventExecutor) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) DefaultConsistencyLevel(com.datastax.oss.driver.api.core.DefaultConsistencyLevel) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) CompletableFuture(java.util.concurrent.CompletableFuture) NettyOptions(com.datastax.oss.driver.internal.core.context.NettyOptions) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Captor(org.mockito.Captor) ByteBuffer(java.nio.ByteBuffer) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) ArgumentCaptor(org.mockito.ArgumentCaptor) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) Duration(java.time.Duration) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) Assertions.assertThat(com.datastax.oss.driver.Assertions.assertThat) Row(com.datastax.oss.driver.api.core.cql.Row) CompletableFutures(com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures) Before(org.junit.Before) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) Instant(java.time.Instant) QueryTrace(com.datastax.oss.driver.api.core.cql.QueryTrace) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) Mockito.verify(org.mockito.Mockito.verify) TraceEvent(com.datastax.oss.driver.api.core.cql.TraceEvent) TimeUnit(java.util.concurrent.TimeUnit) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) Bytes(com.datastax.oss.protocol.internal.util.Bytes) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assertions.assertThatStage(com.datastax.oss.driver.Assertions.assertThatStage) Mockito.mock(org.mockito.Mockito.mock) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) QueryTrace(com.datastax.oss.driver.api.core.cql.QueryTrace) TraceEvent(com.datastax.oss.driver.api.core.cql.TraceEvent) Test(org.junit.Test)

Example 4 with QueryTrace

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

the class QueryTraceFetcherTest method should_succeed_when_both_queries_succeed_immediately.

@Test
public void should_succeed_when_both_queries_succeed_immediately() {
    // Given
    CompletionStage<AsyncResultSet> sessionRow = completeSessionRow();
    CompletionStage<AsyncResultSet> eventRows = singlePageEventRows();
    when(session.executeAsync(any(SimpleStatement.class))).thenAnswer(invocation -> sessionRow).thenAnswer(invocation -> eventRows);
    // When
    QueryTraceFetcher fetcher = new QueryTraceFetcher(TRACING_ID, session, context, config);
    CompletionStage<QueryTrace> traceFuture = fetcher.fetch();
    // Then
    verify(session, times(2)).executeAsync(statementCaptor.capture());
    List<SimpleStatement> statements = statementCaptor.getAllValues();
    assertSessionQuery(statements.get(0));
    SimpleStatement statement = statements.get(1);
    assertEventsQuery(statement);
    verifyNoMoreInteractions(session);
    assertThatStage(traceFuture).isSuccess(trace -> {
        assertThat(trace.getTracingId()).isEqualTo(TRACING_ID);
        assertThat(trace.getRequestType()).isEqualTo("mock request");
        assertThat(trace.getDurationMicros()).isEqualTo(42);
        assertThat(trace.getCoordinatorAddress().getAddress()).isEqualTo(address);
        assertThat(trace.getCoordinatorAddress().getPort()).isEqualTo(PORT);
        assertThat(trace.getParameters()).hasSize(2).containsEntry("key1", "value1").containsEntry("key2", "value2");
        assertThat(trace.getStartedAt()).isEqualTo(0);
        List<TraceEvent> events = trace.getEvents();
        assertThat(events).hasSize(3);
        for (int i = 0; i < events.size(); i++) {
            TraceEvent event = events.get(i);
            assertThat(event.getActivity()).isEqualTo("mock activity " + i);
            assertThat(event.getTimestamp()).isEqualTo(i);
            assertThat(event.getSourceAddress()).isNotNull();
            assertThat(event.getSourceAddress().getAddress()).isEqualTo(address);
            assertThat(event.getSourceAddress().getPort()).isEqualTo(PORT);
            assertThat(event.getSourceElapsedMicros()).isEqualTo(i);
            assertThat(event.getThreadName()).isEqualTo("mock thread " + i);
        }
    });
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) EventExecutor(io.netty.util.concurrent.EventExecutor) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) DefaultConsistencyLevel(com.datastax.oss.driver.api.core.DefaultConsistencyLevel) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) CompletableFuture(java.util.concurrent.CompletableFuture) NettyOptions(com.datastax.oss.driver.internal.core.context.NettyOptions) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Captor(org.mockito.Captor) ByteBuffer(java.nio.ByteBuffer) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) ArgumentCaptor(org.mockito.ArgumentCaptor) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) Duration(java.time.Duration) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) Assertions.assertThat(com.datastax.oss.driver.Assertions.assertThat) Row(com.datastax.oss.driver.api.core.cql.Row) CompletableFutures(com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures) Before(org.junit.Before) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) Instant(java.time.Instant) QueryTrace(com.datastax.oss.driver.api.core.cql.QueryTrace) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) Mockito.verify(org.mockito.Mockito.verify) TraceEvent(com.datastax.oss.driver.api.core.cql.TraceEvent) TimeUnit(java.util.concurrent.TimeUnit) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) Bytes(com.datastax.oss.protocol.internal.util.Bytes) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assertions.assertThatStage(com.datastax.oss.driver.Assertions.assertThatStage) Mockito.mock(org.mockito.Mockito.mock) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) QueryTrace(com.datastax.oss.driver.api.core.cql.QueryTrace) TraceEvent(com.datastax.oss.driver.api.core.cql.TraceEvent) Test(org.junit.Test)

Aggregations

QueryTrace (com.datastax.oss.driver.api.core.cql.QueryTrace)4 Test (org.junit.Test)4 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)3 Row (com.datastax.oss.driver.api.core.cql.Row)3 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)3 InetAddress (java.net.InetAddress)3 Assertions.assertThat (com.datastax.oss.driver.Assertions.assertThat)2 Assertions.assertThatStage (com.datastax.oss.driver.Assertions.assertThatStage)2 CqlSession (com.datastax.oss.driver.api.core.CqlSession)2 DefaultConsistencyLevel (com.datastax.oss.driver.api.core.DefaultConsistencyLevel)2 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)2 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)2 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)2 ColumnDefinitions (com.datastax.oss.driver.api.core.cql.ColumnDefinitions)2 TraceEvent (com.datastax.oss.driver.api.core.cql.TraceEvent)2 Uuids (com.datastax.oss.driver.api.core.uuid.Uuids)2 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)2 NettyOptions (com.datastax.oss.driver.internal.core.context.NettyOptions)2 CompletableFutures (com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures)2 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)2