|
| 1 | +package org.infinispan.server.resp; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.concurrent.CompletionStage; |
| 5 | + |
| 6 | +import org.infinispan.commons.util.Util; |
| 7 | +import org.infinispan.util.concurrent.CompletionStages; |
| 8 | + |
| 9 | +import io.netty.buffer.ByteBuf; |
| 10 | +import io.netty.buffer.Unpooled; |
| 11 | +import io.netty.channel.ChannelHandlerContext; |
| 12 | +import io.netty.util.concurrent.FastThreadLocal; |
| 13 | + |
| 14 | +public class NewDecoder extends RespDecoder { |
| 15 | + public NewDecoder(NewRespHandler initialHandler) { |
| 16 | + super(initialHandler); |
| 17 | + initialHandler.writer = this::flushBuffer; |
| 18 | + initialHandler.allocator = this::retrieveBuffer; |
| 19 | + } |
| 20 | + |
| 21 | + protected static final int THREAD_LOCAL_CAPACITY = 1024; |
| 22 | + |
| 23 | + private ChannelHandlerContext ctx; |
| 24 | + |
| 25 | + private boolean isReading; |
| 26 | + private ByteBuf pendingBuffer; |
| 27 | + |
| 28 | + private static final FastThreadLocal<ByteBuf> BYTE_BUF_FAST_THREAD_LOCAL = new FastThreadLocal<>() { |
| 29 | + @Override |
| 30 | + protected ByteBuf initialValue() { |
| 31 | + return Unpooled.directBuffer(THREAD_LOCAL_CAPACITY, THREAD_LOCAL_CAPACITY); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + protected void onRemoval(ByteBuf value) { |
| 36 | + value.release(); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + protected void flushBuffer(ByteBuf buffer) { |
| 41 | + assert buffer == pendingBuffer : "Buffer mismatch expected " + pendingBuffer + " but was " + buffer; |
| 42 | + // Only flush the buffer it is done outside of the read loop, as we handle that ourselves |
| 43 | + if (!isReading) { |
| 44 | + ctx.writeAndFlush(pendingBuffer, ctx.voidPromise()); |
| 45 | + pendingBuffer = null; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private void flushPendingBuffer() { |
| 50 | + if (pendingBuffer != null) { |
| 51 | + ctx.writeAndFlush(pendingBuffer, ctx.voidPromise()); |
| 52 | + pendingBuffer = null; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void channelInactive(ChannelHandlerContext ctx) throws Exception { |
| 58 | + flushPendingBuffer(); |
| 59 | + super.channelInactive(ctx); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void handlerAdded(ChannelHandlerContext ctx) throws Exception { |
| 64 | + this.ctx = ctx; |
| 65 | + super.handlerAdded(ctx); |
| 66 | + } |
| 67 | + |
| 68 | + protected ByteBuf retrieveBuffer(int requiredBytes) { |
| 69 | + if (pendingBuffer != null) { |
| 70 | + if (requiredBytes < pendingBuffer.writableBytes()) { |
| 71 | + return pendingBuffer; |
| 72 | + } |
| 73 | + ctx.write(pendingBuffer, ctx.voidPromise()); |
| 74 | + pendingBuffer = null; |
| 75 | + } |
| 76 | + ByteBuf buf = BYTE_BUF_FAST_THREAD_LOCAL.get(); |
| 77 | + if (requiredBytes < buf.writableBytes()) { |
| 78 | + // This will reserve the buffer for our usage only, other channels in same event loop may try to reserve |
| 79 | + if (buf.refCnt() == 1) { |
| 80 | + buf.retain(); |
| 81 | + buf.clear(); |
| 82 | + pendingBuffer = buf; |
| 83 | + return buf; |
| 84 | + } |
| 85 | + } |
| 86 | + int reserveSize = Math.max(requiredBytes, 4096); |
| 87 | + pendingBuffer = ctx.alloc().buffer(reserveSize, reserveSize); |
| 88 | + return pendingBuffer; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { |
| 93 | + isReading = false; |
| 94 | + flushPendingBuffer(); |
| 95 | + super.channelReadComplete(ctx); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { |
| 100 | + isReading = true; |
| 101 | + super.channelRead(ctx, msg); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + protected boolean handleCommandAndArguments(ChannelHandlerContext ctx, String command, List<byte[]> arguments) { |
| 106 | + boolean canContinue = handleCommandAndArgumentsOverride(ctx, command, arguments); |
| 107 | + if (canContinue) { |
| 108 | + if (ctx.channel().bytesBeforeUnwritable() < pendingBuffer.readableBytes()) { |
| 109 | + ctx.writeAndFlush(pendingBuffer); |
| 110 | + pendingBuffer = null; |
| 111 | + // TODO: we should probably check for writeability still and block reading if possible until is cleared |
| 112 | + } |
| 113 | + } else { |
| 114 | + assert !ctx.channel().config().isAutoRead(); |
| 115 | + } |
| 116 | + return canContinue; |
| 117 | + } |
| 118 | + |
| 119 | + protected boolean handleCommandAndArgumentsOverride(ChannelHandlerContext ctx, String command, List<byte[]> arguments) { |
| 120 | + if (log.isTraceEnabled()) { |
| 121 | + log.tracef("Received command: %s with arguments %s for %s", command, Util.toStr(arguments), ctx.channel()); |
| 122 | + } |
| 123 | + |
| 124 | + CompletionStage<RespRequestHandler> stage = requestHandler.handleRequest(ctx, command, arguments); |
| 125 | + if (CompletionStages.isCompletedSuccessfully(stage)) { |
| 126 | + requestHandler = CompletionStages.join(stage); |
| 127 | + return true; |
| 128 | + } |
| 129 | + log.tracef("Disabling auto read for channel %s until previous command is complete", ctx.channel()); |
| 130 | + // Disable reading any more from socket - until command is complete |
| 131 | + ctx.channel().config().setAutoRead(false); |
| 132 | + stage.whenComplete((handler, t) -> { |
| 133 | + assert ctx.channel().eventLoop().inEventLoop(); |
| 134 | + log.tracef("Re-enabling auto read for channel %s as previous command is complete", ctx.channel()); |
| 135 | + ctx.channel().config().setAutoRead(true); |
| 136 | + if (t != null) { |
| 137 | + exceptionCaught(ctx, t); |
| 138 | + } else { |
| 139 | + // Instate the new handler if there was no exception |
| 140 | + requestHandler = handler; |
| 141 | + } |
| 142 | + |
| 143 | + ctx.read(); |
| 144 | + }); |
| 145 | + return false; |
| 146 | + } |
| 147 | +} |
0 commit comments