Search in sources :

Example 36 with Cluster

use of com.datastax.driver.core.Cluster in project aroma-data-operations by RedRoma.

the class ModuleCassandraDevClusterTest method testProvideCQLBuilder.

@Test
public void testProvideCQLBuilder() {
    ReconnectionPolicy policy = instance.provideReconnectPolicy();
    Cluster cluster = instance.provideCassandraCluster(policy);
}
Also used : ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) Cluster(com.datastax.driver.core.Cluster) Test(org.junit.Test) IntegrationTest(tech.sirwellington.alchemy.annotations.testing.IntegrationTest)

Example 37 with Cluster

use of com.datastax.driver.core.Cluster in project aroma-data-operations by RedRoma.

the class ModuleCassandraDevClusterTest method testProvideCassandraSession.

@Test
public void testProvideCassandraSession() {
    ReconnectionPolicy policy = instance.provideReconnectPolicy();
    Cluster cluster = instance.provideCassandraCluster(policy);
    Session session = instance.provideCassandraSession(cluster);
    assertThat(session, notNullValue());
}
Also used : ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) Cluster(com.datastax.driver.core.Cluster) Session(com.datastax.driver.core.Session) Test(org.junit.Test) IntegrationTest(tech.sirwellington.alchemy.annotations.testing.IntegrationTest)

Example 38 with Cluster

use of com.datastax.driver.core.Cluster in project aroma-data-operations by RedRoma.

the class ModuleCassandraDevClusterTest method testProvideCassandraCluster.

@Test
public void testProvideCassandraCluster() {
    ReconnectionPolicy policy = instance.provideReconnectPolicy();
    Cluster cluster = instance.provideCassandraCluster(policy);
    assertThat(cluster, notNullValue());
}
Also used : ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) Cluster(com.datastax.driver.core.Cluster) Test(org.junit.Test) IntegrationTest(tech.sirwellington.alchemy.annotations.testing.IntegrationTest)

Example 39 with Cluster

use of com.datastax.driver.core.Cluster in project cas by apereo.

the class DefaultCassandraSessionFactory method initializeCassandraCluster.

private static Cluster initializeCassandraCluster(final BaseCassandraProperties cassandra) {
    final Cluster cluster;
    final PoolingOptions poolingOptions = new PoolingOptions().setMaxRequestsPerConnection(HostDistance.LOCAL, cassandra.getMaxRequestsPerConnection()).setConnectionsPerHost(HostDistance.LOCAL, cassandra.getCoreConnections(), cassandra.getMaxConnections());
    final DCAwareRoundRobinPolicy.Builder dcPolicyBuilder = DCAwareRoundRobinPolicy.builder();
    if (StringUtils.isNotBlank(cassandra.getLocalDc())) {
        dcPolicyBuilder.withLocalDc(cassandra.getLocalDc());
    }
    final TokenAwarePolicy loadBalancingPolicy = new TokenAwarePolicy(dcPolicyBuilder.build(), cassandra.isShuffleReplicas());
    final SocketOptions socketOptions = new SocketOptions().setConnectTimeoutMillis(cassandra.getConnectTimeoutMillis()).setReadTimeoutMillis(cassandra.getReadTimeoutMillis());
    final QueryOptions queryOptions = new QueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(cassandra.getConsistencyLevel())).setSerialConsistencyLevel(ConsistencyLevel.valueOf(cassandra.getSerialConsistencyLevel()));
    final RetryPolicy retryPolicy = RetryPolicyType.valueOf(cassandra.getRetryPolicy()).getRetryPolicy();
    final Cluster.Builder builder = Cluster.builder().withCredentials(cassandra.getUsername(), cassandra.getPassword()).withPoolingOptions(poolingOptions).withProtocolVersion(ProtocolVersion.valueOf(cassandra.getProtocolVersion())).withLoadBalancingPolicy(loadBalancingPolicy).withSocketOptions(socketOptions).withRetryPolicy(new LoggingRetryPolicy(retryPolicy)).withCompression(ProtocolOptions.Compression.valueOf(cassandra.getCompression())).withPort(cassandra.getPort()).withQueryOptions(queryOptions);
    Arrays.stream(StringUtils.split(cassandra.getContactPoints(), ',')).forEach(contactPoint -> builder.addContactPoint(StringUtils.trim(contactPoint)));
    cluster = builder.build();
    if (LOGGER.isDebugEnabled()) {
        cluster.getMetadata().getAllHosts().forEach(clusterHost -> LOGGER.debug("Host [{}]:\n\n\tDC: [{}]\n\tRack: [{}]\n\tVersion: [{}]\n\tDistance: [{}]\n\tUp?: [{}]\n", clusterHost.getAddress(), clusterHost.getDatacenter(), clusterHost.getRack(), clusterHost.getCassandraVersion(), loadBalancingPolicy.distance(clusterHost), clusterHost.isUp()));
    }
    return cluster;
}
Also used : DCAwareRoundRobinPolicy(com.datastax.driver.core.policies.DCAwareRoundRobinPolicy) SocketOptions(com.datastax.driver.core.SocketOptions) PoolingOptions(com.datastax.driver.core.PoolingOptions) Cluster(com.datastax.driver.core.Cluster) TokenAwarePolicy(com.datastax.driver.core.policies.TokenAwarePolicy) QueryOptions(com.datastax.driver.core.QueryOptions) LoggingRetryPolicy(com.datastax.driver.core.policies.LoggingRetryPolicy) RetryPolicy(com.datastax.driver.core.policies.RetryPolicy) LoggingRetryPolicy(com.datastax.driver.core.policies.LoggingRetryPolicy)

