Search in sources :

Example 21 with ConstantReconnectionPolicy

use of com.datastax.driver.core.policies.ConstantReconnectionPolicy in project java-driver by datastax.

the class ClusterInitTest method should_handle_failing_or_missing_contact_points.

/**
 * Test for JAVA-522: when the cluster and session initialize, if some contact points are behaving badly and
 * causing timeouts, we want to ensure that the driver does not wait multiple times on the same host.
 */
@Test(groups = "short")
public void should_handle_failing_or_missing_contact_points() throws UnknownHostException {
    Cluster cluster = null;
    Scassandra scassandra = null;
    List<FakeHost> failingHosts = Lists.newArrayList();
    try {
        // Simulate a cluster of 5 hosts.
        // - 1 is an actual Scassandra instance that will accept connections:
        scassandra = TestUtils.createScassandraServer();
        scassandra.start();
        int port = scassandra.getBinaryPort();
        // - the remaining 4 are fake servers that will throw connect timeouts:
        for (int i = 2; i <= 5; i++) {
            FakeHost failingHost = new FakeHost(TestUtils.ipOfNode(i), port, THROWING_CONNECT_TIMEOUTS);
            failingHosts.add(failingHost);
            failingHost.start();
        }
        // - we also have a "missing" contact point, i.e. there's no server listening at this address,
        // and the address is not listed in the live host's system.peers
        String missingHostAddress = TestUtils.ipOfNode(6);
        primePeerRows(scassandra, failingHosts);
        logger.info("Environment is set up, starting test");
        long start = System.nanoTime();
        // We want to count how many connections were attempted. For that, we rely on the fact that SocketOptions.getKeepAlive
        // is called in Connection.Factory.newBoostrap() each time we prepare to open a new connection.
        SocketOptions socketOptions = spy(new SocketOptions());
        // Set an "infinite" reconnection delay so that reconnection attempts don't pollute our observations
        ConstantReconnectionPolicy reconnectionPolicy = new ConstantReconnectionPolicy(3600 * 1000);
        // Force 1 connection per pool. Otherwise we can't distinguish a failed pool creation from multiple connection
        // attempts, because pools create their connections in parallel (so 1 pool failure equals multiple connection failures).
        PoolingOptions poolingOptions = new PoolingOptions().setConnectionsPerHost(LOCAL, 1, 1);
        cluster = Cluster.builder().withPort(scassandra.getBinaryPort()).addContactPoints(ipOfNode(1), failingHosts.get(0).address, failingHosts.get(1).address, failingHosts.get(2).address, failingHosts.get(3).address, missingHostAddress).withSocketOptions(socketOptions).withReconnectionPolicy(reconnectionPolicy).withPoolingOptions(poolingOptions).build();
        cluster.connect();
        // For information only:
        long initTimeMs = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS);
        logger.info("Cluster and session initialized in {} ms", initTimeMs);
        // Expect :
        // - 2 connections for the live host (1 control connection + 1 pooled connection)
        // - 1 attempt per failing host (either a control connection attempt or a failed pool creation)
        // - 0 or 1 for the missing host. We can't know for sure because contact points are randomized. If it's tried
        // before the live host there will be a connection attempt, otherwise it will be removed directly because
        // it's not in the live host's system.peers.
        verify(socketOptions, atLeast(6)).getKeepAlive();
        verify(socketOptions, atMost(7)).getKeepAlive();
        assertThat(cluster).host(1).isNotNull().isUp();
        // called and submit a task that marks the host down.
        for (FakeHost failingHost : failingHosts) {
            assertThat(cluster).host(failingHost.address).goesDownWithin(10, TimeUnit.SECONDS);
            Host host = TestUtils.findHost(cluster, failingHost.address);
            // startPeriodicReconnectionAttempt, we add a slight delay here if the future isn't set yet.
            if (host != null && (host.getReconnectionAttemptFuture() == null || host.getReconnectionAttemptFuture().isDone())) {
                logger.warn("Periodic Reconnection Attempt hasn't started yet for {}, waiting 1 second and then checking.", host);
                Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
            }
            assertThat(cluster).host(failingHost.address).isReconnectingFromDown();
        }
        assertThat(TestUtils.findHost(cluster, missingHostAddress)).isNull();
    } finally {
        if (cluster != null)
            cluster.close();
        for (FakeHost fakeHost : failingHosts) fakeHost.stop();
        if (scassandra != null)
            scassandra.stop();
    }
}
Also used : Scassandra(org.scassandra.Scassandra) ConstantReconnectionPolicy(com.datastax.driver.core.policies.ConstantReconnectionPolicy) Test(org.testng.annotations.Test)

