Search in sources :

Example 1 with LocalFileBlockWriter

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;
    }
}
Also used : LocalFileBlockWriter(alluxio.worker.block.io.LocalFileBlockWriter) BlockAlreadyExistsException(alluxio.exception.BlockAlreadyExistsException) IOException(java.io.IOException) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException) WorkerOutOfSpaceException(alluxio.exception.WorkerOutOfSpaceException) InvalidWorkerStateException(alluxio.exception.InvalidWorkerStateException)

Example 2 with LocalFileBlockWriter

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"));
}
Also used : LocalFileBlockWriter(alluxio.worker.block.io.LocalFileBlockWriter) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with LocalFileBlockWriter

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);
}
Also used : LocalFileBlockWriter(alluxio.worker.block.io.LocalFileBlockWriter) BlockWriter(alluxio.worker.block.io.BlockWriter) LocalFileBlockWriter(alluxio.worker.block.io.LocalFileBlockWriter) TempBlockMeta(alluxio.worker.block.meta.TempBlockMeta)

Example 4 with LocalFileBlockWriter

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));
}
Also used : LocalFileBlockWriter(alluxio.worker.block.io.LocalFileBlockWriter) EmbeddedNoExceptionChannel(alluxio.EmbeddedNoExceptionChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BlockWorker(alluxio.worker.block.BlockWorker) Before(org.junit.Before)

Example 5 with LocalFileBlockWriter

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;
}
Also used : LocalFileBlockWriter(alluxio.worker.block.io.LocalFileBlockWriter) BlockWriter(alluxio.worker.block.io.BlockWriter) LocalFileBlockWriter(alluxio.worker.block.io.LocalFileBlockWriter) DefaultTempBlockMeta(alluxio.worker.block.meta.DefaultTempBlockMeta) TempBlockMeta(alluxio.worker.block.meta.TempBlockMeta) DefaultTempBlockMeta(alluxio.worker.block.meta.DefaultTempBlockMeta)

Aggregations

LocalFileBlockWriter (alluxio.worker.block.io.LocalFileBlockWriter)10 BlockWriter (alluxio.worker.block.io.BlockWriter)5 TempBlockMeta (alluxio.worker.block.meta.TempBlockMeta)4 DefaultTempBlockMeta (alluxio.worker.block.meta.DefaultTempBlockMeta)3 IOException (java.io.IOException)2 Before (org.junit.Before)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 EmbeddedNoExceptionChannel (alluxio.EmbeddedNoExceptionChannel)1 AlluxioConfiguration (alluxio.conf.AlluxioConfiguration)1 BlockAlreadyExistsException (alluxio.exception.BlockAlreadyExistsException)1 BlockDoesNotExistException (alluxio.exception.BlockDoesNotExistException)1 InvalidWorkerStateException (alluxio.exception.InvalidWorkerStateException)1 WorkerOutOfSpaceException (alluxio.exception.WorkerOutOfSpaceException)1 CreateLocalBlockRequest (alluxio.grpc.CreateLocalBlockRequest)1 CreateLocalBlockResponse (alluxio.grpc.CreateLocalBlockResponse)1 BlockWorker (alluxio.worker.block.BlockWorker)1 NoopBlockWorker (alluxio.worker.block.NoopBlockWorker)1 Closer (com.google.common.io.Closer)1 StreamObserver (io.grpc.stub.StreamObserver)1