Search in sources :

Example 11 with EndPoint

use of com.datastax.oss.driver.api.core.metadata.EndPoint in project java-driver by datastax.

the class CloudConfigFactory method getEndPoints.

@NonNull
protected List<EndPoint> getEndPoints(@NonNull JsonNode proxyMetadata, @NonNull InetSocketAddress sniProxyAddress) {
    JsonNode contactInfo = getContactInfo(proxyMetadata);
    if (contactInfo.has("contact_points")) {
        List<EndPoint> endPoints = new ArrayList<>();
        JsonNode hostIdsJson = contactInfo.get("contact_points");
        for (int i = 0; i < hostIdsJson.size(); i++) {
            endPoints.add(new SniEndPoint(sniProxyAddress, hostIdsJson.get(i).asText()));
        }
        return endPoints;
    } else {
        throw new IllegalStateException("Invalid proxy metadata: missing field contact_points");
    }
}
Also used : SniEndPoint(com.datastax.oss.driver.internal.core.metadata.SniEndPoint) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) SniEndPoint(com.datastax.oss.driver.internal.core.metadata.SniEndPoint) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) SniEndPoint(com.datastax.oss.driver.internal.core.metadata.SniEndPoint) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 12 with EndPoint

use of com.datastax.oss.driver.api.core.metadata.EndPoint in project java-driver by datastax.

the class DefaultTopologyMonitor method refreshNode.

@Override
public CompletionStage<Optional<NodeInfo>> refreshNode(Node node) {
    if (closeFuture.isDone()) {
        return CompletableFutures.failedFuture(new IllegalStateException("closed"));
    }
    LOG.debug("[{}] Refreshing info for {}", logPrefix, node);
    DriverChannel channel = controlConnection.channel();
    EndPoint localEndPoint = channel.getEndPoint();
    if (node.getEndPoint().equals(channel.getEndPoint())) {
        // refreshNode is called for nodes that just came up. If the control node just came up, it
        // means the control connection just reconnected, which means we did a full node refresh. So
        // we don't need to process this call.
        LOG.debug("[{}] Ignoring refresh of control node", logPrefix);
        return CompletableFuture.completedFuture(Optional.empty());
    } else if (node.getBroadcastAddress().isPresent()) {
        CompletionStage<AdminResult> query;
        if (isSchemaV2) {
            query = query(channel, "SELECT * FROM " + getPeerTableName() + " WHERE peer = :address and peer_port = :port", ImmutableMap.of("address", node.getBroadcastAddress().get().getAddress(), "port", node.getBroadcastAddress().get().getPort()));
        } else {
            query = query(channel, "SELECT * FROM " + getPeerTableName() + " WHERE peer = :address", ImmutableMap.of("address", node.getBroadcastAddress().get().getAddress()));
        }
        return query.thenApply(result -> firstPeerRowAsNodeInfo(result, localEndPoint));
    } else {
        return query(channel, "SELECT * FROM " + getPeerTableName()).thenApply(result -> findInPeers(result, node.getHostId(), localEndPoint));
    }
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) SocketAddress(java.net.SocketAddress) ImmutableSet(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet) LoggerFactory(org.slf4j.LoggerFactory) AdminRow(com.datastax.oss.driver.internal.core.adminrequest.AdminRow) ThreadSafe(net.jcip.annotations.ThreadSafe) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) CompletableFuture(java.util.concurrent.CompletableFuture) ControlConnection(com.datastax.oss.driver.internal.core.control.ControlConnection) AdminResult(com.datastax.oss.driver.internal.core.adminrequest.AdminResult) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Version(com.datastax.oss.driver.api.core.Version) UnexpectedResponseException(com.datastax.oss.driver.internal.core.adminrequest.UnexpectedResponseException) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) Error(com.datastax.oss.protocol.internal.response.Error) AdminRequestHandler(com.datastax.oss.driver.internal.core.adminrequest.AdminRequestHandler) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Duration(java.time.Duration) Map(java.util.Map) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) VisibleForTesting(com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting) CompletableFutures(com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) DseNodeProperties(com.datastax.dse.driver.api.core.metadata.DseNodeProperties) Set(java.util.Set) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) ProtocolConstants(com.datastax.oss.protocol.internal.ProtocolConstants) Objects(java.util.Objects) DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) Nullable(edu.umd.cs.findbugs.annotations.Nullable) Optional(java.util.Optional) Collections(java.util.Collections) DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) CompletionStage(java.util.concurrent.CompletionStage)

Example 13 with EndPoint

use of com.datastax.oss.driver.api.core.metadata.EndPoint in project java-driver by datastax.

the class DefaultTopologyMonitor method getNewNodeInfo.

@Override
public CompletionStage<Optional<NodeInfo>> getNewNodeInfo(InetSocketAddress broadcastRpcAddress) {
    if (closeFuture.isDone()) {
        return CompletableFutures.failedFuture(new IllegalStateException("closed"));
    }
    LOG.debug("[{}] Fetching info for new node {}", logPrefix, broadcastRpcAddress);
    DriverChannel channel = controlConnection.channel();
    EndPoint localEndPoint = channel.getEndPoint();
    return query(channel, "SELECT * FROM " + getPeerTableName()).thenApply(result -> findInPeers(result, broadcastRpcAddress, localEndPoint));
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint)

