@Override protected void before() throws Throwable { dependency.before(); Cluster cluster = Cluster.builder().addContactPoint(getHost()).withPort(getPort()) .withNettyOptions(new NettyOptions() { @Override public void onClusterClose(EventLoopGroup eventLoopGroup) { eventLoopGroup.shutdownGracefully(0, 0, TimeUnit.MILLISECONDS).syncUninterruptibly(); } }).build(); Session session = cluster.newSession(); try { if (requiredVersion != null) { Version cassandraReleaseVersion = CassandraVersion.getReleaseVersion(session); if (cassandraReleaseVersion.isLessThan(requiredVersion)) { throw new AssumptionViolatedException( String.format("Cassandra at %s:%s runs in Version %s but we require at least %s", getHost(), getPort(), cassandraReleaseVersion, requiredVersion)); } } session.execute(String.format("CREATE KEYSPACE IF NOT EXISTS %s \n" + "WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };", keyspaceName)); } finally { session.close(); cluster.close(); } }
builder = builder.withNettyOptions(nettyOptions);
@BeforeMethod(groups = "short") public void setUp() { sCluster = ScassandraCluster.builder().withNodes(4).build(); sCluster.init(); cluster = Cluster.builder() .addContactPoints(sCluster.address(1).getAddress()) .withPort(sCluster.getBinaryPort()) .withLoadBalancingPolicy(lbSpy) .withNettyOptions(nonQuietClusterCloseOptions) .build(); session = cluster.connect(); // Reset invocations before entering test. Mockito.reset(lbSpy); }
.withPort(sCluster.getBinaryPort()) .withLoadBalancingPolicy(new RoundRobinPolicy()) .withNettyOptions(nonQuietClusterCloseOptions) .build();
.withPort(sCluster.getBinaryPort()) .withLoadBalancingPolicy(new WhiteListPolicy(new RoundRobinPolicy(), whiteList)) .withNettyOptions(nonQuietClusterCloseOptions) .build();
Cluster.builder() .addContactPointsWithPorts(new InetSocketAddress("127.0.0.1", 65534)) .withNettyOptions(nonQuietClusterCloseOptions) .build(); try {
.addContactPoints(scassandraCluster.address(1).getAddress()) .withPort(scassandraCluster.getBinaryPort()) .withNettyOptions(nonQuietClusterCloseOptions) .build();
@Test(groups = "short") public void should_not_leak_session_when_wrong_keyspace() throws Exception { // Checking for JAVA-806 channelMonitor = new SocketChannelMonitor(); channelMonitor.reportAtFixedInterval(1, TimeUnit.SECONDS); Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints().get(0)) .withPort(ccm().getBinaryPort()) .withNettyOptions(channelMonitor.nettyOptions()) .build()); cluster.init(); assertThat(cluster.manager.sessions.size()).isEqualTo(0); try { // Should be 1 control connection after initialization. assertOpenConnections(1, cluster); cluster.connect("wrong_keyspace"); fail("Should not have connected to a wrong keyspace"); } catch (InvalidQueryException e) { // ok } assertThat(cluster.manager.sessions.size()).isEqualTo(0); cluster.close(); // Ensure no channels remain open. channelMonitor.stop(); channelMonitor.report(); assertThat(channelMonitor.openChannels(ccm().addressOfNode(1), ccm().addressOfNode(2)).size()) .isEqualTo(0); }
.withPort(sCluster.getBinaryPort()) .withLoadBalancingPolicy(new RoundRobinPolicy()) .withNettyOptions(nonQuietClusterCloseOptions) .build();
@BeforeMethod(groups = "short") public void beforeMethod() { scassandras = ScassandraCluster.builder().withNodes(3).build(); scassandras.init(); cluster = Cluster.builder() .addContactPoints(scassandras.address(1).getAddress()) .withPort(scassandras.getBinaryPort()) .withRetryPolicy(retryPolicy) .withLoadBalancingPolicy(new SortingLoadBalancingPolicy()) .withPoolingOptions( new PoolingOptions() .setCoreConnectionsPerHost(HostDistance.LOCAL, 1) .setMaxConnectionsPerHost(HostDistance.LOCAL, 1) .setHeartbeatIntervalSeconds(0)) .withNettyOptions(nonQuietClusterCloseOptions) // Mark everything as idempotent by default so RetryPolicy is exercised. .withQueryOptions(new QueryOptions().setDefaultIdempotence(true)) .build(); session = cluster.connect(); host1 = TestUtils.findHost(cluster, 1); host2 = TestUtils.findHost(cluster, 2); host3 = TestUtils.findHost(cluster, 3); errors = cluster.getMetrics().getErrorMetrics(); Mockito.reset(retryPolicy); for (Scassandra node : scassandras.nodes()) { node.activityClient().clearAllRecordedActivity(); } }
/** * 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(); } }
@BeforeMethod(groups = "short") public void beforeMethod() { scassandra = ScassandraCluster.builder().withNodes(3).build(); scassandra.init(); queryOptions = new QueryOptions(); loadBalancingPolicy = new SortingLoadBalancingPolicy(); cluster = Cluster.builder() .addContactPoints(scassandra.address(2).getAddress()) .withPort(scassandra.getBinaryPort()) .withLoadBalancingPolicy(loadBalancingPolicy) .withQueryOptions(queryOptions) .withNettyOptions(nonQuietClusterCloseOptions) .build(); session = cluster.connect(); host1 = TestUtils.findHost(cluster, 1); host2 = TestUtils.findHost(cluster, 2); host3 = TestUtils.findHost(cluster, 3); // Make sure there are no prepares for (int host : Lists.newArrayList(1, 2, 3)) { assertThat(scassandra.node(host).activityClient().retrievePreparedStatementPreparations()) .hasSize(0); scassandra.node(host).activityClient().clearAllRecordedActivity(); } }
@BeforeMethod(groups = "short") public void beforeMethod() { scassandras = ScassandraCluster.builder().withNodes(3).build(); scassandras.init(); int speculativeExecutionDelay = 200; loadBalancingPolicy = new SortingLoadBalancingPolicy(); cluster = Cluster.builder() .addContactPoints(scassandras.address(2).getAddress()) .withPort(scassandras.getBinaryPort()) .withLoadBalancingPolicy(loadBalancingPolicy) .withSpeculativeExecutionPolicy( new ConstantSpeculativeExecutionPolicy(speculativeExecutionDelay, 1)) .withQueryOptions(new QueryOptions().setDefaultIdempotence(true)) .withRetryPolicy(new CustomRetryPolicy()) .withNettyOptions(nonQuietClusterCloseOptions) .build(); session = cluster.connect(); host1 = TestUtils.findHost(cluster, 1); host2 = TestUtils.findHost(cluster, 2); host3 = TestUtils.findHost(cluster, 3); errors = cluster.getMetrics().getErrorMetrics(); }
public Cluster.Builder builder() { // Note: nonQuietClusterCloseOptions is used to speed up tests return Cluster.builder() .addContactPoints(sCluster.address(1).getAddress()) .withPort(sCluster.getBinaryPort()) .withNettyOptions(nonQuietClusterCloseOptions); }
CreateClusterAndCheckConnections(CountDownLatch startSignal) { this.startSignal = startSignal; this.cluster = Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withPoolingOptions( new PoolingOptions().setCoreConnectionsPerHost(HostDistance.LOCAL, 1)) .withNettyOptions(channelMonitor.nettyOptions()) .build(); }
private Cluster.Builder builder(LoadBalancingPolicy lbp) { return Cluster.builder() .withNettyOptions(nonQuietClusterCloseOptions) .addContactPoints(sCluster.address(1).getAddress()) .withPort(sCluster.getBinaryPort()) .withLoadBalancingPolicy(lbp); }
private Cluster.Builder builder() { return Cluster.builder() // Close cluster immediately to speed up tests. .withNettyOptions(nonQuietClusterCloseOptions); }
public Cluster.Builder builder() { // Note: nonQuietClusterCloseOptions is used to speed up tests return Cluster.builder() .addContactPoints(sCluster.address(1).getAddress()) .withPort(sCluster.getBinaryPort()) .withNettyOptions(nonQuietClusterCloseOptions); }
@Override public Cluster.Builder createClusterBuilder() { channelMonitor = register(new SocketChannelMonitor()); PoolingOptions poolingOptions = new PoolingOptions().setConnectionsPerHost(HostDistance.LOCAL, 8, 8); return Cluster.builder() .withPoolingOptions(poolingOptions) .withNettyOptions(channelMonitor.nettyOptions()) .withReconnectionPolicy(new ConstantReconnectionPolicy(1000)); }