Search in sources :

Example 1 with ReactiveRow

use of com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow in project java-driver by datastax.

the class ReactiveRetryIT method should_retry_at_application_level.

@Test
public void should_retry_at_application_level() {
    // Given
    CqlSession session = spy(SESSION_RULE.session());
    BoundCluster cluster = SIMULACRON_RULE.cluster();
    cluster.node(0).prime(when(QUERY_STRING).then(unavailable(ConsistencyLevel.ONE, 1, 0)));
    cluster.node(1).prime(when(QUERY_STRING).then(unavailable(ConsistencyLevel.ONE, 1, 0)));
    cluster.node(2).prime(when(QUERY_STRING).then(rows().row("col1", "Yay!")));
    // When
    ReactiveRow row = Flowable.defer(() -> session.executeReactive(QUERY_STRING)).retry((retry, error) -> {
        assertThat(error).isInstanceOf(UnavailableException.class);
        UnavailableException ue = (UnavailableException) error;
        Node coordinator = ue.getCoordinator();
        if (retry == 1) {
            assertCoordinator(0, coordinator);
            return true;
        } else if (retry == 2) {
            assertCoordinator(1, coordinator);
            return true;
        } else {
            fail("Unexpected retry attempt");
            return false;
        }
    }).blockingLast();
    // Then
    assertThat(row.getString(0)).isEqualTo("Yay!");
    verify(session, times(3)).executeReactive(QUERY_STRING);
    assertUnavailableMetric(0, 1L);
    assertUnavailableMetric(1, 1L);
    assertUnavailableMetric(2, 0L);
}
Also used : Metrics(com.datastax.oss.driver.api.core.metrics.Metrics) BoundCluster(com.datastax.oss.simulacron.server.BoundCluster) DefaultNodeMetric(com.datastax.oss.driver.api.core.metrics.DefaultNodeMetric) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoadBalancingPolicy(com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy) PrimeDsl.when(com.datastax.oss.simulacron.common.stubbing.PrimeDsl.when) NodeDistance(com.datastax.oss.driver.api.core.loadbalancing.NodeDistance) SimulacronRule(com.datastax.oss.driver.api.testinfra.simulacron.SimulacronRule) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) ClassRule(org.junit.ClassRule) DriverContext(com.datastax.oss.driver.api.core.context.DriverContext) SessionUtils(com.datastax.oss.driver.api.testinfra.session.SessionUtils) Metric(com.codahale.metrics.Metric) UUID(java.util.UUID) Category(org.junit.experimental.categories.Category) List(java.util.List) VerificationModeFactory.times(org.mockito.internal.verification.VerificationModeFactory.times) Optional(java.util.Optional) Queue(java.util.Queue) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) Node(com.datastax.oss.driver.api.core.metadata.Node) ClusterSpec(com.datastax.oss.simulacron.common.cluster.ClusterSpec) Iterables(com.google.common.collect.Iterables) TestRule(org.junit.rules.TestRule) Mockito.spy(org.mockito.Mockito.spy) ParallelizableTests(com.datastax.oss.driver.categories.ParallelizableTests) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) NoRetryPolicy(com.datastax.oss.driver.core.retry.PerProfileRetryPolicyIT.NoRetryPolicy) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) Flowable(io.reactivex.Flowable) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Before(org.junit.Before) Session(com.datastax.oss.driver.api.core.session.Session) Iterator(java.util.Iterator) Test(org.junit.Test) UnavailableException(com.datastax.oss.driver.api.core.servererrors.UnavailableException) Request(com.datastax.oss.driver.api.core.session.Request) PrimeDsl.rows(com.datastax.oss.simulacron.common.stubbing.PrimeDsl.rows) Mockito.verify(org.mockito.Mockito.verify) SessionRule(com.datastax.oss.driver.api.testinfra.session.SessionRule) RuleChain(org.junit.rules.RuleChain) PrimeDsl.unavailable(com.datastax.oss.simulacron.common.stubbing.PrimeDsl.unavailable) ConsistencyLevel(com.datastax.oss.simulacron.common.codec.ConsistencyLevel) Nullable(edu.umd.cs.findbugs.annotations.Nullable) NodeComparator(com.datastax.oss.driver.api.testinfra.loadbalancing.NodeComparator) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) Node(com.datastax.oss.driver.api.core.metadata.Node) UnavailableException(com.datastax.oss.driver.api.core.servererrors.UnavailableException) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) BoundCluster(com.datastax.oss.simulacron.server.BoundCluster) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Example 2 with ReactiveRow

use of com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow in project java-driver by datastax.

the class DefaultReactiveResultSetIT method should_write_cas.

