Search in sources :

Example 11 with InternalDriverContext

use of com.datastax.oss.driver.internal.core.context.InternalDriverContext in project java-driver by datastax.

the class MicroProfileMetricsIT method assertNodeMetricsEvicted.

@Override
protected void assertNodeMetricsEvicted(CqlSession session, Node node) {
    InternalDriverContext context = (InternalDriverContext) session.getContext();
    MetricRegistry registry = (MetricRegistry) context.getMetricRegistry();
    assertThat(registry).isNotNull();
    for (MetricID id : nodeMetricIds(context, node)) {
        assertThat(registry.getMetrics()).doesNotContainKey(id);
    }
}
Also used : MetricID(org.eclipse.microprofile.metrics.MetricID) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext)

Example 12 with InternalDriverContext

use of com.datastax.oss.driver.internal.core.context.InternalDriverContext in project java-driver by datastax.

the class MicroProfileMetricsIT method assertNodeMetricsNotEvicted.

@Override
protected void assertNodeMetricsNotEvicted(CqlSession session, Node node) {
    InternalDriverContext context = (InternalDriverContext) session.getContext();
    MetricRegistry registry = (MetricRegistry) context.getMetricRegistry();
    assertThat(registry).isNotNull();
    for (MetricID id : nodeMetricIds(context, node)) {
        assertThat(registry.getMetrics()).containsKey(id);
    }
}
Also used : MetricID(org.eclipse.microprofile.metrics.MetricID) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext)

Example 13 with InternalDriverContext

use of com.datastax.oss.driver.internal.core.context.InternalDriverContext in project java-driver by datastax.

the class FullNodeListRefresh method compute.

@Override
public Result compute(DefaultMetadata oldMetadata, boolean tokenMapEnabled, InternalDriverContext context) {
    String logPrefix = context.getSessionName();
    TokenFactoryRegistry tokenFactoryRegistry = context.getTokenFactoryRegistry();
    Map<UUID, Node> oldNodes = oldMetadata.getNodes();
    Map<UUID, Node> added = new HashMap<>();
    Set<UUID> seen = new HashSet<>();
    TokenFactory tokenFactory = oldMetadata.getTokenMap().map(m -> ((DefaultTokenMap) m).getTokenFactory()).orElse(null);
    boolean tokensChanged = false;
    for (NodeInfo nodeInfo : nodeInfos) {
        UUID id = nodeInfo.getHostId();
        if (seen.contains(id)) {
            LOG.warn("[{}] Found duplicate entries with host_id {} in system.peers, " + "keeping only the first one", logPrefix, id);
        } else {
            seen.add(id);
            DefaultNode node = (DefaultNode) oldNodes.get(id);
            if (node == null) {
                node = new DefaultNode(nodeInfo.getEndPoint(), context);
                LOG.debug("[{}] Adding new node {}", logPrefix, node);
                added.put(id, node);
            }
            if (tokenFactory == null && nodeInfo.getPartitioner() != null) {
                tokenFactory = tokenFactoryRegistry.tokenFactoryFor(nodeInfo.getPartitioner());
            }
            tokensChanged |= copyInfos(nodeInfo, node, context);
        }
    }
    Set<UUID> removed = Sets.difference(oldNodes.keySet(), seen);
    if (added.isEmpty() && removed.isEmpty()) {
        // The list didn't change
        if (!oldMetadata.getTokenMap().isPresent() && tokenFactory != null) {
            // token map rebuild:
            return new Result(oldMetadata.withNodes(oldMetadata.getNodes(), tokenMapEnabled, true, tokenFactory, context));
        } else {
            // No need to create a new metadata instance
            return new Result(oldMetadata);
        }
    } else {
        ImmutableMap.Builder<UUID, Node> newNodesBuilder = ImmutableMap.builder();
        ImmutableList.Builder<Object> eventsBuilder = ImmutableList.builder();
        newNodesBuilder.putAll(added);
        for (Map.Entry<UUID, Node> entry : oldNodes.entrySet()) {
            if (!removed.contains(entry.getKey())) {
                newNodesBuilder.put(entry.getKey(), entry.getValue());
            }
        }
        for (Node node : added.values()) {
            eventsBuilder.add(NodeStateEvent.added((DefaultNode) node));
        }
        for (UUID id : removed) {
            Node node = oldNodes.get(id);
            eventsBuilder.add(NodeStateEvent.removed((DefaultNode) node));
        }
        return new Result(oldMetadata.withNodes(newNodesBuilder.build(), tokenMapEnabled, tokensChanged, tokenFactory, context), eventsBuilder.build());
    }
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) Sets(com.datastax.oss.driver.shaded.guava.common.collect.Sets) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) TokenFactory(com.datastax.oss.driver.internal.core.metadata.token.TokenFactory) Set(java.util.Set) ThreadSafe(net.jcip.annotations.ThreadSafe) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) HashMap(java.util.HashMap) UUID(java.util.UUID) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) HashSet(java.util.HashSet) TokenFactoryRegistry(com.datastax.oss.driver.internal.core.metadata.token.TokenFactoryRegistry) Map(java.util.Map) VisibleForTesting(com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting) DefaultTokenMap(com.datastax.oss.driver.internal.core.metadata.token.DefaultTokenMap) DefaultTokenMap(com.datastax.oss.driver.internal.core.metadata.token.DefaultTokenMap) HashMap(java.util.HashMap) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) Node(com.datastax.oss.driver.api.core.metadata.Node) TokenFactoryRegistry(com.datastax.oss.driver.internal.core.metadata.token.TokenFactoryRegistry) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) TokenFactory(com.datastax.oss.driver.internal.core.metadata.token.TokenFactory) UUID(java.util.UUID) HashMap(java.util.HashMap) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) Map(java.util.Map) DefaultTokenMap(com.datastax.oss.driver.internal.core.metadata.token.DefaultTokenMap) HashSet(java.util.HashSet)

