Search in sources :

Example 1 with WriteObjectResponse

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);
    }
}
Also used : ApiStreamObserver(com.google.api.gax.rpc.ApiStreamObserver) StorageClient(com.google.storage.v2.StorageClient) WriteObjectRequest(com.google.storage.v2.WriteObjectRequest) WriteObjectResponse(com.google.storage.v2.WriteObjectResponse)

Example 2 with WriteObjectResponse

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");
        }
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) StorageGrpc(com.google.storage.v2.StorageGrpc) ByteString(com.google.protobuf.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) WriteObjectRequest(com.google.storage.v2.WriteObjectRequest) WriteObjectResponse(com.google.storage.v2.WriteObjectResponse)

Example 3 with WriteObjectResponse

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);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) WriteObjectRequest(com.google.storage.v2.WriteObjectRequest) WriteObjectResponse(com.google.storage.v2.WriteObjectResponse) Test(org.junit.Test)

Example 4 with WriteObjectResponse

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();
}
Also used : NoopClientCall(io.grpc.internal.NoopClientCall) WriteObjectRequest(com.google.storage.v2.WriteObjectRequest) WriteObjectResponse(com.google.storage.v2.WriteObjectResponse) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 5 with WriteObjectResponse

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();
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ReadObjectResponse(com.google.storage.v2.ReadObjectResponse) CancellableContext(io.grpc.Context.CancellableContext) Iterator(java.util.Iterator) WriteObjectRequest(com.google.storage.v2.WriteObjectRequest) WriteObjectResponse(com.google.storage.v2.WriteObjectResponse) Test(org.junit.Test)

Aggregations

WriteObjectRequest (com.google.storage.v2.WriteObjectRequest)7 WriteObjectResponse (com.google.storage.v2.WriteObjectResponse)7 StreamObserver (io.grpc.stub.StreamObserver)5 Test (org.junit.Test)4 ByteString (com.google.protobuf.ByteString)2 StorageGrpc (com.google.storage.v2.StorageGrpc)2 ApiStreamObserver (com.google.api.gax.rpc.ApiStreamObserver)1 BackOffFactory (com.google.cloud.hadoop.gcsio.GoogleCloudStorageImpl.BackOffFactory)1 GoogleCloudStorageImpl.encodeMetadata (com.google.cloud.hadoop.gcsio.GoogleCloudStorageImpl.encodeMetadata)1 AbstractGoogleAsyncWriteChannel (com.google.cloud.hadoop.util.AbstractGoogleAsyncWriteChannel)1 AsyncWriteChannelOptions (com.google.cloud.hadoop.util.AsyncWriteChannelOptions)1 ResilientOperation (com.google.cloud.hadoop.util.ResilientOperation)1 RetryDeterminer (com.google.cloud.hadoop.util.RetryDeterminer)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 GoogleLogger (com.google.common.flogger.GoogleLogger)1 Hasher (com.google.common.hash.Hasher)1 Hashing (com.google.common.hash.Hashing)1 BaseEncoding (com.google.common.io.BaseEncoding)1 ByteStreams (com.google.common.io.ByteStreams)1