Search in sources :

Example 6 with DefaultNode

use of com.datastax.oss.driver.internal.core.metadata.DefaultNode in project java-driver by datastax.

the class InsightsClientTest method mockDefaultDriverContext.

private DefaultDriverContext mockDefaultDriverContext() throws UnknownHostException {
    DefaultDriverContext context = mock(DefaultDriverContext.class);
    mockConnectionPools(context);
    MetadataManager manager = mock(MetadataManager.class);
    when(context.getMetadataManager()).thenReturn(manager);
    Metadata metadata = mock(Metadata.class);
    when(manager.getMetadata()).thenReturn(metadata);
    Node node = mock(Node.class);
    when(node.getExtras()).thenReturn(ImmutableMap.of(DseNodeProperties.DSE_VERSION, Objects.requireNonNull(Version.parse("6.0.5"))));
    when(metadata.getNodes()).thenReturn(ImmutableMap.of(UUID.randomUUID(), node));
    DriverExecutionProfile defaultExecutionProfile = mockDefaultExecutionProfile();
    DriverExecutionProfile nonDefaultExecutionProfile = mockNonDefaultRequestTimeoutExecutionProfile();
    Map<String, String> startupOptions = new HashMap<>();
    startupOptions.put(StartupOptionsBuilder.CLIENT_ID_KEY, "client-id");
    startupOptions.put(StartupOptionsBuilder.APPLICATION_VERSION_KEY, "1.0.0");
    startupOptions.put(StartupOptionsBuilder.APPLICATION_NAME_KEY, "app-name");
    startupOptions.put(StartupOptionsBuilder.DRIVER_VERSION_KEY, "2.x");
    startupOptions.put(StartupOptionsBuilder.DRIVER_NAME_KEY, "DataStax Enterprise Java Driver");
    when(context.getStartupOptions()).thenReturn(startupOptions);
    when(context.getProtocolVersion()).thenReturn(DSE_V2);
    DefaultNode contactPoint = mock(DefaultNode.class);
    EndPoint contactEndPoint = mock(EndPoint.class);
    when(contactEndPoint.resolve()).thenReturn(new InetSocketAddress("127.0.0.1", 9999));
    when(contactPoint.getEndPoint()).thenReturn(contactEndPoint);
    when(manager.getContactPoints()).thenReturn(ImmutableSet.of(contactPoint));
    DriverConfig driverConfig = mock(DriverConfig.class);
    when(context.getConfig()).thenReturn(driverConfig);
    Map<String, DriverExecutionProfile> profiles = ImmutableMap.of("default", defaultExecutionProfile, "non-default", nonDefaultExecutionProfile);
    Mockito.<Map<String, ? extends DriverExecutionProfile>>when(driverConfig.getProfiles()).thenReturn(profiles);
    when(driverConfig.getDefaultProfile()).thenReturn(defaultExecutionProfile);
    ControlConnection controlConnection = mock(ControlConnection.class);
    DriverChannel channel = mock(DriverChannel.class);
    EndPoint controlConnectionEndpoint = mock(EndPoint.class);
    when(controlConnectionEndpoint.resolve()).thenReturn(new InetSocketAddress("127.0.0.1", 10));
    when(channel.getEndPoint()).thenReturn(controlConnectionEndpoint);
    when(channel.localAddress()).thenReturn(new InetSocketAddress("127.0.0.1", 10));
    when(controlConnection.channel()).thenReturn(channel);
    when(context.getControlConnection()).thenReturn(controlConnection);
    return context;
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) SessionStateForNode(com.datastax.dse.driver.internal.core.insights.schema.SessionStateForNode) Node(com.datastax.oss.driver.api.core.metadata.Node) Metadata(com.datastax.oss.driver.api.core.metadata.Metadata) InsightMetadata(com.datastax.dse.driver.internal.core.insights.schema.InsightMetadata) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) MetadataManager(com.datastax.oss.driver.internal.core.metadata.MetadataManager) DefaultDriverContext(com.datastax.oss.driver.internal.core.context.DefaultDriverContext) ControlConnection(com.datastax.oss.driver.internal.core.control.ControlConnection) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) DriverConfig(com.datastax.oss.driver.api.core.config.DriverConfig)

Example 7 with DefaultNode

use of com.datastax.oss.driver.internal.core.metadata.DefaultNode in project java-driver by datastax.

the class DefaultTokenMapTest method mockNode.

private DefaultNode mockNode(String dc, String rack, Set<String> tokens) {
    DefaultNode node = mock(DefaultNode.class);
    when(node.getDatacenter()).thenReturn(dc);
    when(node.getRack()).thenReturn(rack);
    when(node.getRawTokens()).thenReturn(tokens);
    return node;
}
Also used : DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode)

Example 8 with DefaultNode

use of com.datastax.oss.driver.internal.core.metadata.DefaultNode 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 9 with DefaultNode

use of com.datastax.oss.driver.internal.core.metadata.DefaultNode in project java-driver by datastax.

the class DefaultTokenMap method buildTokenToPrimaryAndRing.

private static TokenToPrimaryAndRing buildTokenToPrimaryAndRing(Collection<Node> nodes, TokenFactory tokenFactory) {
    ImmutableMap.Builder<Token, Node> tokenToPrimaryBuilder = ImmutableMap.builder();
    SortedSet<Token> sortedTokens = new TreeSet<>();
    for (Node node : nodes) {
        for (String tokenString : ((DefaultNode) node).getRawTokens()) {
            Token token = tokenFactory.parse(tokenString);
            sortedTokens.add(token);
            tokenToPrimaryBuilder.put(token, node);
        }
    }
    return new TokenToPrimaryAndRing(tokenToPrimaryBuilder.build(), ImmutableList.copyOf(sortedTokens));
}
Also used : TreeSet(java.util.TreeSet) Node(com.datastax.oss.driver.api.core.metadata.Node) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) Token(com.datastax.oss.driver.api.core.metadata.token.Token) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)

Aggregations

DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)9 Node (com.datastax.oss.driver.api.core.metadata.Node)4 CqlSession (com.datastax.oss.driver.api.core.CqlSession)3 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)3 Test (org.junit.Test)3 EventBus (com.datastax.oss.driver.internal.core.context.EventBus)2 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)2 NonNull (edu.umd.cs.findbugs.annotations.NonNull)2 Duration (java.time.Duration)2 InsightMetadata (com.datastax.dse.driver.internal.core.insights.schema.InsightMetadata)1 SessionStateForNode (com.datastax.dse.driver.internal.core.insights.schema.SessionStateForNode)1 DriverConfig (com.datastax.oss.driver.api.core.config.DriverConfig)1 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)1 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)1 Metadata (com.datastax.oss.driver.api.core.metadata.Metadata)1 NodeStateListener (com.datastax.oss.driver.api.core.metadata.NodeStateListener)1 Token (com.datastax.oss.driver.api.core.metadata.token.Token)1 DriverChannel (com.datastax.oss.driver.internal.core.channel.DriverChannel)1 DefaultDriverContext (com.datastax.oss.driver.internal.core.context.DefaultDriverContext)1 ControlConnection (com.datastax.oss.driver.internal.core.control.ControlConnection)1