Search in sources :

Example 1 with DefaultNode

use of com.datastax.oss.driver.internal.core.metadata.DefaultNode in project java-driver by datastax.

the class MetricsITBase method should_evict_down_node_metrics_when_timeout_fires.

@Test
public void should_evict_down_node_metrics_when_timeout_fires() throws Exception {
    // given
    Duration expireAfter = Duration.ofSeconds(1);
    DriverConfigLoader loader = allMetricsEnabled().withDuration(DefaultDriverOption.METRICS_NODE_EXPIRE_AFTER, expireAfter).build();
    AbstractMetricUpdater.MIN_EXPIRE_AFTER = expireAfter;
    try (CqlSession session = CqlSession.builder().addContactEndPoints(simulacron().getContactPoints()).withConfigLoader(loader).withMetricRegistry(newMetricRegistry()).build()) {
        queryAllNodes(session);
        DefaultNode node1 = findNode(session, 0);
        DefaultNode node2 = findNode(session, 1);
        DefaultNode node3 = findNode(session, 2);
        EventBus eventBus = ((InternalDriverContext) session.getContext()).getEventBus();
        // trigger node1 UP -> DOWN
        eventBus.fire(NodeStateEvent.changed(NodeState.UP, NodeState.DOWN, node1));
        Thread.sleep(expireAfter.toMillis());
        // then node-level metrics should be evicted from node1, but
        // node2 and node3 metrics should not have been evicted
        await().untilAsserted(() -> assertNodeMetricsEvicted(session, node1));
        assertNodeMetricsNotEvicted(session, node2);
        assertNodeMetricsNotEvicted(session, node3);
    } finally {
        AbstractMetricUpdater.MIN_EXPIRE_AFTER = Duration.ofMinutes(5);
    }
}
Also used : DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) Duration(java.time.Duration) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) EventBus(com.datastax.oss.driver.internal.core.context.EventBus) CqlSession(com.datastax.oss.driver.api.core.CqlSession) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) Test(org.junit.Test)

Example 2 with DefaultNode

use of com.datastax.oss.driver.internal.core.metadata.DefaultNode in project java-driver by datastax.

the class MetricsITBase method should_not_evict_down_node_metrics_when_node_is_back_up_before_timeout.

@Test
public void should_not_evict_down_node_metrics_when_node_is_back_up_before_timeout() throws Exception {
    // given
    Duration expireAfter = Duration.ofSeconds(2);
    DriverConfigLoader loader = allMetricsEnabled().withDuration(DefaultDriverOption.METRICS_NODE_EXPIRE_AFTER, expireAfter).build();
    AbstractMetricUpdater.MIN_EXPIRE_AFTER = expireAfter;
    try (CqlSession session = CqlSession.builder().addContactEndPoints(simulacron().getContactPoints()).withConfigLoader(loader).withMetricRegistry(newMetricRegistry()).build()) {
        queryAllNodes(session);
        DefaultNode node1 = findNode(session, 0);
        DefaultNode node2 = findNode(session, 1);
        DefaultNode node3 = findNode(session, 2);
        EventBus eventBus = ((InternalDriverContext) session.getContext()).getEventBus();
        // trigger nodes UP -> DOWN
        eventBus.fire(NodeStateEvent.changed(NodeState.UP, NodeState.DOWN, node1));
        eventBus.fire(NodeStateEvent.changed(NodeState.UP, NodeState.FORCED_DOWN, node2));
        eventBus.fire(NodeStateEvent.removed(node3));
        Thread.sleep(500);
        // trigger nodes DOWN -> UP, should cancel the timeouts
        eventBus.fire(NodeStateEvent.changed(NodeState.DOWN, NodeState.UP, node1));
        eventBus.fire(NodeStateEvent.changed(NodeState.FORCED_DOWN, NodeState.UP, node2));
        eventBus.fire(NodeStateEvent.added(node3));
        Thread.sleep(expireAfter.toMillis());
        // then no node-level metrics should be evicted
        assertNodeMetricsNotEvicted(session, node1);
        assertNodeMetricsNotEvicted(session, node2);
        assertNodeMetricsNotEvicted(session, node3);
    } finally {
        AbstractMetricUpdater.MIN_EXPIRE_AFTER = Duration.ofMinutes(5);
    }
}
Also used : DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) Duration(java.time.Duration) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) EventBus(com.datastax.oss.driver.internal.core.context.EventBus) CqlSession(com.datastax.oss.driver.api.core.CqlSession) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) Test(org.junit.Test)

Example 3 with DefaultNode

use of com.datastax.oss.driver.internal.core.metadata.DefaultNode in project java-driver by datastax.

the class DefaultSessionPoolsTest method mockLocalNode.