Example 22 with ConstantReconnectionPolicy

use of com.datastax.driver.core.policies.ConstantReconnectionPolicy in project spring-cloud-connectors by spring-cloud.

the class CassandraClusterCreatorTest method shouldCreateClusterWithConfig.

@Test
public void shouldCreateClusterWithConfig() throws Exception {
    CassandraServiceInfo info = new CassandraServiceInfo("local", Collections.singletonList("127.0.0.1"), 9142);
    CassandraClusterConfig config = new CassandraClusterConfig();
    config.setCompression(ProtocolOptions.Compression.NONE);
    config.setPoolingOptions(new PoolingOptions().setPoolTimeoutMillis(1234));
    config.setQueryOptions(new QueryOptions());
    config.setProtocolVersion(ProtocolVersion.NEWEST_SUPPORTED);
    config.setLoadBalancingPolicy(new RoundRobinPolicy());
    config.setReconnectionPolicy(new ConstantReconnectionPolicy(1));
    config.setRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE);
    config.setSocketOptions(new SocketOptions());
    Cluster cluster = creator.create(info, config);
    Configuration configuration = cluster.getConfiguration();
    assertThat(configuration.getProtocolOptions().getCompression(), is(config.getCompression()));
    assertThat(configuration.getQueryOptions(), is(config.getQueryOptions()));
    assertThat(configuration.getSocketOptions(), is(config.getSocketOptions()));
    Policies policies = configuration.getPolicies();
    assertThat(policies.getLoadBalancingPolicy(), is(config.getLoadBalancingPolicy()));
    assertThat(policies.getReconnectionPolicy(), is(config.getReconnectionPolicy()));
    assertThat(policies.getRetryPolicy(), is(config.getRetryPolicy()));
}
Also used : Policies(com.datastax.driver.core.policies.Policies) Configuration(com.datastax.driver.core.Configuration) CassandraServiceInfo(org.springframework.cloud.service.common.CassandraServiceInfo) SocketOptions(com.datastax.driver.core.SocketOptions) PoolingOptions(com.datastax.driver.core.PoolingOptions) Cluster(com.datastax.driver.core.Cluster) QueryOptions(com.datastax.driver.core.QueryOptions) RoundRobinPolicy(com.datastax.driver.core.policies.RoundRobinPolicy) ConstantReconnectionPolicy(com.datastax.driver.core.policies.ConstantReconnectionPolicy) Test(org.junit.Test)

Example 23 with ConstantReconnectionPolicy

use of com.datastax.driver.core.policies.ConstantReconnectionPolicy in project ASAP by salmant.

the class SS1 method Fetch_AVG_response_time_metric.

// ///////////////////////////////////////////
public static double Fetch_AVG_response_time_metric(String metric_name, String haproxy_agentid, String event_date) {
    Cluster cluster;
    Session session;
    cluster = Cluster.builder().addContactPoints("194.249.1.175").withCredentials("catascopia_user", "catascopia_pass").withPort(9042).withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE).withReconnectionPolicy(new ConstantReconnectionPolicy(1000L)).build();
    session = cluster.connect("jcatascopiadb");
    haproxy_agentid = haproxy_agentid + ":" + metric_name;
    ResultSet results = session.execute("select value from jcatascopiadb.metric_value_table where metricid=\'" + haproxy_agentid + "\' and event_date=\'" + event_date + "\' ORDER BY event_timestamp DESC LIMIT 1");
    double AVG_response_time_metric = 0.0;
    for (Row row : results) {
        AVG_response_time_metric = Double.parseDouble(row.getString("value"));
    }
    return Math.abs(AVG_response_time_metric);
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Cluster(com.datastax.driver.core.Cluster) Row(com.datastax.driver.core.Row) Session(com.datastax.driver.core.Session) ConstantReconnectionPolicy(com.datastax.driver.core.policies.ConstantReconnectionPolicy)

Example 24 with ConstantReconnectionPolicy

use of com.datastax.driver.core.policies.ConstantReconnectionPolicy in project ASAP by salmant.

the class SS1 method Fetch_request_rate_metric.

