Search in sources :

Example 1 with TraceEvent

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

the class QueryTraceFetcher method buildTrace.

private QueryTrace buildTrace(Row sessionRow, Iterable<Row> eventRows) {
    ImmutableList.Builder<TraceEvent> eventsBuilder = ImmutableList.builder();
    for (Row eventRow : eventRows) {
        UUID eventId = eventRow.getUuid("event_id");
        int sourcePort = 0;
        if (eventRow.getColumnDefinitions().contains("source_port")) {
            sourcePort = eventRow.getInt("source_port");
        }
        eventsBuilder.add(new DefaultTraceEvent(eventRow.getString("activity"), eventId == null ? -1 : eventId.timestamp(), new InetSocketAddress(eventRow.getInetAddress("source"), sourcePort), eventRow.getInt("source_elapsed"), eventRow.getString("thread")));
    }
    Instant startedAt = sessionRow.getInstant("started_at");
    int coordinatorPort = 0;
    if (sessionRow.getColumnDefinitions().contains("coordinator_port")) {
        coordinatorPort = sessionRow.getInt("coordinator_port");
    }
    return new DefaultQueryTrace(tracingId, sessionRow.getString("request"), sessionRow.getInt("duration"), new InetSocketAddress(sessionRow.getInetAddress("coordinator"), coordinatorPort), sessionRow.getMap("parameters", String.class, String.class), startedAt == null ? -1 : startedAt.toEpochMilli(), eventsBuilder.build());
}
Also used : ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) InetSocketAddress(java.net.InetSocketAddress) Instant(java.time.Instant) Row(com.datastax.oss.driver.api.core.cql.Row) UUID(java.util.UUID) TraceEvent(com.datastax.oss.driver.api.core.cql.TraceEvent)

Example 2 with TraceEvent

use of com.datastax.oss.driver.api.core.cql.TraceEvent 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 3 with TraceEvent

use of com.datastax.oss.driver.api.core.cql.TraceEvent 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

Row (com.datastax.oss.driver.api.core.cql.Row)3 TraceEvent (com.datastax.oss.driver.api.core.cql.TraceEvent)3 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)3 Instant (java.time.Instant)3 UUID (java.util.UUID)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 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)2 QueryTrace (com.datastax.oss.driver.api.core.cql.QueryTrace)2 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)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