Search in sources :

Example 1 with ProtocolVersion

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

the class TokenITBase method should_be_consistent_with_range_queries.

/**
 * Validates that the token metadata is consistent with server-side range queries. That is,
 * querying the data in a range does return a PK that the driver thinks is in that range.
 *
 * @test_category metadata:token
 * @expected_result token ranges are exposed and usable.
 * @jira_ticket JAVA-312
 * @since 2.0.10, 2.1.5
 */
@Test
public void should_be_consistent_with_range_queries() {
    TokenMap tokenMap = getTokenMap();
    // Find the replica for a given partition key of ks1.foo.
    int key = 1;
    ProtocolVersion protocolVersion = session().getContext().getProtocolVersion();
    ByteBuffer serializedKey = TypeCodecs.INT.encodePrimitive(key, protocolVersion);
    assertThat(serializedKey).isNotNull();
    Set<Node> replicas = tokenMap.getReplicas(KS1, serializedKey);
    assertThat(replicas).hasSize(1);
    Node replica = replicas.iterator().next();
    // Iterate the cluster's token ranges. For each one, use a range query to get all the keys of
    // ks1.foo that are in this range.
    PreparedStatement rangeStatement = session().prepare("SELECT i FROM foo WHERE token(i) > ? and token(i) <= ?");
    TokenRange foundRange = null;
    for (TokenRange range : tokenMap.getTokenRanges()) {
        List<Row> rows = rangeQuery(rangeStatement, range);
        for (Row row : rows) {
            if (row.getInt("i") == key) {
                // We should find our initial key exactly once
                assertThat(foundRange).describedAs("found the same key in two ranges: " + foundRange + " and " + range).isNull();
                foundRange = range;
                // That range should be managed by the replica
                assertThat(tokenMap.getReplicas(KS1, range)).contains(replica);
            }
        }
    }
    assertThat(foundRange).isNotNull();
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange) Row(com.datastax.oss.driver.api.core.cql.Row) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with ProtocolVersion

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

the class TokenITBase method should_create_token_from_partition_key.

@Test
public void should_create_token_from_partition_key() {
    TokenMap tokenMap = getTokenMap();
    Row row = session().execute("SELECT token(i) FROM foo WHERE i = 1").one();
    assertThat(row).isNotNull();
    Token expected = row.getToken(0);
    ProtocolVersion protocolVersion = session().getContext().getProtocolVersion();
    assertThat(tokenMap.newToken(TypeCodecs.INT.encodePrimitive(1, protocolVersion))).isEqualTo(expected);
}
Also used : Token(com.datastax.oss.driver.api.core.metadata.token.Token) Row(com.datastax.oss.driver.api.core.cql.Row) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) Test(org.junit.Test)

Example 3 with ProtocolVersion

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

the class ChannelFactory method getProtocolVersion.

public ProtocolVersion getProtocolVersion() {
    ProtocolVersion result = this.protocolVersion;
    Preconditions.checkState(result != null, "Protocol version not known yet, this should only be called after init");
    return result;
}
Also used : ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion)

Example 4 with ProtocolVersion

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

the class ChannelFactory method connect.

