Search in sources :

Example 21 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.

the class DefaultTopologyMonitor method refreshNode.

@Override
public CompletionStage<Optional<NodeInfo>> refreshNode(Node node) {
    if (closeFuture.isDone()) {
        return CompletableFutures.failedFuture(new IllegalStateException("closed"));
    }
    LOG.debug("[{}] Refreshing info for {}", logPrefix, node);
    DriverChannel channel = controlConnection.channel();
    EndPoint localEndPoint = channel.getEndPoint();
    if (node.getEndPoint().equals(channel.getEndPoint())) {
        // refreshNode is called for nodes that just came up. If the control node just came up, it
        // means the control connection just reconnected, which means we did a full node refresh. So
        // we don't need to process this call.
        LOG.debug("[{}] Ignoring refresh of control node", logPrefix);
        return CompletableFuture.completedFuture(Optional.empty());
    } else if (node.getBroadcastAddress().isPresent()) {
        CompletionStage<AdminResult> query;
        if (isSchemaV2) {
            query = query(channel, "SELECT * FROM " + getPeerTableName() + " WHERE peer = :address and peer_port = :port", ImmutableMap.of("address", node.getBroadcastAddress().get().getAddress(), "port", node.getBroadcastAddress().get().getPort()));
        } else {
            query = query(channel, "SELECT * FROM " + getPeerTableName() + " WHERE peer = :address", ImmutableMap.of("address", node.getBroadcastAddress().get().getAddress()));
        }
        return query.thenApply(result -> firstPeerRowAsNodeInfo(result, localEndPoint));
    } else {
        return query(channel, "SELECT * FROM " + getPeerTableName()).thenApply(result -> findInPeers(result, node.getHostId(), localEndPoint));
    }
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) SocketAddress(java.net.SocketAddress) ImmutableSet(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet) LoggerFactory(org.slf4j.LoggerFactory) AdminRow(com.datastax.oss.driver.internal.core.adminrequest.AdminRow) ThreadSafe(net.jcip.annotations.ThreadSafe) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) CompletableFuture(java.util.concurrent.CompletableFuture) ControlConnection(com.datastax.oss.driver.internal.core.control.ControlConnection) AdminResult(com.datastax.oss.driver.internal.core.adminrequest.AdminResult) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Version(com.datastax.oss.driver.api.core.Version) UnexpectedResponseException(com.datastax.oss.driver.internal.core.adminrequest.UnexpectedResponseException) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) Error(com.datastax.oss.protocol.internal.response.Error) AdminRequestHandler(com.datastax.oss.driver.internal.core.adminrequest.AdminRequestHandler) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Duration(java.time.Duration) Map(java.util.Map) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) VisibleForTesting(com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting) CompletableFutures(com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) DseNodeProperties(com.datastax.dse.driver.api.core.metadata.DseNodeProperties) Set(java.util.Set) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) ProtocolConstants(com.datastax.oss.protocol.internal.ProtocolConstants) Objects(java.util.Objects) DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) Nullable(edu.umd.cs.findbugs.annotations.Nullable) Optional(java.util.Optional) Collections(java.util.Collections) DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) CompletionStage(java.util.concurrent.CompletionStage)

Example 22 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.

the class DefaultTopologyMonitor method getNewNodeInfo.

@Override
public CompletionStage<Optional<NodeInfo>> getNewNodeInfo(InetSocketAddress broadcastRpcAddress) {
    if (closeFuture.isDone()) {
        return CompletableFutures.failedFuture(new IllegalStateException("closed"));
    }
    LOG.debug("[{}] Fetching info for new node {}", logPrefix, broadcastRpcAddress);
    DriverChannel channel = controlConnection.channel();
    EndPoint localEndPoint = channel.getEndPoint();
    return query(channel, "SELECT * FROM " + getPeerTableName()).thenApply(result -> findInPeers(result, broadcastRpcAddress, localEndPoint));
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint)

Example 23 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.

the class ControlConnectionTest method should_init_with_first_contact_point_if_reachable.

@Test
public void should_init_with_first_contact_point_if_reachable() {
    // Given
    DriverChannel channel1 = newMockDriverChannel(1);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node1, channel1).build();
    // When
    CompletionStage<Void> initFuture = controlConnection.init(false, false, false);
    factoryHelper.waitForCall(node1);
    // Then
    assertThatStage(initFuture).isSuccess(v -> assertThat(controlConnection.channel()).isEqualTo(channel1));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node1));
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 24 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.

the class ControlConnectionTest method should_close_channel_when_closing.

@Test
public void should_close_channel_when_closing() {
    // Given
    DriverChannel channel1 = newMockDriverChannel(1);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node1, channel1).build();
    CompletionStage<Void> initFuture = controlConnection.init(false, false, false);
    factoryHelper.waitForCall(node1);
    assertThatStage(initFuture).isSuccess();
    // When
    CompletionStage<Void> closeFuture = controlConnection.forceCloseAsync();
    // Then
    assertThatStage(closeFuture).isSuccess();
    verify(channel1, VERIFY_TIMEOUT).forceClose();
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 25 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.

the class ControlConnectionTest method should_force_reconnection_even_if_connected.

@Test
public void should_force_reconnection_even_if_connected() {
    // Given
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node1, channel1).failure(node1, "mock failure").success(node2, channel2).build();
    CompletionStage<Void> initFuture = controlConnection.init(false, false, false);
    factoryHelper.waitForCall(node1);
    assertThatStage(initFuture).isSuccess(v -> assertThat(controlConnection.channel()).isEqualTo(channel1));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node1));
    // When
    controlConnection.reconnectNow();
    // Then
    factoryHelper.waitForCall(node1);
    factoryHelper.waitForCall(node2);
    await().untilAsserted(() -> assertThat(controlConnection.channel()).isEqualTo(channel2));
    verify(channel1, VERIFY_TIMEOUT).forceClose();
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelClosed(node1));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node2));
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Aggregations

DriverChannel (com.datastax.oss.driver.internal.core.channel.DriverChannel)54 Test (org.junit.Test)35 MockChannelFactoryHelper (com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper)30 CompletableFuture (java.util.concurrent.CompletableFuture)17 InOrder (org.mockito.InOrder)13 Node (com.datastax.oss.driver.api.core.metadata.Node)9 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)6 DriverChannelOptions (com.datastax.oss.driver.internal.core.channel.DriverChannelOptions)6 ChannelPromise (io.netty.channel.ChannelPromise)5 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)4 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)4 InetSocketAddress (java.net.InetSocketAddress)4 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 NodeUnavailableException (com.datastax.oss.driver.api.core.NodeUnavailableException)3 AdminResult (com.datastax.oss.driver.internal.core.adminrequest.AdminResult)3 EventCallback (com.datastax.oss.driver.internal.core.channel.EventCallback)3 Before (org.junit.Before)3 Version (com.datastax.oss.driver.api.core.Version)2 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)2 AdminRow (com.datastax.oss.driver.internal.core.adminrequest.AdminRow)2