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