Search in sources :

Example 1 with ProtocolVersionRegistry

use of com.datastax.oss.driver.internal.core.ProtocolVersionRegistry in project java-driver by datastax.

the class BoundStatementCcmIT method supportsPerRequestKeyspace.

private boolean supportsPerRequestKeyspace(CqlSession session) {
    InternalDriverContext context = (InternalDriverContext) session.getContext();
    ProtocolVersionRegistry protocolVersionRegistry = context.getProtocolVersionRegistry();
    return protocolVersionRegistry.supports(context.getProtocolVersion(), DefaultProtocolFeature.PER_REQUEST_KEYSPACE);
}
Also used : InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) ProtocolVersionRegistry(com.datastax.oss.driver.internal.core.ProtocolVersionRegistry)

Example 2 with ProtocolVersionRegistry

use of com.datastax.oss.driver.internal.core.ProtocolVersionRegistry in project java-driver by datastax.

the class ChannelFactoryTestBase method setup.

@Before
public void setup() throws InterruptedException {
    MockitoAnnotations.initMocks(this);
    serverGroup = new DefaultEventLoopGroup(1);
    clientGroup = new DefaultEventLoopGroup(1);
    when(context.getConfig()).thenReturn(driverConfig);
    when(driverConfig.getDefaultProfile()).thenReturn(defaultProfile);
    when(defaultProfile.isDefined(DefaultDriverOption.AUTH_PROVIDER_CLASS)).thenReturn(false);
    when(defaultProfile.getDuration(DefaultDriverOption.CONNECTION_INIT_QUERY_TIMEOUT)).thenReturn(Duration.ofMillis(TIMEOUT_MILLIS));
    when(defaultProfile.getDuration(DefaultDriverOption.CONNECTION_SET_KEYSPACE_TIMEOUT)).thenReturn(Duration.ofMillis(TIMEOUT_MILLIS));
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_MAX_REQUESTS)).thenReturn(1);
    when(defaultProfile.getDuration(DefaultDriverOption.HEARTBEAT_INTERVAL)).thenReturn(Duration.ofSeconds(30));
    when(defaultProfile.getDuration(DefaultDriverOption.CONNECTION_CONNECT_TIMEOUT)).thenReturn(Duration.ofSeconds(5));
    when(context.getProtocolVersionRegistry()).thenReturn(protocolVersionRegistry);
    when(context.getNettyOptions()).thenReturn(nettyOptions);
    when(nettyOptions.ioEventLoopGroup()).thenReturn(clientGroup);
    when(nettyOptions.channelClass()).thenAnswer((Answer<Object>) i -> LocalChannel.class);
    when(nettyOptions.allocator()).thenReturn(ByteBufAllocator.DEFAULT);
    when(context.getFrameCodec()).thenReturn(FrameCodec.defaultClient(new ByteBufPrimitiveCodec(ByteBufAllocator.DEFAULT), Compressor.none()));
    when(context.getSslHandlerFactory()).thenReturn(Optional.empty());
    when(context.getEventBus()).thenReturn(eventBus);
    when(context.getWriteCoalescer()).thenReturn(new PassThroughWriteCoalescer(null));
    when(context.getCompressor()).thenReturn(compressor);
    // Start local server
    ServerBootstrap serverBootstrap = new ServerBootstrap().group(serverGroup).channel(LocalServerChannel.class).localAddress(SERVER_ADDRESS.resolve()).childHandler(new ServerInitializer());
    ChannelFuture channelFuture = serverBootstrap.bind().sync();
    serverAcceptChannel = (LocalServerChannel) channelFuture.sync().channel();
}
Also used : LocalServerChannel(io.netty.channel.local.LocalServerChannel) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) RunWith(org.junit.runner.RunWith) TimeoutException(java.util.concurrent.TimeoutException) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) CompletableFuture(java.util.concurrent.CompletableFuture) NettyOptions(com.datastax.oss.driver.internal.core.context.NettyOptions) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) DriverConfig(com.datastax.oss.driver.api.core.config.DriverConfig) NodeMetricUpdater(com.datastax.oss.driver.internal.core.metrics.NodeMetricUpdater) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) MockitoAnnotations(org.mockito.MockitoAnnotations) Answer(org.mockito.stubbing.Answer) Compressor(com.datastax.oss.protocol.internal.Compressor) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) Message(com.datastax.oss.protocol.internal.Message) ByteBuf(io.netty.buffer.ByteBuf) ProtocolVersionRegistry(com.datastax.oss.driver.internal.core.ProtocolVersionRegistry) LocalChannel(io.netty.channel.local.LocalChannel) EventBus(com.datastax.oss.driver.internal.core.context.EventBus) ByteBufPrimitiveCodec(com.datastax.oss.driver.internal.core.protocol.ByteBufPrimitiveCodec) Duration(java.time.Duration) After(org.junit.After) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) Ready(com.datastax.oss.protocol.internal.response.Ready) Before(org.junit.Before) ChannelInitializer(io.netty.channel.ChannelInitializer) FrameCodec(com.datastax.oss.protocol.internal.FrameCodec) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Mockito.when(org.mockito.Mockito.when) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Assertions.fail(org.assertj.core.api.Assertions.fail) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) TestResponses(com.datastax.oss.driver.internal.core.TestResponses) Frame(com.datastax.oss.protocol.internal.Frame) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Optional(java.util.Optional) Startup(com.datastax.oss.protocol.internal.request.Startup) Options(com.datastax.oss.protocol.internal.request.Options) Collections(java.util.Collections) Exchanger(java.util.concurrent.Exchanger) ChannelFuture(io.netty.channel.ChannelFuture) LocalChannel(io.netty.channel.local.LocalChannel) ByteBufPrimitiveCodec(com.datastax.oss.driver.internal.core.protocol.ByteBufPrimitiveCodec) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Before(org.junit.Before)

