google-cloud-java icon indicating copy to clipboard operation
google-cloud-java copied to clipboard

DEADLINE_EXCEEDED: deadline exceeded after 11998656733ns

Open roku6185 opened this issue 7 years ago • 10 comments

I'm trying to run PubSub sync. pull with long-running tasks, e.g. 15-60 min, but I get DEADLINE errors after 10 minutes. The subscription has an ack deadline of 120 seconds. I'm extending the ack deadline at every 110 seconds using a ScheduledExecutorService. The worker task is simulated with a sleep of 15 minutes. One message is pulled and handled at a time.

Environment details

  • OS: Ubuntu 18.04.1 LTS
  • Java version:
    • openjdk version "1.8.0_162"
    • OpenJDK Runtime Environment (build 1.8.0_162-8u162-b12-1-b12)
    • OpenJDK 64-Bit Server VM (build 25.162-b12, mixed mode)
  • Maven:
    • google-cloud-pubsub: 1.55.0
    • gax-grpc: 1.35.0

Steps to reproduce

  1. Create a new topic
  2. Create a new pull subscription with 120 seconds ack deadline
  3. Update SUBSCRIPTION variable with your project and subscription
  4. Start program
  5. Publish a new message on topic
  6. Wait 10 minutes

Stacktrace

Dec 13, 2018 1:24:26 PM com.dummy.PubSubSyncPull init INFO: Setting up synchronous pull...
Dec 13, 2018 1:24:30 PM com.dummy.PubSubSyncPull init INFO: Got 1 message(s)...
Dec 13, 2018 1:24:30 PM com.dummy.PubSubSyncPull$WorkerRunnable run INFO: Handling task 197502272691014 using thread: pool-1-thread-1
Dec 13, 2018 1:26:20 PM com.dummy.PubSubSyncPull$ModAckDeadlineRunnable run INFO: Extending ack deadline for task 197502272691014
Dec 13, 2018 1:28:10 PM com.dummy.PubSubSyncPull$ModAckDeadlineRunnable run INFO: Extending ack deadline for task 197502272691014
Dec 13, 2018 1:30:00 PM com.dummy.PubSubSyncPull$ModAckDeadlineRunnable run INFO: Extending ack deadline for task 197502272691014
Dec 13, 2018 1:31:50 PM com.dummy.PubSubSyncPull$ModAckDeadlineRunnable run INFO: Extending ack deadline for task 197502272691014
Dec 13, 2018 1:33:40 PM com.dummy.PubSubSyncPull$ModAckDeadlineRunnable run INFO: Extending ack deadline for task 197502272691014
Dec 13, 2018 1:34:17 PM com.dummy.PubSubSyncPull init
WARNING: Error
com.google.api.gax.rpc.DeadlineExceededException: io.grpc.StatusRuntimeException: DEADLINE_EXCEEDED: deadline exceeded after 11998988342ns
	at com.google.api.gax.rpc.ApiExceptionFactory.createException(ApiExceptionFactory.java:51)
	at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:72)
	at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:60)
	at com.google.api.gax.grpc.GrpcExceptionCallable$ExceptionTransformingFuture.onFailure(GrpcExceptionCallable.java:97)
	at com.google.api.core.ApiFutures$1.onFailure(ApiFutures.java:68)
	at com.google.common.util.concurrent.Futures$CallbackListener.run(Futures.java:1052)
	at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:398)
	at com.google.common.util.concurrent.AbstractFuture.executeListener(AbstractFuture.java:1030)
	at com.google.common.util.concurrent.AbstractFuture.complete(AbstractFuture.java:871)
	at com.google.common.util.concurrent.AbstractFuture.setException(AbstractFuture.java:716)
	at io.grpc.stub.ClientCalls$GrpcFuture.setException(ClientCalls.java:507)
	at io.grpc.stub.ClientCalls$UnaryStreamToFuture.onClose(ClientCalls.java:482)
	at io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39)
	at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:23)
	at io.grpc.ForwardingClientCallListener$SimpleForwardingClientCallListener.onClose(ForwardingClientCallListener.java:40)
	at io.grpc.internal.CensusStatsModule$StatsClientInterceptor$1$1.onClose(CensusStatsModule.java:678)
	at io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39)
	at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:23)
	at io.grpc.ForwardingClientCallListener$SimpleForwardingClientCallListener.onClose(ForwardingClientCallListener.java:40)
	at io.grpc.internal.CensusTracingModule$TracingClientInterceptor$1$1.onClose(CensusTracingModule.java:397)
	at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:459)
	at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:63)
	at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.close(ClientCallImpl.java:546)
	at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.access$600(ClientCallImpl.java:467)
	at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:584)
	at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
	at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
	Suppressed: com.google.api.gax.rpc.AsyncTaskException: Asynchronous task failed
		at com.google.api.gax.rpc.ApiExceptions.callAndTranslateApiException(ApiExceptions.java:57)
		at com.google.api.gax.rpc.UnaryCallable.call(UnaryCallable.java:112)
		at com.dummy.PubSubSyncPull.init(PubSubSyncPull.java:44)
		at com.dummy.MainApplication.main(MainApplication.java:6)