@Test
public void should_write_cas() {
    SimpleStatement statement = SimpleStatement.builder("INSERT INTO test_reactive_write (pk, cc, v) VALUES (?, ?, ?) IF NOT EXISTS").addPositionalValue(0).addPositionalValue(1).addPositionalValue(2).setExecutionProfile(sessionRule.slowProfile()).build();
    // execute statement for the first time: the insert should succeed and the server should return
    // only one acknowledgement row with just the [applied] column = true
    ReactiveResultSet rs = sessionRule.session().executeReactive(statement);
    List<ReactiveRow> results = Flowable.fromPublisher(rs).toList().blockingGet();
    assertThat(results).hasSize(1);
    ReactiveRow row = results.get(0);
    assertThat(row.getExecutionInfo()).isNotNull();
    assertThat(row.getColumnDefinitions()).hasSize(1);
    assertThat(row.wasApplied()).isTrue();
    assertThat(row.getBoolean("[applied]")).isTrue();
    List<ExecutionInfo> execInfos = Flowable.<ExecutionInfo>fromPublisher(rs.getExecutionInfos()).toList().blockingGet();
    assertThat(execInfos).hasSize(1).containsExactly(row.getExecutionInfo());
    List<ColumnDefinitions> colDefs = Flowable.<ColumnDefinitions>fromPublisher(rs.getColumnDefinitions()).toList().blockingGet();
    assertThat(colDefs).hasSize(1).containsExactly(row.getColumnDefinitions());
    List<Boolean> wasApplied = Flowable.fromPublisher(rs.wasApplied()).toList().blockingGet();
    assertThat(wasApplied).hasSize(1).containsExactly(row.wasApplied());
    // re-execute same statement: server should return one row with data that failed to be inserted,
    // with [applied] = false
    rs = sessionRule.session().executeReactive(statement);
    results = Flowable.fromPublisher(rs).toList().blockingGet();
    assertThat(results).hasSize(1);
    row = results.get(0);
    assertThat(row.getExecutionInfo()).isNotNull();
    assertThat(row.getColumnDefinitions()).hasSize(4);
    assertThat(row.wasApplied()).isFalse();
    assertThat(row.getBoolean("[applied]")).isFalse();
    assertThat(row.getInt("pk")).isEqualTo(0);
    assertThat(row.getInt("cc")).isEqualTo(1);
    assertThat(row.getInt("v")).isEqualTo(2);
    execInfos = Flowable.<ExecutionInfo>fromPublisher(rs.getExecutionInfos()).toList().blockingGet();
    assertThat(execInfos).hasSize(1).containsExactly(row.getExecutionInfo());
    colDefs = Flowable.<ColumnDefinitions>fromPublisher(rs.getColumnDefinitions()).toList().blockingGet();
    assertThat(colDefs).hasSize(1).containsExactly(row.getColumnDefinitions());
    wasApplied = Flowable.fromPublisher(rs.wasApplied()).toList().blockingGet();
    assertThat(wasApplied).hasSize(1).containsExactly(row.wasApplied());
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) EmptyColumnDefinitions(com.datastax.oss.driver.internal.core.cql.EmptyColumnDefinitions) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ReactiveResultSet(com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) Test(org.junit.Test)

Example 3 with ReactiveRow

use of com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow in project java-driver by datastax.

the class DefaultReactiveResultSetIT method should_retrieve_all_rows.

