Example usage for io.netty.channel ChannelHandlerContext fireChannelRead

List of usage examples for io.netty.channel ChannelHandlerContext fireChannelRead

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext fireChannelRead.

Prototype

@Override
    ChannelHandlerContext fireChannelRead(Object msg);

Source Link

Usage

From source file:alluxio.worker.netty.AbstractReadHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object object) throws Exception {
    if (!acceptMessage(object)) {
        ctx.fireChannelRead(object);
        return;/*  w  ww.  jav a 2  s .  co  m*/
    }
    Protocol.ReadRequest msg = ((RPCProtoMessage) object).getMessage().asReadRequest();
    if (msg.getCancel()) {
        setCancel(ctx.channel());
        return;
    }

    // Expected state: context equals null as this handler is new for request, or the previous
    // context is not active (done / cancel / abort). Otherwise, notify the client an illegal state.
    // Note that, we reset the context before validation msg as validation may require to update
    // error in context.
    try (LockResource lr = new LockResource(mLock)) {
        Preconditions.checkState(mContext == null || !mContext.isPacketReaderActive());
        mContext = createRequestContext(msg);
    }

    validateReadRequest(msg);

    try (LockResource lr = new LockResource(mLock)) {
        mContext.setPosToQueue(mContext.getRequest().getStart());
        mContext.setPosToWrite(mContext.getRequest().getStart());
        mPacketReaderExecutor.submit(createPacketReader(mContext, ctx.channel()));
        mContext.setPacketReaderActive(true);
    }
}

From source file:alluxio.worker.netty.AbstractWriteHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object object) throws Exception {
    if (!acceptMessage(object)) {
        ctx.fireChannelRead(object);
        return;/* ww w .j  a v a 2 s .c  o  m*/
    }

    RPCProtoMessage msg = (RPCProtoMessage) object;
    Protocol.WriteRequest writeRequest = msg.getMessage().asWriteRequest();

    try (LockResource lr = new LockResource(mLock)) {
        boolean isNewContextCreated = false;
        if (mContext == null || mContext.isDoneUnsafe()) {
            // We create a new context if the previous request completes (done flag is true) or the
            // context is still null (an empty channel so far). And in this case, we create a new one as
            // catching exceptions and replying errors
            // leverages data structures in context, regardless of the request is valid or not.
            // TODO(binfan): remove the dependency on an instantiated request context which is required
            // to reply errors to client side.
            mContext = createRequestContext(writeRequest);
            isNewContextCreated = true;
        }
        // Only initialize (open the writers) if this is the first packet in the block/file.
        if (writeRequest.getOffset() == 0) {
            // Expected state: context equals null as this handler is new for request, or the previous
            // context is not active (done / cancel / abort). Otherwise, notify the client an illegal
            // state. Note that, we reset the context before validation msg as validation may require to
            // update error in context.
            Preconditions.checkState(isNewContextCreated);
            initRequestContext(mContext);
        }

        // If we have seen an error, return early and release the data. This can
        // happen for (1) those mis-behaving clients who first sends some invalid requests, then
        // then some random data, or (2) asynchronous requests arrive after the previous request fails
        // and triggers abortion. It can leak memory if we do not release buffers here.
        if (mContext.getError() != null) {
            if (msg.getPayloadDataBuffer() != null) {
                msg.getPayloadDataBuffer().release();
            }
            LOG.warn("Ignore the request {} due to the error {} on context", mContext.getRequest(),
                    mContext.getError());
            return;
        } else {
            // Validate the write request. The validation is performed only when no error is in the
            // context in order to prevent excessive logging on the subsequent arrived asynchronous
            // requests after a previous request fails and triggers the abortion
            validateWriteRequest(writeRequest, msg.getPayloadDataBuffer());
        }

        ByteBuf buf;
        if (writeRequest.getEof()) {
            buf = EOF;
        } else if (writeRequest.getCancel()) {
            buf = CANCEL;
        } else {
            DataBuffer dataBuffer = msg.getPayloadDataBuffer();
            Preconditions.checkState(dataBuffer != null && dataBuffer.getLength() > 0);
            Preconditions.checkState(dataBuffer.getNettyOutput() instanceof ByteBuf);
            buf = (ByteBuf) dataBuffer.getNettyOutput();
            mContext.setPosToQueue(mContext.getPosToQueue() + buf.readableBytes());
        }
        if (!mContext.isPacketWriterActive()) {
            mContext.setPacketWriterActive(true);
            mPacketWriterExecutor.submit(createPacketWriter(mContext, ctx.channel()));
        }
        mContext.getPackets().offer(buf);
        if (tooManyPacketsInFlight()) {
            NettyUtils.disableAutoRead(ctx.channel());
        }
    }
}

From source file:alluxio.worker.netty.AsyncCacheHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object object) throws Exception {
    if (object instanceof RPCProtoMessage && ((RPCProtoMessage) object).getMessage().isAsyncCacheRequest()) {
        Protocol.AsyncCacheRequest request = ((RPCProtoMessage) object).getMessage().asAsyncCacheRequest();
        mRequestManager.submitRequest(request);
        // Note that, because the client side of this RPC end point is fireAndForget, thus expecting
        // no response. No OK response will be returned here.
    } else {//from  w w  w.j  a  v  a2  s .c  om
        ctx.fireChannelRead(object);
    }
}

From source file:alluxio.worker.netty.DataServerHeartbeatHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object object) throws Exception {
    if (object instanceof RPCProtoMessage && ((RPCProtoMessage) object).getMessage().isHeartbeat()) {
        // do nothing
    } else {/*from   w w w. j a  v a 2s. c o  m*/
        ctx.fireChannelRead(object);
    }
}

