Search in sources :

Example 6 with BoundNode

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

the class NodeStateIT method should_mark_unreachable_contact_point_down.

@Test
public void should_mark_unreachable_contact_point_down() {
    // This time we connect with two valid contact points, but is unresponsive, it should be marked
    // down
    Iterator<BoundNode> simulacronNodes = simulacron.cluster().getNodes().iterator();
    BoundNode localSimulacronNode1 = simulacronNodes.next();
    BoundNode localSimulacronNode2 = simulacronNodes.next();
    InetSocketAddress address1 = localSimulacronNode1.inetSocketAddress();
    InetSocketAddress address2 = localSimulacronNode2.inetSocketAddress();
    NodeStateListener localNodeStateListener = mock(NodeStateListener.class);
    localSimulacronNode2.stop();
    try {
        // Since contact points are shuffled, we have a 50% chance that our bad contact point will be
        // hit first. So we retry the scenario a few times if needed.
        DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withDuration(DefaultDriverOption.RECONNECTION_BASE_DELAY, Duration.ofHours(1)).withDuration(DefaultDriverOption.RECONNECTION_MAX_DELAY, Duration.ofHours(1)).build();
        for (int i = 0; i < 10; i++) {
            try (CqlSession localSession = (CqlSession) SessionUtils.baseBuilder().addContactPoint(address1).addContactPoint(address2).withNodeStateListener(localNodeStateListener).withConfigLoader(loader).build()) {
                Metadata metadata = localSession.getMetadata();
                Node localMetadataNode1 = metadata.findNode(address1).orElseThrow(AssertionError::new);
                Node localMetadataNode2 = metadata.findNode(address2).orElseThrow(AssertionError::new);
                if (localMetadataNode2.getState() == NodeState.DOWN) {
                    // Stopped node was tried first and marked down, that's our target scenario
                    verify(localNodeStateListener, timeout(500)).onDown(localMetadataNode2);
                    verify(localNodeStateListener, timeout(500)).onUp(localMetadataNode1);
                    verify(localNodeStateListener, timeout(500)).onSessionReady(localSession);
                    verifyNoMoreInteractions(localNodeStateListener);
                    return;
                } else {
                    // Stopped node was not tried
                    assertThat(localMetadataNode2).isUnknown();
                    verify(localNodeStateListener, timeout(500)).onUp(localMetadataNode1);
                    verifyNoMoreInteractions(localNodeStateListener);
                }
            }
            reset(localNodeStateListener);
        }
        fail("Couldn't get the driver to try stopped node first (tried 5 times)");
    } finally {
        localSimulacronNode2.acceptConnections();
    }
}
Also used : NodeStateListener(com.datastax.oss.driver.api.core.metadata.NodeStateListener) InetSocketAddress(java.net.InetSocketAddress) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) Node(com.datastax.oss.driver.api.core.metadata.Node) BoundNode(com.datastax.oss.simulacron.server.BoundNode) Metadata(com.datastax.oss.driver.api.core.metadata.Metadata) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) BoundNode(com.datastax.oss.simulacron.server.BoundNode) CqlSession(com.datastax.oss.driver.api.core.CqlSession) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) DefaultEndPoint(com.datastax.oss.driver.internal.core.metadata.DefaultEndPoint) Test(org.junit.Test)

Example 7 with BoundNode

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

the class NodeStateIT method should_force_immediate_reconnection_when_up_topology_event.