@Test
@DataProvider(value = { "1", "10", "100", "999", "1000", "1001", "2000" }, format = "%m [page size %p[0]]")
public void should_retrieve_all_rows(int pageSize) {
    DriverExecutionProfile profile = sessionRule.session().getContext().getConfig().getDefaultProfile().withInt(DefaultDriverOption.REQUEST_PAGE_SIZE, pageSize);
    SimpleStatement statement = SimpleStatement.builder("SELECT cc, v FROM test_reactive_read WHERE pk = 0").setExecutionProfile(profile).build();
    ReactiveResultSet rs = sessionRule.session().executeReactive(statement);
    List<ReactiveRow> results = Flowable.fromPublisher(rs).toList().blockingGet();
    assertThat(results.size()).isEqualTo(1000);
    Set<ExecutionInfo> expectedExecInfos = new LinkedHashSet<>();
    for (int i = 0; i < results.size(); i++) {
        ReactiveRow row = results.get(i);
        assertThat(row.getColumnDefinitions()).isNotNull();
        assertThat(row.getExecutionInfo()).isNotNull();
        assertThat(row.wasApplied()).isTrue();
        assertThat(row.getInt("cc")).isEqualTo(i);
        assertThat(row.getInt("v")).isEqualTo(i);
        expectedExecInfos.add(row.getExecutionInfo());
    }
    List<ExecutionInfo> execInfos = Flowable.<ExecutionInfo>fromPublisher(rs.getExecutionInfos()).toList().blockingGet();
    // DSE may send an empty page as it can't always know if it's done paging or not yet.
    // See: CASSANDRA-8871. In this case, this page's execution info appears in
    // rs.getExecutionInfos(), but is not present in expectedExecInfos since the page did not
    // contain any rows.
    assertThat(execInfos).containsAll(expectedExecInfos);
    List<ColumnDefinitions> colDefs = Flowable.<ColumnDefinitions>fromPublisher(rs.getColumnDefinitions()).toList().blockingGet();
    ReactiveRow first = results.get(0);
    assertThat(colDefs).hasSize(1).containsExactly(first.getColumnDefinitions());
    List<Boolean> wasApplied = Flowable.fromPublisher(rs.wasApplied()).toList().blockingGet();
    assertThat(wasApplied).hasSize(1).containsExactly(first.wasApplied());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) EmptyColumnDefinitions(com.datastax.oss.driver.internal.core.cql.EmptyColumnDefinitions) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) ReactiveResultSet(com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 4 with ReactiveRow

use of com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow in project java-driver by datastax.

the class DriverBlockHoundIntegrationIT method should_not_detect_blocking_call_on_asynchronous_execution_prepared.

@Test
public void should_not_detect_blocking_call_on_asynchronous_execution_prepared() {
    try (CqlSession session = SessionUtils.newSession(SIMULACRON_RULE)) {
        Flux<ReactiveRow> rows = Mono.fromCompletionStage(() -> session.prepareAsync("SELECT c1, c2 FROM ks.t1")).flatMapMany(ps -> Flux.range(0, 1000).map(i -> ps.bind()).flatMap(bs -> Flux.from(session.executeReactive(bs)).subscribeOn(Schedulers.parallel())));
        StepVerifier.create(rows).expectNextCount(1000).expectComplete().verify();
    }
}
Also used : ClusterSpec(com.datastax.oss.simulacron.common.cluster.ClusterSpec) Logger(org.slf4j.Logger) BlockHound(reactor.blockhound.BlockHound) StepVerifier(reactor.test.StepVerifier) BeforeClass(org.junit.BeforeClass) SessionUtils(com.datastax.oss.driver.api.testinfra.session.SessionUtils) IsolatedTests(com.datastax.oss.driver.categories.IsolatedTests) LoggerFactory(org.slf4j.LoggerFactory) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) PrimeDsl.when(com.datastax.oss.simulacron.common.stubbing.PrimeDsl.when) UUID(java.util.UUID) Category(org.junit.experimental.categories.Category) Fail.fail(org.assertj.core.api.Fail.fail) PrimeDsl.rows(com.datastax.oss.simulacron.common.stubbing.PrimeDsl.rows) Flux(reactor.core.publisher.Flux) CqlSession(com.datastax.oss.driver.api.core.CqlSession) SimulacronRule(com.datastax.oss.driver.api.testinfra.simulacron.SimulacronRule) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) Schedulers(reactor.core.scheduler.Schedulers) ClassRule(org.junit.ClassRule) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) Before(org.junit.Before) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Example 5 with ReactiveRow

use of com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow in project java-driver by datastax.

the class DeleteReactiveIT method should_delete_by_id_reactive.

@Test
public void should_delete_by_id_reactive() {
    UUID id = FLAMETHROWER.getId();
    assertThat(Flowable.fromPublisher(dao.findByIdReactive(id)).blockingSingle()).isNotNull();
    ReactiveResultSet rs = dao.deleteByIdReactive(id);
    ReactiveRow row = Flowable.fromPublisher(rs).singleElement().blockingGet();
    assertThat(row).isNull();
    assertThat(Flowable.fromPublisher(dao.findByIdReactive(id)).singleElement().blockingGet()).isNull();
    // Non-existing id should be silently ignored
    rs = dao.deleteByIdReactive(id);
    row = Flowable.fromPublisher(rs).singleElement().blockingGet();
    assertThat(row).isNull();
}
Also used : ReactiveResultSet(com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet) MappedReactiveResultSet(com.datastax.dse.driver.api.mapper.reactive.MappedReactiveResultSet) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

ReactiveRow (com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow)22 Test (org.junit.Test)21 ReactiveResultSet (com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet)13 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)12 ColumnDefinitions (com.datastax.oss.driver.api.core.cql.ColumnDefinitions)11 MappedReactiveResultSet (com.datastax.dse.driver.api.mapper.reactive.MappedReactiveResultSet)7 UUID (java.util.UUID)7 CqlSession (com.datastax.oss.driver.api.core.CqlSession)5 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)5 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)4 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)4 EmptyColumnDefinitions (com.datastax.oss.driver.internal.core.cql.EmptyColumnDefinitions)4 RequestHandlerTestHarness (com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness)4 DefaultSession (com.datastax.oss.driver.internal.core.session.DefaultSession)4 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)3 UnavailableException (com.datastax.oss.driver.api.core.servererrors.UnavailableException)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ContinuousReactiveResultSet (com.datastax.dse.driver.api.core.cql.continuous.reactive.ContinuousReactiveResultSet)2 ContinuousCqlRequestAsyncProcessor (com.datastax.dse.driver.internal.core.cql.continuous.ContinuousCqlRequestAsyncProcessor)2 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)2