com.datastax.driver.core.policies.DCAwareRoundRobinPolicy Java Examples

The following examples show how to use com.datastax.driver.core.policies.DCAwareRoundRobinPolicy. 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: CqlCount.java    From cassandra-count with Apache License 2.0 6 votes vote down vote up
private void setup()
throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException,
              CertificateException, UnrecoverableKeyException  {
// Connect to Cassandra
Cluster.Builder clusterBuilder = Cluster.builder()
    .addContactPoint(host)
    .withPort(port)
    .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();
   }
 
Example #2
Source File: DCAwareRoundRobinPolicyFactory.java    From dropwizard-cassandra with Apache License 2.0 6 votes vote down vote up
@Override
public LoadBalancingPolicy build() {
    DCAwareRoundRobinPolicy.Builder builder = DCAwareRoundRobinPolicy.builder();

    if (allowRemoteDCsForLocalConsistencyLevel == Boolean.TRUE) {
        builder.allowRemoteDCsForLocalConsistencyLevel();
    }

    if (localDC != null) {
        builder.withLocalDc(localDC);
    }

    if (usedHostsPerRemoteDC != null) {
        builder.withUsedHostsPerRemoteDc(usedHostsPerRemoteDC);
    }

    return builder.build();
}
 
Example #3
Source File: ClusterManager.java    From scalardb with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
void build() {
  builder =
      Cluster.builder()
          .withClusterName("Scalar Cluster")
          .addContactPoints(config.getContactPoints().toArray(new String[0]))
          .withPort(
              config.getContactPort() == 0 ? DEFAULT_CASSANDRA_PORT : config.getContactPort())
          // .withCompression ?
          // .withPoolingOptions ?
          .withRetryPolicy(DefaultRetryPolicy.INSTANCE)
          .withLoadBalancingPolicy(
              new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().build()));
}
 
Example #4
Source File: UtilsUnitTest.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadBalancingPolicyParsing() throws Exception
{
	String lbPolicyStr = "RoundRobinPolicy()";
	System.out.println(lbPolicyStr);
	assertTrue(Utils.parseLbPolicy(lbPolicyStr) instanceof RoundRobinPolicy);
	System.out.println("====================");
	lbPolicyStr = "TokenAwarePolicy(RoundRobinPolicy())";
	System.out.println(lbPolicyStr);
	assertTrue(Utils.parseLbPolicy(lbPolicyStr) instanceof TokenAwarePolicy);
	System.out.println("====================");
	lbPolicyStr = "DCAwareRoundRobinPolicy(\"dc1\")";
	System.out.println(lbPolicyStr);
	assertTrue(Utils.parseLbPolicy(lbPolicyStr) instanceof DCAwareRoundRobinPolicy);
	System.out.println("====================");
	lbPolicyStr = "TokenAwarePolicy(DCAwareRoundRobinPolicy(\"dc1\"))";
	System.out.println(lbPolicyStr);
	assertTrue(Utils.parseLbPolicy(lbPolicyStr) instanceof TokenAwarePolicy);    	
	System.out.println("====================");
	lbPolicyStr = "TokenAwarePolicy";
	System.out.println(lbPolicyStr);
	assertTrue(Utils.parseLbPolicy(lbPolicyStr)==null);
	System.out.println("====================");
	lbPolicyStr = "LatencyAwarePolicy(TokenAwarePolicy(RoundRobinPolicy()),(double) 10.5,(long) 1,(long) 10,(long)1,10)";    	
	System.out.println(lbPolicyStr);
	assertTrue(Utils.parseLbPolicy(lbPolicyStr) instanceof LatencyAwarePolicy);
	System.out.println("====================");    	    	
	
}
 
Example #5
Source File: DCAwareRoundRobinPolicyFactoryTest.java    From dropwizard-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void buildsPolicyWithNoParams() throws Exception {
    final DCAwareRoundRobinPolicyFactory factory = new DCAwareRoundRobinPolicyFactory();

    final LoadBalancingPolicy policy = factory.build();

    assertThat(policy).isExactlyInstanceOf(DCAwareRoundRobinPolicy.class);
}
 