@Test
public void should_force_immediate_reconnection_when_up_topology_event() throws InterruptedException {
    // This test requires a longer reconnection interval, so create a separate driver instance
    DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withDuration(DefaultDriverOption.RECONNECTION_BASE_DELAY, Duration.ofHours(1)).withDuration(DefaultDriverOption.RECONNECTION_MAX_DELAY, Duration.ofHours(1)).build();
    NodeStateListener localNodeStateListener = mock(NodeStateListener.class);
    try (CqlSession session = SessionUtils.newSession(simulacron, null, localNodeStateListener, null, null, loader)) {
        BoundNode localSimulacronNode = simulacron.cluster().getNodes().iterator().next();
        assertThat(localSimulacronNode).isNotNull();
        DefaultNode localMetadataNode = (DefaultNode) session.getMetadata().findNode(localSimulacronNode.inetSocketAddress()).orElseThrow(AssertionError::new);
        // UP fired a first time as part of the init process
        verify(localNodeStateListener, timeout(500)).onUp(localMetadataNode);
        localSimulacronNode.stop();
        await().alias("Node going down").pollInterval(500, TimeUnit.MILLISECONDS).untilAsserted(() -> assertThat(localMetadataNode).isDown().hasOpenConnections(0).isReconnecting());
        verify(localNodeStateListener, timeout(500)).onDown(localMetadataNode);
        expect(NodeStateEvent.changed(NodeState.UP, NodeState.DOWN, localMetadataNode));
        localSimulacronNode.acceptConnections();
        ((InternalDriverContext) session.getContext()).getEventBus().fire(TopologyEvent.suggestUp(localMetadataNode.getBroadcastRpcAddress().get()));
        await().alias("Node coming back up").pollInterval(500, TimeUnit.MILLISECONDS).untilAsserted(() -> assertThat(localMetadataNode).isUp().isNotReconnecting());
        verify(localNodeStateListener, timeout(500).times(2)).onUp(localMetadataNode);
        expect(NodeStateEvent.changed(NodeState.DOWN, NodeState.UP, localMetadataNode));
    }
}
Also used : NodeStateListener(com.datastax.oss.driver.api.core.metadata.NodeStateListener) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) BoundNode(com.datastax.oss.simulacron.server.BoundNode) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Example 8 with BoundNode

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

Example 9 with BoundNode

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

the class AllLoadBalancingPoliciesSimulacronIT method degradeLocalDc.

private List<Node> degradeLocalDc(CqlSession session) {
    // stop 1 replica and 2 non-replicas in dc1
    List<Node> localReplicas = replicasInDc(session, "dc1");
    assertThat(localReplicas).hasSize(3);
    BoundNode replica1 = findNode(localReplicas.get(0));
    List<Node> localOthers = nonReplicasInDc(session, "dc1");
    assertThat(localOthers).hasSize(2);
    BoundNode other1 = findNode(localOthers.get(0));
    BoundNode other2 = findNode(localOthers.get(1));
    replica1.stop();
    other1.stop();
    other2.stop();
    awaitDown(localReplicas.get(0), localOthers.get(0), localOthers.get(1));
    return localReplicas.subList(1, 3);
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) BoundNode(com.datastax.oss.simulacron.server.BoundNode) BoundNode(com.datastax.oss.simulacron.server.BoundNode)

Example 10 with BoundNode

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

the class ProtocolVersionMixedClusterIT method should_not_downgrade_and_force_down_old_nodes_if_version_forced.

@Test
public void should_not_downgrade_and_force_down_old_nodes_if_version_forced() {
    DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withString(DefaultDriverOption.PROTOCOL_VERSION, "V4").withBoolean(DefaultDriverOption.METADATA_SCHEMA_ENABLED, false).build();
    try (BoundCluster simulacron = mixedVersions("3.0.0", "2.2.0", "2.0.0");
        BoundNode contactPoint = simulacron.node(0);
        CqlSession session = (CqlSession) SessionUtils.baseBuilder().addContactPoint(contactPoint.inetSocketAddress()).withConfigLoader(loader).build()) {
        assertThat(session.getContext().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");
    // Note: the 2.0.0 would be forced down if we try to open a connection to it. We can't check
    // that here because Simulacron can't prime STARTUP requests.
    }
}
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) Test(org.junit.Test)

Aggregations

BoundNode (com.datastax.oss.simulacron.server.BoundNode)11 CqlSession (com.datastax.oss.driver.api.core.CqlSession)7 Test (org.junit.Test)7 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)5 BoundCluster (com.datastax.oss.simulacron.server.BoundCluster)5 Node (com.datastax.oss.driver.api.core.metadata.Node)3 Metadata (com.datastax.oss.driver.api.core.metadata.Metadata)2 NodeStateListener (com.datastax.oss.driver.api.core.metadata.NodeStateListener)2 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)2 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)2 InetSocketAddress (java.net.InetSocketAddress)2 UnsupportedProtocolVersionException (com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException)1 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)1 DefaultEndPoint (com.datastax.oss.driver.internal.core.metadata.DefaultEndPoint)1 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)1 SocketAddress (java.net.SocketAddress)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)1 Before (org.junit.Before)1