static Session connect() { String contactPoint = "localhost"; String keySpace = "ks1"; if(session == null) { PoolingOptions poolingOptions = new PoolingOptions().setConnectionsPerHost(HostDistance.REMOTE, 1, 4); cluster = Cluster.builder().addContactPoint(contactPoint).withPoolingOptions(poolingOptions) .withCompression(Compression.SNAPPY).build(); cluster.init(); for (Host host : cluster.getMetadata().getAllHosts()) { System.out.printf("Address: %s, Rack: %s, Datacenter: %s, Tokens: %s\n", host.getAddress(), host.getDatacenter(), host.getRack(), host.getTokens()); } session = cluster.connect(keySpace); } return session; }
static Cluster buildCluster(Cassandra3Storage cassandra) { Cluster.Builder builder = Cluster.builder(); List<InetSocketAddress> contactPoints = parseContactPoints(cassandra); int defaultPort = findConnectPort(contactPoints); builder.addContactPointsWithPorts(contactPoints); builder.withPort(defaultPort); // This ends up protocolOptions.port if (cassandra.username != null && cassandra.password != null) { builder.withCredentials(cassandra.username, cassandra.password); } builder.withRetryPolicy(ZipkinRetryPolicy.INSTANCE); builder.withLoadBalancingPolicy(new TokenAwarePolicy(new LatencyAwarePolicy.Builder( cassandra.localDc != null ? DCAwareRoundRobinPolicy.builder().withLocalDc(cassandra.localDc).build() : new RoundRobinPolicy() // This can select remote, but LatencyAwarePolicy will prefer local ).build())); builder.withPoolingOptions(new PoolingOptions().setMaxConnectionsPerHost( HostDistance.LOCAL, cassandra.maxConnections )); return builder.build(); }
static Cluster buildCluster(CassandraStorage cassandra) { Cluster.Builder builder = Cluster.builder(); List<InetSocketAddress> contactPoints = parseContactPoints(cassandra); int defaultPort = findConnectPort(contactPoints); builder.addContactPointsWithPorts(contactPoints); builder.withPort(defaultPort); // This ends up protocolOptions.port if (cassandra.username != null && cassandra.password != null) { builder.withCredentials(cassandra.username, cassandra.password); } builder.withRetryPolicy(ZipkinRetryPolicy.INSTANCE); builder.withLoadBalancingPolicy(new TokenAwarePolicy(new LatencyAwarePolicy.Builder( cassandra.localDc != null ? DCAwareRoundRobinPolicy.builder().withLocalDc(cassandra.localDc).build() : new RoundRobinPolicy() // This can select remote, but LatencyAwarePolicy will prefer local ).build())); builder.withPoolingOptions(new PoolingOptions().setMaxConnectionsPerHost( HostDistance.LOCAL, cassandra.maxConnections )); return builder.build(); }
private void connectToMultipleAddresses(String address) { PoolingOptions poolingOptions = new PoolingOptions() .setConnectionsPerHost(HostDistance.LOCAL, 4, 10) .setConnectionsPerHost(HostDistance.REMOTE, 2, 4); String[] music_hosts = address.split(","); if (cluster == null) { logger.debug("Initializing MUSIC Client with endpoints "+address); cluster = Cluster.builder() .withPort(9042) .withPoolingOptions(poolingOptions) .withoutMetrics() .addContactPoints(music_hosts) .build(); Metadata metadata = cluster.getMetadata(); logger.debug("Connected to cluster:"+metadata.getClusterName()+" at address:"+address); } session = cluster.connect(); }
@SuppressWarnings("unused") private void connectToCassaCluster(String address) { PoolingOptions poolingOptions = new PoolingOptions() .setConnectionsPerHost(HostDistance.LOCAL, 4, 10) .setConnectionsPerHost(HostDistance.REMOTE, 2, 4); Iterator<String> it = getAllPossibleLocalIps().iterator(); logger.debug("Iterating through possible ips:"+getAllPossibleLocalIps()); while (it.hasNext()) { try { cluster = Cluster.builder() .withPort(9042) .withPoolingOptions(poolingOptions) .withoutMetrics() .addContactPoint(address) .build(); //cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(Integer.MAX_VALUE); Metadata metadata = cluster.getMetadata(); logger.debug("Connected to cluster:"+metadata.getClusterName()+" at address:"+address); session = cluster.connect(); break; } catch (NoHostAvailableException e) { address = it.next(); } } }
private static void overridePoolingOptions(CassandraFactory cassandraFactory) { PoolingOptionsFactory newPoolingOptionsFactory = new PoolingOptionsFactory() { @Override public PoolingOptions build() { if (null == getPoolTimeout()) { setPoolTimeout(Duration.minutes(2)); } return super.build().setMaxQueueSize(40960); } }; cassandraFactory.getPoolingOptions().ifPresent((originalPoolingOptions) -> { newPoolingOptionsFactory.setHeartbeatInterval(originalPoolingOptions.getHeartbeatInterval()); newPoolingOptionsFactory.setIdleTimeout(originalPoolingOptions.getIdleTimeout()); newPoolingOptionsFactory.setLocal(originalPoolingOptions.getLocal()); newPoolingOptionsFactory.setRemote(originalPoolingOptions.getRemote()); newPoolingOptionsFactory.setPoolTimeout(originalPoolingOptions.getPoolTimeout()); }); cassandraFactory.setPoolingOptions(java.util.Optional.of(newPoolingOptionsFactory)); }
private static PoolingOptions getReadPoolingOptions(Configuration conf) { Optional<Integer> coreConnections = getInputCoreConnections(conf); Optional<Integer> maxConnections = getInputMaxConnections(conf); Optional<Integer> maxSimultaneousRequests = getInputMaxSimultReqPerConnections(conf); Optional<Integer> minSimultaneousRequests = getInputMinSimultReqPerConnections(conf); PoolingOptions poolingOptions = new PoolingOptions(); for (HostDistance hostDistance : Arrays.asList(HostDistance.LOCAL, HostDistance.REMOTE)) { if (coreConnections.isPresent()) poolingOptions.setCoreConnectionsPerHost(hostDistance, coreConnections.get()); if (maxConnections.isPresent()) poolingOptions.setMaxConnectionsPerHost(hostDistance, maxConnections.get()); if (minSimultaneousRequests.isPresent()) poolingOptions.setMinSimultaneousRequestsPerConnectionThreshold(hostDistance, minSimultaneousRequests.get()); if (maxSimultaneousRequests.isPresent()) poolingOptions.setMaxSimultaneousRequestsPerConnectionThreshold(hostDistance, maxSimultaneousRequests.get()); } return poolingOptions; }
private void setup() throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, CertificateException, UnrecoverableKeyException { // Connect to Cassandra PoolingOptions pOpts = new PoolingOptions(); pOpts.setCoreConnectionsPerHost(HostDistance.LOCAL, 4); pOpts.setMaxConnectionsPerHost(HostDistance.LOCAL, 4); Cluster.Builder clusterBuilder = Cluster.builder() .addContactPoint(host) .withPort(port) .withPoolingOptions(pOpts) .withLoadBalancingPolicy(new TokenAwarePolicy( DCAwareRoundRobinPolicy.builder().build())); if (null != username) clusterBuilder = clusterBuilder.withCredentials(username, password); if (null != truststorePath) clusterBuilder = clusterBuilder.withSSL(createSSLOptions()); cluster = clusterBuilder.build(); if (null == cluster) { throw new IOException("Could not create cluster"); } session = cluster.connect(); }
private void copyPoolingOptions(Builder builder) { PoolingOptions opts = new PoolingOptions(); opts.setCoreConnectionsPerHost(HostDistance.REMOTE, remoteCoreConnectionsPerHost); opts.setCoreConnectionsPerHost(HostDistance.LOCAL, localCoreConnectionsPerHost); opts.setMaxConnectionsPerHost(HostDistance.REMOTE, remoteMaxConnectionsPerHost); opts.setMaxConnectionsPerHost(HostDistance.LOCAL, localMaxConnectionsPerHost); opts.setMaxSimultaneousRequestsPerConnectionThreshold( HostDistance.REMOTE, remoteMaxSimultaneousRequestsPerConnectionThreshold); opts.setMaxSimultaneousRequestsPerConnectionThreshold( HostDistance.LOCAL, localMaxSimultaneousRequestsPerConnectionThreshold); opts.setMinSimultaneousRequestsPerConnectionThreshold( HostDistance.REMOTE, remoteMinSimultaneousRequestsPerConnectionThreshold); opts.setMinSimultaneousRequestsPerConnectionThreshold( HostDistance.LOCAL, localMinSimultaneousRequestsPerConnectionThreshold); builder.withPoolingOptions(opts); }
private static PoolingOptions getReadPoolingOptions(Configuration conf) { Optional<Integer> coreConnections = getInputCoreConnections(conf); Optional<Integer> maxConnections = getInputMaxConnections(conf); Optional<Integer> maxSimultaneousRequests = getInputMaxSimultReqPerConnections(conf); PoolingOptions poolingOptions = new PoolingOptions(); for (HostDistance hostDistance : Arrays.asList(HostDistance.LOCAL, HostDistance.REMOTE)) { if (coreConnections.isPresent()) poolingOptions.setCoreConnectionsPerHost(hostDistance, coreConnections.get()); if (maxConnections.isPresent()) poolingOptions.setMaxConnectionsPerHost(hostDistance, maxConnections.get()); if (maxSimultaneousRequests.isPresent()) poolingOptions.setNewConnectionThreshold(hostDistance, maxSimultaneousRequests.get()); } return poolingOptions; }
public PoolingOptions build() { PoolingOptions poolingOptions = new PoolingOptions(); if (local != null) { setPoolingOptions(poolingOptions, HostDistance.LOCAL, local); } if (remote != null) { setPoolingOptions(poolingOptions, HostDistance.REMOTE, remote); } if (heartbeatInterval != null) { poolingOptions.setHeartbeatIntervalSeconds((int) heartbeatInterval.toSeconds()); } if (poolTimeout != null) { poolingOptions.setPoolTimeoutMillis((int) poolTimeout.toMilliseconds()); } if (idleTimeout != null) { poolingOptions.setIdleTimeoutSeconds((int) idleTimeout.toSeconds()); } return poolingOptions; }
@Test public void buildsPoolingOptionsWithConfiguredValues() throws Exception { // given final PoolingOptionsFactory factory = new PoolingOptionsFactory(); factory.setHeartbeatInterval(Duration.minutes(1)); factory.setPoolTimeout(Duration.seconds(2)); factory.setLocal(createHostDistanceOptions(1, 3, 5, 25)); factory.setRemote(createHostDistanceOptions(2, 4, 6, 30)); // when final PoolingOptions poolingOptions = factory.build(); // then assertThat(poolingOptions.getHeartbeatIntervalSeconds()).isEqualTo(60); assertThat(poolingOptions.getPoolTimeoutMillis()).isEqualTo(2000); assertThat(poolingOptions.getCoreConnectionsPerHost(HostDistance.LOCAL)).isEqualTo(1); assertThat(poolingOptions.getMaxConnectionsPerHost(HostDistance.LOCAL)).isEqualTo(3); assertThat(poolingOptions.getMaxRequestsPerConnection(HostDistance.LOCAL)).isEqualTo(5); assertThat(poolingOptions.getNewConnectionThreshold(HostDistance.LOCAL)).isEqualTo(25); assertThat(poolingOptions.getCoreConnectionsPerHost(HostDistance.REMOTE)).isEqualTo(2); assertThat(poolingOptions.getMaxConnectionsPerHost(HostDistance.REMOTE)).isEqualTo(4); assertThat(poolingOptions.getMaxRequestsPerConnection(HostDistance.REMOTE)).isEqualTo(6); assertThat(poolingOptions.getNewConnectionThreshold(HostDistance.REMOTE)).isEqualTo(30); }
private static Cluster createCluster(CentralConfiguration centralConfig, TimestampGenerator defaultTimestampGenerator) { Cluster.Builder builder = Cluster.builder() .addContactPoints( centralConfig.cassandraContactPoint().toArray(new String[0])) // aggressive reconnect policy seems ok since not many clients .withReconnectionPolicy(new ConstantReconnectionPolicy(1000)) // let driver know that only idempotent queries are used so it will retry on timeout .withQueryOptions(new QueryOptions() .setDefaultIdempotence(true) .setConsistencyLevel(centralConfig.cassandraConsistencyLevel())) // central runs lots of parallel async queries and is very spiky since all // aggregates come in right after each minute marker .withPoolingOptions( new PoolingOptions().setMaxQueueSize(Session.MAX_CONCURRENT_QUERIES)) .withTimestampGenerator(defaultTimestampGenerator); String cassandraUsername = centralConfig.cassandraUsername(); if (!cassandraUsername.isEmpty()) { // empty password is strange but valid builder.withCredentials(cassandraUsername, centralConfig.cassandraPassword()); } return builder.build(); }
private static PoolingOptions getReadPoolingOptions(Configuration conf) { Optional<Integer> coreConnections = getInputCoreConnections(conf); Optional<Integer> maxConnections = getInputMaxConnections(conf); Optional<Integer> maxSimultaneousRequests = getInputMaxSimultReqPerConnections(conf); Optional<Integer> minSimultaneousRequests = getInputMinSimultReqPerConnections(conf); PoolingOptions poolingOptions = new PoolingOptions(); if (coreConnections.isPresent()) poolingOptions.setCoreConnectionsPerHost(HostDistance.LOCAL, coreConnections.get()); if (maxConnections.isPresent()) poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, maxConnections.get()); if (maxSimultaneousRequests.isPresent()) poolingOptions.setMaxSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, maxSimultaneousRequests.get()); if (minSimultaneousRequests.isPresent()) poolingOptions.setMinSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, minSimultaneousRequests.get()); poolingOptions.setCoreConnectionsPerHost(HostDistance.REMOTE, 0) .setMaxConnectionsPerHost(HostDistance.REMOTE, 0) .setMaxSimultaneousRequestsPerConnectionThreshold(HostDistance.REMOTE, 0) .setMinSimultaneousRequestsPerConnectionThreshold(HostDistance.REMOTE, 0); return poolingOptions; }
private static Cluster getCluster(){ if(cluster==null){ synchronized (SessionManager.class) { if(cluster==null){ PoolingOptions poolingOptions = new PoolingOptions(); poolingOptions .setMaxRequestsPerConnection(HostDistance.REMOTE, max) .setMaxRequestsPerConnection(HostDistance.LOCAL,max) .setMaxQueueSize(max*10) .setCoreConnectionsPerHost(HostDistance.LOCAL, 1) .setMaxConnectionsPerHost( HostDistance.LOCAL, 2) .setCoreConnectionsPerHost(HostDistance.REMOTE, 1) .setMaxConnectionsPerHost( HostDistance.REMOTE, 2); SocketOptions socketOptions = new SocketOptions(); socketOptions.setConnectTimeoutMillis(60000); socketOptions.setReadTimeoutMillis(60000); cluster = Cluster.builder().addContactPoint(url).withPoolingOptions(poolingOptions).withSocketOptions(socketOptions).build(); Metadata metadata = cluster.getMetadata(); Set<Host> allHosts = metadata.getAllHosts(); for(Host host:allHosts){ System.out.println("host:"+host.getAddress()); } } } } return cluster; }
private Cluster doCreateCluster(CassandraProperties properties) { Cluster cluster = Cluster.builder() .withClusterName(properties.getCluster()) .withPort(properties.getPort()) .addContactPoints(properties.getContactPoints()) .withTimestampGenerator(getTimestampGenerator()) .withPoolingOptions( //TODO some default options - move to config new PoolingOptions() .setConnectionsPerHost(HostDistance.LOCAL, 4, 4) .setConnectionsPerHost(HostDistance.REMOTE, 2, 2) .setMaxRequestsPerConnection(HostDistance.LOCAL, 1024) .setMaxRequestsPerConnection(HostDistance.REMOTE, 256) ) .build(); //almost all queries are idempotent except counter updates, so it's easier to mark them as idempotent cluster.getConfiguration().getQueryOptions().setDefaultIdempotence(true); CodecRegistry codecRegistry = cluster.getConfiguration().getCodecRegistry(); TupleType tupleType = cluster.getMetadata() .newTupleType(DataType.timestamp(), DataType.varchar()); codecRegistry.register(new ZonedDateTimeCodec(tupleType)); QueryLogger queryLogger = QueryLogger.builder() .withConstantThreshold(100) .withMaxQueryStringLength(200) .build(); cluster.register(queryLogger); return cluster; }
/** * @param ip String * @param port String * @param poolingOptions PoolingOptions * @return Cluster Cluster */ private static Cluster createCluster(String ip, String port, PoolingOptions poolingOptions) { return Cluster.builder().addContactPoint(ip).withPort(Integer.parseInt(port)) .withProtocolVersion(ProtocolVersion.V3).withRetryPolicy(DefaultRetryPolicy.INSTANCE) .withTimestampGenerator(new AtomicMonotonicTimestampGenerator()) .withPoolingOptions(poolingOptions).build(); }
public Cluster getCluster() { InetSocketAddress address = new InetSocketAddress(getContainerIpAddress(), getMappedPort(9042)); return Cluster.builder() .addContactPointsWithPorts(address) .withPoolingOptions(new PoolingOptions().setMaxConnectionsPerHost(HostDistance.LOCAL, 1)) .build(); }
@Test public void maxConnections_defaultsTo8() { PoolingOptions poolingOptions = buildCluster(Cassandra3Storage.builder().build()) .getConfiguration().getPoolingOptions(); assertThat(poolingOptions.getMaxConnectionsPerHost(HostDistance.LOCAL)).isEqualTo(8); }
@Test public void maxConnections_setsMaxConnectionsPerDatacenterLocalHost() { PoolingOptions poolingOptions = buildCluster(Cassandra3Storage.builder().maxConnections(16).build()) .getConfiguration().getPoolingOptions(); assertThat(poolingOptions.getMaxConnectionsPerHost(HostDistance.LOCAL)).isEqualTo(16); }
@Test public void maxConnections_defaultsTo8() { PoolingOptions poolingOptions = buildCluster(CassandraStorage.builder().build()) .getConfiguration().getPoolingOptions(); assertThat(poolingOptions.getMaxConnectionsPerHost(HostDistance.LOCAL)).isEqualTo(8); }
@Test public void maxConnections_setsMaxConnectionsPerDatacenterLocalHost() { PoolingOptions poolingOptions = buildCluster(CassandraStorage.builder().maxConnections(16).build()) .getConfiguration().getPoolingOptions(); assertThat(poolingOptions.getMaxConnectionsPerHost(HostDistance.LOCAL)).isEqualTo(16); }
public static Cluster getInputCluster(String[] hosts, Configuration conf) { int port = getInputNativePort(conf); Optional<AuthProvider> authProvider = getAuthProvider(conf); Optional<SSLOptions> sslOptions = getSSLOptions(conf); Optional<Integer> protocolVersion = getProtocolVersion(conf); LoadBalancingPolicy loadBalancingPolicy = getReadLoadBalancingPolicy(conf, hosts); SocketOptions socketOptions = getReadSocketOptions(conf); QueryOptions queryOptions = getReadQueryOptions(conf); PoolingOptions poolingOptions = getReadPoolingOptions(conf); Cluster.Builder builder = Cluster.builder() .addContactPoints(hosts) .withPort(port) .withCompression(ProtocolOptions.Compression.NONE); if (authProvider.isPresent()) builder.withAuthProvider(authProvider.get()); if (sslOptions.isPresent()) builder.withSSL(sslOptions.get()); if (protocolVersion.isPresent()) { builder.withProtocolVersion(protocolVersion.get()); } builder.withLoadBalancingPolicy(loadBalancingPolicy) .withSocketOptions(socketOptions) .withQueryOptions(queryOptions) .withPoolingOptions(poolingOptions); return builder.build(); }
public CassandraConn(String node, String keyspace, String username, String password) { PoolingOptions pools = new PoolingOptions(); pools.setMaxSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, maxRequestPerConnection); pools.setMaxSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, minRequestPerConnection); pools.setCoreConnectionsPerHost(HostDistance.LOCAL, coreConnectionLocalPerHost); pools.setMaxConnectionsPerHost(HostDistance.LOCAL, maxConnectionLocalPerHost); pools.setCoreConnectionsPerHost(HostDistance.REMOTE, coreConnectionRemotePerHost); pools.setMaxConnectionsPerHost(HostDistance.REMOTE, maxConnectionRemotePerHost); cluster = Cluster.builder() .addContactPoint(node) .withPoolingOptions(pools) .withCredentials(username, password) .withSocketOptions(new SocketOptions().setTcpNoDelay(true)) .build(); Metadata metadata = cluster.getMetadata(); System.out.printf("Connected to cluster: %s\n", metadata.getClusterName()); for ( Host host : metadata.getAllHosts() ) { System.out.printf("Datatacenter: %s; Host: %s; Rack: %s\n", host.getDatacenter(), host.getAddress(), host.getRack()); } session = cluster.connect(keyspace); }
static Cluster getCluster(InetSocketAddress contactPoint) { return Cluster.builder() .addContactPointsWithPorts(contactPoint) .withRetryPolicy(ZipkinRetryPolicy.INSTANCE) .withPoolingOptions(new PoolingOptions().setMaxConnectionsPerHost(HostDistance.LOCAL, 1)) .build(); }
private Cluster getCluster() { HostAndPort hap = HostAndPort.fromParts(getContainerIpAddress(), getMappedPort(9042)); InetSocketAddress address = new InetSocketAddress(hap.getHostText(), hap.getPort()); return Cluster.builder() .addContactPointsWithPorts(address) .withRetryPolicy(ZipkinRetryPolicy.INSTANCE) .withPoolingOptions(new PoolingOptions().setMaxConnectionsPerHost(HostDistance.LOCAL, 1)) .build(); }
public AsyncFuture<Connection> construct() { AsyncFuture<Session> session = async.call(() -> { // @formatter:off final PoolingOptions pooling = new PoolingOptions(); final QueryOptions queryOptions = new QueryOptions() .setFetchSize(fetchSize) .setConsistencyLevel(consistencyLevel); final SocketOptions socketOptions = new SocketOptions() .setReadTimeoutMillis((int) readTimeout.toMilliseconds()); final Cluster.Builder cluster = Cluster.builder() .addContactPointsWithPorts(seeds) .withRetryPolicy(retryPolicy) .withPoolingOptions(pooling) .withQueryOptions(queryOptions) .withSocketOptions(socketOptions) .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())); // @formatter:on authentication.accept(cluster); return cluster.build().connect(); }); if (configure) { session = session.lazyTransform(s -> { return schema.configure(s).directTransform(i -> s); }); } return session.lazyTransform(s -> { return schema.instance(s).directTransform(schema -> { return new Connection(s, schema); }); }); }
/** * Sets pooling options. * * @param options pooling options to use. */ @SuppressWarnings("UnusedDeclaration") public void setPoolingOptions(PoolingOptions options) { poolingOptions = options; invalidate(); }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { fetchSize = (Integer)in.readObject(); readConsistency = (ConsistencyLevel)in.readObject(); writeConsistency = (ConsistencyLevel)in.readObject(); user = U.readString(in); pwd = U.readString(in); port = (Integer)in.readObject(); contactPoints = (List<InetAddress>)in.readObject(); contactPointsWithPorts = (List<InetSocketAddress>)in.readObject(); maxSchemaAgreementWaitSeconds = (Integer)in.readObject(); protoVer = (Integer)in.readObject(); compression = U.readString(in); useSSL = (Boolean)in.readObject(); collectMetrix = (Boolean)in.readObject(); jmxReporting = (Boolean)in.readObject(); creds = (Credentials)in.readObject(); loadBalancingPlc = (LoadBalancingPolicy)readObject(in); reconnectionPlc = (ReconnectionPolicy)readObject(in); addrTranslator = (AddressTranslator)readObject(in); speculativeExecutionPlc = (SpeculativeExecutionPolicy)readObject(in); authProvider = (AuthProvider)readObject(in); sslOptions = (SSLOptions)readObject(in); poolingOptions = (PoolingOptions)readObject(in); sockOptions = (SocketOptions)readObject(in); nettyOptions = (NettyOptions)readObject(in); }
public static Cluster getCluster(String[] hosts, Configuration conf, int port) { Optional<AuthProvider> authProvider = getAuthProvider(conf); Optional<SSLOptions> sslOptions = getSSLOptions(conf); Optional<Integer> protocolVersion = getProtocolVersion(conf); LoadBalancingPolicy loadBalancingPolicy = getReadLoadBalancingPolicy(hosts); SocketOptions socketOptions = getReadSocketOptions(conf); QueryOptions queryOptions = getReadQueryOptions(conf); PoolingOptions poolingOptions = getReadPoolingOptions(conf); Cluster.Builder builder = Cluster.builder() .addContactPoints(hosts) .withPort(port) .withCompression(ProtocolOptions.Compression.NONE); if (authProvider.isPresent()) builder.withAuthProvider(authProvider.get()); if (sslOptions.isPresent()) builder.withSSL(sslOptions.get()); if (protocolVersion.isPresent()) { builder.withProtocolVersion(ProtocolVersion.fromInt(protocolVersion.get())); } builder.withLoadBalancingPolicy(loadBalancingPolicy) .withSocketOptions(socketOptions) .withQueryOptions(queryOptions) .withPoolingOptions(poolingOptions); return builder.build(); }
private void setPoolingOptions(PoolingOptions poolingOptions, HostDistance hostDistance, HostDistanceOptions options) { if (options.getCoreConnections() != null) { poolingOptions.setCoreConnectionsPerHost(hostDistance, options.getCoreConnections()); } if (options.getMaxConnections() != null) { poolingOptions.setMaxConnectionsPerHost(hostDistance, options.getMaxConnections()); } if (options.getMaxRequestsPerConnection() != null) { poolingOptions.setMaxRequestsPerConnection(hostDistance, options.getMaxRequestsPerConnection()); } if (options.getNewConnectionThreshold() != null) { poolingOptions.setNewConnectionThreshold(hostDistance, options.getNewConnectionThreshold()); } }
@Test public void buildsPoolingOptionsWithDefaultValues() throws Exception { final PoolingOptionsFactory factory = new PoolingOptionsFactory(); final PoolingOptions defaultPoolingOptions = new PoolingOptions(); final PoolingOptions poolingOptions = factory.build(); assertThat(poolingOptions.getHeartbeatIntervalSeconds()).isEqualTo(defaultPoolingOptions.getHeartbeatIntervalSeconds()); assertThat(poolingOptions.getPoolTimeoutMillis()).isEqualTo(defaultPoolingOptions.getPoolTimeoutMillis()); verifySamePoolingOptions(poolingOptions, defaultPoolingOptions, HostDistance.LOCAL); verifySamePoolingOptions(poolingOptions, defaultPoolingOptions, HostDistance.REMOTE); }
private void verifySamePoolingOptions(PoolingOptions poolingOptions, PoolingOptions defaultPoolingOptions, HostDistance hostDistance) { assertThat(poolingOptions.getCoreConnectionsPerHost(hostDistance)) .isEqualTo(defaultPoolingOptions.getCoreConnectionsPerHost(hostDistance)); assertThat(poolingOptions.getMaxConnectionsPerHost(hostDistance)) .isEqualTo(defaultPoolingOptions.getMaxConnectionsPerHost(hostDistance)); assertThat(poolingOptions.getMaxRequestsPerConnection(hostDistance)) .isEqualTo(defaultPoolingOptions.getMaxRequestsPerConnection(hostDistance)); assertThat(poolingOptions.getNewConnectionThreshold(hostDistance)) .isEqualTo(defaultPoolingOptions.getNewConnectionThreshold(hostDistance)); }
public static Cluster getInputCluster(String[] hosts, Configuration conf) { int port = getInputNativePort(conf); Optional<AuthProvider> authProvider = getAuthProvider(conf); Optional<SSLOptions> sslOptions = getSSLOptions(conf); LoadBalancingPolicy loadBalancingPolicy = getReadLoadBalancingPolicy(conf, hosts); SocketOptions socketOptions = getReadSocketOptions(conf); QueryOptions queryOptions = getReadQueryOptions(conf); PoolingOptions poolingOptions = getReadPoolingOptions(conf); Cluster.Builder builder = Cluster.builder() .addContactPoints(hosts) .withPort(port) .withCompression(ProtocolOptions.Compression.NONE); if (authProvider.isPresent()) builder.withAuthProvider(authProvider.get()); if (sslOptions.isPresent()) builder.withSSL(sslOptions.get()); builder.withLoadBalancingPolicy(loadBalancingPolicy) .withSocketOptions(socketOptions) .withQueryOptions(queryOptions) .withPoolingOptions(poolingOptions); return builder.build(); }
@Override public Session createSession() { Cluster.Builder clusterBuilder = new Cluster.Builder() .withPort(9042) .withoutJMXReporting() .withPoolingOptions(new PoolingOptions() .setMaxConnectionsPerHost(HostDistance.LOCAL, 1024) .setCoreConnectionsPerHost(HostDistance.LOCAL, 1024) .setMaxConnectionsPerHost(HostDistance.REMOTE, 1024) .setCoreConnectionsPerHost(HostDistance.REMOTE, 1024) .setMaxRequestsPerConnection(HostDistance.LOCAL, 1024) .setMaxRequestsPerConnection(HostDistance.REMOTE, 1024) .setMaxQueueSize(1024)); Arrays.stream(nodes.split(",")).forEach(clusterBuilder::addContactPoints); cluster = clusterBuilder.build(); cluster.init(); try { session = cluster.connect("system"); return session; } finally { if (session == null) { cluster.close(); } } }
private Gauge<Integer> createMaxLoad(String hostname) { return () -> { Session.State state = session.getState(); return getHost(state, hostname).map((host) -> { Configuration configuration = session.getCluster().getConfiguration(); PoolingOptions poolingOptions = configuration.getPoolingOptions(); HostDistance distance = configuration.getPolicies().getLoadBalancingPolicy().distance(host); int connections = state.getOpenConnections(host); return connections * poolingOptions.getMaxRequestsPerConnection(distance); }).orElse(0); }; }
public RxSessionImpl(Session session) { this.session = session; this.loadBalancingPolicy = session.getCluster().getConfiguration().getPolicies().getLoadBalancingPolicy(); PoolingOptions poolingOptions = session.getCluster().getConfiguration().getPoolingOptions(); maxInFlightLocal = poolingOptions.getCoreConnectionsPerHost(HostDistance.LOCAL) * poolingOptions.getMaxRequestsPerConnection(HostDistance.LOCAL); maxInFlightRemote = poolingOptions.getCoreConnectionsPerHost(HostDistance.REMOTE) * poolingOptions.getMaxRequestsPerConnection(HostDistance.REMOTE); }