Search in sources :

Example 11 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project spring-boot by spring-projects.

the class CassandraDriverReactiveHealthIndicatorTests method mockCqlSessionWithNodeState.

private CqlSession mockCqlSessionWithNodeState(NodeState... nodeStates) {
    CqlSession session = mock(CqlSession.class);
    Metadata metadata = mock(Metadata.class);
    List<Node> nodes = new ArrayList<>();
    for (NodeState nodeState : nodeStates) {
        Node node = mock(Node.class);
        given(node.getState()).willReturn(nodeState);
        nodes.add(node);
    }
    given(session.getMetadata()).willReturn(metadata);
    given(metadata.getNodes()).willReturn(createNodesWithRandomUUID(nodes));
    return session;
}
Also used : NodeState(com.datastax.oss.driver.api.core.metadata.NodeState) Node(com.datastax.oss.driver.api.core.metadata.Node) Metadata(com.datastax.oss.driver.api.core.metadata.Metadata) ArrayList(java.util.ArrayList) CqlSession(com.datastax.oss.driver.api.core.CqlSession)

Example 12 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project spring-boot by spring-projects.

the class CassandraDriverReactiveHealthIndicatorTests method healthWithOneUnknownNodeShouldReturnDown.

@Test
void healthWithOneUnknownNodeShouldReturnDown() {
    CqlSession session = mockCqlSessionWithNodeState(NodeState.UNKNOWN);
    CassandraDriverReactiveHealthIndicator healthIndicator = new CassandraDriverReactiveHealthIndicator(session);
    Mono<Health> health = healthIndicator.health();
    StepVerifier.create(health).consumeNextWith((h) -> assertThat(h.getStatus()).isEqualTo(Status.DOWN)).verifyComplete();
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) Status(org.springframework.boot.actuate.health.Status) NodeState(com.datastax.oss.driver.api.core.metadata.NodeState) StepVerifier(reactor.test.StepVerifier) DriverTimeoutException(com.datastax.oss.driver.api.core.DriverTimeoutException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) Health(org.springframework.boot.actuate.health.Health) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) Version(com.datastax.oss.driver.api.core.Version) List(java.util.List) CqlSession(com.datastax.oss.driver.api.core.CqlSession) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Metadata(com.datastax.oss.driver.api.core.metadata.Metadata) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) Health(org.springframework.boot.actuate.health.Health) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.jupiter.api.Test)

Example 13 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project zeppelin by apache.

the class CassandraInterpreterTest method setUp.

@BeforeClass
public static synchronized void setUp() throws IOException, InterruptedException {
    System.setProperty("cassandra.skip_wait_for_gossip_to_settle", "0");
    System.setProperty("cassandra.load_ring_state", "false");
    System.setProperty("cassandra.initial_token", "0");
    System.setProperty("cassandra.num_tokens", "nil");
    System.setProperty("cassandra.allocate_tokens_for_local_replication_factor", "nil");
    EmbeddedCassandraServerHelper.startEmbeddedCassandra();
    CqlSession session = EmbeddedCassandraServerHelper.getSession();
    new CQLDataLoader(session).load(new ClassPathCQLDataSet("prepare_all.cql", "zeppelin"));
    Properties properties = new Properties();
    properties.setProperty(CASSANDRA_CLUSTER_NAME, EmbeddedCassandraServerHelper.getClusterName());
    properties.setProperty(CASSANDRA_COMPRESSION_PROTOCOL, "NONE");
    properties.setProperty(CASSANDRA_CREDENTIALS_USERNAME, "none");
    properties.setProperty(CASSANDRA_CREDENTIALS_PASSWORD, "none");
    properties.setProperty(CASSANDRA_LOAD_BALANCING_POLICY, "DEFAULT");
    properties.setProperty(CASSANDRA_RETRY_POLICY, "DEFAULT");
    properties.setProperty(CASSANDRA_RECONNECTION_POLICY, "DEFAULT");
    properties.setProperty(CASSANDRA_SPECULATIVE_EXECUTION_POLICY, "DEFAULT");
    properties.setProperty(CASSANDRA_POOLING_CONNECTION_PER_HOST_LOCAL, "2");
    properties.setProperty(CASSANDRA_POOLING_CONNECTION_PER_HOST_REMOTE, "1");
    properties.setProperty(CASSANDRA_POOLING_MAX_REQUESTS_PER_CONNECTION, "1024");
    properties.setProperty(CASSANDRA_POOLING_POOL_TIMEOUT_MILLIS, "5000");
    properties.setProperty(CASSANDRA_POOLING_HEARTBEAT_INTERVAL_SECONDS, "30");
    properties.setProperty(CASSANDRA_QUERY_DEFAULT_CONSISTENCY, "ONE");
    properties.setProperty(CASSANDRA_QUERY_DEFAULT_SERIAL_CONSISTENCY, "SERIAL");
    properties.setProperty(CASSANDRA_QUERY_DEFAULT_FETCH_SIZE, "5000");
    properties.setProperty(CASSANDRA_SOCKET_CONNECTION_TIMEOUT_MILLIS, "5000");
    properties.setProperty(CASSANDRA_SOCKET_READ_TIMEOUT_MILLIS, "12000");
    properties.setProperty(CASSANDRA_SOCKET_TCP_NO_DELAY, "true");
    properties.setProperty(CASSANDRA_HOSTS, EmbeddedCassandraServerHelper.getHost());
    properties.setProperty(CASSANDRA_PORT, Integer.toString(EmbeddedCassandraServerHelper.getNativeTransportPort()));
    properties.setProperty("datastax-java-driver.advanced.connection.pool.local.size", "1");
    interpreter = new CassandraInterpreter(properties);
    interpreter.open();
}
Also used : Properties(java.util.Properties) CqlSession(com.datastax.oss.driver.api.core.CqlSession) ClassPathCQLDataSet(org.cassandraunit.dataset.cql.ClassPathCQLDataSet) CQLDataLoader(org.cassandraunit.CQLDataLoader) BeforeClass(org.junit.BeforeClass)

