Search in sources :

Example 1 with BlockWriter

use of alluxio.worker.block.io.BlockWriter in project alluxio by Alluxio.

the class DataServerBlockWriteHandler method writeBuf.

@Override
protected void writeBuf(ByteBuf buf, long pos) throws Exception {
    if (mBytesReserved < pos) {
        long bytesToReserve = Math.max(FILE_BUFFER_SIZE, pos - mBytesReserved);
        // Allocate enough space in the existing temporary block for the write.
        mWorker.requestSpace(mRequest.mSessionId, mRequest.mId, bytesToReserve);
        mBytesReserved += bytesToReserve;
    }
    BlockWriter blockWriter = ((BlockWriteRequestInternal) mRequest).mBlockWriter;
    GatheringByteChannel outputChannel = blockWriter.getChannel();
    int sz = buf.readableBytes();
    Preconditions.checkState(buf.readBytes(outputChannel, sz) == sz);
}
Also used : GatheringByteChannel(java.nio.channels.GatheringByteChannel) BlockWriter(alluxio.worker.block.io.BlockWriter)

Example 2 with BlockWriter

use of alluxio.worker.block.io.BlockWriter in project alluxio by Alluxio.

the class BlockWorkerClientRestApiTest method readBlock.

@Test
public void readBlock() throws Exception {
    // Write a block and acquire a lock for it.
    mBlockWorker.createBlock(SESSION_ID, BLOCK_ID, TIER_ALIAS, INITIAL_BYTES);
    BlockWriter writer = mBlockWorker.getTempBlockWriterRemote(SESSION_ID, BLOCK_ID);
    writer.append(BYTE_BUFFER);
    writer.close();
    mBlockWorker.commitBlock(SESSION_ID, BLOCK_ID);
    long lockId = mBlockWorker.lockBlock(SESSION_ID, BLOCK_ID);
    Map<String, String> params = new HashMap<>();
    params.put("blockId", Long.toString(BLOCK_ID));
    params.put("sessionId", Long.toString(SESSION_ID));
    params.put("lockId", Long.toString(lockId));
    params.put("offset", "0");
    params.put("length", Long.toString(INITIAL_BYTES));
    TestCase testCase = new TestCase(mHostname, mPort, getEndpoint(BlockWorkerClientRestServiceHandler.READ_BLOCK), params, HttpMethod.GET, BYTE_BUFFER);
    HttpURLConnection connection = (HttpURLConnection) testCase.createURL().openConnection();
    connection.setRequestMethod(testCase.getMethod());
    connection.connect();
    Assert.assertEquals(testCase.getEndpoint(), Response.Status.OK.getStatusCode(), connection.getResponseCode());
    Assert.assertEquals(new String(BYTE_BUFFER.array()), testCase.getResponse(connection));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HashMap(java.util.HashMap) TestCase(alluxio.rest.TestCase) BlockWriter(alluxio.worker.block.io.BlockWriter) Test(org.junit.Test) RestApiTest(alluxio.rest.RestApiTest)

Example 3 with BlockWriter

use of alluxio.worker.block.io.BlockWriter in project alluxio by Alluxio.

the class BlockWorkerClientRestApiTest method unlockBlock.

@Test
public void unlockBlock() throws Exception {
    // Write a block and acquire a lock for it.
    mBlockWorker.createBlock(SESSION_ID, BLOCK_ID, TIER_ALIAS, INITIAL_BYTES);
    BlockWriter writer = mBlockWorker.getTempBlockWriterRemote(SESSION_ID, BLOCK_ID);
    writer.append(BYTE_BUFFER);
    writer.close();
    mBlockWorker.commitBlock(SESSION_ID, BLOCK_ID);
    mBlockWorker.lockBlock(SESSION_ID, BLOCK_ID);
    Map<String, String> params = new HashMap<>();
    params.put("blockId", Long.toString(BLOCK_ID));
    params.put("sessionId", Long.toString(SESSION_ID));
    new TestCase(mHostname, mPort, getEndpoint(BlockWorkerClientRestServiceHandler.UNLOCK_BLOCK), params, HttpMethod.POST, null).run();
}
Also used : HashMap(java.util.HashMap) TestCase(alluxio.rest.TestCase) BlockWriter(alluxio.worker.block.io.BlockWriter) Test(org.junit.Test) RestApiTest(alluxio.rest.RestApiTest)

Example 4 with BlockWriter

use of alluxio.worker.block.io.BlockWriter in project alluxio by Alluxio.

the class BlockWorkerDataReaderTest method readChunkFullFile.

@Test
public void readChunkFullFile() throws Exception {
    int len = CHUNK_SIZE * 2;
    mBlockWorker.createBlock(SESSION_ID, BLOCK_ID, 0, Constants.MEDIUM_MEM, 1);
    try (BlockWriter writer = mBlockWorker.createBlockWriter(SESSION_ID, BLOCK_ID)) {
        writer.append(BufferUtils.getIncreasingByteBuffer(len));
    }
    mBlockWorker.commitBlock(SESSION_ID, BLOCK_ID, true);
    DataReader dataReader = mDataReaderFactory.create(0, len);
    validateBuffer(dataReader.readChunk(), 0, CHUNK_SIZE);
    assertEquals(CHUNK_SIZE, dataReader.pos());
    validateBuffer(dataReader.readChunk(), CHUNK_SIZE, CHUNK_SIZE);
    assertEquals(len, dataReader.pos());
    dataReader.close();
}
Also used : DataReader(alluxio.client.block.stream.DataReader) BlockWorkerDataReader(alluxio.client.block.stream.BlockWorkerDataReader) BlockWriter(alluxio.worker.block.io.BlockWriter) Test(org.junit.Test)

Example 5 with BlockWriter

use of alluxio.worker.block.io.BlockWriter in project alluxio by Alluxio.

the class BlockWorkerDataReaderTest method readChunkPartial.

@Test
public void readChunkPartial() throws Exception {
    int len = CHUNK_SIZE * 5;
    mBlockWorker.createBlock(SESSION_ID, BLOCK_ID, 0, Constants.MEDIUM_MEM, 1);
    try (BlockWriter writer = mBlockWorker.createBlockWriter(SESSION_ID, BLOCK_ID)) {
        writer.append(BufferUtils.getIncreasingByteBuffer(len));
    }
    mBlockWorker.commitBlock(SESSION_ID, BLOCK_ID, true);
    int start = len / 5 * 2;
    int end = len / 5 * 4;
    DataReader dataReader = mDataReaderFactory.create(start, end);
    for (int s = start; s < end; s += CHUNK_SIZE) {
        int currentLen = Math.min(CHUNK_SIZE, end - s);
        validateBuffer(dataReader.readChunk(), s, currentLen);
    }
}
Also used : DataReader(alluxio.client.block.stream.DataReader) BlockWorkerDataReader(alluxio.client.block.stream.BlockWorkerDataReader) BlockWriter(alluxio.worker.block.io.BlockWriter) Test(org.junit.Test)

Aggregations

BlockWriter (alluxio.worker.block.io.BlockWriter)15 LocalFileBlockWriter (alluxio.worker.block.io.LocalFileBlockWriter)5 TempBlockMeta (alluxio.worker.block.meta.TempBlockMeta)5 Test (org.junit.Test)5 DefaultTempBlockMeta (alluxio.worker.block.meta.DefaultTempBlockMeta)3 IOException (java.io.IOException)3 BlockWorkerDataReader (alluxio.client.block.stream.BlockWorkerDataReader)2 DataReader (alluxio.client.block.stream.DataReader)2 BlockAlreadyExistsException (alluxio.exception.BlockAlreadyExistsException)2 BlockDoesNotExistException (alluxio.exception.BlockDoesNotExistException)2 DataBuffer (alluxio.network.protocol.databuffer.DataBuffer)2 RestApiTest (alluxio.rest.RestApiTest)2 TestCase (alluxio.rest.TestCase)2 StreamObserver (io.grpc.stub.StreamObserver)2 HashMap (java.util.HashMap)2 Before (org.junit.Before)2 AlluxioConfiguration (alluxio.conf.AlluxioConfiguration)1 AlluxioException (alluxio.exception.AlluxioException)1 InvalidWorkerStateException (alluxio.exception.InvalidWorkerStateException)1 WorkerOutOfSpaceException (alluxio.exception.WorkerOutOfSpaceException)1