Search in sources :

Example 1 with DataByteArrayChannel

use of alluxio.network.protocol.databuffer.DataByteArrayChannel in project alluxio by Alluxio.

the class NettyDataServerTest method blockWorkerExceptionCausesFailStatusOnWrite.

@Test
public void blockWorkerExceptionCausesFailStatusOnWrite() throws Exception {
    long sessionId = 0;
    long blockId = 1;
    long offset = 0;
    long length = 2;
    when(mBlockWorker.getTempBlockWriterRemote(sessionId, blockId)).thenThrow(new RuntimeException());
    DataByteArrayChannel data = new DataByteArrayChannel("abc".getBytes(Charsets.UTF_8), 0, 3);
    RPCResponse response = request(new RPCBlockWriteRequest(sessionId, blockId, offset, length, data));
    // Verify that the write request failed.
    assertEquals(RPCResponse.Status.WRITE_ERROR, response.getStatus());
}
Also used : DataByteArrayChannel(alluxio.network.protocol.databuffer.DataByteArrayChannel) RPCBlockWriteRequest(alluxio.network.protocol.RPCBlockWriteRequest) RPCResponse(alluxio.network.protocol.RPCResponse) Test(org.junit.Test)

Example 2 with DataByteArrayChannel

use of alluxio.network.protocol.databuffer.DataByteArrayChannel in project alluxio by Alluxio.

the class NettyDataServerTest method writeFile.

@Test
public void writeFile() throws Exception {
    long tempUfsFileId = 1;
    long offset = 0;
    long length = 3;
    DataByteArrayChannel data = new DataByteArrayChannel("abc".getBytes(Charsets.UTF_8), 0, 3);
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    when(mFileSystemWorker.getUfsOutputStream(tempUfsFileId)).thenReturn(outStream);
    RPCResponse response = request(new RPCFileWriteRequest(tempUfsFileId, offset, length, data));
    // Verify that the write request writes to the OutputStream returned by the worker.
    assertEquals(RPCResponse.Status.SUCCESS, response.getStatus());
    assertEquals("abc", new String(outStream.toByteArray(), Charsets.UTF_8));
}
Also used : DataByteArrayChannel(alluxio.network.protocol.databuffer.DataByteArrayChannel) RPCFileWriteRequest(alluxio.network.protocol.RPCFileWriteRequest) RPCResponse(alluxio.network.protocol.RPCResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 3 with DataByteArrayChannel

use of alluxio.network.protocol.databuffer.DataByteArrayChannel in project alluxio by Alluxio.

the class NettyDataServerTest method writeExistingBlock.

@Test
public void writeExistingBlock() throws Exception {
    long sessionId = 0;
    long blockId = 1;
    // Offset is set to 1 so that the write is directed to an existing block
    long offset = 1;
    long length = 2;
    DataByteArrayChannel data = new DataByteArrayChannel("abc".getBytes(Charsets.UTF_8), 0, 3);
    MockBlockWriter blockWriter = new MockBlockWriter();
    when(mBlockWorker.getTempBlockWriterRemote(sessionId, blockId)).thenReturn(blockWriter);
    RPCResponse response = request(new RPCBlockWriteRequest(sessionId, blockId, offset, length, data));
    // Verify that the write request requests space on an existing block and then writes the
    // specified data.
    assertEquals(RPCResponse.Status.SUCCESS, response.getStatus());
    verify(mBlockWorker).requestSpace(sessionId, blockId, length);
    assertEquals("ab", new String(blockWriter.getBytes(), Charsets.UTF_8));
}
Also used : DataByteArrayChannel(alluxio.network.protocol.databuffer.DataByteArrayChannel) RPCBlockWriteRequest(alluxio.network.protocol.RPCBlockWriteRequest) MockBlockWriter(alluxio.worker.block.io.MockBlockWriter) RPCResponse(alluxio.network.protocol.RPCResponse) Test(org.junit.Test)

Example 4 with DataByteArrayChannel

use of alluxio.network.protocol.databuffer.DataByteArrayChannel 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 5 with DataByteArrayChannel

use of alluxio.network.protocol.databuffer.DataByteArrayChannel in project alluxio by Alluxio.

the class NettyUnderFileSystemFileWriter method write.

@Override
public void write(InetSocketAddress address, long ufsFileId, long fileOffset, byte[] source, int offset, int length) throws IOException {
    Channel channel = null;
    ClientHandler clientHandler = null;
    Metrics.NETTY_UFS_WRITE_OPS.inc();
    try {
        channel = mContext.acquireNettyChannel(address);
        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 RPCFileWriteRequest(ufsFileId, fileOffset, length, new DataByteArrayChannel(source, offset, length))).sync();
        if (channelFuture.isDone() && !channelFuture.isSuccess()) {
            LOG.error("Failed to read ufs file from %s for ufsFilId %d with error %s.", address.toString(), ufsFileId, channelFuture.cause());
            throw new IOException(channelFuture.cause());
        }
        RPCResponse response = listener.get(NettyClient.TIMEOUT_MS, TimeUnit.MILLISECONDS);
        switch(response.getType()) {
            case RPC_FILE_WRITE_RESPONSE:
                RPCFileWriteResponse resp = (RPCFileWriteResponse) response;
                RPCResponse.Status status = resp.getStatus();
                LOG.debug("status: {} from remote machine {} received", status, address);
                if (status != RPCResponse.Status.SUCCESS) {
                    throw new IOException(ExceptionMessage.UNDER_FILE_WRITE_ERROR.getMessage(ufsFileId, address, status.getMessage()));
                }
                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_FILE_WRITE_RESPONSE));
        }
    } catch (Exception e) {
        Metrics.NETTY_UFS_WRITE_FAILURES.inc();
        try {
            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(address, channel);
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) DataByteArrayChannel(alluxio.network.protocol.databuffer.DataByteArrayChannel) RPCFileWriteRequest(alluxio.network.protocol.RPCFileWriteRequest) RPCResponse(alluxio.network.protocol.RPCResponse) IOException(java.io.IOException) IOException(java.io.IOException) DataByteArrayChannel(alluxio.network.protocol.databuffer.DataByteArrayChannel) RPCErrorResponse(alluxio.network.protocol.RPCErrorResponse) RPCFileWriteResponse(alluxio.network.protocol.RPCFileWriteResponse)

Aggregations

RPCResponse (alluxio.network.protocol.RPCResponse)7 DataByteArrayChannel (alluxio.network.protocol.databuffer.DataByteArrayChannel)7 Test (org.junit.Test)5 RPCBlockWriteRequest (alluxio.network.protocol.RPCBlockWriteRequest)4 RPCFileWriteRequest (alluxio.network.protocol.RPCFileWriteRequest)3 RPCErrorResponse (alluxio.network.protocol.RPCErrorResponse)2 MockBlockWriter (alluxio.worker.block.io.MockBlockWriter)2 Channel (io.netty.channel.Channel)2 ChannelFuture (io.netty.channel.ChannelFuture)2 IOException (java.io.IOException)2 RPCBlockWriteResponse (alluxio.network.protocol.RPCBlockWriteResponse)1 RPCFileWriteResponse (alluxio.network.protocol.RPCFileWriteResponse)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1