use of alluxio.worker.block.io.LocalFileBlockWriter in project alluxio by Alluxio.
the class UnderFileSystemBlockReader method updateBlockWriter.
/**
* Updates the block writer given an offset to read. If the offset is beyond the current
* position of the block writer, the block writer will be aborted.
*
* @param offset the read offset
*/
private void updateBlockWriter(long offset) throws IOException {
try {
if (mBlockWriter != null && offset > mBlockWriter.getPosition()) {
mBlockWriter.close();
mBlockWriter = null;
mLocalBlockStore.abortBlock(mBlockMeta.getSessionId(), mBlockMeta.getBlockId());
}
} catch (BlockDoesNotExistException e) {
// This can only happen when the session is expired.
LOG.warn("Block {} does not exist when being aborted.", mBlockMeta.getBlockId());
} catch (BlockAlreadyExistsException | InvalidWorkerStateException | IOException e) {
// reader does not commit the block if it fails to abort the block.
throw CommonUtils.castToIOException(e);
}
try {
if (mBlockWriter == null && offset == 0 && !mNoCache) {
BlockStoreLocation loc = BlockStoreLocation.anyDirInTier(mStorageTierAssoc.getAlias(0));
String blockPath = mLocalBlockStore.createBlock(mBlockMeta.getSessionId(), mBlockMeta.getBlockId(), loc, mInitialBlockSize).getPath();
mBlockWriter = new LocalFileBlockWriter(blockPath);
}
} catch (IOException | BlockAlreadyExistsException | WorkerOutOfSpaceException e) {
// This can happen when there are concurrent UFS readers who are all trying to cache to block.
LOG.debug("Failed to update block writer for UFS block [blockId: {}, ufsPath: {}, offset: {}]", mBlockMeta.getBlockId(), mBlockMeta.getUnderFileSystemPath(), offset, e);
mBlockWriter = null;
}
}
use of alluxio.worker.block.io.LocalFileBlockWriter in project alluxio by Alluxio.
the class LocalFileDataWriterTest method streamCancelled.
@Test
public void streamCancelled() throws Exception {
LocalFileDataWriter writer = LocalFileDataWriter.create(mContext, mAddress, BLOCK_ID, 128, /* unused */
OutStreamOptions.defaults(mClientContext));
// Cancel stream before cancelling the writer
PowerMockito.when(mStream.isCanceled()).thenReturn(true);
PowerMockito.when(mStream.isClosed()).thenReturn(true);
PowerMockito.when(mStream.isOpen()).thenReturn(true);
writer.cancel();
// Verify there are no open files
LocalFileBlockWriter blockWriter = Whitebox.getInternalState(writer, "mWriter");
Assert.assertTrue(Whitebox.getInternalState(blockWriter, "mClosed"));
}
use of alluxio.worker.block.io.LocalFileBlockWriter in project alluxio by Alluxio.
the class TieredBlockStoreTestUtils method cache.
/**
* Caches bytes into {@link BlockStore} at specific location.
*
* @param sessionId session who caches the data
* @param blockId id of the cached block
* @param bytes size of the block in bytes
* @param blockStore block store that the block is written into
* @param location the location where the block resides
* @throws Exception when fail to cache
*/
public static void cache(long sessionId, long blockId, long bytes, BlockStore blockStore, BlockStoreLocation location) throws Exception {
TempBlockMeta tempBlockMeta = blockStore.createBlock(sessionId, blockId, location, bytes);
// write data
FileUtils.createFile(tempBlockMeta.getPath());
BlockWriter writer = new LocalFileBlockWriter(tempBlockMeta.getPath());
writer.append(BufferUtils.getIncreasingByteBuffer(Ints.checkedCast(bytes)));
writer.close();
// commit block
blockStore.commitBlock(sessionId, blockId);
}
use of alluxio.worker.block.io.LocalFileBlockWriter in project alluxio by Alluxio.
the class DataServerBlockWriteHandlerTest method before.
@Before
public void before() throws Exception {
mBlockWorker = Mockito.mock(BlockWorker.class);
Mockito.doNothing().when(mBlockWorker).createBlockRemote(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString(), Mockito.anyLong());
Mockito.doNothing().when(mBlockWorker).requestSpace(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong());
mFile = mTestFolder.newFile().getPath();
mBlockWriter = new LocalFileBlockWriter(mFile);
Mockito.when(mBlockWorker.getTempBlockWriterRemote(Mockito.anyLong(), Mockito.anyLong())).thenReturn(mBlockWriter);
mChecksum = 0;
mChannel = new EmbeddedChannel(new DataServerBlockWriteHandler(NettyExecutors.BLOCK_WRITER_EXECUTOR, mBlockWorker));
mChannelNoException = new EmbeddedNoExceptionChannel(new DataServerBlockWriteHandler(NettyExecutors.BLOCK_WRITER_EXECUTOR, mBlockWorker));
}
use of alluxio.worker.block.io.LocalFileBlockWriter in project alluxio by Alluxio.
the class TieredBlockStoreTestUtils method createTempBlock.
/**
* Makes a temp block of a given size in {@link StorageDir}.
*
* @param sessionId session who caches the data
* @param blockId id of the cached block
* @param bytes size of the block in bytes
* @param dir the {@link StorageDir} the block resides in
* @return the temp block meta
*/
public static TempBlockMeta createTempBlock(long sessionId, long blockId, long bytes, StorageDir dir) throws Exception {
// prepare temp block
TempBlockMeta tempBlockMeta = new DefaultTempBlockMeta(sessionId, blockId, bytes, dir);
dir.addTempBlockMeta(tempBlockMeta);
// write data
FileUtils.createFile(tempBlockMeta.getPath());
BlockWriter writer = new LocalFileBlockWriter(tempBlockMeta.getPath());
writer.append(BufferUtils.getIncreasingByteBuffer(Ints.checkedCast(bytes)));
writer.close();
return tempBlockMeta;
}
Aggregations