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

The following examples show how to use com.datastax.driver.core.policies.TokenAwarePolicy. 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: 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 #3
Source File: CassandraDbProvider.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Currently we connect just once and then reuse the connection.
 * We do not bother with closing the connection.
 *
 * It is normal to use one Session per DB. The Session is thread safe.
 */
private void connect() {

    if (cluster == null) {

        log.info("Connecting to Cassandra server on " + this.dbHost + " at port " + this.dbPort);

        // allow fetching as much data as present in the DB
        QueryOptions queryOptions = new QueryOptions();
        queryOptions.setFetchSize(Integer.MAX_VALUE);
        queryOptions.setConsistencyLevel(ConsistencyLevel.ONE);

        cluster = Cluster.builder()
                         .addContactPoint(this.dbHost)
                         .withPort(this.dbPort)
                         .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()))
                         .withReconnectionPolicy(new ExponentialReconnectionPolicy(500, 30000))
                         .withQueryOptions(queryOptions)
                         .withCredentials(this.dbUser, this.dbPassword)
                         .build();

    }

    if (session == null) {

        log.info("Connecting to Cassandra DB with name " + this.dbName);
        session = cluster.connect(dbName);
    }
}
 
Example #4
Source File: ManagedSetupConnection.java    From heroic with Apache License 2.0 5 votes vote down vote up
public AsyncFuture<Connection> construct() {
    AsyncFuture<Session> session = async.call(() -> {


        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)
            .withQueryOptions(queryOptions)
            .withSocketOptions(socketOptions)
            .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()))
            .withoutJMXReporting();

        authentication.accept(cluster);
        poolingOptions.apply(cluster);

        return cluster.build().connect();
    });

    if (configure) {
        session = session.lazyTransform(s -> schema.configure(s).directTransform(i -> s));
    }

    return session.lazyTransform(
        s -> schema.instance(s).directTransform(schema -> new Connection(s, schema)));
}
 
Example #5
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 #6
Source File: TokenAwarePolicyFactoryTest.java    From dropwizard-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void buildsPolicyWithChildPolicy() throws Exception {
    final TokenAwarePolicyFactory factory = new TokenAwarePolicyFactory();
    factory.setSubPolicy(subPolicyFactory);

    final TokenAwarePolicy policy = (TokenAwarePolicy) factory.build();

    assertThat(policy.getChildPolicy()).isSameAs(subPolicy);
}
 
Example #7
Source File: CassandraMetricBatch.java    From monasca-persister with Apache License 2.0 5 votes vote down vote up
public CassandraMetricBatch(Metadata metadata, ProtocolOptions protocol, CodecRegistry codec,
    TokenAwarePolicy lbPolicy, int batchLimit) {
  this.protocol = protocol;
  this.codec = codec;
  this.metadata = metadata;
  this.policy = lbPolicy;
  metricQueries = new HashMap<>();
  this.batchLimit = batchLimit;

  metricQueries = new HashMap<>();
  dimensionQueries = new HashMap<>();
  dimensionMetricQueries = new HashMap<>();
  metricDimensionQueries = new HashMap<>();
  measurementQueries = new HashMap<>();
}
 
Example #8
Source File: PaasPropertiesModule.java    From staash with Apache License 2.0 5 votes vote down vote up
@Provides
@Named("pooledmetacluster")
Cluster providePooledCluster(@Named("staash.cassclient") String clientType,@Named("staash.metacluster") String clustername) {
    if (clientType.equals("cql")) {
    Cluster cluster = Cluster.builder().withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())).addContactPoint(clustername).build();
    return cluster;
    }else {
        return null;
    }
}
 
Example #9
Source File: TestPaasPropertiesModule.java    From staash with Apache License 2.0 5 votes vote down vote up
@Provides
    @Named("pooledmetacluster")
    Cluster providePooledCluster(@Named("paas.cassclient") String clientType,@Named("paas.metacluster") String clustername) {
        if (clientType.equals("cql")) {
        Cluster cluster = Cluster.builder().withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())).addContactPoint(clustername).build();
//        Cluster cluster = Cluster.builder().addContactPoint(clustername).build();
        return cluster;
        }else {
            return null;
        }
    }
 
Example #10
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 #11
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 #12
Source File: TokenAwarePolicyFactory.java    From dropwizard-cassandra with Apache License 2.0 4 votes vote down vote up
@Override
public LoadBalancingPolicy build() {
    return (shuffleReplicas == null)
            ? new TokenAwarePolicy(subPolicy.build())
            : new TokenAwarePolicy(subPolicy.build(), shuffleReplicas);
}
 
Example #13
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 #14
Source File: CassandraCluster.java    From monasca-persister with Apache License 2.0 4 votes vote down vote up
public TokenAwarePolicy getLoadBalancePolicy() {
  return lbPolicy;
}