com.datastax.driver.core.policies.DefaultRetryPolicy Java Examples

The following examples show how to use com.datastax.driver.core.policies.DefaultRetryPolicy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CassandraClusterXmlConfigTest.java    From spring-cloud-connectors with Apache License 2.0 6 votes vote down vote up
@Test
public void cassandraSessionWithConfiguration() throws Exception {
	ApplicationContext testContext = getTestApplicationContext(
			"cloud-cassandra-with-config.xml", createService("my-service"));
	Cluster cluster = testContext.getBean("cassandra-full-config",
			getConnectorType());

	assertNotNull(cluster.getConfiguration().getSocketOptions());
	assertEquals(15000,
			cluster.getConfiguration().getSocketOptions().getConnectTimeoutMillis());
	assertTrue(DefaultRetryPolicy.class.isAssignableFrom(
			cluster.getConfiguration().getPolicies().getRetryPolicy().getClass()));
	assertTrue(RoundRobinPolicy.class.isAssignableFrom(cluster.getConfiguration()
			.getPolicies().getLoadBalancingPolicy().getClass()));
	assertTrue(ConstantReconnectionPolicy.class.isAssignableFrom(cluster
			.getConfiguration().getPolicies().getReconnectionPolicy().getClass()));
}
 
Example #2
Source File: CassandraConnectionManagerImpl.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
private static Cluster createCluster(String[] hosts, PoolingOptions poolingOptions) {
  Cluster.Builder builder =
      Cluster.builder()
          .addContactPoints(hosts)
          .withProtocolVersion(ProtocolVersion.V3)
          .withRetryPolicy(DefaultRetryPolicy.INSTANCE)
          .withTimestampGenerator(new AtomicMonotonicTimestampGenerator())
          .withPoolingOptions(poolingOptions);

  ConsistencyLevel consistencyLevel = getConsistencyLevel();
  ProjectLogger.log(
      "CassandraConnectionManagerImpl:createCluster: Consistency level = " + consistencyLevel,
      LoggerEnum.INFO);

  if (consistencyLevel != null) {
    builder.withQueryOptions(new QueryOptions().setConsistencyLevel(consistencyLevel));
  }

  return builder.build();
}
 
Example #3
Source File: UtilsUnitTest.java    From cassandra-jdbc-wrapper with Apache License 2.0 6 votes vote down vote up
@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("====================");
	    	

}
 
Example #4
Source File: CassandraStorage.java    From cassandra-reaper with Apache License 2.0 6 votes vote down vote up
@Override
public RetryDecision onReadTimeout(
    Statement stmt,
    ConsistencyLevel cl,
    int required,
    int received,
    boolean retrieved,
    int retry) {

  if (retry > 1) {
    try {
      Thread.sleep(100);
    } catch (InterruptedException expected) { }
  }
  return null != stmt && !Objects.equals(Boolean.FALSE, stmt.isIdempotent())
      ? retry < 10 ? RetryDecision.retry(cl) : RetryDecision.rethrow()
      : DefaultRetryPolicy.INSTANCE.onReadTimeout(stmt, cl, required, received, retrieved, retry);
}
 
Example #5
Source File: DatastaxMetricModule.java    From heroic with Apache License 2.0 6 votes vote down vote up
@JsonCreator
public DatastaxMetricModule(
    @JsonProperty("id") Optional<String> id,
    @JsonProperty("groups") Optional<Groups> groups,
    @JsonProperty("seeds") Optional<Set<String>> seeds,
    @JsonProperty("schema") Optional<SchemaModule> schema,
    @JsonProperty("configure") Optional<Boolean> configure,
    @JsonProperty("fetchSize") Optional<Integer> fetchSize,
    @JsonProperty("readTimeout") Optional<Duration> readTimeout,
    @JsonProperty("consistencyLevel") Optional<ConsistencyLevel> consistencyLevel,
    @JsonProperty("retryPolicy") Optional<RetryPolicy> retryPolicy,
    @JsonProperty("authentication") Optional<DatastaxAuthentication> authentication,
    @JsonProperty("poolingOptions") Optional<DatastaxPoolingOptions> poolingOptions
) {
    this.id = id;
    this.groups = groups.orElseGet(Groups::empty).or("heroic");
    this.seeds = convert(seeds.orElse(DEFAULT_SEEDS));
    this.schema = schema.orElseGet(NextGenSchemaModule.builder()::build);
    this.configure = configure.orElse(DEFAULT_CONFIGURE);
    this.fetchSize = fetchSize.orElse(DEFAULT_FETCH_SIZE);
    this.readTimeout = readTimeout.orElse(DEFAULT_READ_TIMEOUT);
    this.consistencyLevel = consistencyLevel.orElse(ConsistencyLevel.ONE);
    this.retryPolicy = retryPolicy.orElse(DefaultRetryPolicy.INSTANCE);
    this.authentication = authentication.orElseGet(DatastaxAuthentication.None::new);
    this.poolingOptions = poolingOptions.orElseGet(DatastaxPoolingOptions::new);
}
 
