@Test public void testRetryPolicyParsing() throws Exception { String retryPolicyStr = "DefaultRetryPolicy"; System.out.println(retryPolicyStr); assertTrue(Utils.parseRetryPolicy(retryPolicyStr) instanceof DefaultRetryPolicy); System.out.println("===================="); retryPolicyStr = "DowngradingConsistencyRetryPolicy"; System.out.println(retryPolicyStr); assertTrue(Utils.parseRetryPolicy(retryPolicyStr) instanceof DowngradingConsistencyRetryPolicy); System.out.println("===================="); retryPolicyStr = "FallthroughRetryPolicy"; System.out.println(retryPolicyStr); assertTrue(Utils.parseRetryPolicy(retryPolicyStr) instanceof FallthroughRetryPolicy); System.out.println("===================="); }
/** * Derive the retry policy from the given string to the implementation. Defaults to {@link DefaultRetryPolicy}. * * @param retryPolicy * The name of the retry policy to use. Can be "downgrading" {@link DowngradingConsistencyRetryPolicy} or * "fallthrough" {@link FallthroughRetryPolicy}. Everything else defaults to * Policies.defaultRetryPolicy() */ public void setRetryPolicy(String retryPolicy) { switch(retryPolicy) { case "downgrading": setRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE); break; case "fallthrough": setRetryPolicy(FallthroughRetryPolicy.INSTANCE); break; default: setRetryPolicy(Policies.defaultRetryPolicy()); break; } }
@Provides @Singleton Cluster provideCluster() { try { Cluster cluster = Cluster.builder() .addContactPointsWithPorts(Arrays.asList( // new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9042) // mvn cassandra:run + nodetool enablebinary new InetSocketAddress(InetAddress.getByName("localhost"), 9142) // cassandraunit )) .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE) .withReconnectionPolicy(new ExponentialReconnectionPolicy(100L, 5000L)) .build(); Metadata metadata = cluster.getMetadata(); LOGGER.info("Connected to cluster: '{}'", metadata.getClusterName()); metadata.getAllHosts() .forEach(host -> LOGGER.info("Datacenter: '{}'; Host: '{}'; Rack: '{}'", new Object[] { host.getDatacenter(), host.getAddress(), host.getRack() }) ); return cluster; } catch (UnknownHostException e) { LOGGER.error("Can't connect to Cassandra", e); return null; } }
private Builder populateRetrytPolicy(Map<String, String> properties, Builder builder) throws DataServiceFault { String retryPolicy = properties.get(DBConstants.Cassandra.RETRY_POLICY); if (retryPolicy != null) { if ("DefaultRetryPolicy".equals(retryPolicy)) { builder = builder.withRetryPolicy(DefaultRetryPolicy.INSTANCE); } else if ("DowngradingConsistencyRetryPolicy".equals(retryPolicy)) { builder = builder.withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE); } else if ("FallthroughRetryPolicy".equals(retryPolicy)) { builder = builder.withRetryPolicy(FallthroughRetryPolicy.INSTANCE); } else if ("LoggingDefaultRetryPolicy".equals(retryPolicy)) { builder = builder.withRetryPolicy(new LoggingRetryPolicy(DefaultRetryPolicy.INSTANCE)); } else if ("LoggingDowngradingConsistencyRetryPolicy".equals(retryPolicy)) { builder = builder.withRetryPolicy(new LoggingRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)); } else if ("LoggingFallthroughRetryPolicy".equals(retryPolicy)) { builder = builder.withRetryPolicy(new LoggingRetryPolicy(FallthroughRetryPolicy.INSTANCE)); } else { throw new DataServiceFault("Invalid Cassandra retry policy: " + retryPolicy); } } return builder; }
public MetaStoreClient(String... contactPoints) { if (contactPoints.length == 0) throw new RuntimeException("No contact points specified"); cluster = Cluster.builder() .addContactPoints(contactPoints) .withClusterName(Schema.CLUSTER) .withLoadBalancingPolicy(new RoundRobinPolicy()) .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE) .withReconnectionPolicy(new ExponentialReconnectionPolicy(100, 10000)) .withoutMetrics() .build(); }
private Cluster getNewCluster(String cassandraNodes) { return Cluster.builder() .withoutJMXReporting() .withoutMetrics() .addContactPoints(cassandraNodes.split(",")) .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE) .withReconnectionPolicy(new ExponentialReconnectionPolicy(100L, TimeUnit.MINUTES.toMillis(5))) .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())) .build(); }
@Test public void returnsDowngradingConsistencyRetryPolicyInstance() throws Exception { final DowngradingConsistencyRetryPolicyFactory factory = new DowngradingConsistencyRetryPolicyFactory(); final DowngradingConsistencyRetryPolicy policy = (DowngradingConsistencyRetryPolicy) factory.build(); assertThat(policy).isSameAs(DowngradingConsistencyRetryPolicy.INSTANCE); }
@Provides @Singleton Cluster provideCluster() { Cluster cluster = Cluster.builder() .addContactPoints("127.0.0.1") .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE) .withReconnectionPolicy(new ExponentialReconnectionPolicy(100L, 5000L)) .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())) .build(); Metadata metadata = cluster.getMetadata(); LOGGER.info("Connected to cluster: '{}'", metadata.getClusterName()); metadata.getAllHosts() .forEach(host -> LOGGER.info("Datacenter: '{}'; Host: '{}'; Rack: '{}'", new Object[] { host.getDatacenter(), host.getAddress(), host.getRack() }) ); return cluster; }
private Cluster.Builder populateRetrytPolicy(Properties properties, Cluster.Builder builder) { String retryPolicy = properties.getProperty(CassandraStoreParameters.RETRY_POLICY); if (retryPolicy != null) { switch (retryPolicy) { case "DefaultRetryPolicy": builder = builder.withRetryPolicy(DefaultRetryPolicy.INSTANCE); break; case "DowngradingConsistencyRetryPolicy": builder = builder.withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE); break; case "FallthroughRetryPolicy": builder = builder.withRetryPolicy(FallthroughRetryPolicy.INSTANCE); break; case "LoggingDefaultRetryPolicy": builder = builder.withRetryPolicy(new LoggingRetryPolicy(DefaultRetryPolicy.INSTANCE)); break; case "LoggingDowngradingConsistencyRetryPolicy": builder = builder.withRetryPolicy(new LoggingRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)); break; case "LoggingFallthroughRetryPolicy": builder = builder.withRetryPolicy(new LoggingRetryPolicy(FallthroughRetryPolicy.INSTANCE)); break; default: LOG.error("Unsupported retry policy : {} ", retryPolicy); break; } } return builder; }
@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())); }
private static Cluster build(List<String> addresses, int port) { Cluster.Builder builder = Cluster.builder(); for (String address : addresses) { builder.addContactPoint(address); } builder .withPort(port) .withCompression(Compression.LZ4) .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE) .withLoadBalancingPolicy(LatencyAwarePolicy.builder(new RoundRobinPolicy()).build()); Cluster cluster = builder.build(); try { // Attempt to init the cluster to make sure it's usable. I'd prefer to remove this and leave it on the // client to retry when the connect method throws an exception. cluster.init(); return cluster; } catch(NoHostAvailableException e) { LOGGER.warn("Unable to connect to Cassandra, will retry contact points next time", cluster, e); cluster = builder.build(); cluster.init(); } return cluster; }
@Override public RetryPolicy build() { return DowngradingConsistencyRetryPolicy.INSTANCE; }
public CassandraService(StoreConfiguration storeConfiguration) { String query = "SELECT time, data FROM " + storeConfiguration.getKeyspace() + "." + storeConfiguration.getColumnFamily() + " where path = ? and tenant = ? and period = ? and rollup = ? and time >= ? and time <= ? order by time"; SocketOptions socketOptions = new SocketOptions() .setReceiveBufferSize(1024 * 1024) .setSendBufferSize(1024 * 1024) .setTcpNoDelay(false) .setReadTimeoutMillis((int) (storeConfiguration.getReadTimeout() * 1000)) .setConnectTimeoutMillis((int) (storeConfiguration.getConnectTimeout() * 1000)); PoolingOptions poolingOptions = new PoolingOptions(); poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, storeConfiguration.getMaxConnections()); poolingOptions.setMaxConnectionsPerHost(HostDistance.REMOTE, storeConfiguration.getMaxConnections()); poolingOptions.setMaxRequestsPerConnection(HostDistance.REMOTE, storeConfiguration.getMaxRequests()); poolingOptions.setMaxRequestsPerConnection(HostDistance.LOCAL, storeConfiguration.getMaxRequests()); Cluster.Builder builder = Cluster.builder() .withSocketOptions(socketOptions) .withCompression(ProtocolOptions.Compression.LZ4) .withLoadBalancingPolicy(CassandraLoadBalancingPolicies.getLoadBalancingPolicy(storeConfiguration.getLoadBalancingPolicyName())) .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE) .withPoolingOptions(poolingOptions) .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(storeConfiguration.getConsistency()))) .withProtocolVersion(ProtocolVersion.valueOf(storeConfiguration.getProtocolVersion())) .withPort(storeConfiguration.getPort()); if ( storeConfiguration.getUserName() != null && storeConfiguration.getUserPassword() != null) { builder = builder.withCredentials(storeConfiguration.getUserName(), storeConfiguration.getUserPassword()); } for (String cp : storeConfiguration.getCluster()) { builder.addContactPoint(cp); } cluster = builder.build(); Metadata metadata = cluster.getMetadata(); logger.debug("Connected to cluster: " + metadata.getClusterName()); for (Host host : metadata.getAllHosts()) { logger.debug(String.format("Datacenter: %s; Host: %s; Rack: %s", host.getDatacenter(), host.getAddress(), host.getRack())); } session = cluster.connect(); statement = session.prepare(query); }