Skip to content

Commit 18aca86

Browse files
authored
Exposing AutoExecutor and CallbackExecutor directly (#4983)
1 parent 4a26abe commit 18aca86

File tree

4 files changed

+33
-37
lines changed

4 files changed

+33
-37
lines changed

google-cloud-clients/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1/MessageDispatcher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class MessageDispatcher {
6262
@InternalApi static final Duration PENDING_ACKS_SEND_DELAY = Duration.ofMillis(100);
6363

6464
private final Executor executor;
65-
private final SequentialExecutorService sequentialExecutor;
65+
private final SequentialExecutorService.AutoExecutor sequentialExecutor;
6666
private final ScheduledExecutorService systemExecutor;
6767
private final ApiClock clock;
6868

@@ -206,7 +206,7 @@ void sendAckOperations(
206206
jobLock = new ReentrantLock();
207207
messagesWaiter = new MessageWaiter();
208208
this.clock = clock;
209-
this.sequentialExecutor = new SequentialExecutorService(executor);
209+
this.sequentialExecutor = new SequentialExecutorService.AutoExecutor(executor);
210210
}
211211

212212
void start() {

google-cloud-clients/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1/Publisher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public class Publisher {
9999
private final PublisherStub publisherStub;
100100

101101
private final ScheduledExecutorService executor;
102-
private final SequentialExecutorService sequentialExecutor;
102+
private final SequentialExecutorService.CallbackExecutor sequentialExecutor;
103103
private final AtomicBoolean shutdown;
104104
private final List<AutoCloseable> closeables;
105105
private final MessageWaiter messagesWaiter;
@@ -127,7 +127,7 @@ private Publisher(Builder builder) throws IOException {
127127
messagesBatchLock = new ReentrantLock();
128128
activeAlarm = new AtomicBoolean(false);
129129
executor = builder.executorProvider.getExecutor();
130-
sequentialExecutor = new SequentialExecutorService(executor);
130+
sequentialExecutor = new SequentialExecutorService.CallbackExecutor(executor);
131131
if (builder.executorProvider.shouldAutoClose()) {
132132
closeables =
133133
Collections.<AutoCloseable>singletonList(new ExecutorAsBackgroundResource(executor));

google-cloud-clients/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1/SequentialExecutorService.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,36 +42,18 @@ interface CancellableRunnable extends Runnable {
4242
* be run in parallel.
4343
*/
4444
final class SequentialExecutorService {
45-
private static final Logger logger = Logger.getLogger(SequentialExecutorService.class.getName());
4645

47-
private final CallbackExecutor callbackExecutor;
48-
private final AutoExecutor autoExecutor;
49-
50-
SequentialExecutorService(Executor executor) {
51-
this.callbackExecutor = new CallbackExecutor(executor);
52-
this.autoExecutor = new AutoExecutor(executor);
53-
}
54-
55-
/**
56-
* Runs asynchronous {@code Callable} tasks sequentially. If one of the tasks fails, other tasks
57-
* with the same key that have not been executed will be cancelled.
58-
*/
59-
<T> ApiFuture<T> submit(final String key, final Callable<ApiFuture<T>> callable) {
60-
return callbackExecutor.submit(key, callable);
61-
}
62-
63-
/** Runs synchronous {@code Runnable} tasks sequentially. */
64-
void submit(String key, Runnable runnable) {
65-
autoExecutor.execute(key, runnable);
46+
// This class is not directly usable.
47+
private SequentialExecutorService() {
6648
}
6749

6850
/**
69-
* Internal implementation of SequentialExecutorService. Takes a serial stream of string keys and
51+
* This Executor takes a serial stream of string keys and
7052
* {@code Runnable} tasks, and runs the tasks with the same key sequentially. Tasks with the same
7153
* key will be run only when its predecessor has been completed while tasks with different keys
7254
* can be run in parallel.
7355
*/
74-
abstract static class SequentialExecutor {
56+
private abstract static class SequentialExecutor {
7557
// Maps keys to tasks.
7658
protected final Map<String, Deque<Runnable>> tasksByKey;
7759
protected final Executor executor;
@@ -81,7 +63,7 @@ private SequentialExecutor(Executor executor) {
8163
this.tasksByKey = new HashMap<>();
8264
}
8365

84-
void execute(final String key, Runnable task) {
66+
protected void execute(final String key, Runnable task) {
8567
Deque<Runnable> newTasks;
8668
synchronized (tasksByKey) {
8769
newTasks = tasksByKey.get(key);
@@ -110,11 +92,16 @@ protected void invokeCallback(final Deque<Runnable> tasks) {
11092
}
11193
}
11294

113-
private static class AutoExecutor extends SequentialExecutor {
95+
static class AutoExecutor extends SequentialExecutor {
11496
AutoExecutor(Executor executor) {
11597
super(executor);
11698
}
11799

100+
/** Runs synchronous {@code Runnable} tasks sequentially. */
101+
void submit(String key, Runnable task) {
102+
super.execute(key, task);
103+
}
104+
118105
@Override
119106
protected void execute(final String key, final Deque<Runnable> tasks) {
120107
executor.execute(
@@ -142,12 +129,21 @@ private void invokeCallbackAndExecuteNext(final String key, final Deque<Runnable
142129
}
143130
}
144131

145-
private static class CallbackExecutor extends SequentialExecutor {
132+
/**
133+
* Runs asynchronous {@code Callable} tasks sequentially for the same key. If one of the tasks fails, other tasks
134+
* with the same key that have not been executed will be cancelled.
135+
*/
136+
static class CallbackExecutor extends SequentialExecutor {
137+
private static final Logger logger = Logger.getLogger(SequentialExecutorService.SequentialExecutor.class.getName());
138+
146139
CallbackExecutor(Executor executor) {
147140
super(executor);
148141
}
149142

150143
/**
144+
* Runs asynchronous {@code Callable} tasks sequentially. If one of the tasks fails, other tasks
145+
* with the same key that have not been executed will be cancelled.
146+
* <p>
151147
* This method does the following in a chain:
152148
*
153149
* <ol>

google-cloud-clients/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/v1/SequentialExecutorServiceTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public void finish() {
7171

7272
@Test
7373
public void testExecutorRunsNextTaskWhenPrevResponseReceived() throws Exception {
74-
SequentialExecutorService sequentialExecutorService =
75-
new SequentialExecutorService(executorProvider.getExecutor());
74+
SequentialExecutorService.CallbackExecutor sequentialExecutorService =
75+
new SequentialExecutorService.CallbackExecutor(executorProvider.getExecutor());
7676
AsyncTaskCallable callable1 = new AsyncTaskCallable();
7777
AsyncTaskCallable callable2 = new AsyncTaskCallable();
7878
AsyncTaskCallable callable3 = new AsyncTaskCallable();
@@ -97,8 +97,8 @@ public void testExecutorRunsNextTaskWhenPrevResponseReceived() throws Exception
9797

9898
@Test
9999
public void testExecutorRunsDifferentKeySimultaneously() throws Exception {
100-
SequentialExecutorService sequentialExecutorService =
101-
new SequentialExecutorService(executorProvider.getExecutor());
100+
SequentialExecutorService.CallbackExecutor sequentialExecutorService =
101+
new SequentialExecutorService.CallbackExecutor(executorProvider.getExecutor());
102102
AsyncTaskCallable callable1 = new AsyncTaskCallable();
103103
AsyncTaskCallable callable2 = new AsyncTaskCallable();
104104
AsyncTaskCallable callable3 = new AsyncTaskCallable();
@@ -126,8 +126,8 @@ public void testExecutorRunsDifferentKeySimultaneously() throws Exception {
126126

127127
@Test
128128
public void testExecutorCancelsAllTasksWhenOneFailed() throws Exception {
129-
SequentialExecutorService sequentialExecutorService =
130-
new SequentialExecutorService(executorProvider.getExecutor());
129+
SequentialExecutorService.CallbackExecutor sequentialExecutorService =
130+
new SequentialExecutorService.CallbackExecutor(executorProvider.getExecutor());
131131
AsyncTaskCallable callable1 = new AsyncTaskCallable();
132132
AsyncTaskCallable callable2 = new AsyncTaskCallable();
133133
AsyncTaskCallable callable3 = new AsyncTaskCallable();
@@ -207,8 +207,8 @@ public void run() {
207207
public void SequentialExecutorRunsTasksAutomatically() throws Exception {
208208
int numKeys = 100;
209209
int numTasks = 100;
210-
SequentialExecutorService sequentialExecutor =
211-
new SequentialExecutorService(executorProvider.getExecutor());
210+
SequentialExecutorService.AutoExecutor sequentialExecutor =
211+
new SequentialExecutorService.AutoExecutor(executorProvider.getExecutor());
212212
CountDownLatch remainingTasksCount = new CountDownLatch(numKeys * numTasks);
213213
// Maps keys to lists of started and completed tasks.
214214
Map<String, LinkedHashSet<Integer>> startedTasks = new HashMap<>();

0 commit comments

Comments
 (0)