com.datastax.driver.core.HostDistance Java Examples

The following examples show how to use com.datastax.driver.core.HostDistance. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CqlConfigHelper.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: DatastaxPoolingOptions.java    From heroic with Apache License 2.0 6 votes vote down vote up
@JsonCreator
public DatastaxPoolingOptions(
    @JsonProperty("maxRequestsPerConnection")
        Optional<Map<HostDistance, Integer>> maxRequestsPerConnection,
    @JsonProperty("coreConnectionsPerHost")
        Optional<Map<HostDistance, Integer>> coreConnectionsPerHost,
    @JsonProperty("maxConnectionsPerHost")
        Optional<Map<HostDistance, Integer>> maxConnectionsPerHost,
    @JsonProperty("newConnectionThreshold")
        Optional<Map<HostDistance, Integer>> newConnectionThreshold,
    @JsonProperty("maxQueueSize") Optional<Integer> maxQueueSize,
    @JsonProperty("poolTimeoutMillis") Optional<Integer> poolTimeoutMillis,
    @JsonProperty("idleTimeoutSeconds") Optional<Integer> idleTimeoutSeconds,
    @JsonProperty("heartbeatIntervalSeconds") Optional<Integer> heartbeatIntervalSeconds
) {
    this.maxRequestsPerConnection = maxRequestsPerConnection;
    this.coreConnectionsPerHost = coreConnectionsPerHost;
    this.maxConnectionsPerHost = maxConnectionsPerHost;
    this.newConnectionThreshold = newConnectionThreshold;
    this.maxQueueSize = maxQueueSize;
    this.poolTimeoutMillis = poolTimeoutMillis;
    this.idleTimeoutSeconds = idleTimeoutSeconds;
    this.heartbeatIntervalSeconds = heartbeatIntervalSeconds;
}
 
Example #3
Source File: PoolingOptionsFactoryTest.java    From dropwizard-cassandra with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #4
Source File: CassandraConfig.java    From realtime-analytics with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example #5
Source File: PoolingOptionsFactory.java    From dropwizard-cassandra with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #6
Source File: LimitedLocalNodeFirstLocalBalancingPolicy.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Override
public HostDistance distance(Host host)
{
    if (isLocalHost(host))
    {
        return HostDistance.LOCAL;
    }
    else
    {
        return HostDistance.REMOTE;
    }
}
 
Example #7
Source File: CassandraConfig.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private Builder populatePoolingSettings(Map<String, String> properties, Builder builder) {
    String localCoreConnectionsPerHost = properties.get(DBConstants.Cassandra.LOCAL_CORE_CONNECTIONS_PER_HOST);
    String remoteCoreConnectionsPerHost = properties.get(DBConstants.Cassandra.REMOTE_CORE_CONNECTIONS_PER_HOST);
    String localMaxConnectionsPerHost = properties.get(DBConstants.Cassandra.LOCAL_MAX_CONNECTIONS_PER_HOST);
    String remoteMaxConnectionsPerHost = properties.get(DBConstants.Cassandra.REMOTE_MAX_CONNECTIONS_PER_HOST);
    String localNewConnectionThreshold = properties.get(DBConstants.Cassandra.LOCAL_NEW_CONNECTION_THRESHOLD);
    String remoteNewConnectionThreshold = properties.get(DBConstants.Cassandra.REMOTE_NEW_CONNECTION_THRESHOLD);
    String localMaxRequestsPerConnection = properties.get(DBConstants.Cassandra.LOCAL_MAX_REQUESTS_PER_CONNECTION);
    String remoteMaxRequestsPerConnection = properties.get(DBConstants.Cassandra.REMOTE_MAX_REQUESTS_PER_CONNECTION);
    PoolingOptions options = new PoolingOptions();
    if (localCoreConnectionsPerHost != null) {
        options.setCoreConnectionsPerHost(HostDistance.LOCAL, Integer.parseInt(localCoreConnectionsPerHost));
    }
    if (remoteCoreConnectionsPerHost != null) {
        options.setCoreConnectionsPerHost(HostDistance.REMOTE, Integer.parseInt(remoteCoreConnectionsPerHost));
    }
    if (localMaxConnectionsPerHost != null) {
        options.setMaxConnectionsPerHost(HostDistance.LOCAL, Integer.parseInt(localMaxConnectionsPerHost));
    }
    if (remoteMaxConnectionsPerHost != null) {
        options.setMaxConnectionsPerHost(HostDistance.REMOTE, Integer.parseInt(remoteMaxConnectionsPerHost));
    }
    if (localNewConnectionThreshold != null) {
        options.setNewConnectionThreshold(HostDistance.LOCAL, Integer.parseInt(localNewConnectionThreshold));
    }
    if (remoteNewConnectionThreshold != null) {
        options.setNewConnectionThreshold(HostDistance.REMOTE, Integer.parseInt(remoteNewConnectionThreshold));
    }
    if (localMaxRequestsPerConnection != null) {
        options.setMaxRequestsPerConnection(HostDistance.LOCAL, Integer.parseInt(localMaxRequestsPerConnection));
    }
    if (remoteMaxRequestsPerConnection != null) {
        options.setMaxRequestsPerConnection(HostDistance.REMOTE, Integer.parseInt(remoteMaxRequestsPerConnection));
    }
    builder = builder.withPoolingOptions(options);
    return builder;
}
 