Example #6
Source File: DCAwareRoundRobinPolicyFactoryTest.java    From dropwizard-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void buildsPolicyWithAllParams() throws Exception {
    final DCAwareRoundRobinPolicyFactory factory = new DCAwareRoundRobinPolicyFactory();
    factory.setLocalDC("dc1");
    factory.setUsedHostsPerRemoteDC(1);
    factory.setAllowRemoteDCsForLocalConsistencyLevel(true);

    final LoadBalancingPolicy policy = factory.build();

    assertThat(policy).isExactlyInstanceOf(DCAwareRoundRobinPolicy.class);
}
 
Example #7
Source File: JavaDriverClient.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public JavaDriverClient(StressSettings settings, String host, int port, EncryptionOptions.ClientEncryptionOptions encryptionOptions)
{
    this.host = host;
    this.port = port;
    this.username = settings.mode.username;
    this.password = settings.mode.password;
    this.authProvider = settings.mode.authProvider;
    this.encryptionOptions = encryptionOptions;
    if (settings.node.isWhiteList)
        whitelist = new WhiteListPolicy(new DCAwareRoundRobinPolicy(), settings.node.resolveAll(settings.port.nativePort));
    else
        whitelist = null;
}
 
Example #8
Source File: MapConfiguredCqlClientFactory.java    From storm-cassandra-cql with Apache License 2.0 5 votes vote down vote up
private void configureLoadBalancingPolicy() {
    final String dataCenterNameConfiguration = (String) configuration.get(TRIDENT_CASSANDRA_LOCAL_DATA_CENTER_NAME);
    if (StringUtils.isNotEmpty(dataCenterNameConfiguration)) {
        final LoadBalancingPolicy loadBalancingPolicy = DCAwareRoundRobinPolicy.builder().withLocalDc(dataCenterNameConfiguration).build();
        builder = builder.withLoadBalancingPolicy(loadBalancingPolicy);
    }
}
 
Example #9
Source File: DatastaxIO.java    From blueflood with Apache License 2.0 5 votes vote down vote up
private static void connect() {
    Set<InetSocketAddress> dbHosts = ioconfig.getUniqueBinaryTransportHostsAsInetSocketAddresses();

    int readTimeoutMaxRetries = ioconfig.getReadTimeoutMaxRetries();
    int writeTimeoutMaxRetries = ioconfig.getWriteTimeoutMaxRetries();
    int unavailableMaxRetries = ioconfig.getUnavailableMaxRetries();

    CodecRegistry codecRegistry = new CodecRegistry();

    cluster = Cluster.builder()
            .withLoadBalancingPolicy(new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().withLocalDc(ioconfig.getDatacenterName()).build(), false))
            .withPoolingOptions(getPoolingOptions())
            .withRetryPolicy(new RetryNTimes(readTimeoutMaxRetries, writeTimeoutMaxRetries, unavailableMaxRetries))
            .withCodecRegistry(codecRegistry)
            .withSocketOptions(getSocketOptions())
            .addContactPointsWithPorts(dbHosts)
            .build();

    QueryLogger queryLogger = QueryLogger.builder()
            .withConstantThreshold(5000)
            .build();

    cluster.register(queryLogger);

    if ( LOG.isDebugEnabled() ) {
        logDebugConnectionInfo();
    }

    try {
        session = cluster.connect( CassandraModel.QUOTED_KEYSPACE );
    }
    catch (NoHostAvailableException e){
        // TODO: figure out how to bubble this up
        throw new RuntimeException(e);
    }
}
 
