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