Search in sources :

Example 1 with ReadObjectResponse

use of com.google.storage.v2.ReadObjectResponse in project hadoop-connectors by GoogleCloudDataproc.

the class GoogleCloudStorageGrpcReadChannel method requestObjectMedia.

private Iterator<ReadObjectResponse> requestObjectMedia(String objectName, long objectGeneration, long offset, OptionalLong bytesToRead) throws StatusRuntimeException {
    ReadObjectRequest.Builder requestBuilder = ReadObjectRequest.newBuilder().setBucket(GrpcChannelUtils.toV2BucketName(resourceId.getBucketName())).setObject(objectName).setGeneration(objectGeneration).setReadOffset(offset);
    bytesToRead.ifPresent(requestBuilder::setReadLimit);
    ReadObjectRequest request = requestBuilder.build();
    requestContext = Context.current().withCancellation();
    Context toReattach = requestContext.attach();
    StorageBlockingStub blockingStub = stub.withDeadlineAfter(readOptions.getGrpcReadTimeoutMillis(), MILLISECONDS);
    Iterator<ReadObjectResponse> readObjectResponseIterator;
    try {
        if (useZeroCopyMarshaller) {
            Iterator<ReadObjectResponse> responseIterator = ClientCalls.blockingServerStreamingCall(blockingStub.getChannel(), getObjectMediaMethod, blockingStub.getCallOptions(), request);
            readObjectResponseIterator = watchdog.watch(requestContext, responseIterator, Duration.ofMillis(this.gRPCReadMessageTimeout));
        } else {
            readObjectResponseIterator = watchdog.watch(requestContext, blockingStub.readObject(request), Duration.ofMillis(this.gRPCReadMessageTimeout));
        }
    } finally {
        requestContext.detach(toReattach);
    }
    return readObjectResponseIterator;
}
Also used : Context(io.grpc.Context) CancellableContext(io.grpc.Context.CancellableContext) ReadObjectResponse(com.google.storage.v2.ReadObjectResponse) ReadObjectRequest(com.google.storage.v2.ReadObjectRequest) StorageBlockingStub(com.google.storage.v2.StorageGrpc.StorageBlockingStub)

Example 2 with ReadObjectResponse

use of com.google.storage.v2.ReadObjectResponse in project hadoop-connectors by GoogleCloudDataproc.

the class WatchdogTest method watchOnServerStreamingRPCWithoutTimeout.

@Test
public void watchOnServerStreamingRPCWithoutTimeout() {
    CancellableContext requestContext = Context.current().withCancellation();
    Iterator<ReadObjectResponse> responseIterator = new Iterator<>() {

        @Override
        public boolean hasNext() {
            logger.atInfo().log("Sleeping for 10 seconds");
            sleepUninterruptibly(Duration.ofSeconds(10));
            return true;
        }

        @Override
        public ReadObjectResponse next() {
            return null;
        }
    };
    Iterator<ReadObjectResponse> watch = watchdog.watch(requestContext, responseIterator, zeroWaitTime);
    assertThat(watch.hasNext()).isTrue();
    assertThat(requestContext.isCancelled()).isFalse();
}
Also used : ReadObjectResponse(com.google.storage.v2.ReadObjectResponse) CancellableContext(io.grpc.Context.CancellableContext) Iterator(java.util.Iterator) Test(org.junit.Test)

Example 3 with ReadObjectResponse

use of com.google.storage.v2.ReadObjectResponse in project hadoop-connectors by GoogleCloudDataproc.

the class WatchdogTest method watchPassThroughServerStreamingRPC.

