Search in sources :

Example 6 with BlockOutStream

use of alluxio.client.block.stream.BlockOutStream in project alluxio by Alluxio.

the class AlluxioBlockStore method getOutStream.

/**
 * Gets a stream to write data to a block. The stream can only be backed by Alluxio storage.
 *
 * @param blockId the block to write
 * @param blockSize the standard block size to write
 * @param address the address of the worker to write the block to, fails if the worker cannot
 *        serve the request
 * @param options the output stream options
 * @return an {@link BlockOutStream} which can be used to write data to the block in a streaming
 *         fashion
 */
public BlockOutStream getOutStream(long blockId, long blockSize, WorkerNetAddress address, OutStreamOptions options) throws IOException {
    // No specified location to write to.
    Preconditions.checkNotNull(address, "address");
    LOG.debug("Create BlockOutStream for {} of block size {} at address {}, using options: {}", blockId, blockSize, address, options);
    DataWriter dataWriter = DataWriter.Factory.create(mContext, blockId, blockSize, address, options);
    return new BlockOutStream(dataWriter, blockSize, address);
}
Also used : BlockOutStream(alluxio.client.block.stream.BlockOutStream) DataWriter(alluxio.client.block.stream.DataWriter)

Example 7 with BlockOutStream

use of alluxio.client.block.stream.BlockOutStream in project alluxio by Alluxio.

the class FileOutStreamTest method cacheWriteExceptionSyncPersist.

/**
 * Tests that if an exception is thrown by the underlying out stream, and the user is using
 * {@link UnderStorageType#SYNC_PERSIST} for their under storage type, the error is recovered
 * from by writing the data to the under storage out stream.
 */
@Test
public void cacheWriteExceptionSyncPersist() throws IOException {
    BlockOutStream stream = mock(BlockOutStream.class);
    when(mBlockStore.getOutStream(anyLong(), anyLong(), any(OutStreamOptions.class))).thenReturn(stream);
    when(stream.remaining()).thenReturn(BLOCK_LENGTH);
    doThrow(new IOException("test error")).when(stream).write((byte) 7);
    mTestStream.write(7);
    mTestStream.write(8);
    assertArrayEquals(new byte[] { 7, 8 }, mUnderStorageOutputStream.getWrittenData());
    // The cache stream is written to only once - the FileInStream gives up on it after it throws
    // the first exception.
    verify(stream, times(1)).write(anyInt());
}
Also used : TestBlockOutStream(alluxio.client.block.stream.TestBlockOutStream) BlockOutStream(alluxio.client.block.stream.BlockOutStream) OutStreamOptions(alluxio.client.file.options.OutStreamOptions) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with BlockOutStream

use of alluxio.client.block.stream.BlockOutStream in project alluxio by Alluxio.

the class AlluxioBlockStoreTest method getOutStreamRemote.

@Test
public void getOutStreamRemote() throws Exception {
    WorkerNetAddress worker1 = new WorkerNetAddress().setHost("worker1");
    WorkerNetAddress worker2 = new WorkerNetAddress().setHost("worker2");
    OutStreamOptions options = OutStreamOptions.defaults(mClientContext).setBlockSizeBytes(BLOCK_LENGTH).setLocationPolicy(new MockBlockLocationPolicy(Arrays.asList(worker1, worker2))).setWriteType(WriteType.MUST_CACHE);
    BlockOutStream stream1 = mBlockStore.getOutStream(BLOCK_ID, BLOCK_LENGTH, options);
    assertEquals(worker1, stream1.getAddress());
    BlockOutStream stream2 = mBlockStore.getOutStream(BLOCK_ID, BLOCK_LENGTH, options);
    assertEquals(worker2, stream2.getAddress());
}
Also used : OutStreamOptions(alluxio.client.file.options.OutStreamOptions) BlockOutStream(alluxio.client.block.stream.BlockOutStream) WorkerNetAddress(alluxio.wire.WorkerNetAddress) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

BlockOutStream (alluxio.client.block.stream.BlockOutStream)8 Test (org.junit.Test)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 OutStreamOptions (alluxio.client.file.options.OutStreamOptions)5 TestBlockOutStream (alluxio.client.block.stream.TestBlockOutStream)3 IOException (java.io.IOException)3 CreateLocalBlockResponse (alluxio.grpc.CreateLocalBlockResponse)2 ClientCallStreamObserver (io.grpc.stub.ClientCallStreamObserver)2 StreamObserver (io.grpc.stub.StreamObserver)2 File (java.io.File)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 BlockWorkerInfo (alluxio.client.block.BlockWorkerInfo)1 BlockInStream (alluxio.client.block.stream.BlockInStream)1 DataWriter (alluxio.client.block.stream.DataWriter)1 TestBlockInStream (alluxio.client.block.stream.TestBlockInStream)1 CompleteFilePOptions (alluxio.grpc.CompleteFilePOptions)1 OperationId (alluxio.wire.OperationId)1 WorkerNetAddress (alluxio.wire.WorkerNetAddress)1