private void connect(EndPoint endPoint, DriverChannelOptions options, NodeMetricUpdater nodeMetricUpdater, ProtocolVersion currentVersion, boolean isNegotiating, List<ProtocolVersion> attemptedVersions, CompletableFuture<DriverChannel> resultFuture) {
    NettyOptions nettyOptions = context.getNettyOptions();
    Bootstrap bootstrap = new Bootstrap().group(nettyOptions.ioEventLoopGroup()).channel(nettyOptions.channelClass()).option(ChannelOption.ALLOCATOR, nettyOptions.allocator()).handler(initializer(endPoint, currentVersion, options, nodeMetricUpdater, resultFuture));
    nettyOptions.afterBootstrapInitialized(bootstrap);
    ChannelFuture connectFuture = bootstrap.connect(endPoint.resolve());
    connectFuture.addListener(cf -> {
        if (connectFuture.isSuccess()) {
            Channel channel = connectFuture.channel();
            DriverChannel driverChannel = new DriverChannel(endPoint, channel, context.getWriteCoalescer(), currentVersion);
            // cluster name for future connections.
            if (isNegotiating) {
                ChannelFactory.this.protocolVersion = currentVersion;
            }
            if (ChannelFactory.this.clusterName == null) {
                ChannelFactory.this.clusterName = driverChannel.getClusterName();
            }
            Map<String, List<String>> supportedOptions = driverChannel.getOptions();
            if (ChannelFactory.this.productType == null && supportedOptions != null) {
                List<String> productTypes = supportedOptions.get("PRODUCT_TYPE");
                String productType = productTypes != null && !productTypes.isEmpty() ? productTypes.get(0) : UNKNOWN_PRODUCT_TYPE;
                ChannelFactory.this.productType = productType;
                DriverConfig driverConfig = context.getConfig();
                if (driverConfig instanceof TypesafeDriverConfig && productType.equals(DATASTAX_CLOUD_PRODUCT_TYPE)) {
                    ((TypesafeDriverConfig) driverConfig).overrideDefaults(ImmutableMap.of(DefaultDriverOption.REQUEST_CONSISTENCY, ConsistencyLevel.LOCAL_QUORUM.name()));
                }
            }
            resultFuture.complete(driverChannel);
        } else {
            Throwable error = connectFuture.cause();
            if (error instanceof UnsupportedProtocolVersionException && isNegotiating) {
                attemptedVersions.add(currentVersion);
                Optional<ProtocolVersion> downgraded = context.getProtocolVersionRegistry().downgrade(currentVersion);
                if (downgraded.isPresent()) {
                    LOG.debug("[{}] Failed to connect with protocol {}, retrying with {}", logPrefix, currentVersion, downgraded.get());
                    connect(endPoint, options, nodeMetricUpdater, downgraded.get(), true, attemptedVersions, resultFuture);
                } else {
                    resultFuture.completeExceptionally(UnsupportedProtocolVersionException.forNegotiation(endPoint, attemptedVersions));
                }
            } else {
                // Note: might be completed already if the failure happened in initializer(), this is
                // fine
                resultFuture.completeExceptionally(error);
            }
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) UnsupportedProtocolVersionException(com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) NettyOptions(com.datastax.oss.driver.internal.core.context.NettyOptions) TypesafeDriverConfig(com.datastax.oss.driver.internal.core.config.typesafe.TypesafeDriverConfig) Bootstrap(io.netty.bootstrap.Bootstrap) DriverConfig(com.datastax.oss.driver.api.core.config.DriverConfig) TypesafeDriverConfig(com.datastax.oss.driver.internal.core.config.typesafe.TypesafeDriverConfig) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 5 with ProtocolVersion

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

the class DseConversions method toContinuousPagingMessage.

public static Message toContinuousPagingMessage(Statement<?> statement, DriverExecutionProfile config, InternalDriverContext context) {
    ConsistencyLevelRegistry consistencyLevelRegistry = context.getConsistencyLevelRegistry();
    ConsistencyLevel consistency = statement.getConsistencyLevel();
    int consistencyCode = (consistency == null) ? consistencyLevelRegistry.nameToCode(config.getString(DefaultDriverOption.REQUEST_CONSISTENCY)) : consistency.getProtocolCode();
    int pageSize = config.getInt(DseDriverOption.CONTINUOUS_PAGING_PAGE_SIZE);
    boolean pageSizeInBytes = config.getBoolean(DseDriverOption.CONTINUOUS_PAGING_PAGE_SIZE_BYTES);
    int maxPages = config.getInt(DseDriverOption.CONTINUOUS_PAGING_MAX_PAGES);
    int maxPagesPerSecond = config.getInt(DseDriverOption.CONTINUOUS_PAGING_MAX_PAGES_PER_SECOND);
    int maxEnqueuedPages = config.getInt(DseDriverOption.CONTINUOUS_PAGING_MAX_ENQUEUED_PAGES);
    ContinuousPagingOptions options = new ContinuousPagingOptions(maxPages, maxPagesPerSecond, maxEnqueuedPages);
    ConsistencyLevel serialConsistency = statement.getSerialConsistencyLevel();
    int serialConsistencyCode = (serialConsistency == null) ? consistencyLevelRegistry.nameToCode(config.getString(DefaultDriverOption.REQUEST_SERIAL_CONSISTENCY)) : serialConsistency.getProtocolCode();
    long timestamp = statement.getQueryTimestamp();
    if (timestamp == Statement.NO_DEFAULT_TIMESTAMP) {
        timestamp = context.getTimestampGenerator().next();
    }
    CodecRegistry codecRegistry = context.getCodecRegistry();
    ProtocolVersion protocolVersion = context.getProtocolVersion();
    ProtocolVersionRegistry protocolVersionRegistry = context.getProtocolVersionRegistry();
    CqlIdentifier keyspace = statement.getKeyspace();
    if (statement instanceof SimpleStatement) {
        SimpleStatement simpleStatement = (SimpleStatement) statement;
        List<Object> positionalValues = simpleStatement.getPositionalValues();
        Map<CqlIdentifier, Object> namedValues = simpleStatement.getNamedValues();
        if (!positionalValues.isEmpty() && !namedValues.isEmpty()) {
            throw new IllegalArgumentException("Can't have both positional and named values in a statement.");
        }
        if (keyspace != null && !protocolVersionRegistry.supports(protocolVersion, DefaultProtocolFeature.PER_REQUEST_KEYSPACE)) {
            throw new IllegalArgumentException("Can't use per-request keyspace with protocol " + protocolVersion);
        }
        DseQueryOptions queryOptions = new DseQueryOptions(consistencyCode, Conversions.encode(positionalValues, codecRegistry, protocolVersion), Conversions.encode(namedValues, codecRegistry, protocolVersion), false, pageSize, statement.getPagingState(), serialConsistencyCode, timestamp, (keyspace == null) ? null : keyspace.asInternal(), pageSizeInBytes, options);
        return new Query(simpleStatement.getQuery(), queryOptions);
    } else if (statement instanceof BoundStatement) {
        BoundStatement boundStatement = (BoundStatement) statement;
        if (!protocolVersionRegistry.supports(protocolVersion, DefaultProtocolFeature.UNSET_BOUND_VALUES)) {
            Conversions.ensureAllSet(boundStatement);
        }
        boolean skipMetadata = boundStatement.getPreparedStatement().getResultSetDefinitions().size() > 0;
        DseQueryOptions queryOptions = new DseQueryOptions(consistencyCode, boundStatement.getValues(), Collections.emptyMap(), skipMetadata, pageSize, statement.getPagingState(), serialConsistencyCode, timestamp, null, pageSizeInBytes, options);
        PreparedStatement preparedStatement = boundStatement.getPreparedStatement();
        ByteBuffer id = preparedStatement.getId();
        ByteBuffer resultMetadataId = preparedStatement.getResultMetadataId();
        return new Execute(Bytes.getArray(id), (resultMetadataId == null) ? null : Bytes.getArray(resultMetadataId), queryOptions);
    } else {
        throw new IllegalArgumentException("Unsupported statement type: " + statement.getClass().getName());
    }
}
Also used : Query(com.datastax.oss.protocol.internal.request.Query) Execute(com.datastax.oss.protocol.internal.request.Execute) ContinuousPagingOptions(com.datastax.dse.protocol.internal.request.query.ContinuousPagingOptions) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) ProtocolVersionRegistry(com.datastax.oss.driver.internal.core.ProtocolVersionRegistry) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ByteBuffer(java.nio.ByteBuffer) ConsistencyLevel(com.datastax.oss.driver.api.core.ConsistencyLevel) ConsistencyLevelRegistry(com.datastax.oss.driver.internal.core.ConsistencyLevelRegistry) DseQueryOptions(com.datastax.dse.protocol.internal.request.query.DseQueryOptions) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Aggregations

ProtocolVersion (com.datastax.oss.driver.api.core.ProtocolVersion)11 ByteBuffer (java.nio.ByteBuffer)4 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)3 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)3 Row (com.datastax.oss.driver.api.core.cql.Row)3 ProtocolVersionRegistry (com.datastax.oss.driver.internal.core.ProtocolVersionRegistry)3 UnsupportedProtocolVersionException (com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException)2 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)2 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)2 Node (com.datastax.oss.driver.api.core.metadata.Node)2 TokenMap (com.datastax.oss.driver.api.core.metadata.TokenMap)2 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)2 ConsistencyLevelRegistry (com.datastax.oss.driver.internal.core.ConsistencyLevelRegistry)2 Execute (com.datastax.oss.protocol.internal.request.Execute)2 Query (com.datastax.oss.protocol.internal.request.Query)2 List (java.util.List)2 Test (org.junit.Test)2 DseProtocolVersion (com.datastax.dse.driver.api.core.DseProtocolVersion)1 ScriptGraphStatement (com.datastax.dse.driver.api.core.graph.ScriptGraphStatement)1