Example #6
Source File: CassandraConfig.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #7
Source File: CassandraStorage.java    From cassandra-reaper with Apache License 2.0 5 votes vote down vote up
@Override
public RetryDecision onWriteTimeout(
    Statement stmt,
    ConsistencyLevel cl,
    WriteType type,
    int required,
    int received,
    int retry) {

  Preconditions.checkState(WriteType.CAS != type ||  ConsistencyLevel.SERIAL == cl);

  return null != stmt && !Objects.equals(Boolean.FALSE, stmt.isIdempotent())
      ? WriteType.CAS == type ? RetryDecision.retry(ConsistencyLevel.ONE) : RetryDecision.retry(cl)
      : DefaultRetryPolicy.INSTANCE.onWriteTimeout(stmt, cl, type, required, received, retry);
}
 
Example #8
Source File: LogConsistencyAllRetryPolicy.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Override
public RetryDecision onRequestError(Statement statement, ConsistencyLevel cl, DriverException e, int nbRetry) {
    if (cl == ConsistencyLevel.ALL) {
        log(statement);
    }
    return DefaultRetryPolicy.INSTANCE.onRequestError(statement, cl, e, nbRetry);
}
 
Example #9
Source File: LogConsistencyAllRetryPolicy.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Override
public RetryDecision onUnavailable(Statement statement, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry) {
    if (cl == ConsistencyLevel.ALL) {
        log(statement);
    }
    return DefaultRetryPolicy.INSTANCE.onUnavailable(statement, cl, requiredReplica, aliveReplica, nbRetry);
}
 
Example #10
Source File: LogConsistencyAllRetryPolicy.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Override
public RetryDecision onWriteTimeout(Statement statement, ConsistencyLevel cl, WriteType writeType, int requiredAcks, int receivedAcks, int nbRetry) {
    if (cl == ConsistencyLevel.ALL) {
        log(statement);
    }
    return DefaultRetryPolicy.INSTANCE.onWriteTimeout(statement, cl, writeType, requiredAcks, receivedAcks, nbRetry);
}
 
Example #11
Source File: LogConsistencyAllRetryPolicy.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Override
public RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry) {
    if (cl == ConsistencyLevel.ALL) {
        log(statement);
    }
    return DefaultRetryPolicy.INSTANCE.onReadTimeout(statement, cl, requiredResponses, receivedResponses, dataRetrieved, nbRetry);
}
 
Example #12
Source File: CassandraService.java    From OpenIoE with Apache License 2.0 5 votes vote down vote up
@PostConstruct
    @Override
    public void connect() {
        cluster = Cluster.builder().addContactPoint(ioeConfiguration.getCassandra().getCassandraUrl()).withRetryPolicy(DefaultRetryPolicy.INSTANCE).build();

        session = cluster.connect();
        session.execute("CREATE KEYSPACE IF NOT EXISTS " + ioeConfiguration.getCassandra().getCassandraKeyspace() + " WITH replication = {"
            + " 'class': '" + ioeConfiguration.getCassandra().getStrategy() + "', "
            + " 'replication_factor': '" + ioeConfiguration.getCassandra().getReplicationFactor() + "' "
            + "};");
        session.execute("USE " + ioeConfiguration.getCassandra().getCassandraKeyspace());

//        session = cluster.connect(ioeConfiguration.getCassandra().getCassandraKeyspace());
    }
 
