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