Example 3 with ProtocolVersionRegistry

use of com.datastax.oss.driver.internal.core.ProtocolVersionRegistry in project java-driver by datastax.

the class GraphSupportCheckerTest method protocolWithPagingSupport.

private InternalDriverContext protocolWithPagingSupport(boolean pagingSupport) {
    InternalDriverContext context = mock(InternalDriverContext.class);
    when(context.getProtocolVersion()).thenReturn(DseProtocolVersion.DSE_V2);
    ProtocolVersionRegistry protocolVersionRegistry = mock(ProtocolVersionRegistry.class);
    when(protocolVersionRegistry.supports(DseProtocolVersion.DSE_V2, DseProtocolFeature.CONTINUOUS_PAGING)).thenReturn(pagingSupport);
    when(context.getProtocolVersionRegistry()).thenReturn(protocolVersionRegistry);
    return context;
}
Also used : InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) ProtocolVersionRegistry(com.datastax.oss.driver.internal.core.ProtocolVersionRegistry)

Example 4 with ProtocolVersionRegistry

use of com.datastax.oss.driver.internal.core.ProtocolVersionRegistry 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)

Example 5 with ProtocolVersionRegistry

use of com.datastax.oss.driver.internal.core.ProtocolVersionRegistry in project java-driver by datastax.

the class Conversions method toMessage.