Example 14 with InternalDriverContext

use of com.datastax.oss.driver.internal.core.context.InternalDriverContext in project java-driver by datastax.

the class DefaultLoadBalancingPolicyIT method should_hit_non_replicas_when_routing_information_present_but_all_replicas_down.

@Test
public void should_hit_non_replicas_when_routing_information_present_but_all_replicas_down() {
    CqlIdentifier keyspace = CqlIdentifier.fromCql("test");
    ByteBuffer routingKey = TypeCodecs.INT.encodePrimitive(1, ProtocolVersion.DEFAULT);
    TokenMap tokenMap = SESSION_RULE.session().getMetadata().getTokenMap().get();
    InternalDriverContext context = (InternalDriverContext) SESSION_RULE.session().getContext();
    Set<Node> localReplicas = new HashSet<>();
    for (Node replica : tokenMap.getReplicas(keyspace, routingKey)) {
        if (replica.getDatacenter().equals(LOCAL_DC)) {
            localReplicas.add(replica);
            context.getEventBus().fire(TopologyEvent.forceDown(replica.getBroadcastRpcAddress().get()));
            await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).untilAsserted(() -> assertThat(replica.getOpenConnections()).isZero());
        }
    }
    assertThat(localReplicas).hasSize(2);
    // TODO add statements with setKeyspace when that is supported
    List<Statement> statements = ImmutableList.of(SimpleStatement.newInstance("SELECT * FROM test.foo WHERE k = 1").setRoutingKeyspace(keyspace).setRoutingKey(routingKey), SimpleStatement.newInstance("SELECT * FROM test.foo WHERE k = 1").setRoutingKeyspace(keyspace).setRoutingToken(tokenMap.newToken(routingKey)));
    for (Statement statement : statements) {
        List<Node> coordinators = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            ResultSet rs = SESSION_RULE.session().execute(statement);
            Node coordinator = rs.getExecutionInfo().getCoordinator();
            coordinators.add(coordinator);
            assertThat(coordinator.getDatacenter()).isEqualTo(LOCAL_DC);
            assertThat(localReplicas).doesNotContain(coordinator);
        }
        // Should round-robin on the two non-replicas
        for (int i = 0; i < 2; i++) {
            assertThat(coordinators.get(i)).isEqualTo(coordinators.get(2 + i)).isEqualTo(coordinators.get(4 + i));
        }
    }
    for (Node replica : localReplicas) {
        context.getEventBus().fire(TopologyEvent.forceUp(replica.getBroadcastRpcAddress().get()));
        await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).untilAsserted(() -> assertThat(replica.getOpenConnections()).isPositive());
    }
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) Node(com.datastax.oss.driver.api.core.metadata.Node) ArrayList(java.util.ArrayList) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ByteBuffer(java.nio.ByteBuffer) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with InternalDriverContext

use of com.datastax.oss.driver.internal.core.context.InternalDriverContext in project java-driver by datastax.

the class BoundStatementCcmIT method supportsPerRequestKeyspace.

private boolean supportsPerRequestKeyspace(CqlSession session) {
    InternalDriverContext context = (InternalDriverContext) session.getContext();
    ProtocolVersionRegistry protocolVersionRegistry = context.getProtocolVersionRegistry();
    return protocolVersionRegistry.supports(context.getProtocolVersion(), DefaultProtocolFeature.PER_REQUEST_KEYSPACE);
}
Also used : InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) ProtocolVersionRegistry(com.datastax.oss.driver.internal.core.ProtocolVersionRegistry)

Aggregations

InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)45 Test (org.junit.Test)29 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)16 DriverConfig (com.datastax.oss.driver.api.core.config.DriverConfig)15 Node (com.datastax.oss.driver.api.core.metadata.Node)13 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)13 DefaultNodeMetric (com.datastax.oss.driver.api.core.metrics.DefaultNodeMetric)12 MetricIdGenerator (com.datastax.oss.driver.internal.core.metrics.MetricIdGenerator)9 LoggerTest (com.datastax.oss.driver.internal.core.util.LoggerTest)8 NodeMetric (com.datastax.oss.driver.api.core.metrics.NodeMetric)7 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)7 MetricRegistry (com.codahale.metrics.MetricRegistry)5 CqlSession (com.datastax.oss.driver.api.core.CqlSession)5 MetricId (com.datastax.oss.driver.internal.core.metrics.MetricId)5 ReactiveRow (com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow)4 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)4 ColumnDefinitions (com.datastax.oss.driver.api.core.cql.ColumnDefinitions)4 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)4 DefaultSessionMetric (com.datastax.oss.driver.api.core.metrics.DefaultSessionMetric)4 RequestHandlerTestHarness (com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness)4