Search in sources :

Example 11 with DriverChannel

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

the class ChannelSocketOptionsIT method should_report_socket_options.

@Test
public void should_report_socket_options() {
    Session session = SESSION_RULE.session();
    DriverExecutionProfile config = session.getContext().getConfig().getDefaultProfile();
    assertThat(config.getBoolean(SOCKET_TCP_NODELAY)).isTrue();
    assertThat(config.getBoolean(SOCKET_KEEP_ALIVE)).isFalse();
    assertThat(config.getBoolean(SOCKET_REUSE_ADDRESS)).isFalse();
    assertThat(config.getInt(SOCKET_LINGER_INTERVAL)).isEqualTo(10);
    assertThat(config.getInt(SOCKET_RECEIVE_BUFFER_SIZE)).isEqualTo(123456);
    assertThat(config.getInt(SOCKET_SEND_BUFFER_SIZE)).isEqualTo(123456);
    Node node = session.getMetadata().getNodes().values().iterator().next();
    if (session instanceof SessionWrapper) {
        session = ((SessionWrapper) session).getDelegate();
    }
    DriverChannel channel = ((DefaultSession) session).getChannel(node, "test");
    assertThat(channel).isNotNull();
    assertThat(channel.config()).isInstanceOf(SocketChannelConfig.class);
    SocketChannelConfig socketConfig = (SocketChannelConfig) channel.config();
    assertThat(socketConfig.isTcpNoDelay()).isTrue();
    assertThat(socketConfig.isKeepAlive()).isFalse();
    assertThat(socketConfig.isReuseAddress()).isFalse();
    assertThat(socketConfig.getSoLinger()).isEqualTo(10);
    RecvByteBufAllocator allocator = socketConfig.getRecvByteBufAllocator();
    assertThat(allocator).isInstanceOf(FixedRecvByteBufAllocator.class);
    assertThat(allocator.newHandle().guess()).isEqualTo(123456);
// cannot assert around SO_RCVBUF and SO_SNDBUF, such values are just hints
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator) RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) SocketChannelConfig(io.netty.channel.socket.SocketChannelConfig) Node(com.datastax.oss.driver.api.core.metadata.Node) DefaultSession(com.datastax.oss.driver.internal.core.session.DefaultSession) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Session(com.datastax.oss.driver.api.core.session.Session) DefaultSession(com.datastax.oss.driver.internal.core.session.DefaultSession) SessionWrapper(com.datastax.oss.driver.internal.core.session.SessionWrapper) Test(org.junit.Test)

Example 12 with DriverChannel

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

the class ChannelSet method getAvailableIds.

/**
 * @return the number of available stream ids on all channels in this channel set.
 */
int getAvailableIds() {
    int availableIds = 0;
    DriverChannel[] snapshot = this.channels;
    for (DriverChannel channel : snapshot) {
        availableIds += channel.getAvailableIds();
    }
    return availableIds;
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel)

Example 13 with DriverChannel

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

the class ChannelSet method getOrphanedIds.

/**
 * @return the number of stream ids for requests in all channels in this channel set that have
 *     either timed out or been cancelled, but for which we can't release the stream id because a
 *     request might still come from the server.
 */
int getOrphanedIds() {
    int orphanedIds = 0;
    DriverChannel[] snapshot = this.channels;
    for (DriverChannel channel : snapshot) {
        orphanedIds += channel.getOrphanedIds();
    }
    return orphanedIds;
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel)

Example 14 with DriverChannel

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

the class ChannelSet method next.

/**
 * @return null if the set is empty or all are full
 */
DriverChannel next() {
    DriverChannel[] snapshot = this.channels;
    switch(snapshot.length) {
        case 0:
            return null;
        case 1:
            DriverChannel onlyChannel = snapshot[0];
            return onlyChannel.preAcquireId() ? onlyChannel : null;
        default:
            for (int i = 0; i < MAX_ITERATIONS; i++) {
                DriverChannel best = null;
                int bestScore = 0;
                for (DriverChannel channel : snapshot) {
                    int score = channel.getAvailableIds();
                    if (score > bestScore) {
                        bestScore = score;
                        best = channel;
                    }
                }
                if (best == null) {
                    return null;
                } else if (best.preAcquireId()) {
                    return best;
                }
            }
            LOG.trace("Could not select a channel after {} iterations", MAX_ITERATIONS);
            return null;
    }
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel)

Example 15 with DriverChannel

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

the class ChannelPoolInitTest method should_initialize_when_all_channels_succeed.

@Test
public void should_initialize_when_all_channels_succeed() throws Exception {
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(3);
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    DriverChannel channel3 = newMockDriverChannel(3);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).success(node, channel2).success(node, channel3).build();
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 3);
    assertThatStage(poolFuture).isSuccess(pool -> assertThat(pool.channels).containsOnly(channel1, channel2, channel3));
    verify(eventBus, VERIFY_TIMEOUT.times(3)).fire(ChannelEvent.channelOpened(node));
    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