Search in sources :

Example 6 with Context

use of io.grpc.Context in project grpc-java by grpc.

the class CascadingTest method testCascadingCancellationViaOuterContextCancellation.

/**
   * Test {@link Context} cancellation propagates from the first node in the call chain all the way
   * to the last.
   */
@Test
public void testCascadingCancellationViaOuterContextCancellation() throws Exception {
    observedCancellations = new CountDownLatch(2);
    receivedCancellations = new CountDownLatch(3);
    Future<?> chainReady = startChainingServer(3);
    CancellableContext context = Context.current().withCancellation();
    Future<SimpleResponse> future;
    Context prevContext = context.attach();
    try {
        future = futureStub.unaryCall(SimpleRequest.getDefaultInstance());
    } finally {
        context.detach(prevContext);
    }
    chainReady.get(5, TimeUnit.SECONDS);
    context.cancel(null);
    try {
        future.get(5, TimeUnit.SECONDS);
        fail("Expected cancellation");
    } catch (ExecutionException ex) {
        Status status = Status.fromThrowable(ex);
        assertEquals(Status.Code.CANCELLED, status.getCode());
        // Should have observed 2 cancellations responses from downstream servers
        if (!observedCancellations.await(5, TimeUnit.SECONDS)) {
            fail("Expected number of cancellations not observed by clients");
        }
        if (!receivedCancellations.await(5, TimeUnit.SECONDS)) {
            fail("Expected number of cancellations to be received by servers not observed");
        }
    }
}
Also used : Context(io.grpc.Context) CancellableContext(io.grpc.Context.CancellableContext) Status(io.grpc.Status) CancellableContext(io.grpc.Context.CancellableContext) SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 7 with Context

use of io.grpc.Context in project google-cloud-java by GoogleCloudPlatform.

the class ITWriteTest method deadline.

@Test
public void deadline() {
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    // Cloud Spanner is fast, but not this fast :-)
    Context.CancellableContext context = Context.current().withDeadlineAfter(10, TimeUnit.NANOSECONDS, executor);
    Runnable work = context.wrap(new Runnable() {

        @Override
        public void run() {
            write(baseInsert().set("BoolValue").to(true).build());
        }
    });
    try {
        work.run();
    } catch (SpannerException e) {
        MatcherAssert.assertThat(e, isSpannerException(ErrorCode.DEADLINE_EXCEEDED));
    } finally {
        executor.shutdown();
    }
}
Also used : Context(io.grpc.Context) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SpannerMatchers.isSpannerException(com.google.cloud.spanner.SpannerMatchers.isSpannerException) SpannerException(com.google.cloud.spanner.SpannerException) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 8 with Context

use of io.grpc.Context in project google-cloud-java by GoogleCloudPlatform.

the class SpannerImpl method runWithRetries.

/**
   * Helper to execute some work, retrying with backoff on retryable errors.
   *
   * <p>TODO: Consider replacing with RetryHelper from gcloud-core.
   */
static <T> T runWithRetries(Callable<T> callable) {
    // Use same backoff setting as abort, somewhat arbitrarily.
    ExponentialBackOff backOff = newBackOff();
    Context context = Context.current();
    while (true) {
        try {
            return callable.call();
        } catch (SpannerException e) {
            if (!e.isRetryable()) {
                throw e;
            }
            logger.log(Level.FINE, "Retryable exception, will sleep and retry", e);
            backoffSleep(context, backOff);
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
    }
}
Also used : Context(io.grpc.Context) SpannerExceptionFactory.newSpannerException(com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException) ExponentialBackOff(com.google.api.client.util.ExponentialBackOff) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) SpannerExceptionFactory.newSpannerException(com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with Context

use of io.grpc.Context in project google-cloud-java by GoogleCloudPlatform.

the class SpannerImpl method backoffSleep.

private static void backoffSleep(Context context, long backoffMillis) throws SpannerException {
    final CountDownLatch latch = new CountDownLatch(1);
    final Context.CancellationListener listener = new Context.CancellationListener() {

        @Override
        public void cancelled(Context context) {
            // Wakeup on cancellation / DEADLINE_EXCEEDED.
            latch.countDown();
        }
    };
    context.addListener(listener, DirectExecutor.INSTANCE);
    try {
        if (backoffMillis == BackOff.STOP) {
            // Highly unlikely but we handle it just in case.
            backoffMillis = MAX_BACKOFF_MS;
        }
        if (latch.await(backoffMillis, TimeUnit.MILLISECONDS)) {
            // Woken by context cancellation.
            throw newSpannerExceptionForCancellation(context, null);
        }
    } catch (InterruptedException interruptExcept) {
        throw newSpannerExceptionForCancellation(context, interruptExcept);
    } finally {
        context.removeListener(listener);
    }
}
Also used : Context(io.grpc.Context) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 10 with Context

use of io.grpc.Context in project google-cloud-java by GoogleCloudPlatform.

the class SpannerImplRetryTest method contextDeadlineExceeded.

@Test
public void contextDeadlineExceeded() {
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    Context.CancellableContext context = Context.current().withDeadlineAfter(10, TimeUnit.NANOSECONDS, executor);
    Mockito.when(callable.call()).thenThrow(new RetryableException(ErrorCode.UNAVAILABLE, "Failure #1"));
    Runnable work = context.wrap(new Runnable() {

        @Override
        public void run() {
            SpannerImpl.runWithRetries(callable);
        }
    });
    expectedException.expect(isSpannerException(ErrorCode.DEADLINE_EXCEEDED));
    work.run();
}
Also used : Context(io.grpc.Context) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Test(org.junit.Test)

Aggregations

Context (io.grpc.Context)20 Test (org.junit.Test)15 Metadata (io.grpc.Metadata)10 StatsContext (com.google.instrumentation.stats.StatsContext)8 CallOptions (io.grpc.CallOptions)4 Status (io.grpc.Status)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 IntegrationTest (com.google.cloud.spanner.IntegrationTest)2 SpannerException (com.google.cloud.spanner.SpannerException)2 SpannerMatchers.isSpannerException (com.google.cloud.spanner.SpannerMatchers.isSpannerException)2 ClientCall (io.grpc.ClientCall)2 ServerCall (io.grpc.ServerCall)2 ServiceDescriptor (io.grpc.ServiceDescriptor)2 JumpToApplicationThreadServerStreamListener (io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ExecutionException (java.util.concurrent.ExecutionException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ExponentialBackOff (com.google.api.client.util.ExponentialBackOff)1