use of com.datastax.oss.driver.api.core.metadata.EndPoint in project java-driver by datastax.
the class ContactPointsTest method should_warn_if_duplicate_in_configuration.
@Test
public void should_warn_if_duplicate_in_configuration() {
Set<EndPoint> endPoints = ContactPoints.merge(Collections.emptySet(), ImmutableList.of("127.0.0.1:9042", "127.0.0.1:9042"), true);
assertThat(endPoints).containsOnly(new DefaultEndPoint(new InetSocketAddress("127.0.0.1", 9042)));
assertLog(Level.WARN, "Duplicate contact point /127.0.0.1:9042");
}
use of com.datastax.oss.driver.api.core.metadata.EndPoint in project java-driver by datastax.
the class ContactPointsTest method should_parse_ipv4_address_and_port_in_configuration.
@Test
public void should_parse_ipv4_address_and_port_in_configuration() {
Set<EndPoint> endPoints = ContactPoints.merge(Collections.emptySet(), ImmutableList.of("127.0.0.1:9042"), true);
assertThat(endPoints).containsExactly(new DefaultEndPoint(new InetSocketAddress("127.0.0.1", 9042)));
}
use of com.datastax.oss.driver.api.core.metadata.EndPoint in project java-driver by datastax.
the class DefaultTopologyMonitor method nodeInfoBuilder.
/**
* Creates a {@link DefaultNodeInfo.Builder} instance from the given row.
*
* @param broadcastRpcAddress this is a parameter only because we already have it when we come
* from {@link #findInPeers(AdminResult, InetSocketAddress, EndPoint)}. Callers that don't
* already have it can use {@link #getBroadcastRpcAddress}. For the control host, this can be
* null; if this node is a peer however, this cannot be null, since we use that address to
* create the node's endpoint. Callers can use {@link #isPeerValid(AdminRow)} to check that
* before calling this method.
* @param localEndPoint the control node endpoint that was used to query the node's system tables.
* This is a parameter because it would be racy to call {@code
* controlConnection.channel().getEndPoint()} from within this method, as the control
* connection may have changed its channel since. So this parameter must be provided by the
* caller.
*/
@NonNull
protected DefaultNodeInfo.Builder nodeInfoBuilder(@NonNull AdminRow row, @Nullable InetSocketAddress broadcastRpcAddress, @NonNull EndPoint localEndPoint) {
EndPoint endPoint = buildNodeEndPoint(row, broadcastRpcAddress, localEndPoint);
// in system.local
InetAddress broadcastInetAddress = row.getInetAddress("broadcast_address");
if (broadcastInetAddress == null) {
// in system.peers or system.peers_v2
broadcastInetAddress = row.getInetAddress("peer");
}
Integer broadcastPort = 0;
if (row.contains("broadcast_port")) {
// system.local for Cassandra >= 4.0
broadcastPort = row.getInteger("broadcast_port");
} else if (row.contains("peer_port")) {
// system.peers_v2
broadcastPort = row.getInteger("peer_port");
}
InetSocketAddress broadcastAddress = null;
if (broadcastInetAddress != null && broadcastPort != null) {
broadcastAddress = new InetSocketAddress(broadcastInetAddress, broadcastPort);
}
// in system.local only, and only for Cassandra versions >= 2.0.17, 2.1.8, 2.2.0 rc2;
// not present in system.peers nor system.peers_v2
InetAddress listenInetAddress = row.getInetAddress("listen_address");
// in system.local only, and only for Cassandra >= 4.0
Integer listenPort = 0;
if (row.contains("listen_port")) {
listenPort = row.getInteger("listen_port");
}
InetSocketAddress listenAddress = null;
if (listenInetAddress != null && listenPort != null) {
listenAddress = new InetSocketAddress(listenInetAddress, listenPort);
}
DefaultNodeInfo.Builder builder = DefaultNodeInfo.builder().withEndPoint(endPoint).withBroadcastRpcAddress(broadcastRpcAddress).withBroadcastAddress(broadcastAddress).withListenAddress(listenAddress).withDatacenter(row.getString("data_center")).withRack(row.getString("rack")).withCassandraVersion(row.getString("release_version")).withTokens(row.getSetOfString("tokens")).withPartitioner(row.getString("partitioner")).withHostId(Objects.requireNonNull(row.getUuid("host_id"))).withSchemaVersion(row.getUuid("schema_version"));
// Handle DSE-specific columns, if present
String rawVersion = row.getString("dse_version");
if (rawVersion != null) {
builder.withExtra(DseNodeProperties.DSE_VERSION, Version.parse(rawVersion));
}
ImmutableSet.Builder<String> workloadsBuilder = ImmutableSet.builder();
// DSE 5.0
Boolean legacyGraph = row.getBoolean("graph");
if (legacyGraph != null && legacyGraph) {
workloadsBuilder.add("Graph");
}
// DSE 5.0 (other than graph)
String legacyWorkload = row.getString("workload");
if (legacyWorkload != null) {
workloadsBuilder.add(legacyWorkload);
}
// DSE 5.1+
Set<String> modernWorkloads = row.getSetOfString("workloads");
if (modernWorkloads != null) {
workloadsBuilder.addAll(modernWorkloads);
}
ImmutableSet<String> workloads = workloadsBuilder.build();
if (!workloads.isEmpty()) {
builder.withExtra(DseNodeProperties.DSE_WORKLOADS, workloads);
}
// Note: withExtra discards null values
builder.withExtra(DseNodeProperties.SERVER_ID, row.getString("server_id")).withExtra(DseNodeProperties.NATIVE_TRANSPORT_PORT, row.getInteger("native_transport_port")).withExtra(DseNodeProperties.NATIVE_TRANSPORT_PORT_SSL, row.getInteger("native_transport_port_ssl")).withExtra(DseNodeProperties.STORAGE_PORT, row.getInteger("storage_port")).withExtra(DseNodeProperties.STORAGE_PORT_SSL, row.getInteger("storage_port_ssl")).withExtra(DseNodeProperties.JMX_PORT, row.getInteger("jmx_port"));
return builder;
}
use of com.datastax.oss.driver.api.core.metadata.EndPoint in project java-driver by datastax.
the class DefaultTopologyMonitor method refreshNodeList.
@Override
public CompletionStage<Iterable<NodeInfo>> refreshNodeList() {
if (closeFuture.isDone()) {
return CompletableFutures.failedFuture(new IllegalStateException("closed"));
}
LOG.debug("[{}] Refreshing node list", logPrefix);
DriverChannel channel = controlConnection.channel();
EndPoint localEndPoint = channel.getEndPoint();
savePort(channel);
CompletionStage<AdminResult> localQuery = query(channel, "SELECT * FROM system.local");
CompletionStage<AdminResult> peersV2Query = query(channel, "SELECT * FROM system.peers_v2");
CompletableFuture<AdminResult> peersQuery = new CompletableFuture<>();
peersV2Query.whenComplete((r, t) -> {
if (t != null) {
// If system.peers_v2 does not exist, downgrade to system.peers
if (t instanceof UnexpectedResponseException && ((UnexpectedResponseException) t).message instanceof Error) {
Error error = (Error) ((UnexpectedResponseException) t).message;
if (error.code == ProtocolConstants.ErrorCode.INVALID || // 6.0.2 with search enabled)
(error.code == ProtocolConstants.ErrorCode.SERVER_ERROR && error.message.contains("Unknown keyspace/cf pair (system.peers_v2)"))) {
// We should not attempt this query in the future.
this.isSchemaV2 = false;
CompletableFutures.completeFrom(query(channel, "SELECT * FROM system.peers"), peersQuery);
return;
}
}
peersQuery.completeExceptionally(t);
} else {
peersQuery.complete(r);
}
});
return localQuery.thenCombine(peersQuery, (controlNodeResult, peersResult) -> {
List<NodeInfo> nodeInfos = new ArrayList<>();
AdminRow localRow = controlNodeResult.iterator().next();
InetSocketAddress localBroadcastRpcAddress = getBroadcastRpcAddress(localRow, localEndPoint);
nodeInfos.add(nodeInfoBuilder(localRow, localBroadcastRpcAddress, localEndPoint).build());
for (AdminRow peerRow : peersResult) {
if (isPeerValid(peerRow)) {
InetSocketAddress peerBroadcastRpcAddress = getBroadcastRpcAddress(peerRow, localEndPoint);
if (peerBroadcastRpcAddress != null) {
NodeInfo nodeInfo = nodeInfoBuilder(peerRow, peerBroadcastRpcAddress, localEndPoint).build();
nodeInfos.add(nodeInfo);
}
}
}
return nodeInfos;
});
}
use of com.datastax.oss.driver.api.core.metadata.EndPoint in project java-driver by datastax.
the class InsightsClientTest method mockConnectionPools.
private void mockConnectionPools(DefaultDriverContext driverContext) {
Node node1 = mock(Node.class);
EndPoint endPoint1 = mock(EndPoint.class);
when(endPoint1.resolve()).thenReturn(new InetSocketAddress("127.0.0.1", 10));
when(node1.getEndPoint()).thenReturn(endPoint1);
when(node1.getOpenConnections()).thenReturn(1);
ChannelPool channelPool1 = mock(ChannelPool.class);
when(channelPool1.getInFlight()).thenReturn(10);
Node node2 = mock(Node.class);
EndPoint endPoint2 = mock(EndPoint.class);
when(endPoint2.resolve()).thenReturn(new InetSocketAddress("127.0.0.1", 20));
when(node2.getEndPoint()).thenReturn(endPoint2);
when(node2.getOpenConnections()).thenReturn(2);
ChannelPool channelPool2 = mock(ChannelPool.class);
when(channelPool2.getInFlight()).thenReturn(20);
Map<Node, ChannelPool> channelPools = ImmutableMap.of(node1, channelPool1, node2, channelPool2);
PoolManager poolManager = mock(PoolManager.class);
when(poolManager.getPools()).thenReturn(channelPools);
when(driverContext.getPoolManager()).thenReturn(poolManager);
}
Aggregations