Caused by: io.grpc.StatusRuntimeException: DEADLINE_EXCEEDED: deadline exceeded after 11998988342ns
	at io.grpc.Status.asRuntimeException(Status.java:526)
	... 23 more

Code snippet

MainApplication.java

package com.dummy;

public class MainApplication {
    public static void main(String[] args) throws Exception {
        PubSubSyncPull puller = new PubSubSyncPull();
        puller.init();
    }
}

PubSubSyncPull.java

package com.dummy;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub;
import com.google.cloud.pubsub.v1.stub.SubscriberStub;
import com.google.cloud.pubsub.v1.stub.SubscriberStubSettings;
import com.google.pubsub.v1.AcknowledgeRequest;
import com.google.pubsub.v1.ModifyAckDeadlineRequest;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.PullRequest;
import com.google.pubsub.v1.PullResponse;
import com.google.pubsub.v1.ReceivedMessage;

public class PubSubSyncPull {
    private final Logger logger = Logger.getLogger(this.getClass().getName());

    private static final long FAKE_WORKER_DURATION_IN_MILLISECONDS = 15 * 60 * 1_000L; // 15 min.
    private static final int ACK_DEADLINE_IN_SECONDS = 120;
    private static final int EXTEND_ACK_DEADLINE_SAFETY_MARGIN_IN_SECONDS = 10;
    private static final TimeUnit EXTEND_ACK_DEADLINE_UNIT = TimeUnit.SECONDS;
    private static final String SUBSCRIPTION = ProjectSubscriptionName.format("PROJECT_ID", "SUBSCRIPTION_ID");

    public void init() {
        final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
        final ScheduledExecutorService ackDeadlineModifier = Executors
                .newScheduledThreadPool(Runtime.getRuntime().availableProcessors());

        try {
            logger.info("Setting up synchronous pull...");

            SubscriberStubSettings subscriberStubSettings = SubscriberStubSettings.newBuilder().build();

            try (SubscriberStub subscriber = GrpcSubscriberStub.create(subscriberStubSettings)) {
                PullRequest pullRequest = PullRequest.newBuilder()
                    .setMaxMessages(1)
                    .setReturnImmediately(false)
                    .setSubscription(SUBSCRIPTION)
                    .build();

                while (true) {
                    PullResponse pullResponse = subscriber.pullCallable().call(pullRequest);

                    if (pullResponse.getReceivedMessagesList().size() == 0) {
                        continue;
                    }

                    logger.info(String.format("Got %d message(s)...", pullResponse.getReceivedMessagesList().size()));

                    for (ReceivedMessage message : pullResponse.getReceivedMessagesList()) {
                        final Runnable modAckDeadlineTask = new ModAckDeadlineRunnable(subscriber, message);
                        final ScheduledFuture<?> modAckDeadLineFuture = ackDeadlineModifier.scheduleAtFixedRate(
                                modAckDeadlineTask,
                                ACK_DEADLINE_IN_SECONDS - EXTEND_ACK_DEADLINE_SAFETY_MARGIN_IN_SECONDS,
                                ACK_DEADLINE_IN_SECONDS - EXTEND_ACK_DEADLINE_SAFETY_MARGIN_IN_SECONDS,
                                EXTEND_ACK_DEADLINE_UNIT);

                        final Runnable worker = new WorkerRunnable(subscriber, modAckDeadLineFuture, message);
                        executor.submit(worker);
                    }
                }
            }

        } catch (Exception e) {
            logger.warning("Error");
            e.printStackTrace();
        }
    }