@Test
public void watchPassThroughServerStreamingRPC() {
    CancellableContext requestContext = Context.current().withCancellation();
    ReadObjectResponse defaultInstance = ReadObjectResponse.getDefaultInstance();
    Response<ReadObjectResponse> validResponse = new Response<>(defaultInstance);
    Response<ReadObjectResponse> errorResponse = new Response<>(new RuntimeException("Read timeout out"));
    List<Response<ReadObjectResponse>> responseList = Lists.newArrayList(validResponse, errorResponse);
    ResponseIteratorStub<ReadObjectResponse> responseIterator = new ResponseIteratorStub<>(responseList);
    Iterator<ReadObjectResponse> watch = watchdog.watch(requestContext, responseIterator, waitTime);
    ReadObjectResponse next = watch.next();
    assertThat(next).isEqualTo(validResponse.object);
    assertThat(watchdog).isNotNull();
    assertThat(watchdog.getOpenStreams()).hasSize(1);
    assertThrows(RuntimeException.class, watch::hasNext);
    assertThat(watchdog.getOpenStreams().isEmpty()).isTrue();
}
Also used : ReadObjectResponse(com.google.storage.v2.ReadObjectResponse) ReadObjectResponse(com.google.storage.v2.ReadObjectResponse) WriteObjectResponse(com.google.storage.v2.WriteObjectResponse) CancellableContext(io.grpc.Context.CancellableContext) Test(org.junit.Test)

Example 4 with ReadObjectResponse

use of com.google.storage.v2.ReadObjectResponse in project hadoop-connectors by GoogleCloudDataproc.

the class WatchdogTest method watchOnServerStreamingRPCTimeout.

@Test
public void watchOnServerStreamingRPCTimeout() {
    CancellableContext requestContext = Context.current().withCancellation();
    Iterator<ReadObjectResponse> responseIterator = new Iterator<>() {

        @Override
        public boolean hasNext() {
            logger.atInfo().log("Sleeping for 10 seconds");
            sleepUninterruptibly(Duration.ofSeconds(10));
            return true;
        }

        @Override
        public ReadObjectResponse next() {
            return null;
        }
    };
    Iterator<ReadObjectResponse> watch = watchdog.watch(requestContext, responseIterator, waitTime);
    assertThat(watch.hasNext()).isTrue();
    assertThat(requestContext.isCancelled()).isTrue();
}
Also used : ReadObjectResponse(com.google.storage.v2.ReadObjectResponse) CancellableContext(io.grpc.Context.CancellableContext) Iterator(java.util.Iterator) Test(org.junit.Test)

Example 5 with ReadObjectResponse

use of com.google.storage.v2.ReadObjectResponse in project gapic-generator-java by googleapis.

the class AsyncReadObject method asyncReadObject.

public static void asyncReadObject() throws Exception {
    // It may require modifications to work in your environment.
    try (StorageClient storageClient = StorageClient.create()) {
        ReadObjectRequest request = ReadObjectRequest.newBuilder().setBucket("bucket-1378203158").setObject("object-1023368385").setGeneration(305703192).setReadOffset(-715377828).setReadLimit(-164298798).setIfGenerationMatch(-1086241088).setIfGenerationNotMatch(1475720404).setIfMetagenerationMatch(1043427781).setIfMetagenerationNotMatch(1025430873).setCommonObjectRequestParams(CommonObjectRequestParams.newBuilder().build()).setCommonRequestParams(CommonRequestParams.newBuilder().build()).setReadMask(FieldMask.newBuilder().build()).build();
        ServerStream<ReadObjectResponse> stream = storageClient.readObjectCallable().call(request);
        for (ReadObjectResponse response : stream) {
        // Do something when a response is received.
        }
    }
}
Also used : ReadObjectResponse(com.google.storage.v2.ReadObjectResponse) ReadObjectRequest(com.google.storage.v2.ReadObjectRequest) StorageClient(com.google.storage.v2.StorageClient)

Aggregations

ReadObjectResponse (com.google.storage.v2.ReadObjectResponse)9 CancellableContext (io.grpc.Context.CancellableContext)5 ReadObjectRequest (com.google.storage.v2.ReadObjectRequest)4 Test (org.junit.Test)4 ByteString (com.google.protobuf.ByteString)3 StorageBlockingStub (com.google.storage.v2.StorageGrpc.StorageBlockingStub)3 Iterator (java.util.Iterator)3 WriteObjectResponse (com.google.storage.v2.WriteObjectResponse)2 InputStream (java.io.InputStream)2 StorageClient (com.google.storage.v2.StorageClient)1 StorageGrpc (com.google.storage.v2.StorageGrpc)1 WriteObjectRequest (com.google.storage.v2.WriteObjectRequest)1 Context (io.grpc.Context)1 StreamObserver (io.grpc.stub.StreamObserver)1 IOException (java.io.IOException)1 NoSuchElementException (java.util.NoSuchElementException)1 Random (java.util.Random)1