Example #13
Source File: DefaultRetryPolicyFactoryTest.java    From dropwizard-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void returnsDefaultRetryPolicyInstance() throws Exception {
    final DefaultRetryPolicyFactory factory = new DefaultRetryPolicyFactory();

    final DefaultRetryPolicy policy = (DefaultRetryPolicy) factory.build();

    assertThat(policy).isSameAs(DefaultRetryPolicy.INSTANCE);
}
 
Example #14
Source File: ClusterManager.java    From scalardb with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
void build() {
  builder =
      Cluster.builder()
          .withClusterName("Scalar Cluster")
          .addContactPoints(config.getContactPoints().toArray(new String[0]))
          .withPort(
              config.getContactPort() == 0 ? DEFAULT_CASSANDRA_PORT : config.getContactPort())
          // .withCompression ?
          // .withPoolingOptions ?
          .withRetryPolicy(DefaultRetryPolicy.INSTANCE)
          .withLoadBalancingPolicy(
              new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().build()));
}
 
Example #15
Source File: BackplaneConfiguration.java    From elasticactors with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void initialize() {
    String cassandraHosts = env.getProperty("ea.cassandra.hosts","localhost:9042");
    String cassandraClusterName = env.getProperty("ea.cassandra.cluster","ElasticActorsCluster");
    String cassandraKeyspaceName = env.getProperty("ea.cassandra.keyspace","\"ElasticActors\"");
    Integer cassandraPort = env.getProperty("ea.cassandra.port", Integer.class, 9042);

    Set<String> hostSet = StringUtils.commaDelimitedListToSet(cassandraHosts);

    String[] contactPoints = new String[hostSet.size()];
    int i=0;
    for (String host : hostSet) {
        if(host.contains(":")) {
            contactPoints[i] = host.substring(0,host.indexOf(":"));
        } else {
            contactPoints[i] = host;
        }
        i+=1;
    }

    PoolingOptions poolingOptions = new PoolingOptions();
    poolingOptions.setHeartbeatIntervalSeconds(60);
    poolingOptions.setConnectionsPerHost(HostDistance.LOCAL, 2, env.getProperty("ea.cassandra.maxActive",Integer.class,Runtime.getRuntime().availableProcessors() * 3));
    poolingOptions.setPoolTimeoutMillis(2000);

    Cluster cassandraCluster =
            Cluster.builder().withClusterName(cassandraClusterName)
                    .addContactPoints(contactPoints)
                    .withPort(cassandraPort)
            .withLoadBalancingPolicy(new RoundRobinPolicy())
            .withRetryPolicy(new LoggingRetryPolicy(DefaultRetryPolicy.INSTANCE))
            .withPoolingOptions(poolingOptions)
            .withReconnectionPolicy(new ConstantReconnectionPolicy(env.getProperty("ea.cassandra.retryDownedHostsDelayInSeconds",Integer.class,1) * 1000))
            .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.QUORUM)).build();

    this.cassandraSession = cassandraCluster.connect(cassandraKeyspaceName);

    
}
 
Example #16
Source File: CassandraStorage.java    From copper-engine with Apache License 2.0 5 votes vote down vote up
protected void prepareStatements() throws Exception {
    prepare(CQL_UPD_WORKFLOW_INSTANCE_NOT_WAITING);
    prepare(CQL_UPD_WORKFLOW_INSTANCE_WAITING);
    prepare(CQL_DEL_WORKFLOW_INSTANCE_WAITING);
    prepare(CQL_SEL_WORKFLOW_INSTANCE);
    prepare(CQL_UPD_WORKFLOW_INSTANCE_STATE);
    prepare(CQL_INS_EARLY_RESPONSE);
    prepare(CQL_DEL_EARLY_RESPONSE);
    prepare(CQL_SEL_EARLY_RESPONSE);
    prepare(CQL_UPD_WORKFLOW_INSTANCE_STATE_AND_RESPONSE_MAP);
    prepare(CQL_INS_WFI_ID);
    prepare(CQL_DEL_WFI_ID);
    prepare(CQL_SEL_WFI_ID_ALL, DefaultRetryPolicy.INSTANCE);
}
 