Example #10
Source File: CassandraIO.java    From beam with Apache License 2.0 4 votes vote down vote up
/** Get a Cassandra cluster using hosts and port. */
private static Cluster getCluster(
    ValueProvider<List<String>> hosts,
    ValueProvider<Integer> port,
    ValueProvider<String> username,
    ValueProvider<String> password,
    ValueProvider<String> localDc,
    ValueProvider<String> consistencyLevel,
    ValueProvider<Integer> connectTimeout,
    ValueProvider<Integer> readTimeout) {

  Cluster.Builder builder =
      Cluster.builder().addContactPoints(hosts.get().toArray(new String[0])).withPort(port.get());

  if (username != null) {
    builder.withAuthProvider(new PlainTextAuthProvider(username.get(), password.get()));
  }

  DCAwareRoundRobinPolicy.Builder dcAwarePolicyBuilder = new DCAwareRoundRobinPolicy.Builder();
  if (localDc != null) {
    dcAwarePolicyBuilder.withLocalDc(localDc.get());
  }

  builder.withLoadBalancingPolicy(new TokenAwarePolicy(dcAwarePolicyBuilder.build()));

  if (consistencyLevel != null) {
    builder.withQueryOptions(
        new QueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevel.get())));
  }

  SocketOptions socketOptions = new SocketOptions();

  builder.withSocketOptions(socketOptions);

  if (connectTimeout != null) {
    socketOptions.setConnectTimeoutMillis(connectTimeout.get());
  }

  if (readTimeout != null) {
    socketOptions.setReadTimeoutMillis(readTimeout.get());
  }

  return builder.build();
}
 
Example #11
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 #12
Source File: DataStaxClusterImpl.java    From usergrid with Apache License 2.0 4 votes vote down vote up
public synchronized Cluster buildCluster(){

        ConsistencyLevel defaultConsistencyLevel;
        try {
            defaultConsistencyLevel = cassandraConfig.getDataStaxReadCl();
        } catch (IllegalArgumentException e){

            logger.error("Unable to parse provided consistency level in property: {}, defaulting to: {}",
                CassandraFig.READ_CL,
                ConsistencyLevel.LOCAL_QUORUM);

            defaultConsistencyLevel = ConsistencyLevel.LOCAL_QUORUM;
        }


        LoadBalancingPolicy loadBalancingPolicy;
        if( !cassandraConfig.getLocalDataCenter().isEmpty() ){

            loadBalancingPolicy = new DCAwareRoundRobinPolicy.Builder()
                .withLocalDc( cassandraConfig.getLocalDataCenter() ).build();
        }else{
            loadBalancingPolicy = new DCAwareRoundRobinPolicy.Builder().build();
        }

        final PoolingOptions poolingOptions = new PoolingOptions()
            .setCoreConnectionsPerHost(HostDistance.LOCAL, cassandraConfig.getConnections())
            .setMaxConnectionsPerHost(HostDistance.LOCAL, cassandraConfig.getConnections())
            .setIdleTimeoutSeconds( cassandraConfig.getPoolTimeout() / 1000 )
            .setPoolTimeoutMillis( cassandraConfig.getPoolTimeout());

        // purposely add a couple seconds to the driver's lower level socket timeouts vs. cassandra timeouts
        final SocketOptions socketOptions = new SocketOptions()
            .setConnectTimeoutMillis( cassandraConfig.getTimeout())
            .setReadTimeoutMillis( cassandraConfig.getTimeout())
            .setKeepAlive(true);

        final QueryOptions queryOptions = new QueryOptions()
            .setConsistencyLevel(defaultConsistencyLevel)
            .setMetadataEnabled(true); // choose whether to have the driver store metadata such as schema info

        Cluster.Builder datastaxCluster = Cluster.builder()
            .withClusterName(cassandraConfig.getClusterName())
            .addContactPoints(cassandraConfig.getHosts().split(","))
            .withMaxSchemaAgreementWaitSeconds(45)
            .withCompression(ProtocolOptions.Compression.LZ4)
            .withLoadBalancingPolicy(loadBalancingPolicy)
            .withPoolingOptions(poolingOptions)
            .withQueryOptions(queryOptions)
            .withSocketOptions(socketOptions)
            .withReconnectionPolicy(Policies.defaultReconnectionPolicy())
            // client side timestamp generation is IMPORTANT; otherwise successive writes are left up to the server
            // to determine the ts and bad network delays, clock sync, etc. can result in bad behaviors
            .withTimestampGenerator(new AtomicMonotonicTimestampGenerator())
            .withProtocolVersion(getProtocolVersion(cassandraConfig.getVersion()));

        // only add auth credentials if they were provided
        if ( !cassandraConfig.getUsername().isEmpty() && !cassandraConfig.getPassword().isEmpty() ){
            datastaxCluster.withCredentials(
                cassandraConfig.getUsername(),
                cassandraConfig.getPassword()
            );
        }


        return datastaxCluster.build();

    }