private void run() { // Check that a SaslAuthenticator can be provided by the configured // IAuthenticator. If not, don't start the server. IAuthenticator authenticator = DatabaseDescriptor.getAuthenticator(); if (authenticator.requireAuthentication() && !(authenticator instanceof ISaslAwareAuthenticator)) { logger.error("Not starting native transport as the configured IAuthenticator is not capable of SASL authentication"); isRunning.compareAndSet(true, false); return; } // Configure the server. eventExecutorGroup = new RequestThreadPoolExecutor(); boolean hasEpoll = enableEpoll ? Epoll.isAvailable() : false; if (hasEpoll) { workerGroup = new EpollEventLoopGroup(); logger.info("Netty using native Epoll event loop"); } else { workerGroup = new NioEventLoopGroup(); logger.info("Netty using Java NIO event loop"); } ServerBootstrap bootstrap = new ServerBootstrap() .group(workerGroup) .channel(hasEpoll ? EpollServerSocketChannel.class : NioServerSocketChannel.class) .childOption(ChannelOption.TCP_NODELAY, true) .childOption(ChannelOption.SO_LINGER, 0) .childOption(ChannelOption.SO_KEEPALIVE, DatabaseDescriptor.getRpcKeepAlive()) .childOption(ChannelOption.ALLOCATOR, CBUtil.allocator) .childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024) .childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); final EncryptionOptions.ClientEncryptionOptions clientEnc = DatabaseDescriptor.getClientEncryptionOptions(); if (clientEnc.enabled) { logger.info("Enabling encrypted CQL connections between client and server"); bootstrap.childHandler(new SecureInitializer(this, clientEnc)); } else { bootstrap.childHandler(new Initializer(this)); } // Bind and start to accept incoming connections. logger.info("Using Netty Version: {}", Version.identify().entrySet()); logger.info("Starting listening for CQL clients on {}...", socket); ChannelFuture bindFuture = bootstrap.bind(socket); if (!bindFuture.awaitUninterruptibly().isSuccess()) throw new IllegalStateException(String.format("Failed to bind port %d on %s.", socket.getPort(), socket.getAddress().getHostAddress())); connectionTracker.allChannels.add(bindFuture.channel()); isRunning.set(true); }
public synchronized void start() { if(isRunning()) return; // Configure the server. ServerBootstrap bootstrap = new ServerBootstrap() .channel(useEpoll ? EpollServerSocketChannel.class : NioServerSocketChannel.class) .childOption(ChannelOption.TCP_NODELAY, true) .childOption(ChannelOption.SO_LINGER, 0) .childOption(ChannelOption.SO_KEEPALIVE, DatabaseDescriptor.getRpcKeepAlive()) .childOption(ChannelOption.ALLOCATOR, CBUtil.allocator) .childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024) .childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); if (workerGroup != null) bootstrap = bootstrap.group(workerGroup); final EncryptionOptions.ClientEncryptionOptions clientEnc = DatabaseDescriptor.getClientEncryptionOptions(); if (this.useSSL) { if (clientEnc.optional) { logger.info("Enabling optionally encrypted CQL connections between client and server"); bootstrap.childHandler(new OptionalSecureInitializer(this, clientEnc)); } else { logger.info("Enabling encrypted CQL connections between client and server"); bootstrap.childHandler(new SecureInitializer(this, clientEnc)); } } else { bootstrap.childHandler(new Initializer(this)); } // Bind and start to accept incoming connections. logger.info("Using Netty Version: {}", Version.identify().entrySet()); logger.info("Starting listening for CQL clients on {} ({})...", socket, this.useSSL ? "encrypted" : "unencrypted"); ChannelFuture bindFuture = bootstrap.bind(socket); if (!bindFuture.awaitUninterruptibly().isSuccess()) throw new IllegalStateException(String.format("Failed to bind port %d on %s.", socket.getPort(), socket.getAddress().getHostAddress())); connectionTracker.allChannels.add(bindFuture.channel()); isRunning.set(true); }
@Test public void commandNotExecutedChannelClosesWhileFlush() { assumeTrue(Version.identify().get("netty-transport").artifactVersion().startsWith("4.0.2")); RedisCommands<String, String> connection = client.connect().sync(); RedisCommands<String, String> verificationConnection = client.connect().sync(); RedisChannelWriter<String, String> channelWriter = getRedisChannelHandler(connection).getChannelWriter(); connection.set(key, "1"); assertThat(verificationConnection.get(key)).isEqualTo("1"); final CountDownLatch block = new CountDownLatch(1); AsyncCommand<String, String, Object> command = new AsyncCommand<String, String, Object>(new Command<>(CommandType.INCR, new IntegerOutput(CODEC), new CommandArgs<>(CODEC).addKey(key))) { @Override public void encode(ByteBuf buf) { try { block.await(); } catch (InterruptedException e) { } super.encode(buf); } }; channelWriter.write(command); Channel channel = getChannel(getRedisChannelHandler(connection)); channel.unsafe().disconnect(channel.newPromise()); assertThat(channel.isOpen()).isFalse(); assertThat(command.isCancelled()).isFalse(); assertThat(command.isDone()).isFalse(); block.countDown(); assertThat(command.await(2, TimeUnit.SECONDS)).isTrue(); assertThat(command.isCancelled()).isFalse(); assertThat(command.isDone()).isTrue(); assertThat(verificationConnection.get(key)).isEqualTo("1"); assertThat(getDisconnectedBuffer(getRedisChannelHandler(connection))).isEmpty(); assertThat(getCommandBuffer(getRedisChannelHandler(connection))).isEmpty(); connection.close(); }
@Test public void commandNotFailedChannelClosesWhileFlush() throws Exception { assumeTrue(Version.identify().get("netty-transport").artifactVersion().startsWith("4.0.2")); RedisCommands<String, String> connection = client.connect().sync(); RedisCommands<String, String> verificationConnection = client.connect().sync(); RedisChannelWriter<String, String> channelWriter = getRedisChannelHandler(connection).getChannelWriter(); connection.set(key, "1"); assertThat(verificationConnection.get(key)).isEqualTo("1"); final CountDownLatch block = new CountDownLatch(1); ConnectionWatchdog connectionWatchdog = Connections.getConnectionWatchdog(connection.getStatefulConnection()); AsyncCommand<String, String, Object> command = getBlockOnEncodeCommand(block); channelWriter.write(command); connectionWatchdog.setReconnectSuspended(true); Channel channel = getChannel(getRedisChannelHandler(connection)); channel.unsafe().disconnect(channel.newPromise()); assertThat(channel.isOpen()).isFalse(); assertThat(command.isCancelled()).isFalse(); assertThat(command.isDone()).isFalse(); block.countDown(); assertThat(command.await(2, TimeUnit.SECONDS)).isFalse(); assertThat(command.isCancelled()).isFalse(); assertThat(command.isDone()).isFalse(); assertThat(verificationConnection.get(key)).isEqualTo("1"); assertThat(getDisconnectedBuffer(getRedisChannelHandler(connection))).isEmpty(); assertThat(getCommandBuffer(getRedisChannelHandler(connection))).isNotEmpty().contains(command); connection.close(); }
@Test public void commandRetriedChannelClosesWhileFlush() throws Exception { assumeTrue(Version.identify().get("netty-transport").artifactVersion().startsWith("4.0.2")); RedisCommands<String, String> connection = client.connect().sync(); RedisCommands<String, String> verificationConnection = client.connect().sync(); RedisChannelWriter<String, String> channelWriter = getRedisChannelHandler(connection).getChannelWriter(); connection.set(key, "1"); assertThat(verificationConnection.get(key)).isEqualTo("1"); final CountDownLatch block = new CountDownLatch(1); ConnectionWatchdog connectionWatchdog = Connections.getConnectionWatchdog(connection.getStatefulConnection()); AsyncCommand<String, String, Object> command = getBlockOnEncodeCommand(block); channelWriter.write(command); connectionWatchdog.setReconnectSuspended(true); Channel channel = getChannel(getRedisChannelHandler(connection)); channel.unsafe().disconnect(channel.newPromise()); assertThat(channel.isOpen()).isFalse(); assertThat(command.isCancelled()).isFalse(); assertThat(command.isDone()).isFalse(); block.countDown(); assertThat(command.await(2, TimeUnit.SECONDS)).isFalse(); connectionWatchdog.setReconnectSuspended(false); connectionWatchdog.scheduleReconnect(); assertThat(command.await(2, TimeUnit.SECONDS)).isTrue(); assertThat(command.isCancelled()).isFalse(); assertThat(command.isDone()).isTrue(); assertThat(verificationConnection.get(key)).isEqualTo("2"); assertThat(getDisconnectedBuffer(getRedisChannelHandler(connection))).isEmpty(); assertThat(getCommandBuffer(getRedisChannelHandler(connection))).isEmpty(); connection.close(); verificationConnection.close(); }