Example #17
Source File: DefaultRetryPolicyFactory.java    From dropwizard-cassandra with Apache License 2.0 4 votes vote down vote up
@Override
public RetryPolicy build() {
    return DefaultRetryPolicy.INSTANCE;
}
 
Example #18
Source File: CassandraStorage.java    From cassandra-reaper with Apache License 2.0 4 votes vote down vote up
@Override
public RetryDecision onRequestError(Statement stmt, ConsistencyLevel cl, DriverException ex, int nbRetry) {
  return DefaultRetryPolicy.INSTANCE.onRequestError(stmt, cl, ex, nbRetry);
}
 
Example #19
Source File: CassandraStorage.java    From cassandra-reaper with Apache License 2.0 4 votes vote down vote up
@Override
public RetryDecision onUnavailable(Statement stmt, ConsistencyLevel cl, int required, int aliveReplica, int retry) {
  return DefaultRetryPolicy.INSTANCE.onUnavailable(stmt, cl, required, aliveReplica, retry == 1 ? 0 : retry);
}
 
Example #20
Source File: AppBase.java    From yb-sample-apps with Apache License 2.0 4 votes vote down vote up
/**
 * Private method that is thread-safe and creates the Cassandra client. Exactly one calling thread
 * will succeed in creating the client. This method does nothing for the other threads.
 * @param contactPoints list of contact points for the client.
 */
protected synchronized void createCassandraClient(List<ContactPoint> contactPoints) {
  Cluster.Builder builder;
  if (cassandra_cluster == null) {
    builder = Cluster.builder();
    if (appConfig.dbUsername != null) {
      if (appConfig.dbPassword == null) {
        throw new IllegalArgumentException("Password required when providing a username");
      }
      builder = builder
          .withCredentials(appConfig.dbUsername, appConfig.dbPassword);
    }
    if (appConfig.sslCert != null) {
      builder = builder
          .withSSL(createSSLHandler(appConfig.sslCert));
    }
    Integer port = null;
    SocketOptions socketOptions = new SocketOptions();
    if (appConfig.cqlConnectTimeoutMs > 0) {
      socketOptions.setConnectTimeoutMillis(appConfig.cqlConnectTimeoutMs);
    }
    if (appConfig.cqlReadTimeoutMs > 0) {
      socketOptions.setReadTimeoutMillis(appConfig.cqlReadTimeoutMs);
    }
    builder.withSocketOptions(socketOptions);
    for (ContactPoint cp : contactPoints) {
      if (port == null) {
        port = cp.getPort();
        builder.withPort(port);
      } else if (port != cp.getPort()) {
        throw new IllegalArgumentException("Using multiple CQL ports is not supported.");
      }
      builder.addContactPoint(cp.getHost());
    }
    LOG.info("Connecting with " + appConfig.concurrentClients + " clients to nodes: "
        + builder.getContactPoints()
            .stream().map(it -> it.toString()).collect(Collectors.joining(",")));
    PoolingOptions poolingOptions = new PoolingOptions();
    poolingOptions
        .setCoreConnectionsPerHost(HostDistance.LOCAL, appConfig.concurrentClients)
        .setMaxConnectionsPerHost(HostDistance.LOCAL, appConfig.concurrentClients)
        .setCoreConnectionsPerHost(HostDistance.REMOTE, appConfig.concurrentClients)
        .setMaxConnectionsPerHost(HostDistance.REMOTE, appConfig.concurrentClients);
    cassandra_cluster =
        builder.withLoadBalancingPolicy(getLoadBalancingPolicy())
               .withPoolingOptions(poolingOptions)
               .withQueryOptions(new QueryOptions().setDefaultIdempotence(true))
               .withRetryPolicy(new LoggingRetryPolicy(DefaultRetryPolicy.INSTANCE))
               .build();
    LOG.debug("Connected to cluster: " + cassandra_cluster.getClusterName());
  }
  if (cassandra_session == null) {
    LOG.debug("Creating a session...");
    cassandra_session = cassandra_cluster.connect();
    createKeyspace(cassandra_session, keyspace);
  }
}
 
