Search in sources :

Example 1 with RPCBlockWriteResponse

use of alluxio.network.protocol.RPCBlockWriteResponse in project alluxio by Alluxio.

the class NettyRemoteBlockWriter method write.

@Override
public void write(byte[] bytes, int offset, int length) throws IOException {
    Channel channel = null;
    ClientHandler clientHandler = null;
    Metrics.NETTY_BLOCK_WRITE_OPS.inc();
    try {
        channel = mContext.acquireNettyChannel(mAddress);
        if (!(channel.pipeline().last() instanceof ClientHandler)) {
            channel.pipeline().addLast(new ClientHandler());
        }
        clientHandler = (ClientHandler) channel.pipeline().last();
        SingleResponseListener listener = new SingleResponseListener();
        clientHandler.addListener(listener);
        ChannelFuture channelFuture = channel.writeAndFlush(new RPCBlockWriteRequest(mSessionId, mBlockId, mWrittenBytes, length, new DataByteArrayChannel(bytes, offset, length))).sync();
        if (channelFuture.isDone() && !channelFuture.isSuccess()) {
            LOG.error("Failed to write to %s for block %d with error %s.", mAddress.toString(), mBlockId, channelFuture.cause());
            throw new IOException(channelFuture.cause());
        }
        RPCResponse response = listener.get(NettyClient.TIMEOUT_MS, TimeUnit.MILLISECONDS);
        switch(response.getType()) {
            case RPC_BLOCK_WRITE_RESPONSE:
                RPCBlockWriteResponse resp = (RPCBlockWriteResponse) response;
                RPCResponse.Status status = resp.getStatus();
                LOG.debug("status: {} from remote machine {} received", status, mAddress);
                if (status != RPCResponse.Status.SUCCESS) {
                    throw new IOException(ExceptionMessage.BLOCK_WRITE_ERROR.getMessage(mBlockId, mSessionId, mAddress, status.getMessage()));
                }
                mWrittenBytes += length;
                break;
            case RPC_ERROR_RESPONSE:
                RPCErrorResponse error = (RPCErrorResponse) response;
                throw new IOException(error.getStatus().getMessage());
            default:
                throw new IOException(ExceptionMessage.UNEXPECTED_RPC_RESPONSE.getMessage(response.getType(), RPCMessage.Type.RPC_BLOCK_WRITE_RESPONSE));
        }
    } catch (Exception e) {
        Metrics.NETTY_BLOCK_WRITE_FAILURES.inc();
        try {
            // TODO(peis): We should not close the channel unless it is an exception caused by network.
            if (channel != null) {
                channel.close().sync();
            }
        } catch (InterruptedException ee) {
            Throwables.propagate(ee);
        }
        throw new IOException(e);
    } finally {
        if (clientHandler != null) {
            clientHandler.removeListeners();
        }
        if (channel != null) {
            mContext.releaseNettyChannel(mAddress, channel);
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RPCBlockWriteRequest(alluxio.network.protocol.RPCBlockWriteRequest) Channel(io.netty.channel.Channel) DataByteArrayChannel(alluxio.network.protocol.databuffer.DataByteArrayChannel) RPCResponse(alluxio.network.protocol.RPCResponse) IOException(java.io.IOException) IOException(java.io.IOException) DataByteArrayChannel(alluxio.network.protocol.databuffer.DataByteArrayChannel) RPCErrorResponse(alluxio.network.protocol.RPCErrorResponse) RPCBlockWriteResponse(alluxio.network.protocol.RPCBlockWriteResponse)

Example 2 with RPCBlockWriteResponse

use of alluxio.network.protocol.RPCBlockWriteResponse in project alluxio by Alluxio.

the class BlockDataServerHandler method handleBlockWriteRequest.

/**
   * Handles a {@link RPCBlockWriteRequest} by writing the data through a {@link BlockWriter}
   * provided by the block worker. This method takes care of requesting space and creating the
   * block if necessary.
   *
   * @param ctx The context of this request which handles the result of this operation
   * @param req The initiating {@link RPCBlockWriteRequest}
   * @throws IOException if an I/O exception occurs when writing the data
   */
// TODO(hy): This write request handler is very simple in order to be stateless. Therefore, the
// block file is opened and closed for every request. If this is too slow, then this handler
// should be optimized to keep state.
void handleBlockWriteRequest(final ChannelHandlerContext ctx, final RPCBlockWriteRequest req) throws IOException {
    final long sessionId = req.getSessionId();
    final long blockId = req.getBlockId();
    final long offset = req.getOffset();
    final long length = req.getLength();
    final DataBuffer data = req.getPayloadDataBuffer();
    BlockWriter writer = null;
    try {
        req.validate();
        ByteBuffer buffer = data.getReadOnlyByteBuffer();
        if (offset == 0) {
            // This is the first write to the block, so create the temp block file. The file will only
            // be created if the first write starts at offset 0. This allocates enough space for the
            // write.
            mWorker.createBlockRemote(sessionId, blockId, mStorageTierAssoc.getAlias(0), length);
        } else {
            // Allocate enough space in the existing temporary block for the write.
            mWorker.requestSpace(sessionId, blockId, length);
        }
        writer = mWorker.getTempBlockWriterRemote(sessionId, blockId);
        writer.append(buffer);
        Metrics.BYTES_WRITTEN_REMOTE.inc(data.getLength());
        RPCBlockWriteResponse resp = new RPCBlockWriteResponse(sessionId, blockId, offset, length, RPCResponse.Status.SUCCESS);
        ChannelFuture future = ctx.writeAndFlush(resp);
        future.addListener(new ClosableResourceChannelListener(writer));
    } catch (Exception e) {
        LOG.error("Error writing remote block : {}", e.getMessage(), e);
        RPCBlockWriteResponse resp = RPCBlockWriteResponse.createErrorResponse(req, RPCResponse.Status.WRITE_ERROR);
        ChannelFuture future = ctx.writeAndFlush(resp);
        future.addListener(ChannelFutureListener.CLOSE);
        if (writer != null) {
            writer.close();
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) BlockWriter(alluxio.worker.block.io.BlockWriter) RPCBlockWriteResponse(alluxio.network.protocol.RPCBlockWriteResponse) ByteBuffer(java.nio.ByteBuffer) DataByteBuffer(alluxio.network.protocol.databuffer.DataByteBuffer) IOException(java.io.IOException) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Aggregations

RPCBlockWriteResponse (alluxio.network.protocol.RPCBlockWriteResponse)2 ChannelFuture (io.netty.channel.ChannelFuture)2 IOException (java.io.IOException)2 BlockDoesNotExistException (alluxio.exception.BlockDoesNotExistException)1 RPCBlockWriteRequest (alluxio.network.protocol.RPCBlockWriteRequest)1 RPCErrorResponse (alluxio.network.protocol.RPCErrorResponse)1 RPCResponse (alluxio.network.protocol.RPCResponse)1 DataBuffer (alluxio.network.protocol.databuffer.DataBuffer)1 DataByteArrayChannel (alluxio.network.protocol.databuffer.DataByteArrayChannel)1 DataByteBuffer (alluxio.network.protocol.databuffer.DataByteBuffer)1 BlockWriter (alluxio.worker.block.io.BlockWriter)1 Channel (io.netty.channel.Channel)1 ByteBuffer (java.nio.ByteBuffer)1