Example #8
Source File: RxSessionImpl.java    From hawkular-metrics with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #9
Source File: LiveCassandraManager.java    From hawkular-metrics with Apache License 2.0 5 votes vote down vote up
@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();
        }
    }
}
 
Example #10
Source File: CentralModule.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private static Cluster createCluster(CentralConfiguration centralConfig,
        TimestampGenerator defaultTimestampGenerator) {
    Cluster.Builder builder = Cluster.builder()
            .addContactPoints(
                    centralConfig.cassandraContactPoint().toArray(new String[0]))
            .withPort(centralConfig.cassandraPort())
            // 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.cassandraReadConsistencyLevel()))
            .withPoolingOptions(new PoolingOptions()
                    .setMaxRequestsPerConnection(HostDistance.LOCAL,
                            centralConfig.cassandraMaxConcurrentQueries())
                    .setMaxRequestsPerConnection(HostDistance.REMOTE,
                            centralConfig.cassandraMaxConcurrentQueries())
                    // using 2x "max requests per connection", so that thread-based
                    // throttling can allow up to 3x "max requests per connection", which is
                    // split 50% writes / 25% reads / 25% rollups, which will keep the pool
                    // saturated with writes alone
                    .setMaxQueueSize(centralConfig.cassandraMaxConcurrentQueries() * 2)
                    .setPoolTimeoutMillis(centralConfig.cassandraPoolTimeoutMillis()))
            .withTimestampGenerator(defaultTimestampGenerator);
    String cassandraUsername = centralConfig.cassandraUsername();
    if (!cassandraUsername.isEmpty()) {
        // empty password is strange but valid
        builder.withCredentials(cassandraUsername, centralConfig.cassandraPassword());
    }
    if (centralConfig.cassandraSSL()) {
    	builder.withSSL();
    }
    return builder.build();
}
 
Example #11
Source File: PoolingOptionsFactoryTest.java    From dropwizard-cassandra with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example #12
Source File: PoolingOptionsFactoryTest.java    From dropwizard-cassandra with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #13
Source File: PoolingOptionsFactory.java    From dropwizard-cassandra with Apache License 2.0 5 votes vote down vote up
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());
    }
}
 
Example #14
Source File: ClusterConfiguration.java    From james-project with Apache License 2.0 5 votes vote down vote up
private static Optional<PoolingOptions> readPoolingOptions(Configuration configuration) {
    Optional<Integer> maxConnections = Optional.ofNullable(configuration.getInteger("cassandra.pooling.local.max.connections", null));
    Optional<Integer> maxRequests = Optional.ofNullable(configuration.getInteger("cassandra.pooling.local.max.requests", null));
    Optional<Integer> poolingTimeout = Optional.ofNullable(configuration.getInteger("cassandra.pooling.timeout", null));
    Optional<Integer> heartbeatTimeout = Optional.ofNullable(configuration.getInteger("cassandra.pooling.heartbeat.timeout", null));
    Optional<Integer> maxQueueSize = Optional.ofNullable(configuration.getInteger("cassandra.pooling.max.queue.size", null));

    if (!maxConnections.isPresent()
        && !maxRequests.isPresent()
        && !poolingTimeout.isPresent()
        && !heartbeatTimeout.isPresent()
        && !maxQueueSize.isPresent()) {
        return Optional.empty();
    }
    PoolingOptions result = new PoolingOptions();

    maxConnections.ifPresent(value -> {
        result.setMaxConnectionsPerHost(HostDistance.LOCAL, value);
        result.setMaxConnectionsPerHost(HostDistance.REMOTE, value);
    });
    maxRequests.ifPresent(value -> {
        result.setMaxRequestsPerConnection(HostDistance.LOCAL, value);
        result.setMaxRequestsPerConnection(HostDistance.REMOTE, value);
    });
    poolingTimeout.ifPresent(result::setPoolTimeoutMillis);
    heartbeatTimeout.ifPresent(result::setHeartbeatIntervalSeconds);
    maxQueueSize.ifPresent(result::setMaxQueueSize);

    return Optional.of(result);
}
 