Example 14 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project zeppelin by apache.

the class CassandraInterpreterTest method should_execute_statement_with_timestamp_option.

@Test
public void should_execute_statement_with_timestamp_option() throws Exception {
    // Given
    String statement1 = "INSERT INTO zeppelin.ts(key,val) VALUES('k','v1');";
    String statement2 = "@timestamp=15\n" + "INSERT INTO zeppelin.ts(key,val) VALUES('k','v2');";
    CqlSession session = EmbeddedCassandraServerHelper.getSession();
    // Insert v1 with current timestamp
    interpreter.interpret(statement1, intrContext);
    System.out.println("going to read data from zeppelin.ts;");
    session.execute("SELECT val FROM zeppelin.ts LIMIT 1").forEach(x -> System.out.println("row " + x));
    Thread.sleep(1);
    // When
    // Insert v2 with past timestamp
    interpreter.interpret(statement2, intrContext);
    System.out.println("going to read data from zeppelin.ts;");
    session.execute("SELECT val FROM zeppelin.ts LIMIT 1").forEach(x -> System.out.println("row " + x));
    final String actual = session.execute("SELECT val FROM zeppelin.ts LIMIT 1").one().getString("val");
    // Then
    assertThat(actual).isEqualTo("v1");
}
Also used : CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Example 15 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project cas by apereo.

the class DefaultCassandraSessionFactory method initializeCassandraSession.

private CqlSession initializeCassandraSession(final BaseCassandraProperties cassandra) {
    val builder = CqlSession.builder().withKeyspace(cassandra.getKeyspace());
    if (StringUtils.isNotBlank(cassandra.getUsername()) && StringUtils.isNotBlank(cassandra.getPassword())) {
        builder.withAuthCredentials(cassandra.getUsername(), cassandra.getPassword());
    }
    if (StringUtils.isNotBlank(cassandra.getLocalDc())) {
        builder.withLocalDatacenter(cassandra.getLocalDc());
    }
    builder.withSslContext(this.sslContext);
    cassandra.getContactPoints().stream().map(contactPoint -> {
        val hostAndPort = Splitter.on(":").splitToList(contactPoint);
        val host = hostAndPort.get(0).trim();
        val port = Integer.parseInt(hostAndPort.get(1).trim());
        return new InetSocketAddress(host, port);
    }).forEach(builder::addContactPoint);
    return builder.build();
}
Also used : lombok.val(lombok.val) CassandraTemplate(org.springframework.data.cassandra.core.CassandraTemplate) SSLContext(javax.net.ssl.SSLContext) Getter(lombok.Getter) lombok.val(lombok.val) StringUtils(org.apache.commons.lang3.StringUtils) InetSocketAddress(java.net.InetSocketAddress) LoggingUtils(org.apereo.cas.util.LoggingUtils) Slf4j(lombok.extern.slf4j.Slf4j) BaseCassandraProperties(org.apereo.cas.configuration.model.support.cassandra.authentication.BaseCassandraProperties) CqlSession(com.datastax.oss.driver.api.core.CqlSession) DisposableBean(org.springframework.beans.factory.DisposableBean) CqlTemplate(org.springframework.data.cassandra.core.cql.CqlTemplate) Splitter(com.google.common.base.Splitter) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

CqlSession (com.datastax.oss.driver.api.core.CqlSession)41 Node (com.datastax.oss.driver.api.core.metadata.Node)20 Test (org.junit.jupiter.api.Test)20 Health (org.springframework.boot.actuate.health.Health)20 Metadata (com.datastax.oss.driver.api.core.metadata.Metadata)19 ArrayList (java.util.ArrayList)14 NodeState (com.datastax.oss.driver.api.core.metadata.NodeState)12 DriverTimeoutException (com.datastax.oss.driver.api.core.DriverTimeoutException)11 List (java.util.List)11 UUID (java.util.UUID)11 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)11 Version (com.datastax.oss.driver.api.core.Version)10 Collections (java.util.Collections)10 HashMap (java.util.HashMap)10 Map (java.util.Map)10 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)10 BDDMockito.given (org.mockito.BDDMockito.given)10 Mockito.mock (org.mockito.Mockito.mock)10 Status (org.springframework.boot.actuate.health.Status)10 Mono (reactor.core.publisher.Mono)10