/** * Forcefully shuts down the connection to this tablet server and fails all the outstanding RPCs. * Only use when shutting down a client. * * @return deferred object to use to track the shutting down of this connection */ public Deferred<Void> shutdown() { // First, check whether we have RPCs in flight and cancel them. for (Iterator<KuduRpc<?>> ite = rpcs_inflight.values().iterator(); ite.hasNext(); ) { KuduRpc<?> rpc = ite.next(); rpc.errback(new ConnectionResetException(null)); ite.remove(); } // Same for the pending RPCs. synchronized (this) { if (pending_rpcs != null) { for (Iterator<KuduRpc<?>> ite = pending_rpcs.iterator(); ite.hasNext(); ) { ite.next().errback(new ConnectionResetException(null)); ite.remove(); } } } final Channel chancopy = chan; if (chancopy == null) { return Deferred.fromResult(null); } if (chancopy.isConnected()) { Channels.disconnect(chancopy); // ... this is going to set it to null. // At this point, all in-flight RPCs are going to be failed. } if (chancopy.isBound()) { Channels.unbind(chancopy); } // It's OK to call close() on a Channel if it's already closed. final ChannelFuture future = Channels.close(chancopy); // Now wrap the ChannelFuture in a Deferred. final Deferred<Void> d = new Deferred<Void>(); // Opportunistically check if it's already completed successfully. if (future.isSuccess()) { d.callback(null); } else { // If we get here, either the future failed (yeah, that sounds weird) // or the future hasn't completed yet (heh). future.addListener( new ChannelFutureListener() { public void operationComplete(final ChannelFuture future) { if (future.isSuccess()) { d.callback(null); return; } final Throwable t = future.getCause(); if (t instanceof Exception) { d.callback(t); } else { // Wrap the Throwable because Deferred doesn't handle Throwables, // it only uses Exception. d.callback( new NonRecoverableException("Failed to shutdown: " + TabletClient.this, t)); } } }); } return d; }
private void sendByteArray(ChannelHandlerContext ctx, byte[] buffer) { try { Channel channel = ctx.getChannel(); ChannelFuture future = Channels.future(ctx.getChannel()); ChannelBuffer b = ChannelBuffers.dynamicBuffer(); // first send encoded key bytes size b.writeInt(buffer.length); // then the public key b.writeBytes(buffer); Channels.write(ctx, future, b); } catch (Exception e) { e.printStackTrace(); } }
private static FilteredPipeline findAccepting(final HttpRequest request) { final FilteredPipeline candidate = null; for (final FilteredPipeline f : pipelines) { if (f.checkAccepts(request)) { return f; } } if (request.getHeader(HttpHeaders.Names.HOST).contains("amazonaws.com") || request.getHeader(HttpHeaders.Names.HOST).contains(subDomain.get())) { String hostHeader = request.getHeader(HttpHeaders.Names.HOST); LOG.debug("Trying to intercept request for " + hostHeader); for (final FilteredPipeline f : pipelines) { if (Ats.from(f).has(ComponentPart.class)) { Class<? extends ComponentId> compIdClass = Ats.from(f).get(ComponentPart.class).value(); ComponentId compId = ComponentIds.lookup(compIdClass); if (Ats.from(compIdClass).has(PublicService.class)) { if (request.getHeaderNames().contains("SOAPAction") && f.addHandlers(Channels.pipeline()).get(SoapHandler.class) == null) { continue; // Skip pipeline which doesn't handle SOAP for this SOAP request } else if (!request.getHeaderNames().contains("SOAPAction") && f.addHandlers(Channels.pipeline()).get(SoapHandler.class) != null) { continue; // Skip pipeline which handles SOAP for this non-SOAP request } LOG.debug("Maybe intercepting: " + hostHeader + " using " + f.getClass()); if (Ats.from(compIdClass).has(AwsServiceName.class) && request .getHeader(HttpHeaders.Names.HOST) .matches( "[\\w\\.-_]*" + compId.getAwsServiceName() + "\\.\\w+\\.amazonaws.com")) { return f; // Return pipeline which can handle the request for // ${service}.${region}.amazonaws.com } else if (request .getHeader(HttpHeaders.Names.HOST) .matches("[\\w\\.-_]*" + compId.name() + "\\." + subDomain.get())) { return f; // Return pipeline which can handle the request for // ${service}.${system.dns.dnsdomain} } } } } } if (candidate == null) { for (final FilteredPipeline f : internalPipelines) { if (f.checkAccepts(request)) { return f; } } } return candidate; }
public static void handle(String service, MuleMessage responseMessage, BaseMessage request) { BaseMessage reply = null; if (responseMessage.getExceptionPayload() == null) { reply = (BaseMessage) responseMessage.getPayload(); } else { Throwable t = responseMessage.getExceptionPayload().getException(); t = (t.getCause() == null) ? t : t.getCause(); reply = new EucalyptusErrorMessageType(service, request, t.getMessage()); } String corrId = reply.getCorrelationId(); EventRecord.here( EuareReplyQueue.class, EventType.MSG_REPLY, reply.getCorrelationId(), reply.getClass().getSimpleName()) .debug(); try { Context context = Contexts.lookup(corrId); Channel channel = context.getChannel(); Channels.write(channel, reply); Contexts.clear(context); } catch (NoSuchContextException e) { LOG.trace(e, e); } }
public void operationComplete(ChannelFuture closeNotifyFuture) throws Exception { if (!(closeNotifyFuture.getCause() instanceof ClosedChannelException)) { Channels.close(context, e.getFuture()); } else { e.getFuture().setSuccess(); } }
private void cleanup(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { try { ChannelBuffer cumulation = this.cumulation; if (cumulation == null) { return; } else { this.cumulation = null; } if (cumulation.readable()) { // Make sure all frames are read before notifying a closed // channel. callDecode(ctx, ctx.getChannel(), cumulation, null); } // Call decodeLast() finally. Please note that decodeLast() is // called even if there's nothing more to read from the buffer to // notify a user that the connection was closed explicitly. Object partialFrame = decodeLast(ctx, ctx.getChannel(), cumulation); if (partialFrame != null) { Channels.fireMessageReceived(ctx, partialFrame, null); } } finally { ctx.sendUpstream(e); } }
/** * Constructs a valid request and passes it on to the next handler. It also creates the * 'StoreClient' object corresponding to the store name specified in the REST request. * * @param requestValidator The Validator object used to construct the request object * @param ctx Context of the Netty channel * @param messageEvent Message Event used to write the response / exception */ @Override protected void registerRequest( RestRequestValidator requestValidator, ChannelHandlerContext ctx, MessageEvent messageEvent) { // At this point we know the request is valid and we have a // error handler. So we construct the composite Voldemort // request object. CompositeVoldemortRequest<ByteArray, byte[]> requestObject = requestValidator.constructCompositeVoldemortRequestObject(); if (requestObject != null) { DynamicTimeoutStoreClient<ByteArray, byte[]> storeClient = null; if (!requestValidator.getStoreName().equalsIgnoreCase(RestMessageHeaders.SCHEMATA_STORE)) { storeClient = this.fatClientMap.get(requestValidator.getStoreName()); if (storeClient == null) { logger.error("Error when getting store. Non Existing store client."); RestErrorHandler.writeErrorResponse( messageEvent, HttpResponseStatus.BAD_REQUEST, "Non Existing store client. Critical error."); return; } } else { requestObject.setOperationType(VoldemortOpCode.GET_METADATA_OP_CODE); } CoordinatorStoreClientRequest coordinatorRequest = new CoordinatorStoreClientRequest(requestObject, storeClient); Channels.fireMessageReceived(ctx, coordinatorRequest); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { Throwable cause = e.getCause(); if (cause instanceof IOException) { if (cause instanceof ClosedChannelException) { synchronized (ignoreClosedChannelExceptionLock) { if (ignoreClosedChannelException > 0) { ignoreClosedChannelException--; logger.debug("Swallowing an exception raised while " + "writing non-app data", cause); return; } } } else if (engine.isOutboundDone()) { String message = String.valueOf(cause.getMessage()).toLowerCase(); if (IGNORABLE_ERROR_MESSAGE.matcher(message).matches()) { // It is safe to ignore the 'connection reset by peer' or // 'broken pipe' error after sending closure_notify. logger.debug( "Swallowing a 'connection reset by peer / " + "broken pipe' error occurred while writing " + "'closure_notify'", cause); // Close the connection explicitly just in case the transport // did not close the connection automatically. Channels.close(ctx, succeededFuture(e.getChannel())); return; } } } ctx.sendUpstream(e); }
@Override public ChannelPipeline getPipeline() throws Exception { final ChannelPipeline p = Channels.pipeline(); p.addLast("sm", ipcMessageHandler); // upstream 6 return p; }
public synchronized void disableFraming(final ChannelHandlerContext ctx) { this.framingEnabled = false; if (this.cumulation != null && this.cumulation.readable()) { final ChannelBuffer spareBytes = this.cumulation.readBytes(this.cumulation.readableBytes()); Channels.fireMessageReceived(ctx, spareBytes); } }
@Override public void exceptionCaught(final ChannelHandlerContext ctx, final ExceptionEvent event) { final Throwable e = event.getCause(); final Channel c = event.getChannel(); if (e instanceof RejectedExecutionException) { LOG.warn( getPeerUuidLoggingString() + "RPC rejected by the executor," + " ignore this if we're shutting down", e); } else if (e instanceof ReadTimeoutException) { LOG.debug(getPeerUuidLoggingString() + "Encountered a read timeout"); // Doing the cleanup here since we want to invalidate all the RPCs right _now_, and not let // the ReplayingDecoder continue decoding through Channels.close() below. cleanup(c); } else { LOG.error(getPeerUuidLoggingString() + "Unexpected exception from downstream on " + c, e); // For any other exception, likely a connection error, we clear the leader state // for those tablets that this TS is the cached leader of. kuduClient.demoteAsLeaderForAllTablets(this); } if (c.isOpen()) { Channels.close(c); // Will trigger channelClosed(), which will cleanup() } else { // else: presumably a connection timeout. cleanup(c); // => need to cleanup() from here directly. } }
/** Quick and dirty way to close a connection to a tablet server, if it wasn't already closed. */ @VisibleForTesting void disconnect() { Channel chancopy = chan; if (chancopy != null && chancopy.isConnected()) { Channels.disconnect(chancopy); } }
@Nullable @Override public Future<Callbacks.ConstantAffection> request( String ownerClassName, String fieldName, int accessFlags, boolean fieldRemoved, boolean accessChanged) { final CmdlineRemoteProto.Message.BuilderMessage.ConstantSearchTask.Builder task = CmdlineRemoteProto.Message.BuilderMessage.ConstantSearchTask.newBuilder(); task.setOwnerClassName(ownerClassName); task.setFieldName(fieldName); task.setAccessFlags(accessFlags); task.setIsAccessChanged(accessChanged); task.setIsFieldRemoved(fieldRemoved); final ConstantSearchFuture future = new ConstantSearchFuture(BuildSession.this); final ConstantSearchFuture prev = mySearchTasks.put(new Pair<String, String>(ownerClassName, fieldName), future); if (prev != null) { prev.setDone(); } Channels.write( myChannel, CmdlineProtoUtil.toMessage( mySessionId, CmdlineRemoteProto.Message.BuilderMessage.newBuilder() .setType(CmdlineRemoteProto.Message.BuilderMessage.Type.CONSTANT_SEARCH_TASK) .setConstantSearchTask(task.build()) .build())); return future; }
public ChannelPipeline getPipeline() throws Exception { log.debug("Creating client channel pipeline"); ChannelPipeline pipeline = Channels.pipeline(); SSLEngine engine = sslContext.createSSLEngine(); engine.setUseClientMode(true); SslHandler sslHandler = new SslHandler(engine); sslHandler.setCloseOnSSLException(true); pipeline.addLast("ssl", sslHandler); pipeline.addLast("chunker", new ChunkedWriteHandler()); pipeline.addLast( "framer", new DelimiterBasedFrameDecoder(protocol.maxHeaderLength(), protocol.headerDelimiter())); pipeline.addLast("stringDecoder", new StringDecoder(protocol.headerCharset())); pipeline.addLast("stringEncoder", new StringEncoder(protocol.headerCharset())); HeaderCodec headerCodec = new HeaderCodec(protocol); pipeline.addLast("headerDecoder", headerCodec.decoder()); pipeline.addLast("headerEncoder", headerCodec.encoder()); pipeline.addLast("client", new ClientHandler(store)); return pipeline; }
/* (non-Javadoc) * @see org.jboss.netty.channel.SimpleChannelHandler#messageReceived(org.jboss.netty.channel.ChannelHandlerContext, org.jboss.netty.channel.MessageEvent) */ @Override public void messageReceived(final ChannelHandlerContext context, final MessageEvent e) throws Exception { String decoded; synchronized (stringWriter) { // extract the buffer from the message event final Object message = e.getMessage(); if (!(message instanceof ChannelBuffer)) { context.sendUpstream(e); return; } final ChannelBuffer buffer = (ChannelBuffer) message; if (!buffer.readable()) { return; } // read all bytes from the buffer to the decoder stream final byte[] bytes = new byte[buffer.readableBytes()]; buffer.readBytes(bytes); writerOutputStream.write(bytes); writerOutputStream.flush(); decoded = stringWriter.toString(); stringWriter.getBuffer().setLength(0); } // push all successfully decoded characters upstream Channels.fireMessageReceived(context, decoded, e.getRemoteAddress()); }
public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); addLengthFieldPipes(pipeline); BlockingReadHandler<ChannelBuffer> reader = new BlockingReadHandler<ChannelBuffer>(new ArrayBlockingQueue<ChannelEvent>(3, false)); pipeline.addLast("blockingHandler", reader); return pipeline; }
@Test public void testChannelClosedEventFromReal_replicatedOnVirtual() { upstreamEvents.events.clear(); Channels.fireChannelClosed(realChannelFactory.createdChannel); assertEquals(1, upstreamEvents.events.size()); checkIsUpstreamChannelStateEvent( upstreamEvents.events.poll(), virtualChannel, ChannelState.OPEN, Boolean.FALSE); }
@Test public void testChannelUnboundEventFromReal_replicatedOnVirtual() { upstreamEvents.events.clear(); Channels.fireChannelUnbound(realChannelFactory.createdChannel); assertEquals(1, upstreamEvents.events.size()); checkIsUpstreamChannelStateEvent( upstreamEvents.events.poll(), virtualChannel, ChannelState.BOUND, null); }
@Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("monitor", new MonitorChannelHandler(byteCounterMonitor)); addLengthFieldPipes(pipeline, frameLength); pipeline.addLast("serverHandler", this); return pipeline; }
public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("encoder", new HttpResponseEncoder()); pipeline.addLast("JsonConverter", new JsonEncoder()); pipeline.addLast("executionHandler", executionHandler); pipeline.addLast("handler", new WeightedRandomNumberGenerator(connection)); return pipeline; }
/** {@inheritDoc} */ @Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipleline = Channels.pipeline(); pipleline.addLast("encode", new SocksMessageEncoder()); pipleline.addLast("decode", new FixedLengthFrameDecoder(fixedLength)); pipleline.addLast("handler", new NettyServerHandler()); return pipleline; }
@Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("decoder", new HttpRequestDecoder()); pipeline.addLast("encoder", new HttpResponseEncoder()); pipeline.addLast("handler", handler); return pipeline; }
@Override public ChannelPipeline getPipeline() throws Exception { final ChannelPipeline p = Channels.pipeline(); p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder()); // upstream 2 p.addLast("frameEncoder", FRAME_ENCODER); // downstream 2 p.addLast("ipcSessionManager", ipcSessionManager); // upstream 5 return p; }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent me) throws Exception { if (!(me.getMessage() instanceof HttpRequest)) { ctx.sendUpstream(me); return; } HttpRequest request = (HttpRequest) me.getMessage(); Object response; // Look up resource String path = request.getUri(); String host = request.getHeader("host"); log.debug("Received request for path:" + path); boolean showHtml = false; if (path.endsWith("?html")) { showHtml = true; path = path.replace("?html", ""); } for (String service : resources.keySet()) { log.debug("Available Service: " + service); } Model model = resources.get(path); if (model != null) { if (request.getMethod() == HttpMethod.GET) { if (showHtml) { String html = HtmlCreator.createModelPage(model, new URI(path), host); response = ChannelBuffers.wrappedBuffer(html.getBytes(Charset.forName("UTF-8"))); log.debug("Returned html page for resource: " + path); } else { response = new SelfDescription(model, new URI(request.getUri())); log.debug("Resource found: " + path); } } else { response = new DefaultHttpResponse( request.getProtocolVersion(), HttpResponseStatus.METHOD_NOT_ALLOWED); log.debug("Method not allowed: " + request.getMethod()); } } else { response = HttpResponseFactory.createHttpResponse( request.getProtocolVersion(), HttpResponseStatus.NOT_FOUND); log.debug("Resource not found: " + path); } // Send response ChannelFuture future = Channels.write(ctx.getChannel(), response); future.addListener(ChannelFutureListener.CLOSE); }
@Test public void testChannelBoundEventFromReal_replicatedOnVirtual() { upstreamEvents.events.clear(); InetSocketAddress boundAddr = InetSocketAddress.createUnresolved("mycomputer", 12345); Channels.fireChannelBound(realChannelFactory.createdChannel, boundAddr); assertEquals(1, upstreamEvents.events.size()); checkIsUpstreamChannelStateEvent( upstreamEvents.events.poll(), virtualChannel, ChannelState.BOUND, boundAddr); }
@Override public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception { UnixTime time = (UnixTime) e.getMessage(); ChannelBuffer buf = buffer(4); buf.writeInt(time.getValue()); Channels.write(ctx, e.getFuture(), buf); }
@Test public void testNettyConfigBuilder() { NettyConfigBuilder configBuilder = new NettyConfigBuilder(); configBuilder.getServerSocketChannelConfig().setReceiveBufferSize(10000); configBuilder.getServerSocketChannelConfig().setBacklog(1000); configBuilder.getServerSocketChannelConfig().setReuseAddress(true); ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory()); bootstrap.setOptions(configBuilder.getOptions()); bootstrap.setPipelineFactory(Channels.pipelineFactory(Channels.pipeline())); Channel serverChannel = bootstrap.bind(new InetSocketAddress(port)); Assert.assertEquals( ((ServerSocketChannelConfig) serverChannel.getConfig()).getReceiveBufferSize(), 10000); Assert.assertEquals(((ServerSocketChannelConfig) serverChannel.getConfig()).getBacklog(), 1000); Assert.assertTrue(((ServerSocketChannelConfig) serverChannel.getConfig()).isReuseAddress()); }
@Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("requestDecoder", REQUEST_DECODER); pipeline.addLast("handler", new RequestHandler()); return pipeline; }
public SendPingRequestEvent( final ID messageReference, final Channel channel, final PingRequest message) { super( messageReference, MessageEventType.SEND_PING_REQUEST, channel, Channels.future(channel, false), message); }
private void writeReset(final InetSocketAddress remoteEndpoint, final int messageID) { try { CoapMessage resetMessage = CoapMessage.createEmptyReset(messageID); ChannelFuture future = Channels.future(ctx.getChannel()); Channels.write(this.getChannelHandlerContext(), future, resetMessage, remoteEndpoint); future.addListener( new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { log.info("RST for message ID {} succesfully sent to {}.", messageID, remoteEndpoint); } }); } catch (IllegalArgumentException e) { log.error("This should never happen.", e); } }