Search in sources :

Example 11 with ReadObjectRequest

use of com.google.storage.v2.ReadObjectRequest 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)

Example 12 with ReadObjectRequest

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

the class GoogleCloudStorageGrpcReadChannelTest method readWithStrictGenerationReadConsistencySucceeds.

@Test
public void readWithStrictGenerationReadConsistencySucceeds() throws Exception {
    int objectSize = 100;
    storageObject.setSize(BigInteger.valueOf(objectSize));
    fakeService.setObject(DEFAULT_OBJECT.toBuilder().setSize(objectSize).setGeneration(1).build());
    GoogleCloudStorageReadOptions options = GoogleCloudStorageReadOptions.builder().setMinRangeRequestSize(4).build();
    GoogleCloudStorageGrpcReadChannel readChannel = newReadChannel(options);
    ByteBuffer buffer = ByteBuffer.allocate(10);
    readChannel.read(buffer);
    fakeService.setObject(DEFAULT_OBJECT.toBuilder().setSize(objectSize).setGeneration(2).build());
    readChannel.position(0);
    buffer.clear();
    readChannel.read(buffer);
    ArgumentCaptor<ReadObjectRequest> requestCaptor = ArgumentCaptor.forClass(ReadObjectRequest.class);
    verify(get).setFields(METADATA_FIELDS);
    verify(get).execute();
    verify(fakeService, times(3)).readObject(requestCaptor.capture(), any());
}
Also used : ReadObjectRequest(com.google.storage.v2.ReadObjectRequest) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 13 with ReadObjectRequest

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

the class GrpcClient method makeReadObjectRequest.

private void makeReadObjectRequest(ManagedChannel channel, ResultTable results, int threadId) {
    StorageGrpc.StorageBlockingStub blockingStub = StorageGrpc.newBlockingStub(channel);
    if (creds != null) {
        blockingStub = blockingStub.withCallCredentials(MoreCallCredentials.from(creds));
    }
    byte[] scratch = new byte[4 * 1024 * 1024];
    for (int i = 0; i < args.calls; i++) {
        String object = objectResolver.Resolve(threadId, i);
        ReadObjectRequest readRequest = ReadObjectRequest.newBuilder().setBucket(toV2BucketName(args.bkt)).setObject(object).build();
        long start = System.currentTimeMillis();
        long totalBytes = 0;
        Iterator<ReadObjectResponse> resIterator;
        if (useZeroCopy) {
            resIterator = io.grpc.stub.ClientCalls.blockingServerStreamingCall(blockingStub.getChannel(), readObjectMethod, blockingStub.getCallOptions(), readRequest);
        } else {
            resIterator = blockingStub.readObject(readRequest);
        }
        try {
            while (true) {
                ReadObjectResponse res = resIterator.next();
                // When zero-copy mashaller is used, the stream that backs ReadObjectResponse
                // should be closed when the mssage is no longed needed so that all buffers in the
                // stream can be reclaimed. If zero-copy is not used, stream will be null.
                InputStream stream = ReadObjectResponseMarshaller.popStream(res);
                try {
                    // Just copy to scratch memory to ensure its data is consumed.
                    ByteString content = res.getChecksummedData().getContent();
                    totalBytes += content.size();
                    content.copyTo(scratch, 0);
                } finally {
                    if (stream != null) {
                        try {
                            stream.close();
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        } catch (NoSuchElementException e) {
        }
        long dur = System.currentTimeMillis() - start;
        results.reportResult(args.bkt, object, totalBytes, dur);
    }
}
Also used : ReadObjectResponse(com.google.storage.v2.ReadObjectResponse) ReadObjectRequest(com.google.storage.v2.ReadObjectRequest) InputStream(java.io.InputStream) ByteString(com.google.protobuf.ByteString) StorageGrpc(com.google.storage.v2.StorageGrpc) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) StorageBlockingStub(com.google.storage.v2.StorageGrpc.StorageBlockingStub) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

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