Search in sources :

Example 1 with TypesafeDriverConfig

use of com.datastax.oss.driver.internal.core.config.typesafe.TypesafeDriverConfig 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 2 with TypesafeDriverConfig

use of com.datastax.oss.driver.internal.core.config.typesafe.TypesafeDriverConfig in project java-driver by datastax.

the class ReflectionTest method should_build_policies_per_profile.

@Test
public void should_build_policies_per_profile() {
    String configSource = "advanced.speculative-execution-policy {\n" + "  class = ConstantSpeculativeExecutionPolicy\n" + "  max-executions = 3\n" + "  delay = 100 milliseconds\n" + "}\n" + "profiles {\n" + // Inherits from default profile
    "  profile1 {}\n" + // Inherits but changes one option
    "  profile2 { \n" + "    advanced.speculative-execution-policy.max-executions = 2" + "  }\n" + // Same as previous profile, should share the same policy instance
    "  profile3 { \n" + "    advanced.speculative-execution-policy.max-executions = 2" + "  }\n" + // Completely overrides default profile
    "  profile4 { \n" + "    advanced.speculative-execution-policy.class = NoSpeculativeExecutionPolicy\n" + "  }\n" + "}\n";
    InternalDriverContext context = mock(InternalDriverContext.class);
    TypesafeDriverConfig config = new TypesafeDriverConfig(ConfigFactory.parseString(configSource));
    when(context.getConfig()).thenReturn(config);
    Map<String, SpeculativeExecutionPolicy> policies = Reflection.buildFromConfigProfiles(context, DefaultDriverOption.SPECULATIVE_EXECUTION_POLICY_CLASS, DefaultDriverOption.SPECULATIVE_EXECUTION_POLICY, SpeculativeExecutionPolicy.class, "com.datastax.oss.driver.internal.core.specex");
    assertThat(policies).hasSize(5);
    SpeculativeExecutionPolicy defaultPolicy = policies.get(DriverExecutionProfile.DEFAULT_NAME);
    SpeculativeExecutionPolicy policy1 = policies.get("profile1");
    SpeculativeExecutionPolicy policy2 = policies.get("profile2");
    SpeculativeExecutionPolicy policy3 = policies.get("profile3");
    SpeculativeExecutionPolicy policy4 = policies.get("profile4");
    assertThat(defaultPolicy).isInstanceOf(ConstantSpeculativeExecutionPolicy.class).isSameAs(policy1);
    assertThat(policy2).isInstanceOf(ConstantSpeculativeExecutionPolicy.class).isSameAs(policy3);
    assertThat(policy4).isInstanceOf(NoSpeculativeExecutionPolicy.class);
}
Also used : TypesafeDriverConfig(com.datastax.oss.driver.internal.core.config.typesafe.TypesafeDriverConfig) ConstantSpeculativeExecutionPolicy(com.datastax.oss.driver.internal.core.specex.ConstantSpeculativeExecutionPolicy) NoSpeculativeExecutionPolicy(com.datastax.oss.driver.internal.core.specex.NoSpeculativeExecutionPolicy) SpeculativeExecutionPolicy(com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) ConstantSpeculativeExecutionPolicy(com.datastax.oss.driver.internal.core.specex.ConstantSpeculativeExecutionPolicy) Test(org.junit.Test)

Aggregations

TypesafeDriverConfig (com.datastax.oss.driver.internal.core.config.typesafe.TypesafeDriverConfig)2 ProtocolVersion (com.datastax.oss.driver.api.core.ProtocolVersion)1 UnsupportedProtocolVersionException (com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException)1 DriverConfig (com.datastax.oss.driver.api.core.config.DriverConfig)1 SpeculativeExecutionPolicy (com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy)1 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)1 NettyOptions (com.datastax.oss.driver.internal.core.context.NettyOptions)1 ConstantSpeculativeExecutionPolicy (com.datastax.oss.driver.internal.core.specex.ConstantSpeculativeExecutionPolicy)1 NoSpeculativeExecutionPolicy (com.datastax.oss.driver.internal.core.specex.NoSpeculativeExecutionPolicy)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Test (org.junit.Test)1