Search in sources :

Example 16 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project zipkin by openzipkin.

the class CassandraStorageExtension method clear.

void clear(CassandraStorage storage) {
    // Clear any key cache
    CassandraSpanConsumer spanConsumer = storage.spanConsumer;
    if (spanConsumer != null)
        spanConsumer.clear();
    CqlSession session = storage.session.session;
    if (session == null)
        session = globalSession;
    List<String> toTruncate = new ArrayList<>(SEARCH_TABLES);
    toTruncate.add(TABLE_DEPENDENCY);
    toTruncate.add(TABLE_SPAN);
    for (String table : toTruncate) {
        try {
            session.execute("TRUNCATE " + storage.keyspace + "." + table);
        } catch (InvalidQueryException e) {
            assertThat(e).hasMessage("unconfigured table " + table);
        }
    }
    blockWhileInFlight(storage);
}
Also used : ArrayList(java.util.ArrayList) CqlSession(com.datastax.oss.driver.api.core.CqlSession) InvalidQueryException(com.datastax.oss.driver.api.core.servererrors.InvalidQueryException)

Example 17 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project zipkin by openzipkin.

the class CassandraStorageExtension method poolInFlight.

// Use metrics to wait for in-flight requests to settle per
// https://groups.google.com/a/lists.datastax.com/g/java-driver-user/c/5um_yGNynow/m/cInH5I5jBgAJ
static boolean poolInFlight(CqlSession session) {
    Collection<Node> nodes = session.getMetadata().getNodes().values();
    Optional<Metrics> metrics = session.getMetrics();
    for (Node node : nodes) {
        int inFlight = metrics.flatMap(m -> m.getNodeMetric(node, DefaultNodeMetric.IN_FLIGHT)).map(m -> ((Gauge<Integer>) m).getValue()).orElse(0);
        if (inFlight > 0)
            return true;
    }
    return false;
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) Metrics(com.datastax.oss.driver.api.core.metrics.Metrics) DefaultNodeMetric(com.datastax.oss.driver.api.core.metrics.DefaultNodeMetric) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ArrayList(java.util.ArrayList) Slf4jLogConsumer(org.testcontainers.containers.output.Slf4jLogConsumer) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) SEARCH_TABLES(zipkin2.storage.cassandra.ITCassandraStorage.SEARCH_TABLES) TABLE_SPAN(zipkin2.storage.cassandra.Schema.TABLE_SPAN) GenericContainer(org.testcontainers.containers.GenericContainer) InvalidQueryException(com.datastax.oss.driver.api.core.servererrors.InvalidQueryException) Call.propagateIfFatal(zipkin2.Call.propagateIfFatal) DockerImageName.parse(org.testcontainers.utility.DockerImageName.parse) Wait(org.testcontainers.containers.wait.strategy.Wait) TABLE_DEPENDENCY(zipkin2.storage.cassandra.Schema.TABLE_DEPENDENCY) Logger(org.slf4j.Logger) Collection(java.util.Collection) TestAbortedException(org.opentest4j.TestAbortedException) List(java.util.List) BeforeAllCallback(org.junit.jupiter.api.extension.BeforeAllCallback) AfterAllCallback(org.junit.jupiter.api.extension.AfterAllCallback) Optional(java.util.Optional) Gauge(com.codahale.metrics.Gauge) Metrics(com.datastax.oss.driver.api.core.metrics.Metrics) Node(com.datastax.oss.driver.api.core.metadata.Node) Gauge(com.codahale.metrics.Gauge)

Example 18 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project zipkin by openzipkin.

the class CassandraStorageExtension method tryToInitializeSession.

// Builds a session without trying to use a namespace or init UDTs
static CqlSession tryToInitializeSession(String contactPoint) {
    CassandraStorage storage = newStorageBuilder(contactPoint).build();
    CqlSession session = null;
    try {
        session = DefaultSessionFactory.buildSession(storage);
        session.execute("SELECT now() FROM system.local");
    } catch (Throwable e) {
        propagateIfFatal(e);
        if (session != null)
            session.close();
        assumeTrue(false, e.getMessage());
    }
    return session;
}
Also used : CqlSession(com.datastax.oss.driver.api.core.CqlSession)

Example 19 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project zipkin by openzipkin.

the class InternalForTests method writeDependencyLinks.

static void writeDependencyLinks(CassandraStorage storage, List<DependencyLink> links, long midnightUTC) {
    CqlSession session = storage.session();
    PreparedStatement prepared = session.prepare("INSERT INTO " + Schema.TABLE_DEPENDENCY + " (day,parent,child,calls,errors)" + " VALUES (?,?,?,?,?)");
    LocalDate day = Instant.ofEpochMilli(midnightUTC).atZone(ZoneOffset.UTC).toLocalDate();
    for (DependencyLink link : links) {
        int i = 0;
        storage.session().execute(prepared.bind().setLocalDate(i++, day).setString(i++, link.parent()).setString(i++, link.child()).setLong(i++, link.callCount()).setLong(i, link.errorCount()));
    }
}
Also used : PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) DependencyLink(zipkin2.DependencyLink) CqlSession(com.datastax.oss.driver.api.core.CqlSession) LocalDate(java.time.LocalDate)

Example 20 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project zipkin by openzipkin.

the class InternalForTests method mockSession.

static CqlSession mockSession() {
    CqlSession session = mock(CqlSession.class);
    Metadata metadata = mock(Metadata.class);
    Node node = mock(Node.class);
    when(session.getMetadata()).thenReturn(metadata);
    when(metadata.getNodes()).thenReturn(Collections.singletonMap(UUID.fromString("11111111-1111-1111-1111-111111111111"), node));
    when(node.getCassandraVersion()).thenReturn(Version.parse("3.11.9"));
    KeyspaceMetadata keyspaceMetadata = mock(KeyspaceMetadata.class);
    when(session.getMetadata()).thenReturn(metadata);
    when(metadata.getKeyspace("zipkin2")).thenReturn(Optional.of(keyspaceMetadata));
    when(keyspaceMetadata.getTable(TABLE_SERVICE_REMOTE_SERVICES)).thenReturn(Optional.of(mock(TableMetadata.class)));
    return session;
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) Metadata(com.datastax.oss.driver.api.core.metadata.Metadata) CqlSession(com.datastax.oss.driver.api.core.CqlSession) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata)

Aggregations

CqlSession (com.datastax.oss.driver.api.core.CqlSession)41 Node (com.datastax.oss.driver.api.core.metadata.Node)20 Test (org.junit.jupiter.api.Test)20 Health (org.springframework.boot.actuate.health.Health)20 Metadata (com.datastax.oss.driver.api.core.metadata.Metadata)19 ArrayList (java.util.ArrayList)14 NodeState (com.datastax.oss.driver.api.core.metadata.NodeState)12 DriverTimeoutException (com.datastax.oss.driver.api.core.DriverTimeoutException)11 List (java.util.List)11 UUID (java.util.UUID)11 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)11 Version (com.datastax.oss.driver.api.core.Version)10 Collections (java.util.Collections)10 HashMap (java.util.HashMap)10 Map (java.util.Map)10 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)10 BDDMockito.given (org.mockito.BDDMockito.given)10 Mockito.mock (org.mockito.Mockito.mock)10 Status (org.springframework.boot.actuate.health.Status)10 Mono (reactor.core.publisher.Mono)10