public static Session getClientSession(String hostAddr) { if(REGISTRY.containsKey(hostAddr)) { return REGISTRY.get(hostAddr); } else { Cluster.Builder clientClusterBuilder = new Cluster.Builder() .addContactPoint(hostAddr) .withQueryOptions(new QueryOptions() .setConsistencyLevel(ConsistencyLevel.ONE) .setSerialConsistencyLevel(ConsistencyLevel.LOCAL_SERIAL)) .withoutJMXReporting() .withoutMetrics() .withReconnectionPolicy(new ConstantReconnectionPolicy(RECONNECT_DELAY_IN_MS)); long startTimeInMillis = System.currentTimeMillis(); Cluster clientCluster = clientClusterBuilder.build(); Session clientSession = clientCluster.connect(); LOG.info("Client session established after {} ms.", System.currentTimeMillis() - startTimeInMillis); REGISTRY.putIfAbsent(hostAddr, clientSession); return clientSession; } }
public CassandraConfigDb(List<String> contactPoints, int port) { this.contactPoints = new ArrayList<InetAddress> (contactPoints.size()); for (String contactPoint : contactPoints) { try { this.contactPoints.add(InetAddress.getByName(contactPoint)); } catch (UnknownHostException e) { throw new IllegalArgumentException(e.getMessage()); } } this.port = port; cluster = (new Cluster.Builder()).withPort (this.port) .addContactPoints(this.contactPoints) .withSocketOptions(new SocketOptions().setReadTimeoutMillis(60000).setKeepAlive(true).setReuseAddress(true)) .withLoadBalancingPolicy(new RoundRobinPolicy()) .withReconnectionPolicy(new ConstantReconnectionPolicy(500L)) .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.ONE)) .build (); session = cluster.newSession(); preparedStatements = new ConcurrentHashMap<StatementName, PreparedStatement> (); prepareStatementCreateLock = new Object(); }
/** * Get a Cassandra cluster using hosts and port. */ private Cluster getCluster(List<String> hosts, int port, String username, String password, String localDc, String consistencyLevel) { Cluster.Builder builder = Cluster.builder() .addContactPoints(hosts.toArray(new String[0])) .withPort(port); if (username != null) { builder.withAuthProvider(new PlainTextAuthProvider(username, password)); } if (localDc != null) { builder.withLoadBalancingPolicy( new TokenAwarePolicy(new DCAwareRoundRobinPolicy.Builder().withLocalDc(localDc).build())); } else { builder.withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())); } if (consistencyLevel != null) { builder.withQueryOptions( new QueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevel))); } return builder.build(); }
private void configureQueryOptions() { final String consistencyConfiguration = (String) configuration.get(TRIDENT_CASSANDRA_CONSISTENCY); final String serialConsistencyConfiguration = (String) configuration.get(TRIDENT_CASSANDRA_SERIAL_CONSISTENCY); final QueryOptions queryOptions = builder.getConfiguration().getQueryOptions(); if (StringUtils.isNotEmpty(consistencyConfiguration)) { queryOptions.setConsistencyLevel(ConsistencyLevel.valueOf(consistencyConfiguration)); } if (StringUtils.isNotEmpty(serialConsistencyConfiguration)) { queryOptions.setSerialConsistencyLevel(ConsistencyLevel.valueOf(serialConsistencyConfiguration)); } builder = builder.withQueryOptions(queryOptions); }
protected void createCache(Map<String, String> mapParams) throws Exception { final Cluster.Builder bluePrint = Cluster.builder().withClusterName("BluePrint") .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM)) .withRetryPolicy(DefaultRetryPolicy.INSTANCE) .withLoadBalancingPolicy(new TokenAwarePolicy(new DCAwareRoundRobinPolicy())) .addContactPoint(mapParams.get("cassandra.server.ip.address")).withPort(9042); cache1 = mache(String.class, CassandraTestEntity.class) .cachedBy(guava()) .storedIn(cassandra() .withCluster(bluePrint) .withKeyspace(mapParams.get("keyspace.name")) .withSchemaOptions(SchemaOptions.CREATE_SCHEMA_IF_NEEDED) .build()) .withMessaging(kafka() .withKafkaMqConfig(KafkaMqConfigBuilder.builder() .withZkHost(mapParams.get("kafka.connection")) .build()) .withTopic(mapParams.get("kafka.topic")) .build()) .macheUp(); }
/** * Creates the query options for this factory. * * @param configuration the configuration. * @return the query options for this factory. */ private QueryOptions configureQueryOptions(final Configuration<Map<String, Object>> configuration) { final QueryOptions queryOptions = new QueryOptions(); queryOptions.setConsistencyLevel( ConsistencyLevel.valueOf( configuration.getParameter( "consistency_level", "ONE"))); queryOptions.setSerialConsistencyLevel( ConsistencyLevel.valueOf( configuration.getParameter( "serial_consistency_level", "ONE"))); queryOptions.setFetchSize(configuration.getParameter( "fetch_size", 1000)); return queryOptions; }
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(); }
@BeforeSuite public static void initSuite() { Cluster cluster = Cluster.builder() .addContactPoints("127.0.0.01") .withQueryOptions(new QueryOptions().setRefreshSchemaIntervalMillis(0)) .build(); String keyspace = System.getProperty("keyspace", "hawkulartest"); session = cluster.connect("system"); rxSession = new RxSessionImpl(session); boolean resetdb = Boolean.valueOf(System.getProperty("resetdb", "true")); SchemaService schemaService = new SchemaService(); schemaService.run(session, keyspace, resetdb); session.execute("USE " + keyspace); jobsService = new JobsService(rxSession); findFinishedJobs = session.prepare("SELECT job_id FROM finished_jobs_idx WHERE time_slice = ?"); getActiveTimeSlices = session.prepare("SELECT distinct time_slice FROM active_time_slices"); addActiveTimeSlice = session.prepare("INSERT INTO active_time_slices (time_slice) VALUES (?)"); }
@BeforeSuite public static void initSuite() { String nodeAddresses = System.getProperty("nodes", "127.0.0.1"); Cluster cluster = new Cluster.Builder() .addContactPoints(nodeAddresses.split(",")) .withQueryOptions(new QueryOptions().setRefreshSchemaIntervalMillis(0)) .build(); session = cluster.connect(); rxSession = new RxSessionImpl(session); SchemaService schemaService = new SchemaService(); schemaService.run(session, getKeyspace(), Boolean.valueOf(System.getProperty("resetdb", "true"))); session.execute("USE " + getKeyspace()); metricRegistry = new HawkularMetricRegistry(); metricRegistry.setMetricNameService(new MetricNameService()); }
private Cluster.Builder populateQueryOptions(Properties properties, Cluster.Builder builder) { String consistencyLevelProp = properties.getProperty(CassandraStoreParameters.CONSISTENCY_LEVEL); String serialConsistencyLevelProp = properties.getProperty(CassandraStoreParameters.SERIAL_CONSISTENCY_LEVEL); String fetchSize = properties.getProperty(CassandraStoreParameters.FETCH_SIZE); QueryOptions options = new QueryOptions(); if (consistencyLevelProp != null) { options.setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevelProp)); } if (serialConsistencyLevelProp != null) { options.setSerialConsistencyLevel(ConsistencyLevel.valueOf(serialConsistencyLevelProp)); } if (fetchSize != null) { options.setFetchSize(Integer.parseInt(fetchSize)); } return builder.withQueryOptions(options); }
private Builder populateQueryOptions(Map<String, String> properties, Builder builder) { String consistencyLevelProp = properties.get(DBConstants.Cassandra.CONSISTENCY_LEVEL); String serialConsistencyLevelProp = properties.get(DBConstants.Cassandra.SERIAL_CONSISTENCY_LEVEL); String fetchSize = properties.get(DBConstants.Cassandra.FETCH_SIZE); QueryOptions options = new QueryOptions(); if (consistencyLevelProp != null) { options.setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevelProp)); } if (serialConsistencyLevelProp != null) { options.setSerialConsistencyLevel(ConsistencyLevel.valueOf(serialConsistencyLevelProp)); } if (fetchSize != null) { options.setFetchSize(Integer.parseInt(fetchSize)); } return builder.withQueryOptions(options); }
private QueryOptions getQueryOptions(CassandraProperties properties) { QueryOptions options = new QueryOptions(); if (properties.getConsistencyLevel() != null) { options.setConsistencyLevel(properties.getConsistencyLevel()); } if (properties.getSerialConsistencyLevel() != null) { options.setSerialConsistencyLevel(properties.getSerialConsistencyLevel()); } options.setFetchSize(properties.getFetchSize()); return options; }
/** * 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); } }
private static QueryOptions getQueryOptions(CassandraProperties properties) { QueryOptions options = new QueryOptions(); if (properties.getConsistencyLevel() != null) { options.setConsistencyLevel(properties.getConsistencyLevel()); } if (properties.getSerialConsistencyLevel() != null) { options.setSerialConsistencyLevel(properties.getSerialConsistencyLevel()); } options.setFetchSize(properties.getFetchSize()); return options; }
private QueryOptions getQueryOptions() { QueryOptions options = new QueryOptions(); if (eventStoreConfig.getConsistencyLevel() != null) { options.setConsistencyLevel(eventStoreConfig.getConsistencyLevel()); } if (eventStoreConfig.getSerialConsistencyLevel() != null) { options.setSerialConsistencyLevel(eventStoreConfig.getSerialConsistencyLevel()); } options.setFetchSize(eventStoreConfig.getFetchSize()); return options; }
/** * initConnection * tries connect to one of cassandra instances defined in CassandraProperties * @throws Exception */ private synchronized void initConnection () throws Exception { for (int i = 0; i < this.topology.size(); i ++) { for (int j = 0; j < this.connectRetries; j ++) { try { this.cluster = Cluster.builder() .addContactPoint(this.topology.get(i).getIp() ) .withQueryOptions( new QueryOptions().setFetchSize(this.fetchSize) ) .build(); this.cluster.init(); this.session = this.cluster.connect(); reconnectFlag.set(false); return; } catch (Exception e) { if (i == (this.topology.size() - 1) && j == (this.connectRetries - 1) ) { reconnectFlag.set(false); throw e; } } } } }
private QueryOptions getQueryOptions() { QueryOptions options = new QueryOptions(); CassandraProperties properties = this.properties; if (properties.getConsistencyLevel() != null) { options.setConsistencyLevel(properties.getConsistencyLevel()); } if (properties.getSerialConsistencyLevel() != null) { options.setSerialConsistencyLevel(properties.getSerialConsistencyLevel()); } options.setFetchSize(properties.getFetchSize()); return options; }
public static void dropTestKeyspace( String keyspace, String[] hosts, int port ) { Cluster.Builder builder = Cluster.builder(); for ( String host : hosts ) { builder = builder.addContactPoint( host ).withPort( port ); } final QueryOptions queryOptions = new QueryOptions().setConsistencyLevel( ConsistencyLevel.LOCAL_QUORUM ); builder.withQueryOptions( queryOptions ); Cluster cluster = builder.build(); Session session = cluster.connect(); logger.info("Dropping test keyspace: {}", keyspace); session.execute( "DROP KEYSPACE IF EXISTS " + keyspace ); }
@Override protected Cluster buildCluster(Cluster.Builder builder) { return builder .addContactPointsWithPorts(new InetSocketAddress(HOST, PORT)) .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.ONE).setSerialConsistencyLevel(ConsistencyLevel.LOCAL_SERIAL)) .withoutJMXReporting() .withoutMetrics().build(); }
@Override protected Cluster buildCluster(Cluster.Builder builder) { return builder .addContactPoint("127.0.0.1") .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.ONE).setSerialConsistencyLevel(ConsistencyLevel.LOCAL_SERIAL)) .withoutJMXReporting() .withoutMetrics().build(); }
private static void overrideQueryOptions(CassandraFactory cassandraFactory) { // all INSERT and DELETE stmt prepared in this class are idempoten if (cassandraFactory.getQueryOptions().isPresent() && ConsistencyLevel.LOCAL_ONE != cassandraFactory.getQueryOptions().get().getConsistencyLevel()) { LOG.warn("Customization of cassandra's queryOptions is not supported and will be overridden"); } cassandraFactory.setQueryOptions(java.util.Optional.of(new QueryOptions().setDefaultIdempotence(true))); }
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(); }
private static QueryOptions getReadQueryOptions(Configuration conf) { String CL = ConfigHelper.getReadConsistencyLevel(conf); Optional<Integer> fetchSize = getInputPageRowSize(conf); QueryOptions queryOptions = new QueryOptions(); if (CL != null && !CL.isEmpty()) queryOptions.setConsistencyLevel(com.datastax.driver.core.ConsistencyLevel.valueOf(CL)); if (fetchSize.isPresent()) queryOptions.setFetchSize(fetchSize.get()); return queryOptions; }
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); }); }); }
public Cluster.Builder getClusterBuilder() { final List<InetSocketAddress> sockets = new ArrayList<InetSocketAddress>(); for (String host : hosts) { if(StringUtils.contains(host, ":")) { String hostParts [] = StringUtils.split(host, ":"); sockets.add(new InetSocketAddress(hostParts[0], Integer.valueOf(hostParts[1]))); LOG.debug("Connecting to [" + host + "] with port [" + hostParts[1] + "]"); } else { sockets.add(new InetSocketAddress(host, ProtocolOptions.DEFAULT_PORT)); LOG.debug("Connecting to [" + host + "] with port [" + ProtocolOptions.DEFAULT_PORT + "]"); } } Cluster.Builder builder = Cluster.builder().addContactPointsWithPorts(sockets).withCompression(compression); QueryOptions queryOptions = new QueryOptions(); queryOptions.setConsistencyLevel(clusterConsistencyLevel); queryOptions.setSerialConsistencyLevel(serialConsistencyLevel); builder = builder.withQueryOptions(queryOptions); if (StringUtils.isNotEmpty(clusterName)) { builder = builder.withClusterName(clusterName); } return builder; }
public static boolean isClusterActive(){ try{ Builder builder = Cluster.builder().withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.QUORUM).setSerialConsistencyLevel(ConsistencyLevel.LOCAL_SERIAL)); cluster = builder.addContactPoint("127.0.0.1").build(); session = cluster.connect(); return true; } catch(Exception e){ return false; } }
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(); }
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(); }
static Cluster newCluster() { return Cluster.builder().addContactPoint("127.0.0.1") // long read timeout is sometimes needed on slow travis ci machines .withSocketOptions(new SocketOptions().setReadTimeoutMillis(30000)) // let driver know that only idempotent queries are used so it will retry on timeout .withQueryOptions(new QueryOptions().setDefaultIdempotence(true)) .build(); }
private static QueryOptions getQueryOptions() { QueryOptions queryOptions = new QueryOptions(); Method setDefaultIdempotenceMethod; try { setDefaultIdempotenceMethod = QueryOptions.class.getMethod("setDefaultIdempotence", boolean.class); setDefaultIdempotenceMethod.invoke(queryOptions, true); } catch (Exception e) { // early version of driver } return queryOptions; }
public CqlSession(final String nodes, final int port, final String keyspace, final SocketOptions socketOptions, final RetryPolicy retryPolicy, final QueryOptions queryOptions, final LoadBalancingPolicy loadBalancingPolicy, final int maxConnectionsPerHost, final MetricFactory metricFactory) { // this is temp. to reuse current hosts properties: final Iterable<String> nodesIter = Splitter.on(",").split(nodes); final String[] nodesArr = Iterables.toArray( StreamSupport.stream(nodesIter.spliterator(), false).map(input -> { if (input == null) return null; final int idx = input.lastIndexOf(":"); return input.substring(0, idx); }).collect(Collectors.toList()), String.class); /*PoolingOptions poolingOptions = new PoolingOptions(); poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, maxConnectionsPerHost); poolingOptions.setMaxConnectionsPerHost(HostDistance.REMOTE, maxConnectionsPerHost);*/ final Cluster cluster = Cluster.builder(). withPort(port). withSocketOptions(socketOptions). withQueryOptions(queryOptions). withLoadBalancingPolicy(loadBalancingPolicy). // withPoolingOptions(poolingOptions). addContactPoints(nodesArr).build(); //cluster.init(); this.session = cluster.connect(keyspace); this.retryPolicy = Preconditions.checkNotNull(retryPolicy); this.metricFactory = Preconditions.checkNotNull(metricFactory); }