Example 40 with Cluster

use of com.datastax.driver.core.Cluster in project ignite by apache.

the class CassandraSessionImplTest method executeFailureTest.

@SuppressWarnings("unchecked")
@Test
public void executeFailureTest() {
    Session session1 = mock(Session.class);
    Session session2 = mock(Session.class);
    when(session1.prepare(any(String.class))).thenReturn(preparedStatement1);
    when(session2.prepare(any(String.class))).thenReturn(preparedStatement2);
    ResultSetFuture rsFuture = mock(ResultSetFuture.class);
    ResultSet rs = mock(ResultSet.class);
    Iterator it = mock(Iterator.class);
    when(it.hasNext()).thenReturn(true);
    when(it.next()).thenReturn(mock(Row.class));
    when(rs.iterator()).thenReturn(it);
    when(rsFuture.getUninterruptibly()).thenReturn(rs);
    /* @formatter:off */
    when(session1.executeAsync(any(Statement.class))).thenThrow(new InvalidQueryException("You may have used a PreparedStatement that was created with another Cluster instance")).thenThrow(new RuntimeException("this session should be refreshed / recreated"));
    when(session2.executeAsync(boundStatement1)).thenThrow(new InvalidQueryException("You may have used a PreparedStatement that was created with another Cluster instance"));
    when(session2.executeAsync(boundStatement2)).thenReturn(rsFuture);
    /* @formatter:on */
    Cluster cluster = mock(Cluster.class);
    when(cluster.connect()).thenReturn(session1).thenReturn(session2);
    when(session1.getCluster()).thenReturn(cluster);
    when(session2.getCluster()).thenReturn(cluster);
    Cluster.Builder builder = mock(Cluster.Builder.class);
    when(builder.build()).thenReturn(cluster);
    CassandraSessionImpl cassandraSession = new CassandraSessionImpl(builder, null, ConsistencyLevel.ONE, ConsistencyLevel.ONE, 0, mock(IgniteLogger.class));
    BatchExecutionAssistant<String, String> batchExecutionAssistant = new MyBatchExecutionAssistant();
    ArrayList<String> data = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        data.add(String.valueOf(i));
    }
    cassandraSession.execute(batchExecutionAssistant, data);
    verify(cluster, times(2)).connect();
    verify(session1, times(1)).prepare(any(String.class));
    verify(session2, times(1)).prepare(any(String.class));
    assertEquals(10, batchExecutionAssistant.processedCount());
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) ArrayList(java.util.ArrayList) Cluster(com.datastax.driver.core.Cluster) CassandraSessionImpl(org.apache.ignite.cache.store.cassandra.session.CassandraSessionImpl) ResultSet(com.datastax.driver.core.ResultSet) Iterator(java.util.Iterator) Row(com.datastax.driver.core.Row) IgniteLogger(org.apache.ignite.IgniteLogger) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Aggregations

Cluster (com.datastax.driver.core.Cluster)41 Session (com.datastax.driver.core.Session)17 Test (org.junit.Test)17 ResultSet (com.datastax.driver.core.ResultSet)11 Row (com.datastax.driver.core.Row)9 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)6 ArrayList (java.util.ArrayList)5 PoolingOptions (com.datastax.driver.core.PoolingOptions)4 ReconnectionPolicy (com.datastax.driver.core.policies.ReconnectionPolicy)4 TokenAwarePolicy (com.datastax.driver.core.policies.TokenAwarePolicy)4 IntegrationTest (tech.sirwellington.alchemy.annotations.testing.IntegrationTest)4 QueryOptions (com.datastax.driver.core.QueryOptions)3 TableMetadata (com.datastax.driver.core.TableMetadata)3 DCAwareRoundRobinPolicy (com.datastax.driver.core.policies.DCAwareRoundRobinPolicy)3 Update (com.datastax.driver.core.querybuilder.Update)3 InetSocketAddress (java.net.InetSocketAddress)3 Host (com.datastax.driver.core.Host)2 JdkSSLOptions (com.datastax.driver.core.JdkSSLOptions)2 Metadata (com.datastax.driver.core.Metadata)2 PlainTextAuthProvider (com.datastax.driver.core.PlainTextAuthProvider)2