From source file:alluxio.worker.netty.DataServerReadHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object object) throws Exception {
    if (!acceptMessage(object)) {
        ctx.fireChannelRead(object);
        return;// w w w.  j  a  v a2 s.  c  om
    }
    Protocol.ReadRequest msg = ((RPCProtoMessage) object).getMessage().getMessage();
    if (msg.getCancel()) {
        setCancel(ctx.channel());
        return;
    }

    reset();
    String error = validateReadRequest(msg);
    if (!error.isEmpty()) {
        setError(ctx.channel(),
                new Error(new IllegalArgumentException(error), true, Protocol.Status.Code.INVALID_ARGUMENT));
        return;
    }

    initializeRequest(msg);
    try (LockResource lr = new LockResource(mLock)) {
        mPosToQueue = mRequest.mStart;
        mPosToWrite = mRequest.mStart;

        mPacketReaderExecutor.submit(new PacketReader(ctx.channel()));
        mPacketReaderActive = true;
    }
}

From source file:alluxio.worker.netty.DataServerShortCircuitReadHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (!(msg instanceof RPCProtoMessage)) {
        ctx.fireChannelRead(msg);
        return;/*from  ww  w  . java 2  s .  co  m*/
    }

    ProtoMessage message = ((RPCProtoMessage) msg).getMessage();
    if (message.isLocalBlockOpenRequest()) {
        handleBlockOpenRequest(ctx, message.asLocalBlockOpenRequest());
    } else if (message.isLocalBlockCloseRequest()) {
        handleBlockCloseRequest(ctx, message.asLocalBlockCloseRequest());
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:alluxio.worker.netty.DataServerShortCircuitWriteHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (!(msg instanceof RPCProtoMessage)) {
        ctx.fireChannelRead(msg);
        return;/* w  w w. ja v  a2  s .c o  m*/
    }

    ProtoMessage message = ((RPCProtoMessage) msg).getMessage();
    if (message.isLocalBlockCreateRequest()) {
        handleBlockCreateRequest(ctx, message.asLocalBlockCreateRequest());
    } else if (message.isLocalBlockCompleteRequest()) {
        handleBlockCompleteRequest(ctx, message.asLocalBlockCompleteRequest());
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:alluxio.worker.netty.DataServerUnsupportedMessageHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object object) throws Exception {
    if (object instanceof RPCProtoMessage) { // Unknown proto message, reply proto.
        RPCProtoMessage resp = RPCProtoMessage
                .createResponse(new UnimplementedException("Unrecognized RPC: " + object));
        ctx.writeAndFlush(resp);/*from  ww w . ja  va2s  .c  o  m*/
    } else if (object instanceof RPCMessage) { // Unknown non-proto message, reply non-proto.
        RPCErrorResponse resp = new RPCErrorResponse(RPCResponse.Status.UNKNOWN_MESSAGE_ERROR);
        ctx.writeAndFlush(resp);
    } else { // Unknown message, this should not happen.
        ctx.fireChannelRead(object);
    }
}

From source file:alluxio.worker.netty.DataServerWriteHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object object) throws Exception {
    if (!acceptMessage(object)) {
        ctx.fireChannelRead(object);
        return;/*from w w  w.ja  va2 s  .com*/
    }

    RPCProtoMessage msg = (RPCProtoMessage) object;
    Protocol.WriteRequest writeRequest = msg.getMessage().getMessage();
    // Only initialize (open the readers) if this is the first packet in the block/file.
    if (writeRequest.getOffset() == 0) {
        initializeRequest(msg);
    }

    // Validate msg and return error if invalid. Init variables if necessary.
    String error = validateRequest(msg);
    if (!error.isEmpty()) {
        pushAbortPacket(ctx.channel(),
                new Error(new IllegalArgumentException(error), true, Protocol.Status.Code.INVALID_ARGUMENT));
        return;
    }

    try (LockResource lr = new LockResource(mLock)) {
        // If we have seen an error, return early and release the data. This can only
        // happen for those mis-behaving clients who first sends some invalid requests, then
        // then some random data. It can leak memory if we do not release buffers here.
        if (mError != null) {
            if (msg.getPayloadDataBuffer() != null) {
                msg.getPayloadDataBuffer().release();
            }
            return;
        }

        ByteBuf buf;
        if (writeRequest.getEof()) {
            buf = EOF;
        } else if (writeRequest.getCancel()) {
            buf = CANCEL;
        } else {
            DataBuffer dataBuffer = msg.getPayloadDataBuffer();
            Preconditions.checkState(dataBuffer != null && dataBuffer.getLength() > 0);
            assert dataBuffer.getNettyOutput() instanceof ByteBuf;
            buf = (ByteBuf) dataBuffer.getNettyOutput();
            mPosToQueue += buf.readableBytes();
        }
        if (!mPacketWriterActive) {
            mPacketWriterActive = true;
            mPacketWriterExecutor.submit(new PacketWriter(ctx.channel()));
        }
        mPackets.offer(buf);
        if (tooManyPacketsInFlight()) {
            NettyUtils.disableAutoRead(ctx.channel());
        }
    }
}

From source file:alluxio.worker.netty.RPCHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (!(msg instanceof RPCProtoMessage)) {
        ctx.fireChannelRead(msg);
        return;/*from  w  w w .  j av a 2 s. c o m*/
    }

    ProtoMessage message = ((RPCProtoMessage) msg).getMessage();
    if (message.isRemoveBlockRequest()) {
        handleRemoveBlockRequest(ctx, message.asRemoveBlockRequest());
    } else {
        ctx.fireChannelRead(msg);
    }
}