use of com.google.storage.v2.WriteObjectResponse in project gapic-generator-java by googleapis.
the class AsyncWriteObject method asyncWriteObject.
public static void asyncWriteObject() throws Exception {
// It may require modifications to work in your environment.
try (StorageClient storageClient = StorageClient.create()) {
ApiStreamObserver<WriteObjectRequest> responseObserver = new ApiStreamObserver<WriteObjectRequest>() {
@Override
public void onNext(WriteObjectResponse response) {
// Do something when a response is received.
}
@Override
public void onError(Throwable t) {
// Add error-handling
}
@Override
public void onCompleted() {
// Do something when complete.
}
};
ApiStreamObserver<WriteObjectRequest> requestObserver = storageClient.writeObject().clientStreamingCall(responseObserver);
WriteObjectRequest request = WriteObjectRequest.newBuilder().setWriteOffset(-1559543565).setObjectChecksums(ObjectChecksums.newBuilder().build()).setFinishWrite(true).setCommonObjectRequestParams(CommonObjectRequestParams.newBuilder().build()).setCommonRequestParams(CommonRequestParams.newBuilder().build()).build();
requestObserver.onNext(request);
}
}
use of com.google.storage.v2.WriteObjectResponse in project grpc-gcp-java by GoogleCloudPlatform.
the class GrpcClient method makeInsertRequest.
private void makeInsertRequest(ManagedChannel channel, ResultTable results, int threadId) throws InterruptedException {
StorageGrpc.StorageStub asyncStub = StorageGrpc.newStub(channel);
if (creds != null) {
asyncStub = asyncStub.withCallCredentials(MoreCallCredentials.from(creds));
}
int totalBytes = args.size * 1024;
byte[] data = new byte[totalBytes];
for (int i = 0; i < args.calls; i++) {
String obj = objectResolver.Resolve(threadId, i);
int offset = 0;
boolean isFirst = true;
boolean isLast = false;
final CountDownLatch finishLatch = new CountDownLatch(1);
StreamObserver<WriteObjectResponse> responseObserver = new StreamObserver<WriteObjectResponse>() {
long start = System.currentTimeMillis();
@Override
public void onNext(WriteObjectResponse value) {
}
@Override
public void onError(Throwable t) {
logger.warning("InsertObject failed with: " + Status.fromThrowable(t));
finishLatch.countDown();
}
@Override
public void onCompleted() {
long dur = System.currentTimeMillis() - start;
results.reportResult(args.bkt, obj, totalBytes, dur);
finishLatch.countDown();
}
};
StreamObserver<WriteObjectRequest> requestObserver = asyncStub.writeObject(responseObserver);
while (offset < totalBytes) {
int add;
if (offset + Values.MAX_WRITE_CHUNK_BYTES_VALUE <= totalBytes) {
add = Values.MAX_WRITE_CHUNK_BYTES_VALUE;
} else {
add = totalBytes - offset;
}
if (offset + add == totalBytes) {
isLast = true;
}
WriteObjectRequest req = getWriteRequest(isFirst, isLast, offset, ByteString.copyFrom(data, offset, add), obj);
requestObserver.onNext(req);
if (finishLatch.getCount() == 0) {
logger.warning("Stream completed before finishing sending requests");
return;
}
offset += add;
}
requestObserver.onCompleted();
if (!finishLatch.await(20, TimeUnit.MINUTES)) {
logger.warning("insertObject cannot finish within 20 minutes");
}
}
}
use of com.google.storage.v2.WriteObjectResponse in project hadoop-connectors by GoogleCloudDataproc.
the class WatchdogTest method watchOnClientStreamingRPCTimeout.
@Test
public void watchOnClientStreamingRPCTimeout() {
NoopClientCallStub<WriteObjectRequest, WriteObjectResponse> clientCall = new NoopClientCallStub<>();
StreamObserver<WriteObjectRequest> timeoutStreamObserver = new StreamObserver<>() {
@Override
public void onNext(WriteObjectRequest value) {
logger.atInfo().log("Sleeping for 10 seconds");
sleepUninterruptibly(Duration.ofSeconds(10));
}
@Override
public void onError(Throwable t) {
}
@Override
public void onCompleted() {
}
};
StreamObserver<WriteObjectRequest> watch = watchdog.watch(clientCall, timeoutStreamObserver, waitTime);
WriteObjectRequest value = WriteObjectRequest.newBuilder().build();
watch.onNext(value);
assertThat(clientCall.cancelled).isTrue();
assertThat(clientCall.cause).isInstanceOf(TimeoutException.class);
}
use of com.google.storage.v2.WriteObjectResponse in project hadoop-connectors by GoogleCloudDataproc.
the class WatchdogTest method watchPassThroughClientStreamingRPC.
@Test
public void watchPassThroughClientStreamingRPC() {
ClientCall<WriteObjectRequest, WriteObjectResponse> clientCall = new NoopClientCall<>();
StreamObserverStub<WriteObjectRequest> streamObserver = new StreamObserverStub<>();
StreamObserver<WriteObjectRequest> watch = watchdog.watch(clientCall, streamObserver, waitTime);
assertThat(watchdog).isNotNull();
assertThat(watchdog.getOpenStreams()).hasSize(1);
WriteObjectRequest value = WriteObjectRequest.newBuilder().build();
watch.onNext(value);
assertThat(streamObserver.getObjects()).containsExactly(value);
Throwable t = new TimeoutException("Request timeout out");
watch.onError(t);
assertThat(streamObserver.getErrors()).containsExactly(t);
watch.onCompleted();
assertThat(streamObserver.isCompleted()).isTrue();
assertThat(watchdog.getOpenStreams().isEmpty()).isTrue();
}
use of com.google.storage.v2.WriteObjectResponse in project hadoop-connectors by GoogleCloudDataproc.
the class WatchdogTest method watchMultipleStreams.
@Test
public void watchMultipleStreams() {
NoopClientCallStub<WriteObjectRequest, WriteObjectResponse> clientCall = new NoopClientCallStub<>();
StreamObserver<WriteObjectRequest> timeoutStreamObserver = new StreamObserver<>() {
@Override
public void onNext(WriteObjectRequest value) {
logger.atInfo().log("Sleeping for 10 seconds");
sleepUninterruptibly(Duration.ofSeconds(10));
}
@Override
public void onError(Throwable t) {
}
@Override
public void onCompleted() {
}
};
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> clientStreamingRPCWatch = watchdog.watch(requestContext, responseIterator, waitTime);
StreamObserver<WriteObjectRequest> serverStreamingRPCWatch = watchdog.watch(clientCall, timeoutStreamObserver, waitTime);
assertThat(watchdog).isNotNull();
assertThat(watchdog.getOpenStreams()).hasSize(2);
boolean actual = clientStreamingRPCWatch.hasNext();
WriteObjectRequest value = WriteObjectRequest.newBuilder().build();
serverStreamingRPCWatch.onNext(value);
assertThat(actual).isTrue();
assertThat(requestContext.isCancelled()).isTrue();
assertThat(clientCall.cancelled).isTrue();
assertThat(clientCall.cause).isInstanceOf(TimeoutException.class);
assertThat(watchdog.getOpenStreams().isEmpty()).isTrue();
}
Aggregations