Example #15
Source File: CassandraStorageExtension.java    From zipkin-dependencies with Apache License 2.0 5 votes vote down vote up
static Cluster getCluster(InetSocketAddress contactPoint) {
  return Cluster.builder()
    .withoutJMXReporting()
    .addContactPointsWithPorts(contactPoint)
    .withRetryPolicy(ZipkinRetryPolicy.INSTANCE)
    .withPoolingOptions(new PoolingOptions().setMaxConnectionsPerHost(HostDistance.LOCAL, 1))
    .build();
}
 
Example #16
Source File: CassandraStorageExtension.java    From zipkin-dependencies with Apache License 2.0 5 votes vote down vote up
static Cluster getCluster(InetSocketAddress contactPoint) {
  return Cluster.builder()
    .withoutJMXReporting()
    .addContactPointsWithPorts(contactPoint)
    .withRetryPolicy(ZipkinRetryPolicy.INSTANCE)
    .withPoolingOptions(new PoolingOptions().setMaxConnectionsPerHost(HostDistance.LOCAL, 1))
    .build();
}
 
Example #17
Source File: CassandraContainer.java    From spark-dependencies with Apache License 2.0 5 votes vote down vote up
public Cluster getCluster() {
  InetSocketAddress address = new InetSocketAddress(getContainerIpAddress(), getMappedPort(9042));

  return Cluster.builder()
      .addContactPointsWithPorts(address)
      .withPoolingOptions(new PoolingOptions().setMaxConnectionsPerHost(HostDistance.LOCAL, 1))
      .build();
}
 
Example #18
Source File: CassandraController.java    From FlareBot with MIT License 5 votes vote down vote up
public CassandraController(JSONConfig config) {
    Cluster.Builder builder = Cluster.builder().withClusterName("FlareBot Nodes")
            .withCredentials(config.getString("cassandra.username").get(), config.getString("cassandra.password").get())
            .withPoolingOptions(new PoolingOptions().setConnectionsPerHost(HostDistance.LOCAL, 2, 4).setConnectionsPerHost(HostDistance.REMOTE, 2, 4));
    config.getArray("cassandra.nodes").ifPresent(array -> array.forEach(ip -> builder.addContactPoint(ip.getAsString())));
    cluster = builder.build();
    session = cluster.connect();
}
 
Example #19
Source File: YugabyteCloudConfig.java    From yugastore-java with Apache License 2.0 5 votes vote down vote up
@Override
public PoolingOptions getPoolingOptions() {
	
	PoolingOptions poolingOptions = new PoolingOptions()
    .setMaxRequestsPerConnection(HostDistance.LOCAL, 32768)
    .setMaxRequestsPerConnection(HostDistance.REMOTE, 2000);
	
	return poolingOptions;
}
 
Example #20
Source File: CassandraTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private int getMaxConnections() {
  return cluster.getConfiguration().getPoolingOptions().getMaxRequestsPerConnection(HostDistance.REMOTE);
}
 
Example #21
Source File: DatasourceSerializationTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override public HostDistance distance(Host host) {
    return plc.distance(host);
}
 
