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);
}
}
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));
}
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);
}
});
}
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);
}
});
}
Aggregations