public static Message toMessage(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 = statement.getPageSize();
    if (pageSize <= 0) {
        pageSize = config.getInt(DefaultDriverOption.REQUEST_PAGE_SIZE);
    }
    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();
    int nowInSeconds = statement.getNowInSeconds();
    if (nowInSeconds != Statement.NO_NOW_IN_SECONDS && !protocolVersionRegistry.supports(protocolVersion, DefaultProtocolFeature.NOW_IN_SECONDS)) {
        throw new IllegalArgumentException("Can't use nowInSeconds with protocol " + protocolVersion);
    }
    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);
        }
        QueryOptions queryOptions = new QueryOptions(consistencyCode, encode(positionalValues, codecRegistry, protocolVersion), encode(namedValues, codecRegistry, protocolVersion), false, pageSize, statement.getPagingState(), serialConsistencyCode, timestamp, (keyspace == null) ? null : keyspace.asInternal(), nowInSeconds);
        return new Query(simpleStatement.getQuery(), queryOptions);
    } else if (statement instanceof BoundStatement) {
        BoundStatement boundStatement = (BoundStatement) statement;
        if (!protocolVersionRegistry.supports(protocolVersion, DefaultProtocolFeature.UNSET_BOUND_VALUES)) {
            ensureAllSet(boundStatement);
        }
        boolean skipMetadata = boundStatement.getPreparedStatement().getResultSetDefinitions().size() > 0;
        QueryOptions queryOptions = new QueryOptions(consistencyCode, boundStatement.getValues(), Collections.emptyMap(), skipMetadata, pageSize, statement.getPagingState(), serialConsistencyCode, timestamp, null, nowInSeconds);
        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 if (statement instanceof BatchStatement) {
        BatchStatement batchStatement = (BatchStatement) statement;
        if (!protocolVersionRegistry.supports(protocolVersion, DefaultProtocolFeature.UNSET_BOUND_VALUES)) {
            ensureAllSet(batchStatement);
        }
        if (keyspace != null && !protocolVersionRegistry.supports(protocolVersion, DefaultProtocolFeature.PER_REQUEST_KEYSPACE)) {
            throw new IllegalArgumentException("Can't use per-request keyspace with protocol " + protocolVersion);
        }
        List<Object> queriesOrIds = new ArrayList<>(batchStatement.size());
        List<List<ByteBuffer>> values = new ArrayList<>(batchStatement.size());
        for (BatchableStatement<?> child : batchStatement) {
            if (child instanceof SimpleStatement) {
                SimpleStatement simpleStatement = (SimpleStatement) child;
                if (simpleStatement.getNamedValues().size() > 0) {
                    throw new IllegalArgumentException(String.format("Batch statements cannot contain simple statements with named values " + "(offending statement: %s)", simpleStatement.getQuery()));
                }
                queriesOrIds.add(simpleStatement.getQuery());
                values.add(encode(simpleStatement.getPositionalValues(), codecRegistry, protocolVersion));
            } else if (child instanceof BoundStatement) {
                BoundStatement boundStatement = (BoundStatement) child;
                queriesOrIds.add(Bytes.getArray(boundStatement.getPreparedStatement().getId()));
                values.add(boundStatement.getValues());
            } else {
                throw new IllegalArgumentException("Unsupported child statement: " + child.getClass().getName());
            }
        }
        return new Batch(batchStatement.getBatchType().getProtocolCode(), queriesOrIds, values, consistencyCode, serialConsistencyCode, timestamp, (keyspace == null) ? null : keyspace.asInternal(), nowInSeconds);
    } 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) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ArrayList(java.util.ArrayList) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) ProtocolVersionRegistry(com.datastax.oss.driver.internal.core.ProtocolVersionRegistry) QueryOptions(com.datastax.oss.protocol.internal.request.query.QueryOptions) ConsistencyLevel(com.datastax.oss.driver.api.core.ConsistencyLevel) Batch(com.datastax.oss.protocol.internal.request.Batch) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) NullAllowingImmutableList(com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableList) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ByteBuffer(java.nio.ByteBuffer) BatchStatement(com.datastax.oss.driver.api.core.cql.BatchStatement) ConsistencyLevelRegistry(com.datastax.oss.driver.internal.core.ConsistencyLevelRegistry) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Aggregations

ProtocolVersionRegistry (com.datastax.oss.driver.internal.core.ProtocolVersionRegistry)6 ProtocolVersion (com.datastax.oss.driver.api.core.ProtocolVersion)4 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)3 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)2 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)2 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)2 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)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 ByteBuffer (java.nio.ByteBuffer)2 ContinuousPagingOptions (com.datastax.dse.protocol.internal.request.query.ContinuousPagingOptions)1 DseQueryOptions (com.datastax.dse.protocol.internal.request.query.DseQueryOptions)1 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)1 DriverConfig (com.datastax.oss.driver.api.core.config.DriverConfig)1 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)1 BatchStatement (com.datastax.oss.driver.api.core.cql.BatchStatement)1 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)1