Search in sources :

Example 1 with StargateClientState

use of io.stargate.db.dse.impl.StargateClientState in project stargate by stargate.

the class ProxyProtocolQueryInterceptor method interceptQuery.

@Override
public Single<ResultMessage> interceptQuery(CQLStatement statement, QueryState state, QueryOptions options, Map<String, ByteBuffer> customPayload, long queryStartNanoTime) {
    // see DseConnection
    assert state.getClientState() instanceof StargateClientState;
    StargateClientState clientState = (StargateClientState) state.getClientState();
    if (!isSystemLocalOrPeers(statement) || !clientState.proxyDestinationAddress().isPresent()) {
        return wrapped.map(i -> i.interceptQuery(statement, state, options, customPayload, queryStartNanoTime)).orElse(null);
    }
    InetSocketAddress destinationAddress = clientState.proxyDestinationAddress().get();
    boolean isPrivateDestination = destinationAddress.getAddress().isSiteLocalAddress();
    // If the destination is private, we want to use the "source" address of the PROXY header.
    // We stored that in clientState.getRemoteAddress().
    InetAddress systemLocalAddress = isPrivateDestination ? clientState.getRemoteAddress().getAddress() : destinationAddress.getAddress();
    SelectStatement selectStatement = (SelectStatement) statement;
    List<List<ByteBuffer>> rows;
    String tableName = selectStatement.table();
    boolean isPrivateLocal = systemLocalAddress.isSiteLocalAddress();
    Set<InetAddress> peers = isPrivateLocal ? privatePeers : publicPeers;
    if (tableName.equals(PeersSystemView.NAME)) {
        rows = peers.isEmpty() ? Collections.emptyList() : Lists.newArrayListWithCapacity(peers.size() - 1);
        for (InetAddress peer : peers) {
            if (!peer.equals(systemLocalAddress)) {
                rows.add(buildRow(selectStatement.getResultMetadata(), peer, peers));
            }
        }
    } else {
        assert tableName.equals(LocalNodeSystemView.NAME);
        rows = Collections.singletonList(buildRow(selectStatement.getResultMetadata(), systemLocalAddress, peers));
    }
    ResultSet resultSet = new ResultSet(selectStatement.getResultMetadata(), rows);
    return Single.just(new ResultMessage.Rows(resultSet));
}
Also used : InetAddressType(org.apache.cassandra.db.marshal.InetAddressType) LoadingCache(com.datastax.oss.driver.shaded.guava.common.cache.LoadingCache) Arrays(java.util.Arrays) Strings(com.datastax.oss.driver.shaded.guava.common.base.Strings) LoggerFactory(org.slf4j.LoggerFactory) Security(java.security.Security) CQLStatement(org.apache.cassandra.cql3.CQLStatement) ByteBuffer(java.nio.ByteBuffer) InetAddress(java.net.InetAddress) Lists(com.datastax.oss.driver.shaded.guava.common.collect.Lists) SelectStatement(org.apache.cassandra.cql3.statements.SelectStatement) Map(java.util.Map) VisibleForTesting(com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting) StargateClientState(io.stargate.db.dse.impl.StargateClientState) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) StargateSystemKeyspace.isSystemLocalOrPeers(io.stargate.db.dse.impl.StargateSystemKeyspace.isSystemLocalOrPeers) StargateSystemKeyspace(io.stargate.db.dse.impl.StargateSystemKeyspace) BootstrapState(com.datastax.bdp.db.nodes.BootstrapState) QueryState(org.apache.cassandra.service.QueryState) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ServerError(org.apache.cassandra.stargate.transport.ServerError) ProductVersion(com.datastax.bdp.db.util.ProductVersion) PeersSystemView(com.datastax.bdp.db.nodes.virtual.PeersSystemView) SetType(org.apache.cassandra.db.marshal.SetType) ResultSet(org.apache.cassandra.cql3.ResultSet) QueryProcessor(org.apache.cassandra.cql3.QueryProcessor) Single(io.reactivex.Single) Int32Type(org.apache.cassandra.db.marshal.Int32Type) UTF8Type(org.apache.cassandra.db.marshal.UTF8Type) ResultMetadata(org.apache.cassandra.cql3.ResultSet.ResultMetadata) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) LocalNodeSystemView(com.datastax.bdp.db.nodes.virtual.LocalNodeSystemView) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) Sets(com.datastax.oss.driver.shaded.guava.common.collect.Sets) Logger(org.slf4j.Logger) UnknownHostException(java.net.UnknownHostException) TimeUnit(java.util.concurrent.TimeUnit) UUIDType(org.apache.cassandra.db.marshal.UUIDType) EventListener(io.stargate.db.EventListener) Collections(java.util.Collections) QueryOptions(org.apache.cassandra.cql3.QueryOptions) InetSocketAddress(java.net.InetSocketAddress) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) SelectStatement(org.apache.cassandra.cql3.statements.SelectStatement) ResultSet(org.apache.cassandra.cql3.ResultSet) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) InetAddress(java.net.InetAddress) StargateClientState(io.stargate.db.dse.impl.StargateClientState)

