Search in sources :

Example 1 with StorageBlockingStub

use of com.google.storage.v2.StorageGrpc.StorageBlockingStub 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 StorageBlockingStub

use of com.google.storage.v2.StorageGrpc.StorageBlockingStub in project grpc-gcp-java by GoogleCloudPlatform.

the class GrpcClient method makeRandomReadRequest.

private void makeRandomReadRequest(ManagedChannel channel, ResultTable results, int threadId) {
    StorageBlockingStub blockingStub = StorageGrpc.newBlockingStub(channel);
    if (creds != null) {
        blockingStub = blockingStub.withCallCredentials(MoreCallCredentials.from(creds));
    }
    String object = objectResolver.Resolve(threadId, /*objectId=*/
    0);
    ReadObjectRequest.Builder reqBuilder = ReadObjectRequest.newBuilder().setBucket(toV2BucketName(args.bkt)).setObject(object);
    Random r = new Random();
    long buffSize = args.buffSize * 1024;
    byte[] scratch = new byte[4 * 1024 * 1024];
    for (int i = 0; i < args.calls; i++) {
        long offset = (long) r.nextInt(args.size - args.buffSize) * 1024;
        reqBuilder.setReadOffset(offset);
        reqBuilder.setReadLimit(buffSize);
        ReadObjectRequest req = reqBuilder.build();
        long start = System.currentTimeMillis();
        Iterator<ReadObjectResponse> resIterator = blockingStub.readObject(req);
        while (resIterator.hasNext()) {
            ReadObjectResponse res = resIterator.next();
            ByteString content = res.getChecksummedData().getContent();
            content.copyTo(scratch, 0);
        }
        long dur = System.currentTimeMillis() - start;
        results.reportResult(args.bkt, object, buffSize, dur);
    }
}
Also used : ReadObjectResponse(com.google.storage.v2.ReadObjectResponse) ReadObjectRequest(com.google.storage.v2.ReadObjectRequest) Random(java.util.Random) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) StorageBlockingStub(com.google.storage.v2.StorageGrpc.StorageBlockingStub)

Aggregations

ReadObjectRequest (com.google.storage.v2.ReadObjectRequest)2 ReadObjectResponse (com.google.storage.v2.ReadObjectResponse)2 StorageBlockingStub (com.google.storage.v2.StorageGrpc.StorageBlockingStub)2 ByteString (com.google.protobuf.ByteString)1 Context (io.grpc.Context)1 CancellableContext (io.grpc.Context.CancellableContext)1 Random (java.util.Random)1