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();
}
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);
}
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;
}
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);
}
}
});
}
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());
}
}
Aggregations