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);
}
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));
}
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();
}
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();
}
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);
}
}
Aggregations