@Override public void sendResponse(Throwable error) throws IOException { BytesStreamOutput stream; try { stream = BytesStreamOutput.Cached.cached(); writeResponseExceptionHeader(stream); RemoteTransportException tx = new RemoteTransportException( transport.nodeName(), transport.wrapAddress(channel.getLocalAddress()), action, error); ThrowableObjectOutputStream too = new ThrowableObjectOutputStream(stream); too.writeObject(tx); too.close(); } catch (NotSerializableException e) { stream = BytesStreamOutput.Cached.cached(); writeResponseExceptionHeader(stream); RemoteTransportException tx = new RemoteTransportException( transport.nodeName(), transport.wrapAddress(channel.getLocalAddress()), action, new NotSerializableTransportException(error)); ThrowableObjectOutputStream too = new ThrowableObjectOutputStream(stream); too.writeObject(tx); too.close(); } ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(stream.copiedByteArray()); buffer.setInt(0, buffer.writerIndex() - 4); // update real size. channel.write(buffer); }
public void testThatDefaultProfileInheritsFromStandardSettings() throws Exception { Settings settings = Settings.builder() .put("network.host", host) .put(TransportSettings.PORT.getKey(), 0) .put("transport.profiles.client1.port", 0) .build(); ThreadPool threadPool = new ThreadPool("tst"); try (NettyTransport transport = startNettyTransport(settings, threadPool)) { assertEquals(1, transport.profileBoundAddresses().size()); assertEquals(1, transport.boundAddress().boundAddresses().length); } finally { terminate(threadPool); } }
public void testThatDefaultProfilePortOverridesGeneralConfiguration() throws Exception { Settings settings = Settings.builder() .put("network.host", host) .put(TransportSettings.PORT.getKey(), 22) // will not actually bind to this .put("transport.profiles.default.port", 0) .build(); ThreadPool threadPool = new ThreadPool("tst"); try (NettyTransport transport = startNettyTransport(settings, threadPool)) { assertEquals(0, transport.profileBoundAddresses().size()); assertEquals(1, transport.boundAddress().boundAddresses().length); } finally { terminate(threadPool); } }
public void testThatProfileWithoutPortSettingsFails() throws Exception { Settings settings = Settings.builder() .put("network.host", host) .put(TransportSettings.PORT.getKey(), 0) .put("transport.profiles.client1.whatever", "foo") .build(); ThreadPool threadPool = new ThreadPool("tst"); try (NettyTransport transport = startNettyTransport(settings, threadPool)) { assertEquals(0, transport.profileBoundAddresses().size()); assertEquals(1, transport.boundAddress().boundAddresses().length); } finally { terminate(threadPool); } }
private NettyTransport startNettyTransport(Settings settings, ThreadPool threadPool) { BigArrays bigArrays = new MockBigArrays( new PageCacheRecycler(settings, threadPool), new NoneCircuitBreakerService()); NettyTransport nettyTransport = new NettyTransport( settings, threadPool, new NetworkService(settings), bigArrays, Version.CURRENT, new NamedWriteableRegistry()); nettyTransport.start(); assertThat(nettyTransport.lifecycleState(), is(Lifecycle.State.STARTED)); return nettyTransport; }
public void testThatProfileWithoutValidNameIsIgnored() throws Exception { Settings settings = Settings.builder() .put("network.host", host) .put(TransportSettings.PORT.getKey(), 0) // mimics someone trying to define a profile for .local which is the profile for a node // request to itself .put( "transport.profiles." + TransportService.DIRECT_RESPONSE_PROFILE + ".port", 22) // will not actually bind to this .put("transport.profiles..port", 23) // will not actually bind to this .build(); ThreadPool threadPool = new ThreadPool("tst"); try (NettyTransport transport = startNettyTransport(settings, threadPool)) { assertEquals(0, transport.profileBoundAddresses().size()); assertEquals(1, transport.boundAddress().boundAddresses().length); } finally { terminate(threadPool); } }
private void handlerResponseError(StreamInput buffer, final TransportResponseHandler handler) { Throwable error; try { ThrowableObjectInputStream ois = new ThrowableObjectInputStream(buffer, transport.settings().getClassLoader()); error = (Throwable) ois.readObject(); } catch (Throwable e) { error = new TransportSerializationException( "Failed to deserialize exception response from stream", e); } handleException(handler, error); }
public MessageChannelHandler(NettyTransport transport, ESLogger logger) { this.threadPool = transport.threadPool(); this.transportServiceAdapter = transport.transportServiceAdapter(); this.transport = transport; this.logger = logger; }
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { transport.exceptionCaught(ctx, e); }
/** Starts listening for the given member. */ private void listen(Address address, Consumer<Connection> listener, ThreadContext context) { channelGroup = new DefaultChannelGroup("catalyst-acceptor-channels", GlobalEventExecutor.INSTANCE); handler = new ServerHandler(connections, listener, context, transport.properties()); final ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap .group(transport.eventLoopGroup()) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.DEBUG)) .childHandler( new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); if (transport.properties().sslEnabled()) { pipeline.addFirst( new SslHandler(new NettyTls(transport.properties()).initSslEngine(false))); } pipeline.addLast(FIELD_PREPENDER); pipeline.addLast( new LengthFieldBasedFrameDecoder( transport.properties().maxFrameSize(), 0, 4, 0, 4)); pipeline.addLast(handler); } }) .option(ChannelOption.SO_BACKLOG, transport.properties().acceptBacklog()) .option(ChannelOption.TCP_NODELAY, transport.properties().tcpNoDelay()) .option(ChannelOption.SO_REUSEADDR, transport.properties().reuseAddress()) .childOption(ChannelOption.ALLOCATOR, ALLOCATOR) .childOption(ChannelOption.SO_KEEPALIVE, transport.properties().tcpKeepAlive()); if (transport.properties().sendBufferSize() != -1) { bootstrap.childOption(ChannelOption.SO_SNDBUF, transport.properties().sendBufferSize()); } if (transport.properties().receiveBufferSize() != -1) { bootstrap.childOption(ChannelOption.SO_RCVBUF, transport.properties().receiveBufferSize()); } LOGGER.info("Binding to {}", address); ChannelFuture bindFuture = bootstrap.bind(address.socketAddress()); bindFuture.addListener( (ChannelFutureListener) channelFuture -> { if (channelFuture.isSuccess()) { listening = true; context .executor() .execute( () -> { LOGGER.info("Listening at {}", bindFuture.channel().localAddress()); listenFuture.complete(null); }); } else { context.execute(() -> listenFuture.completeExceptionally(channelFuture.cause())); } }); channelGroup.add(bindFuture.channel()); }