// ///////////////////////////////////////////
public static double Fetch_request_rate_metric(String metric_name, String haproxy_agentid, String event_date) {
    Cluster cluster;
    Session session;
    cluster = Cluster.builder().addContactPoints("194.249.1.175").withCredentials("catascopia_user", "catascopia_pass").withPort(9042).withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE).withReconnectionPolicy(new ConstantReconnectionPolicy(1000L)).build();
    session = cluster.connect("jcatascopiadb");
    haproxy_agentid = haproxy_agentid + ":" + metric_name;
    ResultSet results = session.execute("select value from jcatascopiadb.metric_value_table where metricid=\'" + haproxy_agentid + "\' and event_date=\'" + event_date + "\' ORDER BY event_timestamp DESC LIMIT 1");
    double GoodPut = 0.0;
    for (Row row : results) {
        GoodPut = Double.parseDouble(row.getString("value"));
    }
    return Math.abs(GoodPut);
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Cluster(com.datastax.driver.core.Cluster) Row(com.datastax.driver.core.Row) Session(com.datastax.driver.core.Session) ConstantReconnectionPolicy(com.datastax.driver.core.policies.ConstantReconnectionPolicy)

Example 25 with ConstantReconnectionPolicy

use of com.datastax.driver.core.policies.ConstantReconnectionPolicy in project ASAP by salmant.

the class THRES1 method Calculate_AVG.

// ///////////////////////////////////////////
public static double Calculate_AVG(String metric_name, String subid, String event_date) {
    Cluster cluster;
    Session session;
    cluster = Cluster.builder().addContactPoints("194.249.1.175").withCredentials("catascopia_user", "catascopia_pass").withPort(9042).withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE).withReconnectionPolicy(new ConstantReconnectionPolicy(1000L)).build();
    session = cluster.connect("jcatascopiadb");
    ResultSet results = session.execute("SELECT agentid FROM jcatascopiadb.subscription_agents_table WHERE subid=\'" + subid + "\'");
    double AVG = 0.0;
    double count = 0.0;
    for (Row row : results) {
        String AgentIDs = new String();
        AgentIDs = row.getString("agentid");
        AgentIDs = AgentIDs + ":" + metric_name;
        ResultSet results2 = session.execute("select value, toTimestamp(event_timestamp) AS ET from jcatascopiadb.metric_value_table where metricid=\'" + AgentIDs + "\' and event_date=\'" + event_date + "\' ORDER BY event_timestamp DESC LIMIT 1");
        for (Row row2 : results2) {
            AVG = AVG + Double.parseDouble(row2.getString("value"));
            count = count + 1;
        }
    }
    AVG = AVG / count;
    return AVG;
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Cluster(com.datastax.driver.core.Cluster) Row(com.datastax.driver.core.Row) Session(com.datastax.driver.core.Session) ConstantReconnectionPolicy(com.datastax.driver.core.policies.ConstantReconnectionPolicy)

Aggregations

ConstantReconnectionPolicy (com.datastax.driver.core.policies.ConstantReconnectionPolicy)44 Cluster (com.datastax.driver.core.Cluster)29 ResultSet (com.datastax.driver.core.ResultSet)28 Session (com.datastax.driver.core.Session)28 Row (com.datastax.driver.core.Row)26 Test (org.testng.annotations.Test)10 RoundRobinPolicy (com.datastax.driver.core.policies.RoundRobinPolicy)7 Builder (com.datastax.driver.core.Cluster.Builder)3 CacheBuilder (com.google.common.cache.CacheBuilder)3 IOException (java.io.IOException)3 PreparedStatement (com.datastax.driver.core.PreparedStatement)2 BeforeClass (org.junit.BeforeClass)2 Configuration (com.datastax.driver.core.Configuration)1 PoolingOptions (com.datastax.driver.core.PoolingOptions)1 QueryOptions (com.datastax.driver.core.QueryOptions)1 SocketOptions (com.datastax.driver.core.SocketOptions)1 ExponentialReconnectionPolicy (com.datastax.driver.core.policies.ExponentialReconnectionPolicy)1 LimitingLoadBalancingPolicy (com.datastax.driver.core.policies.LimitingLoadBalancingPolicy)1 Policies (com.datastax.driver.core.policies.Policies)1 ReconnectionPolicy (com.datastax.driver.core.policies.ReconnectionPolicy)1