Search in sources :

Example 1 with NodeStateListener

use of com.datastax.oss.driver.api.core.metadata.NodeStateListener in project java-driver by datastax.

the class NodeStateIT method should_signal_non_contact_points_as_added.

@Test
public void should_signal_non_contact_points_as_added() {
    // Since we need to observe the behavior of non-contact points, build a dedicated session with
    // just one contact point.
    Iterator<EndPoint> contactPoints = simulacron.getContactPoints().iterator();
    EndPoint endPoint1 = contactPoints.next();
    EndPoint endPoint2 = contactPoints.next();
    NodeStateListener localNodeStateListener = mock(NodeStateListener.class);
    DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withDuration(DefaultDriverOption.RECONNECTION_BASE_DELAY, Duration.ofHours(1)).withDuration(DefaultDriverOption.RECONNECTION_MAX_DELAY, Duration.ofHours(1)).withInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE, 0).build();
    try (CqlSession localSession = (CqlSession) SessionUtils.baseBuilder().addContactEndPoint(endPoint1).withNodeStateListener(localNodeStateListener).withConfigLoader(loader).build()) {
        Metadata metadata = localSession.getMetadata();
        Node localMetadataNode1 = metadata.findNode(endPoint1).orElseThrow(AssertionError::new);
        Node localMetadataNode2 = metadata.findNode(endPoint2).orElseThrow(AssertionError::new);
        // Successful contact point goes to up directly
        verify(localNodeStateListener, timeout(500)).onUp(localMetadataNode1);
        // Non-contact point only added since we don't have a connection or events for it yet
        verify(localNodeStateListener, timeout(500)).onAdd(localMetadataNode2);
    }
}
Also used : NodeStateListener(com.datastax.oss.driver.api.core.metadata.NodeStateListener) 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) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) DefaultEndPoint(com.datastax.oss.driver.internal.core.metadata.DefaultEndPoint) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Example 2 with NodeStateListener

use of com.datastax.oss.driver.api.core.metadata.NodeStateListener in project java-driver by datastax.

the class DefaultDriverContext method buildNodeStateListener.

protected NodeStateListener buildNodeStateListener(NodeStateListener nodeStateListenerFromBuilder) {
    List<NodeStateListener> listeners = new ArrayList<>();
    if (nodeStateListenerFromBuilder != null) {
        listeners.add(nodeStateListenerFromBuilder);
    }
    DefaultDriverOption newOption = DefaultDriverOption.METADATA_NODE_STATE_LISTENER_CLASSES;
    @SuppressWarnings("deprecation") DefaultDriverOption legacyOption = DefaultDriverOption.METADATA_NODE_STATE_LISTENER_CLASS;
    DriverExecutionProfile profile = config.getDefaultProfile();
    if (profile.isDefined(newOption)) {
        listeners.addAll(Reflection.buildFromConfigList(this, newOption, NodeStateListener.class, "com.datastax.oss.driver.internal.core.metadata"));
    }
    if (profile.isDefined(legacyOption)) {
        LOG.warn("Option {} has been deprecated and will be removed in a future release; please use option {} instead.", legacyOption, newOption);
        Reflection.buildFromConfig(this, legacyOption, NodeStateListener.class, "com.datastax.oss.driver.internal.core.metadata").ifPresent(listeners::add);
    }
    if (listeners.isEmpty()) {
        return new NoopNodeStateListener(this);
    } else if (listeners.size() == 1) {
        return listeners.get(0);
    } else {
        return new MultiplexingNodeStateListener(listeners);
    }
}
Also used : NoopNodeStateListener(com.datastax.oss.driver.internal.core.metadata.NoopNodeStateListener) MultiplexingNodeStateListener(com.datastax.oss.driver.internal.core.metadata.MultiplexingNodeStateListener) NodeStateListener(com.datastax.oss.driver.api.core.metadata.NodeStateListener) MultiplexingNodeStateListener(com.datastax.oss.driver.internal.core.metadata.MultiplexingNodeStateListener) NoopNodeStateListener(com.datastax.oss.driver.internal.core.metadata.NoopNodeStateListener) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) ArrayList(java.util.ArrayList)

Example 3 with NodeStateListener

use of com.datastax.oss.driver.api.core.metadata.NodeStateListener in project java-driver by datastax.

the class NodeStateIT method should_remove_invalid_contact_point.

@Test
public void should_remove_invalid_contact_point() {
    Iterator<EndPoint> contactPoints = simulacron.getContactPoints().iterator();
    EndPoint endPoint1 = contactPoints.next();
    EndPoint endPoint2 = contactPoints.next();
    NodeStateListener localNodeStateListener = mock(NodeStateListener.class);
    // Initialize the driver with 1 wrong address and 1 valid address
    EndPoint wrongContactPoint = withUnusedPort(endPoint1);
    DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withDuration(DefaultDriverOption.RECONNECTION_BASE_DELAY, Duration.ofHours(1)).withDuration(DefaultDriverOption.RECONNECTION_MAX_DELAY, Duration.ofHours(1)).build();
    try (CqlSession localSession = (CqlSession) SessionUtils.baseBuilder().addContactEndPoint(endPoint1).addContactEndPoint(wrongContactPoint).withNodeStateListener(localNodeStateListener).withConfigLoader(loader).build()) {
        Metadata metadata = localSession.getMetadata();
        assertThat(metadata.findNode(wrongContactPoint)).isEmpty();
        Node localMetadataNode1 = metadata.findNode(endPoint1).orElseThrow(AssertionError::new);
        Node localMetadataNode2 = metadata.findNode(endPoint2).orElseThrow(AssertionError::new);
        // The order of the calls is not deterministic because contact points are shuffled, but it
        // does not matter here since Mockito.verify does not enforce order.
        verify(localNodeStateListener, timeout(500)).onRemove(nodeCaptor.capture());
        assertThat(nodeCaptor.getValue().getEndPoint()).isEqualTo(wrongContactPoint);
        verify(localNodeStateListener, timeout(500)).onUp(localMetadataNode1);
        verify(localNodeStateListener, timeout(500)).onAdd(localMetadataNode2);
    // Note: there might be an additional onDown for wrongContactPoint if it was hit first at
    // init. This is hard to test since the node was removed later, so we simply don't call
    // verifyNoMoreInteractions.
    }
}
Also used : NodeStateListener(com.datastax.oss.driver.api.core.metadata.NodeStateListener) 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) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) DefaultEndPoint(com.datastax.oss.driver.internal.core.metadata.DefaultEndPoint) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Example 4 with NodeStateListener

use of com.datastax.oss.driver.api.core.metadata.NodeStateListener 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 5 with NodeStateListener

use of com.datastax.oss.driver.api.core.metadata.NodeStateListener 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)

Aggregations

NodeStateListener (com.datastax.oss.driver.api.core.metadata.NodeStateListener)5 CqlSession (com.datastax.oss.driver.api.core.CqlSession)4 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)4 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)4 BoundNode (com.datastax.oss.simulacron.server.BoundNode)4 Test (org.junit.Test)4 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)3 Metadata (com.datastax.oss.driver.api.core.metadata.Metadata)3 Node (com.datastax.oss.driver.api.core.metadata.Node)3 DefaultEndPoint (com.datastax.oss.driver.internal.core.metadata.DefaultEndPoint)3 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)1 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)1 MultiplexingNodeStateListener (com.datastax.oss.driver.internal.core.metadata.MultiplexingNodeStateListener)1 NoopNodeStateListener (com.datastax.oss.driver.internal.core.metadata.NoopNodeStateListener)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1