.withProtocolVersion(V3) .withClusterName("TestCluster") .addContactPointsWithPorts(ImmutableList.of( new InetSocketAddress(HOST, PORT))) .withMaxSchemaAgreementWaitSeconds(30);
/** * Create new cassandra cluster using builder. */ @PostConstruct public void init() { Cluster.Builder builder = Cluster.builder() .addContactPointsWithPorts(parseNodeList(nodeList)) .withClusterName(clusterName) .withSocketOptions(clientOptions.getSocketOptions()); if (!useJmx) { builder.withoutJMXReporting(); } if (disableMetrics) { builder.withoutMetrics(); } if (useCredentials) { builder.withCredentials(username, password); } if (useSsl) { builder.withSSL(); } builder.withQueryOptions(clientOptions.getQueryOptions()); builder.withCompression(parseCompression(compression)); cluster = builder.build(); }
builder = builder.addContactPointsWithPorts(contactPointsWithPorts);
@Test(groups = "short") public void should_init_cluster_and_session_if_needed() throws Exception { // For this test we need an uninitialized cluster, so we can't reuse the one provided by the // parent class. Rebuild a new one with the same (unique) host. Host host = cluster().getMetadata().allHosts().iterator().next(); Cluster cluster2 = register( Cluster.builder() .addContactPointsWithPorts(Lists.newArrayList(host.getSocketAddress())) .build()); try { Session session2 = cluster2.newSession(); // Neither cluster2 nor session2 are initialized at this point assertThat(cluster2.manager.metadata).isNull(); ResultSetFuture future = session2.executeAsync("select release_version from system.local"); Row row = Uninterruptibles.getUninterruptibly(future).one(); assertThat(row.getString(0)).isNotEmpty(); } finally { cluster2.close(); } }
private void configureHosts() { final String hostConfiguration = (String) configuration.get(TRIDENT_CASSANDRA_CQL_HOSTS); final String[] hosts = hostConfiguration.split(","); final List<InetSocketAddress> sockets = new ArrayList<InetSocketAddress>(); for (final String host : hosts) { if(StringUtils.contains(host, ":")) { final String hostParts [] = StringUtils.split(host, ":"); sockets.add(new InetSocketAddress(hostParts[0], Integer.valueOf(hostParts[1]))); LOG.debug("Configuring [" + host + "] with port [" + hostParts[1] + "]"); } else { sockets.add(new InetSocketAddress(host, ProtocolOptions.DEFAULT_PORT)); LOG.debug("Configuring [" + host + "] with port [" + ProtocolOptions.DEFAULT_PORT + "]"); } } builder = builder.addContactPointsWithPorts(sockets); }
@Test(groups = "short", expectedExceptions = InvalidQueryException.class) public void should_not_use_keyspace_if_set_and_protocol_does_not_support() { ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion(); while (ProtocolFeature.KEYSPACE_ON_QUERY.isSupportedBy(protocolVersion)) { // Downgrade until we hit a protocol version that doesn't support keyspace on query. protocolVersion = protocolVersion.getLowerSupported(); } Cluster cluster = createClusterBuilderNoDebouncing() .addContactPointsWithPorts(getContactPointsWithPorts()) .withNettyOptions(TestUtils.nonQuietClusterCloseOptions) .withProtocolVersion(protocolVersion) .build(); queryWithKeyspaceOnStatement(cluster, keyspace); }
@Test(groups = "short", expectedExceptions = InvalidQueryException.class) public void should_not_use_keyspace_if_set_and_protocol_does_not_support() { ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion(); while (ProtocolFeature.KEYSPACE_ON_QUERY.isSupportedBy(protocolVersion)) { // Downgrade until we hit a protocol version that doesn't support keyspace on query. protocolVersion = protocolVersion.getLowerSupported(); } Cluster cluster = createClusterBuilderNoDebouncing() .addContactPointsWithPorts(getContactPointsWithPorts()) .withNettyOptions(TestUtils.nonQuietClusterCloseOptions) .withProtocolVersion(protocolVersion) .build(); insertWithKeyspaceOnStatement(cluster, keyspace); }
@Override public Cluster build( CassandraEntityStoreConfiguration config ) { this.config = config; String clusterName = clusterName( config ); Collection<InetSocketAddress> connectionPoints = cassandraConnectionPoints(); Cluster.Builder builder = Cluster.builder() .withClusterName( clusterName ) .addContactPointsWithPorts( connectionPoints ) .withCredentials( username(), password() ); builder = customConfiguration( builder ); return builder.build(); }
private void createTableSpace() { if (cluster == null) { cluster = Cluster.builder().addContactPointsWithPorts(contactPoints).build(); log.info("Connected Cassandra to cluster: " + cluster.getMetadata().getClusterName()); session = cluster.connect(); } session.execute("CREATE KEYSPACE IF NOT EXISTS " + serviceName + " WITH replication " + "= {'class':'NetworkTopologyStrategy', '"+this.dataCenter+"': "+this.replicationFactor+"};"); session.execute("CREATE TABLE IF NOT EXISTS " + serviceName + ".igep (" + "id text PRIMARY KEY," + "host text," + "port int);"); }
public void init() { if (cluster == null) { cluster = Cluster.builder().addContactPointsWithPorts(contactPoints).build(); SDFSLogger.getLog().info("Connected Cassandra to cluster: " + cluster.getMetadata().getClusterName()); session = cluster.connect(); } session.execute("CREATE KEYSPACE IF NOT EXISTS " + serviceName + " WITH replication " + "= {'class':'NetworkTopologyStrategy', '"+this.dataCenter+"': "+this.replicationFactor+"};"); }
.addContactPointsWithPorts(this.getContactPointsWithPorts()) .build(); try {
.addContactPointsWithPorts(sCluster.address(1)) .withNettyOptions(nonQuietClusterCloseOptions);
register( Cluster.builder() .addContactPointsWithPorts(ccm().addressOfNode(1)) .withPort(ccm().getBinaryPort()) .withLoadBalancingPolicy(loadBalancingPolicy)
Cluster cluster = Cluster.builder() .addContactPointsWithPorts(new InetSocketAddress("127.0.0.1", 65534)) .withNettyOptions(nonQuietClusterCloseOptions) .build();
Cluster cluster = Cluster.builder() .addContactPointsWithPorts(sCluster.address(3)) .withPort(sCluster.getBinaryPort()) .withLoadBalancingPolicy(new WhiteListPolicy(new RoundRobinPolicy(), whiteList))
/** * Validates that a Cluster that was never able to successfully establish connection a session can * be closed properly. * * @test_category connection * @expected_result Cluster closes within 1 second. */ @Test(groups = "short") public void should_be_able_to_close_cluster_that_never_successfully_connected() throws Exception { Cluster cluster = Cluster.builder() .addContactPointsWithPorts(new InetSocketAddress("127.0.0.1", 65534)) .withNettyOptions(nonQuietClusterCloseOptions) .build(); try { cluster.connect(); fail("Should not have been able to connect."); } catch (NoHostAvailableException e) { // Expected. CloseFuture closeFuture = cluster.closeAsync(); try { closeFuture.get(1, TimeUnit.SECONDS); } catch (TimeoutException e1) { fail("Close Future did not complete quickly."); } } finally { cluster.close(); } }
/** * Ensures that if cluster does not have the system.peers_v2 table that cluster initialization * still succeeds. * * @jira_ticket JAVA-1388 * @since 3.6.0 */ @Test(groups = "short") @CCMConfig(createCcm = false) public void should_connect_when_peers_v2_table_not_present() { ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(5).withPeersV2(false).build(); Cluster cluster = Cluster.builder() .addContactPointsWithPorts(sCluster.address(1)) .withNettyOptions(nonQuietClusterCloseOptions) .withPort(sCluster.getBinaryPort()) .build(); try { sCluster.init(); cluster.connect(); assertThat(cluster.getMetadata().getAllHosts()).hasSize(5); } finally { cluster.close(); sCluster.stop(); } }
@Test(groups = "short") public void should_init_cluster_and_session_if_needed() throws Exception { // For this test we need an uninitialized cluster, so we can't reuse the one provided by the // parent class. Rebuild a new one with the same (unique) host. Host host = cluster().getMetadata().allHosts().iterator().next(); Cluster cluster2 = register( Cluster.builder() .addContactPointsWithPorts(Lists.newArrayList(host.getSocketAddress())) .build()); try { Session session2 = cluster2.newSession(); // Neither cluster2 nor session2 are initialized at this point assertThat(cluster2.manager.metadata).isNull(); ResultSetFuture future = session2.executeAsync("select release_version from system.local"); Row row = Uninterruptibles.getUninterruptibly(future).one(); assertThat(row.getString(0)).isNotEmpty(); } finally { cluster2.close(); } }
/** * Uses a Cluster.Builder to create a Cassandra cluster reference using the given parameters * * @param contactPoints The contact points (hostname:port list of Cassandra nodes) * @param sslContext The SSL context (used for secure connections) * @param username The username for connection authentication * @param password The password for connection authentication * @return A reference to the Cluster object associated with the given Cassandra configuration */ protected Cluster createCluster(List<InetSocketAddress> contactPoints, SSLContext sslContext, String username, String password) { Cluster.Builder builder = Cluster.builder().addContactPointsWithPorts(contactPoints); if (sslContext != null) { JdkSSLOptions sslOptions = JdkSSLOptions.builder() .withSSLContext(sslContext) .build(); builder = builder.withSSL(sslOptions); } if (username != null && password != null) { builder = builder.withCredentials(username, password); } return builder.build(); }
private Cluster createCluster(List<InetSocketAddress> contactPoints, SSLContext sslContext, String username, String password) { Cluster.Builder builder = Cluster.builder().addContactPointsWithPorts(contactPoints); if (sslContext != null) { JdkSSLOptions sslOptions = JdkSSLOptions.builder() .withSSLContext(sslContext) .build(); builder = builder.withSSL(sslOptions); } if (username != null && password != null) { builder = builder.withCredentials(username, password); } return builder.build(); } }