Example #21
Source File: CassandraFactory.java    From database-transform-tool with Apache License 2.0 4 votes vote down vote up
/**
	 * 描述: 初始化配置
	 * 时间: 2017年11月15日 上午11:25:07
	 * @author yi.zhang
	 * @param servers	服务地址
	 * @param keyspace	命名空间
	 * @param username	账号
	 * @param password	密码
	 */
	public void init(String servers,String keyspace,String username,String password) {
		try {
			// socket 链接配置
			SocketOptions socket = new SocketOptions();
			socket.setKeepAlive(true);
			socket.setReceiveBufferSize(1024* 1024);
			socket.setSendBufferSize(1024* 1024);
			socket.setConnectTimeoutMillis(5 * 1000);
			socket.setReadTimeoutMillis(1000);
			//设置连接池
			PoolingOptions pool = new PoolingOptions();
			// pool.setMaxRequestsPerConnection(HostDistance.LOCAL, 32);
			// pool.setMaxRequestsPerConnection(HostDistance.REMOTE, 32);
			// pool.setCoreConnectionsPerHost(HostDistance.LOCAL, 2);
			// pool.setCoreConnectionsPerHost(HostDistance.REMOTE, 2);
			// pool.setMaxConnectionsPerHost(HostDistance.LOCAL, 4);
			// pool.setMaxConnectionsPerHost(HostDistance.REMOTE, 4);
			pool.setHeartbeatIntervalSeconds(60);
			pool.setIdleTimeoutSeconds(120);
			pool.setPoolTimeoutMillis(5 * 1000);
			List<InetSocketAddress> saddress = new ArrayList<InetSocketAddress>();
			if (servers != null && !"".equals(servers)) {
				for (String server : servers.split(",")) {
					String[] address = server.split(":");
					String ip = address[0];
					int port = 9042;
					if (address != null && address.length > 1) {
						port = Integer.valueOf(address[1]);
					}
					saddress.add(new InetSocketAddress(ip, port));
				}
			}
			InetSocketAddress[] addresses = new InetSocketAddress[saddress.size()];
			saddress.toArray(addresses);
			
			Builder builder = Cluster.builder();
	        builder.withSocketOptions(socket);
	        // 设置压缩方式
	        builder.withCompression(ProtocolOptions.Compression.LZ4);
	        // 负载策略
//	        DCAwareRoundRobinPolicy loadBalance = DCAwareRoundRobinPolicy.builder().withLocalDc("localDc").withUsedHostsPerRemoteDc(2).allowRemoteDCsForLocalConsistencyLevel().build();
//	        builder.withLoadBalancingPolicy(loadBalance);
	        // 重试策略
	        builder.withRetryPolicy(DefaultRetryPolicy.INSTANCE);
			builder.withPoolingOptions(pool);
			builder.addContactPointsWithPorts(addresses);
			builder.withCredentials(username, password);
			Cluster cluster = builder.build();
			if (keyspace != null && !"".equals(keyspace)) {
				session = cluster.connect(keyspace);
			} else {
				session = cluster.connect();
			}
			mapping = new MappingManager(session);
		} catch (Exception e) {
			logger.error("-----Cassandra Config init Error-----", e);
		}
	}
 
Example #22
Source File: BackoffRetryPolicy.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public RetryDecision onWriteTimeout(Statement statement, ConsistencyLevel cl, WriteType writeType, int requiredAcks, int receivedAcks, int nbRetry)
{
    return DefaultRetryPolicy.INSTANCE.onWriteTimeout(statement, cl, writeType, requiredAcks, receivedAcks, nbRetry);
}
 
Example #23
Source File: BackoffRetryPolicy.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry)
{
    return DefaultRetryPolicy.INSTANCE.onReadTimeout(statement, cl, requiredResponses, receivedResponses, dataRetrieved, nbRetry);
}