Search in sources :

Example 1 with ExecutionInfo

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

the class QueryTraceIT method should_not_have_tracing_id_when_tracing_disabled.

@Test
public void should_not_have_tracing_id_when_tracing_disabled() {
    ExecutionInfo executionInfo = SESSION_RULE.session().execute("SELECT release_version FROM system.local").getExecutionInfo();
    assertThat(executionInfo.getTracingId()).isNull();
    Throwable t = catchThrowable(executionInfo::getQueryTrace);
    assertThat(t).isInstanceOf(IllegalStateException.class).hasMessage("Tracing was disabled for this request");
}
Also used : ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Test(org.junit.Test)

Example 2 with ExecutionInfo

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

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

the class ExecutionInfoWarningsIT method should_expose_warnings_on_execution_info.

@Test
@CassandraRequirement(min = "2.2")
public void should_expose_warnings_on_execution_info() {
    // the default batch size warn threshold is 5 * 1024 bytes, but after CASSANDRA-10876 there must
    // be multiple mutations in a batch to trigger this warning so the batch includes 2 different
    // inserts.
    final String query = String.format("BEGIN UNLOGGED BATCH\n" + "INSERT INTO test (k, v) VALUES (1, '%s')\n" + "INSERT INTO test (k, v) VALUES (2, '%s')\n" + "APPLY BATCH", Strings.repeat("1", 2 * 1024), Strings.repeat("1", 3 * 1024));
    Statement<?> st = SimpleStatement.builder(query).build();
    ResultSet result = sessionRule.session().execute(st);
    ExecutionInfo executionInfo = result.getExecutionInfo();
    assertThat(executionInfo).isNotNull();
    List<String> warnings = executionInfo.getWarnings();
    assertThat(warnings).isNotEmpty();
    // verify the log was generated
    verify(appender, timeout(500).atLeast(1)).doAppend(loggingEventCaptor.capture());
    List<String> logMessages = loggingEventCaptor.getAllValues().stream().map(ILoggingEvent::getFormattedMessage).collect(Collectors.toList());
    assertThat(logMessages).anySatisfy(logMessage -> assertThat(logMessage).startsWith("Query '").contains(query.substring(0, RequestLogger.DEFAULT_REQUEST_LOGGER_MAX_QUERY_LENGTH)).contains("' generated server side warning(s): ").contains("Batch").contains("for").contains(String.format("%s.test", sessionRule.keyspace().asCql(true))).contains("is of size").containsPattern("exceeding specified .*threshold"));
}
Also used : ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) Test(org.junit.Test) CassandraRequirement(com.datastax.oss.driver.api.testinfra.CassandraRequirement)

Example 4 with ExecutionInfo

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

the class ExecutionInfoWarningsIT method should_execute_query_and_log_server_side_warnings.

@Test
@CassandraRequirement(min = "3.0")
public void should_execute_query_and_log_server_side_warnings() {
    final String query = "SELECT count(*) FROM test;";
    Statement<?> st = SimpleStatement.builder(query).build();
    ResultSet result = sessionRule.session().execute(st);
    ExecutionInfo executionInfo = result.getExecutionInfo();
    assertThat(executionInfo).isNotNull();
    List<String> warnings = executionInfo.getWarnings();
    assertThat(warnings).isNotEmpty();
    String warning = warnings.get(0);
    assertThat(warning).isEqualTo("Aggregation query used without partition key");
    // verify the log was generated
    verify(appender, timeout(500).times(1)).doAppend(loggingEventCaptor.capture());
    assertThat(loggingEventCaptor.getValue().getMessage()).isNotNull();
    String logMessage = loggingEventCaptor.getValue().getFormattedMessage();
    assertThat(logMessage).startsWith("Query '[0 values] " + query + "' generated server side warning(s): Aggregation query used without partition key");
}
Also used : ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) Test(org.junit.Test) CassandraRequirement(com.datastax.oss.driver.api.testinfra.CassandraRequirement)

Example 5 with ExecutionInfo

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

the class DirectCompressionIT method createAndCheckCluster.

private void createAndCheckCluster(String compressorOption) {
    DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withString(DefaultDriverOption.PROTOCOL_COMPRESSION, compressorOption).build();
    try (CqlSession session = SessionUtils.newSession(CCM_RULE, SCHEMA_SESSION_RULE.keyspace(), loader)) {
        // Run a couple of simple test queries
        ResultSet rs = session.execute(SimpleStatement.newInstance("INSERT INTO test (k, t, i, f) VALUES (?, ?, ?, ?)", "key", "foo", 42, 24.03f));
        assertThat(rs.iterator().hasNext()).isFalse();
        ResultSet rs1 = session.execute("SELECT * FROM test WHERE k = 'key'");
        assertThat(rs1.iterator().hasNext()).isTrue();
        Row row = rs1.iterator().next();
        assertThat(rs1.iterator().hasNext()).isFalse();
        assertThat(row.getString("k")).isEqualTo("key");
        assertThat(row.getString("t")).isEqualTo("foo");
        assertThat(row.getInt("i")).isEqualTo(42);
        assertThat(row.getFloat("f")).isEqualTo(24.03f, offset(0.1f));
        ExecutionInfo executionInfo = rs.getExecutionInfo();
        // There's not much more we can check without hard-coding sizes.
        // We are testing with small responses, so the compressed payload is not even guaranteed to be
        // smaller.
        assertThat(executionInfo.getResponseSizeInBytes()).isGreaterThan(0);
        if (session.getContext().getProtocolVersion().getCode() == 5) {
            // in protocol v5, compression is done at segment level
            assertThat(executionInfo.getCompressedResponseSizeInBytes()).isEqualTo(-1);
        } else {
            assertThat(executionInfo.getCompressedResponseSizeInBytes()).isGreaterThan(0);
        }
    }
}
Also used : ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) Row(com.datastax.oss.driver.api.core.cql.Row) CqlSession(com.datastax.oss.driver.api.core.CqlSession)

Aggregations

ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)50 Test (org.junit.Test)36 Row (com.datastax.oss.driver.api.core.cql.Row)20 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)19 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)13 ColumnDefinitions (com.datastax.oss.driver.api.core.cql.ColumnDefinitions)13 ReactiveRow (com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow)12 RequestHandlerTestHarness (com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness)12 ContinuousAsyncResultSet (com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet)8 ReactiveResultSet (com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet)6 PoolBehavior (com.datastax.oss.driver.internal.core.cql.PoolBehavior)6 DefaultSession (com.datastax.oss.driver.internal.core.session.DefaultSession)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)5 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)5 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)5 ReactiveGraphNode (com.datastax.dse.driver.api.core.graph.reactive.ReactiveGraphNode)4 CqlSession (com.datastax.oss.driver.api.core.CqlSession)4 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)4 EmptyColumnDefinitions (com.datastax.oss.driver.internal.core.cql.EmptyColumnDefinitions)4