    class WorkerRunnable implements Runnable {
        private final SubscriberStub subscriber;
        private final ScheduledFuture<?> modAckDeadLineFuture;
        private final ReceivedMessage message;

        public WorkerRunnable(final SubscriberStub subscriber, final ScheduledFuture<?> modAckDeadLineFuture,
                final ReceivedMessage message) {
            this.subscriber = subscriber;
            this.modAckDeadLineFuture = modAckDeadLineFuture;
            this.message = message;
        }

        @Override
        public void run() {
            try {
                String id = message.getMessage().getMessageId();

                logger.info(String.format("Handling task %s using thread: %s", id, Thread.currentThread().getName()));

                try {
                    Thread.sleep(FAKE_WORKER_DURATION_IN_MILLISECONDS);

                } catch (Exception e) {
                    logger.warning("Thread error");
                    e.printStackTrace();
                }

                logger.info(String.format("Finishing task: %s", id));

                AcknowledgeRequest request = AcknowledgeRequest.newBuilder()
                    .setSubscription(SUBSCRIPTION)
                    .addAckIds(message.getAckId())
                    .build();

                modAckDeadLineFuture.cancel(true);
                subscriber.acknowledgeCallable().call(request);

            } catch (Exception e) {
                logger.warning("Error");
                e.printStackTrace();
            }
        }
    }

    class ModAckDeadlineRunnable implements Runnable {
        private final SubscriberStub subscriber;
        private final ReceivedMessage message;

        public ModAckDeadlineRunnable(final SubscriberStub subscriber, final ReceivedMessage message) {
            this.subscriber = subscriber;
            this.message = message;
        }

        @Override
        public void run() {
            try {
                logger.info(String.format("Extending ack deadline for task %s", message.getMessage().getMessageId()));
                ModifyAckDeadlineRequest request = ModifyAckDeadlineRequest.newBuilder()
                    .setSubscription(SUBSCRIPTION)
                    .setAckDeadlineSeconds(ACK_DEADLINE_IN_SECONDS)
                    .addAckIds(message.getAckId())
                    .build();

                subscriber.modifyAckDeadlineCallable().call(request);

            } catch (Exception e) {
                logger.warning("Error");
                e.printStackTrace();
            }
        }
    }
}

External references such as API reference guides used

roku6185 avatar Dec 13 '18 12:12 roku6185

This happens while pulling the message, when there are no message for max-timeout. In this case server keeps on sending "DEADLINE_EXCEEDED" if there are no msg for rpc timeout. Clients keep retrying "DEADLINE_EXCEEDED" until it pass max timeout.

javabrown avatar Dec 28 '18 22:12 javabrown

@jadekler, how should clients handle a deadline exceeded in Pub/Sub when there are no messages for the duration of the client's timeout settings?

sduskis avatar Jan 02 '19 15:01 sduskis

@sduskis Do we know what's causing the deadline to be exceeded? I doubt it's the server, nor the GFE (though it does kill connections after 10m, but that conn should be re-established by gRPC opaquely).

edit: Also, do we know which deadline is being exceeded?

jeanbza avatar Jan 02 '19 20:01 jeanbza

@jadekler, Server keep sending DEADLINE_EXCEEDED every 10-15 sec, which is getting retried by the GAX until max rpc timeout, causing lot of error message logging at client-side.

