Search in sources :

Example 1 with BoundCluster

use of com.datastax.oss.simulacron.server.BoundCluster 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 BoundCluster

use of com.datastax.oss.simulacron.server.BoundCluster in project java-driver by datastax.

the class AllLoadBalancingPoliciesSimulacronIT method findNode.

private BoundNode findNode(Node node) {
    BoundCluster simulacron = SIMULACRON_RULE.cluster();
    SocketAddress toFind = node.getEndPoint().resolve();
    for (BoundNode boundNode : simulacron.getNodes()) {
        if (boundNode.getAddress().equals(toFind)) {
            return boundNode;
        }
    }
    throw new AssertionError("Could not find node: " + toFind);
}
Also used : BoundCluster(com.datastax.oss.simulacron.server.BoundCluster) BoundNode(com.datastax.oss.simulacron.server.BoundNode) SocketAddress(java.net.SocketAddress)

Example 3 with BoundCluster

use of com.datastax.oss.simulacron.server.BoundCluster in project java-driver by datastax.

the class ProtocolVersionMixedClusterIT method should_downgrade_if_peer_does_not_support_negotiated_version.

@Test
public void should_downgrade_if_peer_does_not_support_negotiated_version() {
    DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withBoolean(DefaultDriverOption.METADATA_SCHEMA_ENABLED, false).build();
    try (BoundCluster simulacron = mixedVersions("3.0.0", "2.2.0", "2.1.0");
        BoundNode contactPoint = simulacron.node(0);
        CqlSession session = (CqlSession) SessionUtils.baseBuilder().addContactPoint(contactPoint.inetSocketAddress()).withConfigLoader(loader).build()) {
        InternalDriverContext context = (InternalDriverContext) session.getContext();
        // General version should have been downgraded to V3
        assertThat(context.getProtocolVersion()).isEqualTo(DefaultProtocolVersion.V3);
        // But control connection should still be using protocol V4 since node0 supports V4
        assertThat(context.getControlConnection().channel().protocolVersion()).isEqualTo(DefaultProtocolVersion.V4);
        assertThat(queries(simulacron)).hasSize(4);
        assertThat(protocolQueries(contactPoint, 4)).containsExactly(// Initial connection with protocol v4
        "SELECT cluster_name FROM system.local", "SELECT * FROM system.local", "SELECT * FROM system.peers_v2", "SELECT * FROM system.peers");
    }
}
Also used : DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) BoundCluster(com.datastax.oss.simulacron.server.BoundCluster) BoundNode(com.datastax.oss.simulacron.server.BoundNode) CqlSession(com.datastax.oss.driver.api.core.CqlSession) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) Test(org.junit.Test)

Example 4 with BoundCluster

use of com.datastax.oss.simulacron.server.BoundCluster in project java-driver by datastax.

the class ConnectIT method should_mark_unreachable_contact_points_as_local_and_schedule_reconnections.

/**
 * Test for JAVA-2177. This ensures that even if the first attempted contact point is unreachable,
 * its distance is set to LOCAL and reconnections are scheduled.
 */
@Test
public void should_mark_unreachable_contact_points_as_local_and_schedule_reconnections() {
    // Reject connections only on one node
    BoundCluster boundCluster = SIMULACRON_RULE.cluster();
    boundCluster.node(0).rejectConnections(0, RejectScope.STOP);
    try (CqlSession session = SessionUtils.newSession(SIMULACRON_RULE)) {
        Map<UUID, Node> nodes = session.getMetadata().getNodes();
        // Node states are updated asynchronously, so guard against race conditions
        await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).untilAsserted(() -> {
            // Before JAVA-2177, this would fail every other time because if the node was tried
            // first for the initial connection, it was marked down and not passed to
            // LBP.init(), and therefore stayed at distance IGNORED.
            Node node0 = nodes.get(boundCluster.node(0).getHostId());
            assertThat(node0.getState()).isEqualTo(NodeState.DOWN);
            assertThat(node0.getDistance()).isEqualTo(NodeDistance.LOCAL);
            assertThat(node0.getOpenConnections()).isEqualTo(0);
            assertThat(node0.isReconnecting()).isTrue();
            Node node1 = nodes.get(boundCluster.node(1).getHostId());
            assertThat(node1.getState()).isEqualTo(NodeState.UP);
            assertThat(node1.getDistance()).isEqualTo(NodeDistance.LOCAL);
            // control + regular
            assertThat(node1.getOpenConnections()).isEqualTo(2);
            assertThat(node1.isReconnecting()).isFalse();
        });
    }
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) BoundCluster(com.datastax.oss.simulacron.server.BoundCluster) UUID(java.util.UUID) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Example 5 with BoundCluster

use of com.datastax.oss.simulacron.server.BoundCluster in project java-driver by datastax.

the class ProtocolVersionMixedClusterIT method should_keep_current_if_supported_by_all_peers.

@Test
public void should_keep_current_if_supported_by_all_peers() {
    DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withBoolean(DefaultDriverOption.METADATA_SCHEMA_ENABLED, false).build();
    try (BoundCluster simulacron = mixedVersions("3.0.0", "2.2.0", "3.11");
        BoundNode contactPoint = simulacron.node(0);
        CqlSession session = (CqlSession) SessionUtils.baseBuilder().addContactPoint(contactPoint.inetSocketAddress()).withConfigLoader(loader).build()) {
        InternalDriverContext context = (InternalDriverContext) session.getContext();
        assertThat(context.getProtocolVersion()).isEqualTo(DefaultProtocolVersion.V4);
        assertThat(queries(simulacron)).hasSize(4);
        assertThat(protocolQueries(contactPoint, 4)).containsExactly(// Initial connection with protocol v4
        "SELECT cluster_name FROM system.local", "SELECT * FROM system.local", "SELECT * FROM system.peers_v2", "SELECT * FROM system.peers");
    }
}
Also used : DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) BoundCluster(com.datastax.oss.simulacron.server.BoundCluster) BoundNode(com.datastax.oss.simulacron.server.BoundNode) CqlSession(com.datastax.oss.driver.api.core.CqlSession) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) Test(org.junit.Test)

Aggregations

BoundCluster (com.datastax.oss.simulacron.server.BoundCluster)7 CqlSession (com.datastax.oss.driver.api.core.CqlSession)6 Test (org.junit.Test)6 BoundNode (com.datastax.oss.simulacron.server.BoundNode)5 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)3 Node (com.datastax.oss.driver.api.core.metadata.Node)2 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)2 UUID (java.util.UUID)2 Metric (com.codahale.metrics.Metric)1 ReactiveRow (com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow)1 UnsupportedProtocolVersionException (com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException)1 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)1 DriverContext (com.datastax.oss.driver.api.core.context.DriverContext)1 LoadBalancingPolicy (com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy)1 NodeDistance (com.datastax.oss.driver.api.core.loadbalancing.NodeDistance)1 DefaultNodeMetric (com.datastax.oss.driver.api.core.metrics.DefaultNodeMetric)1 Metrics (com.datastax.oss.driver.api.core.metrics.Metrics)1 UnavailableException (com.datastax.oss.driver.api.core.servererrors.UnavailableException)1 Request (com.datastax.oss.driver.api.core.session.Request)1 Session (com.datastax.oss.driver.api.core.session.Session)1