Example #22
Source File: CassandraCluster.java    From monasca-persister with Apache License 2.0 4 votes vote down vote up
@Inject
public CassandraCluster(final PersisterConfig config) {

  this.dbConfig = config.getCassandraDbConfiguration();

  QueryOptions qo = new QueryOptions();
  qo.setConsistencyLevel(ConsistencyLevel.valueOf(dbConfig.getConsistencyLevel()));
  qo.setDefaultIdempotence(true);

  String[] contactPoints = dbConfig.getContactPoints();
  int retries = dbConfig.getMaxWriteRetries();
  Builder builder = Cluster.builder().addContactPoints(contactPoints).withPort(dbConfig.getPort());
  builder
      .withSocketOptions(new SocketOptions().setConnectTimeoutMillis(dbConfig.getConnectionTimeout())
          .setReadTimeoutMillis(dbConfig.getReadTimeout()));
  builder.withQueryOptions(qo).withRetryPolicy(new MonascaRetryPolicy(retries, retries, retries));

  lbPolicy = new TokenAwarePolicy(
      DCAwareRoundRobinPolicy.builder().withLocalDc(dbConfig.getLocalDataCenter()).build());
  builder.withLoadBalancingPolicy(lbPolicy);

  String user = dbConfig.getUser();
  if (user != null && !user.isEmpty()) {
    builder.withAuthProvider(new PlainTextAuthProvider(dbConfig.getUser(), dbConfig.getPassword()));
  }
  cluster = builder.build();

  PoolingOptions poolingOptions = cluster.getConfiguration().getPoolingOptions();

  poolingOptions.setConnectionsPerHost(HostDistance.LOCAL, dbConfig.getMaxConnections(),
      dbConfig.getMaxConnections()).setConnectionsPerHost(HostDistance.REMOTE,
          dbConfig.getMaxConnections(), dbConfig.getMaxConnections());

  poolingOptions.setMaxRequestsPerConnection(HostDistance.LOCAL, dbConfig.getMaxRequests())
      .setMaxRequestsPerConnection(HostDistance.REMOTE, dbConfig.getMaxRequests());

  metricsSession = cluster.connect(dbConfig.getKeySpace());

  measurementInsertStmt = metricsSession.prepare(MEASUREMENT_INSERT_CQL).setIdempotent(true);
  // TODO: Remove update statements, TTL issues
  measurementUpdateStmt = metricsSession.prepare(MEASUREMENT_UPDATE_CQL).setIdempotent(true);
  metricInsertStmt = metricsSession.prepare(METRICS_INSERT_CQL).setIdempotent(true);
  dimensionStmt = metricsSession.prepare(DIMENSION_INSERT_CQL).setIdempotent(true);
  dimensionMetricStmt = metricsSession.prepare(DIMENSION_METRIC_INSERT_CQL).setIdempotent(true);
  metricDimensionStmt = metricsSession.prepare(METRIC_DIMENSION_INSERT_CQL).setIdempotent(true);

  retrieveMetricIdStmt = metricsSession.prepare(RETRIEVE_METRIC_ID_CQL).setIdempotent(true);
  retrieveMetricDimensionStmt = metricsSession.prepare(RETRIEVE_METRIC_DIMENSION_CQL)
      .setIdempotent(true);

  alarmsSession = cluster.connect(dbConfig.getKeySpace());

  alarmHistoryInsertStmt = alarmsSession.prepare(INSERT_ALARM_STATE_HISTORY_SQL).setIdempotent(true);

  metricIdCache = CacheBuilder.newBuilder()
      .maximumSize(config.getCassandraDbConfiguration().getDefinitionMaxCacheSize()).build();

  dimensionCache = CacheBuilder.newBuilder()
      .maximumSize(config.getCassandraDbConfiguration().getDefinitionMaxCacheSize()).build();

  metricDimensionCache = CacheBuilder.newBuilder()
      .maximumSize(config.getCassandraDbConfiguration().getDefinitionMaxCacheSize()).build();

  logger.info("loading cached definitions from db");

  ExecutorService executor = Executors.newFixedThreadPool(250);

  //a majority of the ids are for metrics not actively receiving msgs anymore
  //loadMetricIdCache(executor);

  loadDimensionCache();

  loadMetricDimensionCache(executor);

  executor.shutdown();
}
 