private static DefaultNode mockLocalNode(int i) {
    DefaultNode node = mock(DefaultNode.class);
    when(node.getHostId()).thenReturn(UUID.randomUUID());
    DefaultEndPoint endPoint = TestNodeFactory.newEndPoint(i);
    when(node.getEndPoint()).thenReturn(endPoint);
    when(node.getBroadcastRpcAddress()).thenReturn(Optional.of(endPoint.resolve()));
    when(node.getDistance()).thenReturn(NodeDistance.LOCAL);
    when(node.toString()).thenReturn("node" + i);
    return node;
}
Also used : DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) DefaultEndPoint(com.datastax.oss.driver.internal.core.metadata.DefaultEndPoint)

Example 4 with DefaultNode

use of com.datastax.oss.driver.internal.core.metadata.DefaultNode in project java-driver by datastax.

the class InferringLocalDcHelper method discoverLocalDc.

/**
 * @return The local datacenter; always present.
 */
@NonNull
@Override
public Optional<String> discoverLocalDc(@NonNull Map<UUID, Node> nodes) {
    Optional<String> optionalLocalDc = super.discoverLocalDc(nodes);
    if (optionalLocalDc.isPresent()) {
        return optionalLocalDc;
    }
    Set<String> datacenters = new HashSet<>();
    Set<DefaultNode> contactPoints = context.getMetadataManager().getContactPoints();
    for (Node node : contactPoints) {
        String datacenter = node.getDatacenter();
        if (datacenter != null) {
            datacenters.add(datacenter);
        }
    }
    if (datacenters.size() == 1) {
        String localDc = datacenters.iterator().next();
        LOG.info("[{}] Inferred local DC from contact points: {}", logPrefix, localDc);
        return Optional.of(localDc);
    }
    if (datacenters.isEmpty()) {
        throw new IllegalStateException("The local DC could not be inferred from contact points, please set it explicitly (see " + DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER.getPath() + " in the config, or set it programmatically with SessionBuilder.withLocalDatacenter)");
    }
    throw new IllegalStateException(String.format("No local DC was provided, but the contact points are from different DCs: %s; " + "please set the local DC explicitly (see " + DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER.getPath() + " in the config, or set it programmatically with SessionBuilder.withLocalDatacenter)", formatNodesAndDcs(contactPoints)));
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) HashSet(java.util.HashSet) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 5 with DefaultNode

use of com.datastax.oss.driver.internal.core.metadata.DefaultNode in project java-driver by datastax.

the class MandatoryLocalDcHelper method discoverLocalDc.

/**
 * @return The local datacenter; always present.
 */
@NonNull
@Override
public Optional<String> discoverLocalDc(@NonNull Map<UUID, Node> nodes) {
    Optional<String> optionalLocalDc = super.discoverLocalDc(nodes);
    if (optionalLocalDc.isPresent()) {
        return optionalLocalDc;
    }
    Set<DefaultNode> contactPoints = context.getMetadataManager().getContactPoints();
    if (context.getMetadataManager().wasImplicitContactPoint()) {
        // We only allow automatic inference of the local DC in this specific case
        assert contactPoints.size() == 1;
        Node contactPoint = contactPoints.iterator().next();
        String localDc = contactPoint.getDatacenter();
        if (localDc != null) {
            LOG.debug("[{}] Local DC set from implicit contact point {}: {}", logPrefix, contactPoint, localDc);
            return Optional.of(localDc);
        } else {
            throw new IllegalStateException("The local DC could not be inferred from implicit contact point, please set it explicitly (see " + DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER.getPath() + " in the config, or set it programmatically with SessionBuilder.withLocalDatacenter)");
        }
    } else {
        throw new IllegalStateException("Since you provided explicit contact points, the local DC must be explicitly set (see " + DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER.getPath() + " in the config, or set it programmatically with SessionBuilder.withLocalDatacenter). " + "Current contact points are: " + formatNodesAndDcs(contactPoints) + ". Current DCs in this cluster are: " + formatDcs(nodes.values()));
    }
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)9 Node (com.datastax.oss.driver.api.core.metadata.Node)4 CqlSession (com.datastax.oss.driver.api.core.CqlSession)3 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)3 Test (org.junit.Test)3 EventBus (com.datastax.oss.driver.internal.core.context.EventBus)2 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)2 NonNull (edu.umd.cs.findbugs.annotations.NonNull)2 Duration (java.time.Duration)2 InsightMetadata (com.datastax.dse.driver.internal.core.insights.schema.InsightMetadata)1 SessionStateForNode (com.datastax.dse.driver.internal.core.insights.schema.SessionStateForNode)1 DriverConfig (com.datastax.oss.driver.api.core.config.DriverConfig)1 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)1 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)1 Metadata (com.datastax.oss.driver.api.core.metadata.Metadata)1 NodeStateListener (com.datastax.oss.driver.api.core.metadata.NodeStateListener)1 Token (com.datastax.oss.driver.api.core.metadata.token.Token)1 DriverChannel (com.datastax.oss.driver.internal.core.channel.DriverChannel)1 DefaultDriverContext (com.datastax.oss.driver.internal.core.context.DefaultDriverContext)1 ControlConnection (com.datastax.oss.driver.internal.core.control.ControlConnection)1