Search in sources :

Example 1 with BoundNode

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

the class AllLoadBalancingPoliciesSimulacronIT method should_use_local_replicas_when_dc_aware_and_token_aware_and_enough_local_replicas_up.

@Test
@DataProvider({ "BasicLoadBalancingPolicy,dc1,ONE", "BasicLoadBalancingPolicy,dc1,LOCAL_ONE", "BasicLoadBalancingPolicy,dc1,TWO", "BasicLoadBalancingPolicy,dc1,QUORUM", "BasicLoadBalancingPolicy,dc1,LOCAL_QUORUM", "DefaultLoadBalancingPolicy,dc1,ONE", "DefaultLoadBalancingPolicy,dc1,LOCAL_ONE", "DefaultLoadBalancingPolicy,dc1,TWO", "DefaultLoadBalancingPolicy,dc1,QUORUM", "DefaultLoadBalancingPolicy,dc1,LOCAL_QUORUM", "DcInferringLoadBalancingPolicy,dc1,ONE", "DcInferringLoadBalancingPolicy,dc1,LOCAL_ONE", "DcInferringLoadBalancingPolicy,dc1,TWO", "DcInferringLoadBalancingPolicy,dc1,QUORUM", "DcInferringLoadBalancingPolicy,dc1,LOCAL_QUORUM", "DcInferringLoadBalancingPolicy,null,ONE", "DcInferringLoadBalancingPolicy,null,LOCAL_ONE", "DcInferringLoadBalancingPolicy,null,TWO", "DcInferringLoadBalancingPolicy,null,QUORUM", "DcInferringLoadBalancingPolicy,null,LOCAL_QUORUM" })
public void should_use_local_replicas_when_dc_aware_and_token_aware_and_enough_local_replicas_up(String lbp, String dc, DefaultConsistencyLevel cl) {
    // given: DC is provided or inferred, token awareness enabled, remotes allowed, CL <= 2
    try (CqlSession session = newSession(lbp, dc, 2, true)) {
        // given: one replica and 2 non-replicas down in local DC, but CL <= 2 still achievable
        List<Node> aliveReplicas = degradeLocalDc(session);
        // when: a query is executed 50 times and some nodes are down in the local DC.
        for (int i = 0; i < 50; i++) {
            session.execute(SimpleStatement.newInstance(QUERY).setConsistencyLevel(cl).setRoutingKeyspace("test").setRoutingKey(ROUTING_KEY));
        }
        // then: all requests should be distributed to the remaining up replicas in local DC
        BoundNode alive1 = findNode(aliveReplicas.get(0));
        BoundNode alive2 = findNode(aliveReplicas.get(1));
        assertThat(queries(alive1).count() + queries(alive2).count()).isEqualTo(50);
        // then: no node in the remote DCs should get a request.
        assertThat(queries(1).count()).isEqualTo(0);
        assertThat(queries(2).count()).isEqualTo(0);
    }
}
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) CqlSession(com.datastax.oss.driver.api.core.CqlSession) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 2 with BoundNode

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

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

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

the class NodeTargetingIT method getNode.

private Node getNode(int id) {
    BoundNode boundNode = SIMULACRON_RULE.cluster().node(id);
    assertThat(boundNode).isNotNull();
    InetSocketAddress address = (InetSocketAddress) boundNode.getAddress();
    return SESSION_RULE.session().getMetadata().findNode(address).orElseThrow(() -> new AssertionError(String.format("Expected to find node %d in metadata", id)));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BoundNode(com.datastax.oss.simulacron.server.BoundNode)

Example 5 with BoundNode

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

the class NodeStateIT method setup.

@Before
public void setup() {
    inOrder = inOrder(nodeStateListener);
    AtomicBoolean nonInitialEvent = new AtomicBoolean(false);
    driverContext = (InternalDriverContext) sessionRule.session().getContext();
    driverContext.getEventBus().register(NodeStateEvent.class, (e) -> {
        // may not have depending on timing.
        if (!nonInitialEvent.get() && e.oldState == NodeState.UNKNOWN && e.newState == NodeState.UP) {
            return;
        }
        nonInitialEvent.set(true);
        stateEvents.add(e);
    });
    defaultLoadBalancingPolicy = (ConfigurableIgnoresPolicy) driverContext.getLoadBalancingPolicy(DriverExecutionProfile.DEFAULT_NAME);
    // Sanity check: the driver should have connected to simulacron
    await().alias("Connections established").pollInterval(500, TimeUnit.MILLISECONDS).until(() -> simulacron.cluster().getActiveConnections() == 5);
    // Find out which node is the control node, and identify the corresponding Simulacron and driver
    // metadata objects.
    simulacronControlNode = simulacronRegularNode = null;
    for (BoundNode boundNode : simulacron.cluster().getNodes()) {
        if (boundNode.getActiveConnections() == 3) {
            simulacronControlNode = boundNode;
        } else {
            simulacronRegularNode = boundNode;
        }
    }
    assertThat(simulacronControlNode).isNotNull();
    assertThat(simulacronRegularNode).isNotNull();
    Metadata metadata = sessionRule.session().getMetadata();
    metadataControlNode = (DefaultNode) metadata.findNode(simulacronControlNode.inetSocketAddress()).orElseThrow(AssertionError::new);
    metadataRegularNode = (DefaultNode) metadata.findNode(simulacronRegularNode.inetSocketAddress()).orElseThrow(AssertionError::new);
    // SessionRule uses all nodes as contact points, so we only get onUp notifications for them (no
    // onAdd)
    inOrder.verify(nodeStateListener, timeout(500)).onUp(metadataControlNode);
    inOrder.verify(nodeStateListener, timeout(500)).onUp(metadataRegularNode);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Metadata(com.datastax.oss.driver.api.core.metadata.Metadata) BoundNode(com.datastax.oss.simulacron.server.BoundNode) Before(org.junit.Before)

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