Example #23
Source File: CassandraSessionImpl.java    From newts with Apache License 2.0 4 votes vote down vote up
@Inject
public CassandraSessionImpl(@Named("cassandra.keyspace") String keyspace, @Named("cassandra.hostname") String hostname,
        @Named("cassandra.port") int port, @Named("cassandra.compression") String compression,
        @Named("cassandra.username") String username, @Named("cassandra.password") String password,
        @Named("cassandra.ssl") boolean ssl,
        @Named("cassandra.pool.core-connections-per-host") Integer coreConnectionsPerHost,
        @Named("cassandra.pool.max-connections-per-host") Integer maxConnectionsPerHost,
        @Named("cassandra.pool.max-requests-per-connection") Integer maxRequestsPerConnection) {

    checkNotNull(keyspace, "keyspace argument");
    checkNotNull(hostname, "hostname argument");
    checkArgument(port > 0 && port < 65535, "not a valid port number: %d", port);
    checkNotNull(compression, "compression argument");

    LOG.info("Setting up session with {}:{} using compression {}", hostname, port, compression.toUpperCase());

    final PoolingOptions poolingOptions = new PoolingOptions();
    if (coreConnectionsPerHost != null) {
        LOG.debug("Using {} core connections per host.", coreConnectionsPerHost);
        poolingOptions.setCoreConnectionsPerHost(HostDistance.LOCAL, coreConnectionsPerHost)
                .setCoreConnectionsPerHost(HostDistance.REMOTE, coreConnectionsPerHost);
    }
    if (maxConnectionsPerHost != null) {
        LOG.debug("Using {} max connections per host.", maxConnectionsPerHost);
        poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, maxConnectionsPerHost)
                .setMaxConnectionsPerHost(HostDistance.REMOTE, maxConnectionsPerHost);
    }
    if (maxRequestsPerConnection != null) {
        LOG.debug("Using {} max requests per connection.", maxRequestsPerConnection);
        poolingOptions.setMaxRequestsPerConnection(HostDistance.LOCAL, maxRequestsPerConnection)
                .setMaxRequestsPerConnection(HostDistance.REMOTE, maxRequestsPerConnection);
    }

    Builder builder = Cluster
            .builder()
            .withPort(port)
            .addContactPoints(hostname.split(","))
            .withReconnectionPolicy(new ExponentialReconnectionPolicy(1000, 2 * 60 * 1000))
            .withPoolingOptions(poolingOptions)
            .withCompression(Compression.valueOf(compression.toUpperCase()));

    if (username != null && password != null) {
        LOG.info("Using username: {} and password: XXXXXXXX", username);
        builder.withCredentials(username, password);
    }

    if (ssl) {
        LOG.info("Enabling SSL.");
        builder.withSSL();
    }

    m_session = builder.build().connect(keyspace);
}
 
Example #24
Source File: CassandraConfiguration.java    From emodb with Apache License 2.0 4 votes vote down vote up
private com.datastax.driver.core.Cluster.Builder newCqlDriverBuilder(ConnectionPoolConfiguration poolConfig,
                                                                     MetricRegistry metricRegistry) {
    performHostDiscovery(metricRegistry);

    String[] seeds = _seeds.split(",");
    List<String> contactPoints = Lists.newArrayListWithCapacity(seeds.length);

    // Each seed may be a host name or a host name and port (e.g.; "1.2.3.4" or "1.2.3.4:9160").  These need
    // to be converted into host names only.
    for (String seed : seeds) {
        HostAndPort hostAndPort = HostAndPort.fromString(seed);
        seed = hostAndPort.getHostText();
        if (hostAndPort.hasPort()) {
            if (hostAndPort.getPort() == _thriftPort) {
                _log.debug("Seed {} found using RPC port; swapping for native port {}", seed, _cqlPort);
            } else if (hostAndPort.getPort() != _cqlPort) {
                throw new IllegalArgumentException(String.format(
                        "Seed %s found with invalid port %s.  The port must match either the RPC (thrift) port %s " +
                        "or the native (CQL) port %s", seed, hostAndPort.getPort(), _thriftPort, _cqlPort));
            }
        }

        contactPoints.add(seed);
    }

    PoolingOptions poolingOptions = new PoolingOptions();
    if (poolConfig.getMaxConnectionsPerHost().or(getMaxConnectionsPerHost()).isPresent()) {
        poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, poolConfig.getMaxConnectionsPerHost().or(getMaxConnectionsPerHost()).get());
    }
    if (poolConfig.getCoreConnectionsPerHost().or(getCoreConnectionsPerHost()).isPresent()) {
        poolingOptions.setCoreConnectionsPerHost(HostDistance.LOCAL, poolConfig.getCoreConnectionsPerHost().or(getCoreConnectionsPerHost()).get());
    }

    SocketOptions socketOptions = new SocketOptions();
    if (poolConfig.getConnectTimeout().or(getConnectTimeout()).isPresent()) {
        socketOptions.setConnectTimeoutMillis(poolConfig.getConnectTimeout().or(getConnectTimeout()).get());
    }
    if (poolConfig.getSocketTimeout().or(getSocketTimeout()).isPresent()) {
        socketOptions.setReadTimeoutMillis(poolConfig.getSocketTimeout().or(getSocketTimeout()).get());
    }

    AuthProvider authProvider = _authenticationCredentials != null
            ? new PlainTextAuthProvider(_authenticationCredentials.getUsername(), _authenticationCredentials.getPassword())
            : AuthProvider.NONE;

    return com.datastax.driver.core.Cluster.builder()
            .addContactPoints(contactPoints.toArray(new String[contactPoints.size()]))
            .withPort(_cqlPort)
            .withPoolingOptions(poolingOptions)
            .withSocketOptions(socketOptions)
            .withRetryPolicy(Policies.defaultRetryPolicy())
            .withAuthProvider(authProvider);
}
 
