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;
}
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();
}
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();
}
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();
}
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.
}
}
}
Aggregations