Search in sources :

Example 1 with CreateLocalBlockResponse

use of alluxio.grpc.CreateLocalBlockResponse in project alluxio by Alluxio.

the class AlluxioBlockStoreTest method getOutStreamLocal.

@Test
public void getOutStreamLocal() throws Exception {
    File file = File.createTempFile("test", ".tmp");
    CreateLocalBlockResponse response = CreateLocalBlockResponse.newBuilder().setPath(file.getAbsolutePath()).build();
    when(mWorkerClient.createLocalBlock(any(StreamObserver.class))).thenAnswer(new Answer() {

        public Object answer(InvocationOnMock invocation) {
            StreamObserver<CreateLocalBlockResponse> observer = invocation.getArgument(0, StreamObserver.class);
            observer.onNext(response);
            return mStreamObserver;
        }
    });
    OutStreamOptions options = OutStreamOptions.defaults(mClientContext).setBlockSizeBytes(BLOCK_LENGTH).setLocationPolicy(new MockBlockLocationPolicy(Lists.newArrayList(WORKER_NET_ADDRESS_LOCAL))).setWriteType(WriteType.MUST_CACHE);
    BlockOutStream stream = mBlockStore.getOutStream(BLOCK_ID, BLOCK_LENGTH, options);
    assertEquals(WORKER_NET_ADDRESS_LOCAL, stream.getAddress());
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ClientCallStreamObserver(io.grpc.stub.ClientCallStreamObserver) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) CreateLocalBlockResponse(alluxio.grpc.CreateLocalBlockResponse) OutStreamOptions(alluxio.client.file.options.OutStreamOptions) BlockOutStream(alluxio.client.block.stream.BlockOutStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with CreateLocalBlockResponse

use of alluxio.grpc.CreateLocalBlockResponse in project alluxio by Alluxio.

the class AlluxioBlockStoreTest method getOutStreamWithReplicated.

@Test
public void getOutStreamWithReplicated() throws Exception {
    File file = File.createTempFile("test", ".tmp");
    CreateLocalBlockResponse response = CreateLocalBlockResponse.newBuilder().setPath(file.getAbsolutePath()).build();
    when(mWorkerClient.createLocalBlock(any(StreamObserver.class))).thenAnswer(new Answer() {

        public Object answer(InvocationOnMock invocation) {
            StreamObserver<CreateLocalBlockResponse> observer = invocation.getArgument(0, StreamObserver.class);
            observer.onNext(response);
            return mStreamObserver;
        }
    });
    when(mContext.getCachedWorkers()).thenReturn(Lists.newArrayList(new BlockWorkerInfo(WORKER_NET_ADDRESS_LOCAL, -1, -1), new BlockWorkerInfo(WORKER_NET_ADDRESS_REMOTE, -1, -1)));
    OutStreamOptions options = OutStreamOptions.defaults(mClientContext).setBlockSizeBytes(BLOCK_LENGTH).setLocationPolicy(new MockBlockLocationPolicy(Lists.newArrayList(WORKER_NET_ADDRESS_LOCAL, WORKER_NET_ADDRESS_REMOTE))).setWriteType(WriteType.MUST_CACHE).setReplicationMin(2);
    BlockOutStream stream = mBlockStore.getOutStream(BLOCK_ID, BLOCK_LENGTH, options);
    assertEquals(alluxio.client.block.stream.BlockOutStream.class, stream.getClass());
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ClientCallStreamObserver(io.grpc.stub.ClientCallStreamObserver) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) CreateLocalBlockResponse(alluxio.grpc.CreateLocalBlockResponse) OutStreamOptions(alluxio.client.file.options.OutStreamOptions) BlockOutStream(alluxio.client.block.stream.BlockOutStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with CreateLocalBlockResponse

use of alluxio.grpc.CreateLocalBlockResponse in project alluxio by Alluxio.

the class ShortCircuitBlockWriteHandler method onNext.

/**
 * Handles request to create local block. No exceptions should be thrown.
 *
 * @param request a create request
 */
@Override
public void onNext(CreateLocalBlockRequest request) {
    final String methodName = request.getOnlyReserveSpace() ? "ReserveSpace" : "CreateBlock";
    RpcUtils.streamingRPCAndLog(LOG, new RpcUtils.StreamingRpcCallable<CreateLocalBlockResponse>() {

        @Override
        public CreateLocalBlockResponse call() throws Exception {
            if (request.getOnlyReserveSpace()) {
                mBlockWorker.requestSpace(mSessionId, request.getBlockId(), request.getSpaceToReserve());
                return CreateLocalBlockResponse.newBuilder().build();
            } else {
                Preconditions.checkState(mRequest == null);
                mRequest = request;
                if (mSessionId == INVALID_SESSION_ID) {
                    mSessionId = IdUtils.createSessionId();
                    String path = mBlockWorker.createBlock(mSessionId, request.getBlockId(), request.getTier(), request.getMediumType(), request.getSpaceToReserve());
                    CreateLocalBlockResponse response = CreateLocalBlockResponse.newBuilder().setPath(path).build();
                    return response;
                } else {
                    LOG.warn("Create block {} without closing the previous session {}.", request.getBlockId(), mSessionId);
                    throw new InvalidWorkerStateException(ExceptionMessage.SESSION_NOT_CLOSED.getMessage(mSessionId));
                }
            }
        }

        @Override
        public void exceptionCaught(Throwable throwable) {
            if (mSessionId != INVALID_SESSION_ID) {
                // In case the client is a UfsFallbackDataWriter, DO NOT clean the temp blocks.
                if (throwable instanceof alluxio.exception.WorkerOutOfSpaceException && request.hasCleanupOnFailure() && !request.getCleanupOnFailure()) {
                    mResponseObserver.onError(GrpcExceptionUtils.fromThrowable(throwable));
                    return;
                }
                mBlockWorker.cleanupSession(mSessionId);
                mSessionId = INVALID_SESSION_ID;
            }
            mResponseObserver.onError(GrpcExceptionUtils.fromThrowable(throwable));
        }
    }, methodName, true, false, mResponseObserver, "Session=%d, Request=%s", mSessionId, request);
}
Also used : RpcUtils(alluxio.RpcUtils) CreateLocalBlockResponse(alluxio.grpc.CreateLocalBlockResponse) InvalidWorkerStateException(alluxio.exception.InvalidWorkerStateException) StatusRuntimeException(io.grpc.StatusRuntimeException) InvalidWorkerStateException(alluxio.exception.InvalidWorkerStateException)

Example 4 with CreateLocalBlockResponse

use of alluxio.grpc.CreateLocalBlockResponse in project alluxio by Alluxio.

the class LocalFileDataWriter method create.

/**
 * Creates an instance of {@link LocalFileDataWriter}. This requires the block to be locked
 * beforehand.
 *
 * @param context the file system context
 * @param address the worker network address
 * @param blockId the block ID
 * @param blockSize the block size in bytes
 * @param options the output stream options
 * @return the {@link LocalFileDataWriter} created
 */
public static LocalFileDataWriter create(final FileSystemContext context, final WorkerNetAddress address, long blockId, long blockSize, OutStreamOptions options) throws IOException {
    AlluxioConfiguration conf = context.getClusterConf();
    long chunkSize = conf.getBytes(PropertyKey.USER_LOCAL_WRITER_CHUNK_SIZE_BYTES);
    Closer closer = Closer.create();
    try {
        CloseableResource<BlockWorkerClient> blockWorker = context.acquireBlockWorkerClient(address);
        closer.register(blockWorker);
        int writerBufferSizeMessages = conf.getInt(PropertyKey.USER_STREAMING_WRITER_BUFFER_SIZE_MESSAGES);
        long fileBufferBytes = conf.getBytes(PropertyKey.USER_FILE_BUFFER_BYTES);
        long dataTimeout = conf.getMs(PropertyKey.USER_STREAMING_DATA_WRITE_TIMEOUT);
        // in cases we know precise block size, make more accurate reservation.
        long reservedBytes = Math.min(blockSize, conf.getBytes(PropertyKey.USER_FILE_RESERVED_BYTES));
        CreateLocalBlockRequest.Builder builder = CreateLocalBlockRequest.newBuilder().setBlockId(blockId).setTier(options.getWriteTier()).setSpaceToReserve(reservedBytes).setMediumType(options.getMediumType()).setPinOnCreate(options.getWriteType() == WriteType.ASYNC_THROUGH);
        if (options.getWriteType() == WriteType.ASYNC_THROUGH && conf.getBoolean(PropertyKey.USER_FILE_UFS_TIER_ENABLED)) {
            builder.setCleanupOnFailure(false);
        }
        CreateLocalBlockRequest createRequest = builder.build();
        GrpcBlockingStream<CreateLocalBlockRequest, CreateLocalBlockResponse> stream = new GrpcBlockingStream<>(blockWorker.get()::createLocalBlock, writerBufferSizeMessages, MoreObjects.toStringHelper(LocalFileDataWriter.class).add("request", createRequest).add("address", address).toString());
        stream.send(createRequest, dataTimeout);
        CreateLocalBlockResponse response = stream.receive(dataTimeout);
        Preconditions.checkState(response != null && response.hasPath());
        LocalFileBlockWriter writer = closer.register(new LocalFileBlockWriter(response.getPath()));
        return new LocalFileDataWriter(chunkSize, writer, createRequest, stream, closer, fileBufferBytes, dataTimeout);
    } catch (Exception e) {
        throw CommonUtils.closeAndRethrow(closer, e);
    }
}
Also used : Closer(com.google.common.io.Closer) AlluxioConfiguration(alluxio.conf.AlluxioConfiguration) IOException(java.io.IOException) CreateLocalBlockResponse(alluxio.grpc.CreateLocalBlockResponse) LocalFileBlockWriter(alluxio.worker.block.io.LocalFileBlockWriter) CreateLocalBlockRequest(alluxio.grpc.CreateLocalBlockRequest)

Example 5 with CreateLocalBlockResponse

use of alluxio.grpc.CreateLocalBlockResponse in project alluxio by Alluxio.

the class LocalFileDataWriter method ensureReserved.

/**
 * Reserves enough space in the block worker.
 *
 * @param pos the pos of the file/block to reserve to
 */
private void ensureReserved(long pos) throws IOException {
    if (pos <= mPosReserved) {
        return;
    }
    long toReserve = Math.max(pos - mPosReserved, mFileBufferBytes);
    CreateLocalBlockRequest request = mCreateRequest.toBuilder().setSpaceToReserve(toReserve).setOnlyReserveSpace(true).build();
    mStream.send(request, mDataTimeoutMs);
    CreateLocalBlockResponse response = mStream.receive(mDataTimeoutMs);
    Preconditions.checkState(response != null, String.format("Stream closed while waiting for reserve request %s", request.toString()));
    Preconditions.checkState(!response.hasPath(), String.format("Invalid response for reserve request %s", request.toString()));
    mPosReserved += toReserve;
}
Also used : CreateLocalBlockResponse(alluxio.grpc.CreateLocalBlockResponse) CreateLocalBlockRequest(alluxio.grpc.CreateLocalBlockRequest)

Aggregations

CreateLocalBlockResponse (alluxio.grpc.CreateLocalBlockResponse)5 BlockOutStream (alluxio.client.block.stream.BlockOutStream)2 OutStreamOptions (alluxio.client.file.options.OutStreamOptions)2 CreateLocalBlockRequest (alluxio.grpc.CreateLocalBlockRequest)2 ClientCallStreamObserver (io.grpc.stub.ClientCallStreamObserver)2 StreamObserver (io.grpc.stub.StreamObserver)2 File (java.io.File)2 Test (org.junit.Test)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 RpcUtils (alluxio.RpcUtils)1 AlluxioConfiguration (alluxio.conf.AlluxioConfiguration)1 InvalidWorkerStateException (alluxio.exception.InvalidWorkerStateException)1 LocalFileBlockWriter (alluxio.worker.block.io.LocalFileBlockWriter)1 Closer (com.google.common.io.Closer)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 IOException (java.io.IOException)1