Example #25
Source File: AppBase.java    From yb-sample-apps with Apache License 2.0 4 votes vote down vote up
/**
 * Private method that is thread-safe and creates the Cassandra client. Exactly one calling thread
 * will succeed in creating the client. This method does nothing for the other threads.
 * @param contactPoints list of contact points for the client.
 */
protected synchronized void createCassandraClient(List<ContactPoint> contactPoints) {
  Cluster.Builder builder;
  if (cassandra_cluster == null) {
    builder = Cluster.builder();
    if (appConfig.dbUsername != null) {
      if (appConfig.dbPassword == null) {
        throw new IllegalArgumentException("Password required when providing a username");
      }
      builder = builder
          .withCredentials(appConfig.dbUsername, appConfig.dbPassword);
    }
    if (appConfig.sslCert != null) {
      builder = builder
          .withSSL(createSSLHandler(appConfig.sslCert));
    }
    Integer port = null;
    SocketOptions socketOptions = new SocketOptions();
    if (appConfig.cqlConnectTimeoutMs > 0) {
      socketOptions.setConnectTimeoutMillis(appConfig.cqlConnectTimeoutMs);
    }
    if (appConfig.cqlReadTimeoutMs > 0) {
      socketOptions.setReadTimeoutMillis(appConfig.cqlReadTimeoutMs);
    }
    builder.withSocketOptions(socketOptions);
    for (ContactPoint cp : contactPoints) {
      if (port == null) {
        port = cp.getPort();
        builder.withPort(port);
      } else if (port != cp.getPort()) {
        throw new IllegalArgumentException("Using multiple CQL ports is not supported.");
      }
      builder.addContactPoint(cp.getHost());
    }
    LOG.info("Connecting with " + appConfig.concurrentClients + " clients to nodes: "
        + builder.getContactPoints()
            .stream().map(it -> it.toString()).collect(Collectors.joining(",")));
    PoolingOptions poolingOptions = new PoolingOptions();
    poolingOptions
        .setCoreConnectionsPerHost(HostDistance.LOCAL, appConfig.concurrentClients)
        .setMaxConnectionsPerHost(HostDistance.LOCAL, appConfig.concurrentClients)
        .setCoreConnectionsPerHost(HostDistance.REMOTE, appConfig.concurrentClients)
        .setMaxConnectionsPerHost(HostDistance.REMOTE, appConfig.concurrentClients);
    cassandra_cluster =
        builder.withLoadBalancingPolicy(getLoadBalancingPolicy())
               .withPoolingOptions(poolingOptions)
               .withQueryOptions(new QueryOptions().setDefaultIdempotence(true))
               .withRetryPolicy(new LoggingRetryPolicy(DefaultRetryPolicy.INSTANCE))
               .build();
    LOG.debug("Connected to cluster: " + cassandra_cluster.getClusterName());
  }
  if (cassandra_session == null) {
    LOG.debug("Creating a session...");
    cassandra_session = cassandra_cluster.connect();
    createKeyspace(cassandra_session, keyspace);
  }
}
 
Example #26
Source File: LocalMachineLoadBalancingPolicy.java    From deep-spark with Apache License 2.0 2 votes vote down vote up
/**
 * Return the HostDistance for the provided host.
 * <p/>
 * This policy consider all nodes as local. This is generally the right
 * thing to do in a single datacenter deployment. If you use multiple
 * datacenter, see {@link com.datastax.driver.core.policies.DCAwareRoundRobinPolicy} instead.
 *
 * @param host the host of which to return the distance of.
 * @return the HostDistance to {@code host}.
 */
@Override
public HostDistance distance(Host host) {
    return HostDistance.LOCAL;
}