scylladb / scylla-cdc-java

Apache License 2.0
24 stars 15 forks source link

Gracefully shutdown TaskActionsQueue #27

Closed avelanarius closed 3 years ago

avelanarius commented 3 years ago

This PR is dependent on #26 (therefore this many commits - only the last one is related to this PR).

Previously, some started Futures in TaskActionsQueue were not properly finished in case of shutdown of Worker.

The core issue was that in TaskActionsQueue we run() TaskActions, which return a CompleteableFuture, but no one waited on that Future or checked its status.

The other issue was that sleeps generated by ScheduledExecutorService were not immediately stopped in case of Worker stop. One unwanted effect of this was that after we closed Driver3's Session, a few seconds later some Futures "sleeping" on that Executor would "wake up" and start to use recently closed Session, causing many exceptions.

This commit fixes the first issue by saving started Futures into a set of running Futures. Those Futures delete themselves from that set after they have finished running. When closing TaskActionsQueue we wait for this set to get empty (with some timeout).

This commit also adds DelayedFutureService which generates "sleeping" futures and allows to immediately complete them in case of shutdown (so that the shutdown is very quick).