javabrown avatar Jan 03 '19 20:01 javabrown

seems this is same as #4246

ajaaym avatar Jan 03 '19 23:01 ajaaym

I tried the fix described in #4246 and it allows the "long-running" task (15 min duration) to finish, but the main thread is terminated after 1 hour with the following exception:

com.google.api.gax.rpc.UnavailableException: io.grpc.StatusRuntimeException: UNAVAILABLE: HTTP/2 error code: NO_ERROR
Received Goaway
max_age
	at com.google.api.gax.rpc.ApiExceptionFactory.createException(ApiExceptionFactory.java:69)
	at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:72)
	at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:60)
	at com.google.api.gax.grpc.GrpcExceptionCallable$ExceptionTransformingFuture.onFailure(GrpcExceptionCallable.java:97)
	at com.google.api.core.ApiFutures$1.onFailure(ApiFutures.java:68)
	at com.google.common.util.concurrent.Futures$CallbackListener.run(Futures.java:1052)
	at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:398)
	at com.google.common.util.concurrent.AbstractFuture.executeListener(AbstractFuture.java:1030)
	at com.google.common.util.concurrent.AbstractFuture.complete(AbstractFuture.java:871)
	at com.google.common.util.concurrent.AbstractFuture.setException(AbstractFuture.java:716)
	at io.grpc.stub.ClientCalls$GrpcFuture.setException(ClientCalls.java:507)
	at io.grpc.stub.ClientCalls$UnaryStreamToFuture.onClose(ClientCalls.java:482)
	at io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39)
	at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:23)
	at io.grpc.ForwardingClientCallListener$SimpleForwardingClientCallListener.onClose(ForwardingClientCallListener.java:40)
	at io.grpc.internal.CensusStatsModule$StatsClientInterceptor$1$1.onClose(CensusStatsModule.java:678)
	at io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39)
	at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:23)
	at io.grpc.ForwardingClientCallListener$SimpleForwardingClientCallListener.onClose(ForwardingClientCallListener.java:40)
	at io.grpc.internal.CensusTracingModule$TracingClientInterceptor$1$1.onClose(CensusTracingModule.java:397)
	at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:459)
	at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:63)
	at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.close(ClientCallImpl.java:546)
	at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.access$600(ClientCallImpl.java:467)
	at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:584)
	at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
	at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
	Suppressed: com.google.api.gax.rpc.AsyncTaskException: Asynchronous task failed
		at com.google.api.gax.rpc.ApiExceptions.callAndTranslateApiException(ApiExceptions.java:57)
		at com.google.api.gax.rpc.UnaryCallable.call(UnaryCallable.java:112)
		at com.dummy.PubSubSyncPull.init(PubSubSyncPull.java:59)
		at com.dummy.MainApplication.main(MainApplication.java:6)
Caused by: io.grpc.StatusRuntimeException: UNAVAILABLE: HTTP/2 error code: NO_ERROR
Received Goaway
max_age
	at io.grpc.Status.asRuntimeException(Status.java:526)
	... 23 more

roku6185 avatar Jan 07 '19 14:01 roku6185

@roku6185 can you please wrap PullResponse pullResponse = subscriber.pullCallable().call(pullRequest); inside try catch block and retry the request on any exception?

ajaaym avatar Jan 07 '19 15:01 ajaaym

@ajaaym Seems to work fine :+1: Thanks a lot.

roku6185 avatar Jan 08 '19 07:01 roku6185

Thanks @roku6185 Closing this issue. Please feel free to reopen this one or create new one if needed.

ajaaym avatar Jan 08 '19 14:01 ajaaym

@roku6185 can you please wrap PullResponse pullResponse = subscriber.pullCallable().call(pullRequest); inside try catch block and retry the request on any exception?

@ajaaym Is there no other way to handle this error. I have tried multiple versions as well (for eg the most stable one 1.102.0 and older one) but some time or other I receive this deadline exceeded error after an hour so.

Vishesh00 avatar Feb 06 '20 07:02 Vishesh00