Search in sources :

Example 1 with UnexpectedResponseException

use of com.datastax.oss.driver.internal.core.adminrequest.UnexpectedResponseException 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;
    });
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) Error(com.datastax.oss.protocol.internal.response.Error) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) AdminResult(com.datastax.oss.driver.internal.core.adminrequest.AdminResult) CompletableFuture(java.util.concurrent.CompletableFuture) UnexpectedResponseException(com.datastax.oss.driver.internal.core.adminrequest.UnexpectedResponseException) AdminRow(com.datastax.oss.driver.internal.core.adminrequest.AdminRow)

Aggregations

EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)1 AdminResult (com.datastax.oss.driver.internal.core.adminrequest.AdminResult)1 AdminRow (com.datastax.oss.driver.internal.core.adminrequest.AdminRow)1 UnexpectedResponseException (com.datastax.oss.driver.internal.core.adminrequest.UnexpectedResponseException)1 DriverChannel (com.datastax.oss.driver.internal.core.channel.DriverChannel)1 Error (com.datastax.oss.protocol.internal.response.Error)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 CompletableFuture (java.util.concurrent.CompletableFuture)1