Search in sources :

Example 1 with NettyOptions

use of com.datastax.oss.driver.internal.core.context.NettyOptions 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 2 with NettyOptions

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

Aggregations

ProtocolVersion (com.datastax.oss.driver.api.core.ProtocolVersion)2 DriverConfig (com.datastax.oss.driver.api.core.config.DriverConfig)2 NettyOptions (com.datastax.oss.driver.internal.core.context.NettyOptions)2 Channel (io.netty.channel.Channel)2 ChannelFuture (io.netty.channel.ChannelFuture)2 UnsupportedProtocolVersionException (com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException)1 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)1 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)1 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)1 ProtocolVersionRegistry (com.datastax.oss.driver.internal.core.ProtocolVersionRegistry)1 TestResponses (com.datastax.oss.driver.internal.core.TestResponses)1 TypesafeDriverConfig (com.datastax.oss.driver.internal.core.config.typesafe.TypesafeDriverConfig)1 EventBus (com.datastax.oss.driver.internal.core.context.EventBus)1 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)1 NodeMetricUpdater (com.datastax.oss.driver.internal.core.metrics.NodeMetricUpdater)1 ByteBufPrimitiveCodec (com.datastax.oss.driver.internal.core.protocol.ByteBufPrimitiveCodec)1 Compressor (com.datastax.oss.protocol.internal.Compressor)1 Frame (com.datastax.oss.protocol.internal.Frame)1 FrameCodec (com.datastax.oss.protocol.internal.FrameCodec)1 Message (com.datastax.oss.protocol.internal.Message)1