Example 14 with EndPoint

use of com.datastax.oss.driver.api.core.metadata.EndPoint in project java-driver by datastax.

the class InitialNodeListRefresh method compute.

@Override
public Result compute(DefaultMetadata oldMetadata, boolean tokenMapEnabled, InternalDriverContext context) {
    String logPrefix = context.getSessionName();
    TokenFactoryRegistry tokenFactoryRegistry = context.getTokenFactoryRegistry();
    // metadata is empty.
    assert oldMetadata == DefaultMetadata.EMPTY;
    TokenFactory tokenFactory = null;
    Map<UUID, DefaultNode> newNodes = new HashMap<>();
    for (NodeInfo nodeInfo : nodeInfos) {
        UUID hostId = nodeInfo.getHostId();
        if (newNodes.containsKey(hostId)) {
            LOG.warn("[{}] Found duplicate entries with host_id {} in system.peers, " + "keeping only the first one", logPrefix, hostId);
        } else {
            EndPoint endPoint = nodeInfo.getEndPoint();
            DefaultNode node = findIn(contactPoints, endPoint);
            if (node == null) {
                node = new DefaultNode(endPoint, context);
                LOG.debug("[{}] Adding new node {}", logPrefix, node);
            } else {
                LOG.debug("[{}] Copying contact point {}", logPrefix, node);
            }
            if (tokenMapEnabled && tokenFactory == null && nodeInfo.getPartitioner() != null) {
                tokenFactory = tokenFactoryRegistry.tokenFactoryFor(nodeInfo.getPartitioner());
            }
            copyInfos(nodeInfo, node, context);
            newNodes.put(hostId, node);
        }
    }
    ImmutableList.Builder<Object> eventsBuilder = ImmutableList.builder();
    for (DefaultNode newNode : newNodes.values()) {
        if (findIn(contactPoints, newNode.getEndPoint()) == null) {
            eventsBuilder.add(NodeStateEvent.added(newNode));
        }
    }
    for (DefaultNode contactPoint : contactPoints) {
        if (findIn(newNodes.values(), contactPoint.getEndPoint()) == null) {
            eventsBuilder.add(NodeStateEvent.removed(contactPoint));
        }
    }
    return new Result(oldMetadata.withNodes(ImmutableMap.copyOf(newNodes), tokenMapEnabled, true, tokenFactory, context), eventsBuilder.build());
}
Also used : HashMap(java.util.HashMap) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) TokenFactoryRegistry(com.datastax.oss.driver.internal.core.metadata.token.TokenFactoryRegistry) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) TokenFactory(com.datastax.oss.driver.internal.core.metadata.token.TokenFactory) UUID(java.util.UUID)

Example 15 with EndPoint

use of com.datastax.oss.driver.api.core.metadata.EndPoint in project java-driver by datastax.

the class MetadataManager method addContactPoints.

public void addContactPoints(Set<EndPoint> providedContactPoints) {
    // Convert the EndPoints to Nodes, but we can't put them into the Metadata yet, because we
    // don't know their host_id. So store them in a volatile field instead, they will get copied
    // during the first node refresh.
    ImmutableSet.Builder<DefaultNode> contactPointsBuilder = ImmutableSet.builder();
    if (providedContactPoints == null || providedContactPoints.isEmpty()) {
        LOG.info("[{}] No contact points provided, defaulting to {}", logPrefix, DEFAULT_CONTACT_POINT);
        this.wasImplicitContactPoint = true;
        contactPointsBuilder.add(new DefaultNode(DEFAULT_CONTACT_POINT, context));
    } else {
        for (EndPoint endPoint : providedContactPoints) {
            contactPointsBuilder.add(new DefaultNode(endPoint, context));
        }
    }
    this.contactPoints = contactPointsBuilder.build();
    LOG.debug("[{}] Adding initial contact points {}", logPrefix, contactPoints);
}
Also used : ImmutableSet(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint)

Aggregations

EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)24 InetSocketAddress (java.net.InetSocketAddress)14 DefaultEndPoint (com.datastax.oss.driver.internal.core.metadata.DefaultEndPoint)11 Test (org.junit.Test)11 Node (com.datastax.oss.driver.api.core.metadata.Node)7 NonNull (edu.umd.cs.findbugs.annotations.NonNull)5 DriverChannel (com.datastax.oss.driver.internal.core.channel.DriverChannel)4 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)4 CqlSession (com.datastax.oss.driver.api.core.CqlSession)3 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)3 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)3 Metadata (com.datastax.oss.driver.api.core.metadata.Metadata)3 ImmutableSet (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet)3 SessionStateForNode (com.datastax.dse.driver.internal.core.insights.schema.SessionStateForNode)2 NodeStateListener (com.datastax.oss.driver.api.core.metadata.NodeStateListener)2 AdminResult (com.datastax.oss.driver.internal.core.adminrequest.AdminResult)2 AdminRow (com.datastax.oss.driver.internal.core.adminrequest.AdminRow)2 UnexpectedResponseException (com.datastax.oss.driver.internal.core.adminrequest.UnexpectedResponseException)2 ControlConnection (com.datastax.oss.driver.internal.core.control.ControlConnection)2 SniEndPoint (com.datastax.oss.driver.internal.core.metadata.SniEndPoint)2