private Byte readContinuationHeader( ChannelBuffer buffer, final Channel channel ) { byte[] header = new byte[2]; buffer.readBytes( header ); try { // Read request header and assert correct internal/application protocol version assertSameProtocolVersion( header, getInternalProtocolVersion(), applicationProtocolVersion ); } catch ( final IllegalProtocolVersionException e ) { // Version mismatch, fail with a good exception back to the client submitSilent( targetCallExecutor, () -> writeFailureResponse( e, newChunkingBuffer( channel ) ) ); return null; } return (byte) (header[0] & 0x1); }
protected void tryToFinishOffChannel( Channel channel, RequestContext slave ) { try { stopConversation( slave ); unmapSlave( channel ); } catch ( Throwable failure ) // Unknown error trying to finish off the tx { submitSilent( unfinishedTransactionExecutor, newTransactionFinisher( slave ) ); msgLog.warn( "Could not finish off dead channel", failure ); } }
private ChunkingChannelBuffer newChunkingBuffer( Channel channel ) { return newChunkingBuffer( ChannelBuffers.dynamicBuffer(), channel, chunkSize, getInternalProtocolVersion(), applicationProtocolVersion ); }
protected void handleRequest( ChannelBuffer buffer, final Channel channel ) Byte continuation = readContinuationHeader( buffer, channel ); if ( continuation == null ) RequestType type = getRequestContext( buffer.readByte() ); RequestContext context = readContext( buffer ); ChannelBuffer targetBuffer = mapSlave( channel, context ); partialRequest = new PartialRequest( type, context, targetBuffer ); partialRequests.put( channel, partialRequest ); type = getRequestContext( buffer.readByte() ); context = readContext( buffer ); targetBuffer = mapSlave( channel, context ); bufferToReadFrom = buffer; bufferToWriteTo = targetBuffer; ChunkingChannelBuffer chunkingBuffer = newChunkingBuffer( bufferToWriteTo, channel, chunkSize, getInternalProtocolVersion(), applicationProtocolVersion ); submitSilent( targetCallExecutor, new TargetCaller( type, channel, context, chunkingBuffer, bufferToReadFrom ) );
@Override public void messageReceived( ChannelHandlerContext ctx, MessageEvent event ) { try { ChannelBuffer message = (ChannelBuffer) event.getMessage(); handleRequest( message, event.getChannel() ); } catch ( Throwable e ) { msgLog.error( "Error handling request", e ); // Attempt to reply to the client ChunkingChannelBuffer buffer = newChunkingBuffer( event.getChannel() ); buffer.clear( /* failure = */true ); writeFailureResponse( e, buffer ); ctx.getChannel().close(); tryToCloseChannel( ctx.getChannel() ); throw e; } }
@Override public void run() { try { stopConversation( slave ); } catch ( Throwable e ) { // Introduce some delay here. it becomes like a busy wait if it never succeeds sleepNicely( 200 ); unfinishedTransactionExecutor.submit( this ); } }