Example 2 with StargateClientState

use of io.stargate.db.dse.impl.StargateClientState in project stargate by stargate.

the class DefaultQueryInterceptor method interceptSystemLocalOrPeers.

private static Single<ResultMessage> interceptSystemLocalOrPeers(CQLStatement statement, QueryState state, QueryOptions options, long queryStartNanoTime) {
    SelectStatement selectStatement = ((SelectStatement) statement);
    // Re-parse so that we can intercept and replace the keyspace.
    SelectStatement.Raw rawStatement = (SelectStatement.Raw) QueryProcessor.parseStatement(selectStatement.queryString);
    rawStatement.setKeyspace(SYSTEM_KEYSPACE_NAME);
    SelectStatement interceptStatement = rawStatement.prepare(state.getClientState());
    Single<ResultMessage.Rows> rows = interceptStatement.execute(state, options, queryStartNanoTime);
    return rows.map(r -> {
        // see DseConnection
        assert state.getClientState() instanceof StargateClientState;
        StargateClientState clientState = (StargateClientState) state.getClientState();
        clientState.boundPort().ifPresent(port -> replaceNativeTransportPort(r.result, port));
        return new ResultMessage.Rows(new ResultSet(selectStatement.getResultMetadata(), r.result.rows));
    });
}
Also used : SelectStatement(org.apache.cassandra.cql3.statements.SelectStatement) ResultSet(org.apache.cassandra.cql3.ResultSet) StargateClientState(io.stargate.db.dse.impl.StargateClientState)

Aggregations

StargateClientState (io.stargate.db.dse.impl.StargateClientState)2 ResultSet (org.apache.cassandra.cql3.ResultSet)2 SelectStatement (org.apache.cassandra.cql3.statements.SelectStatement)2 BootstrapState (com.datastax.bdp.db.nodes.BootstrapState)1 LocalNodeSystemView (com.datastax.bdp.db.nodes.virtual.LocalNodeSystemView)1 PeersSystemView (com.datastax.bdp.db.nodes.virtual.PeersSystemView)1 ProductVersion (com.datastax.bdp.db.util.ProductVersion)1 VisibleForTesting (com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting)1 Strings (com.datastax.oss.driver.shaded.guava.common.base.Strings)1 LoadingCache (com.datastax.oss.driver.shaded.guava.common.cache.LoadingCache)1 Lists (com.datastax.oss.driver.shaded.guava.common.collect.Lists)1 Sets (com.datastax.oss.driver.shaded.guava.common.collect.Sets)1 Single (io.reactivex.Single)1 EventListener (io.stargate.db.EventListener)1 StargateSystemKeyspace (io.stargate.db.dse.impl.StargateSystemKeyspace)1 StargateSystemKeyspace.isSystemLocalOrPeers (io.stargate.db.dse.impl.StargateSystemKeyspace.isSystemLocalOrPeers)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 UnknownHostException (java.net.UnknownHostException)1 ByteBuffer (java.nio.ByteBuffer)1