This repository has been archived by the owner on Mar 10, 2022. It is now read-only.
/
AbstractExecutionService.java
511 lines (419 loc) · 19.1 KB
/
AbstractExecutionService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//
// AbstractExecutionService.java
//
// Copyright (c) 2017 Couchbase, Inc All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package com.couchbase.lite.internal;
import android.support.annotation.GuardedBy;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.couchbase.lite.LogDomain;
import com.couchbase.lite.internal.support.Log;
import com.couchbase.lite.internal.utils.Preconditions;
/**
* Base ExecutionService that provides the default implementation of serial and concurrent
* executor.
*/
public abstract class AbstractExecutionService implements ExecutionService {
//---------------------------------------------
// Constants
//---------------------------------------------
private static final LogDomain DOMAIN = LogDomain.DATABASE;
private static final int DUMP_INTERVAL_MS = 2000; // 2 seconds
@VisibleForTesting
public static final int MIN_CAPACITY = 64;
private static final Object DUMP_LOCK = new Object();
//---------------------------------------------
// Class members
//---------------------------------------------
private static long lastDump;
//---------------------------------------------
// Types
//---------------------------------------------
@VisibleForTesting
static class InstrumentedTask implements Runnable {
// Putting a `new Exception()` here is useful but extremely expensive
final Exception origin = null;
@NonNull
private final Runnable task;
private final long createdAt = System.currentTimeMillis();
private long startedAt;
private long finishedAt;
private long completedAt;
@Nullable
private volatile Runnable onComplete;
InstrumentedTask(@NonNull Runnable task) { this(task, null); }
InstrumentedTask(@NonNull Runnable task, @Nullable Runnable onComplete) {
this.task = task;
this.onComplete = onComplete;
}
public void setCompletion(@NonNull Runnable onComplete) { this.onComplete = onComplete; }
public void run() {
try {
startedAt = System.currentTimeMillis();
task.run();
finishedAt = System.currentTimeMillis();
}
finally {
onComplete.run();
}
completedAt = System.currentTimeMillis();
}
public String toString() {
return "task[" + createdAt + "," + startedAt + "," + finishedAt + "," + completedAt + " @" + task + "]";
}
}
/**
* This executor schedules tasks on an underlying thread pool executor
* (probably some application-wide executor: the Async Task's on Android).
* </br>If the underlying executor is low on resources, this executor reverts
* to serial execution, using an unbounded pending queue.
* </br>If the executor is stopped while there are unscheduled pending tasks
* (in the pendingTask queue), all of those tasks are simply discarded.
* If the pendingTask queue is non-empty, either the head task is scheduled
* or <code>needsRestart</code> is true (see below) .
* </br>Soft resource exhaustion, <code>spaceAvailable</code>is intended to make it
* unlikely that this executor ever encounters a <code>RejectedExecutionException</code>.
* There are two circumstances under which a <code>RejectedExecutionException</code>
* is possible:
* <nl>
* </li> The underlying executor rejects the execution of a new task, even though
* <code>spaceAvailable</code> returns true. This exception will be passed back
* to client code.
* </li> A task on the pending queue attempts to schedule the next task from the queue
* for execution. When this happens, the queue is stalled and <code>needsRestart</code>
* is set true. Subsequent calls to <code>execute</code> will make a best-effort attempt
* to restart the queue.
* </nl>
*/
private static class ConcurrentExecutor implements CloseableExecutor {
@NonNull
private final ThreadPoolExecutor executor;
@GuardedBy("this")
@NonNull
private final Queue<InstrumentedTask> pendingTasks = new LinkedList<>();
// a non-null stop latch is the flag that this executor has been stopped
@GuardedBy("this")
@Nullable
private CountDownLatch stopLatch;
@GuardedBy("this")
private int running;
@GuardedBy("this")
private boolean needsRestart;
ConcurrentExecutor(@NonNull ThreadPoolExecutor executor) {
Preconditions.checkArgNotNull(executor, "executor");
this.executor = executor;
}
/**
* Schedule a task for concurrent execution.
* There are absolutely no guarantees about execution order, on this executor,
* particularly once it fails back to using the pending task queue.
* If there is insufficient room to schedule the task, safely, on the underlying
* executor, the task is added to the pendingTask queue and executed when space
* is available.
* This method may throw a <code>RejectedExecutionException</code> if the underlying
* executor's resources are completely exhausted even though <code>spaceAvailable</code>
* returns true.
*
* @param task a task for concurrent execution.
* @throws ExecutorClosedException if the executor has been stopped
* @throws RejectedExecutionException if the underlying executor rejects the task
*/
@Override
public void execute(@NonNull Runnable task) {
Preconditions.checkArgNotNull(task, "task");
final int pendingTaskCount;
synchronized (this) {
if (stopLatch != null) { throw new ExecutorClosedException("Executor has been stopped"); }
if (spaceAvailable()) {
if (needsRestart) { restartQueue(); }
executeTask(new InstrumentedTask(task, this::finishTask));
return;
}
pendingTasks.add(new InstrumentedTask(task));
pendingTaskCount = pendingTasks.size();
if (needsRestart || (pendingTaskCount == 1)) { restartQueue(); }
}
Log.w(DOMAIN, "Parallel executor overflow: " + pendingTaskCount);
}
/**
* Stop the executor.
* If there are pending (unscheduled) tasks, they are abandoned.
* If this call returns false, the executor has *not* yet stopped: tasks it scheduled are still running.
*
* @param timeout time to wait for shutdown
* @param unit time unit for shutdown wait
* @return true if all currently scheduled tasks have completed
*/
@Override
public boolean stop(long timeout, @NonNull TimeUnit unit) {
Preconditions.testArg(timeout, "timeout must be >= 0", x -> x >= 0);
Preconditions.checkArgNotNull(unit, "time unit");
final CountDownLatch latch;
synchronized (this) {
if (stopLatch == null) {
pendingTasks.clear();
stopLatch = new CountDownLatch(1);
}
if (running <= 0) { return true; }
latch = stopLatch;
}
try { return latch.await(timeout, unit); }
catch (InterruptedException ignore) { }
return false;
}
void finishTask() {
final CountDownLatch latch;
synchronized (this) {
if (--running > 0) { return; }
latch = stopLatch;
}
if (latch != null) { latch.countDown(); }
}
// Called on completion of the task at the head of the pending queue.
void scheduleNext() {
synchronized (this) {
// the executor has been stopped
if (pendingTasks.size() <= 0) { return; }
// completing task is head of queue: remove it
pendingTasks.remove();
// run as many tasks as possible
try {
while (true) {
final InstrumentedTask task = pendingTasks.peek();
if (task == null) { return; }
if (!spaceAvailable()) { break; }
task.setCompletion(this::finishTask);
executeTask(task);
pendingTasks.remove();
}
}
catch (RejectedExecutionException ignore) { }
// assert: on exiting the loop, head of queue is first unexecutable (soft or hard) task
// it has not been submitted, successfully, for execution.
restartQueue();
}
}
// assert: queue is not empty.
private void restartQueue() {
final InstrumentedTask task = pendingTasks.peek();
try {
if (task != null) {
task.setCompletion(this::scheduleNext);
executeTask(task);
}
needsRestart = false;
return;
}
catch (RejectedExecutionException ignore) { }
needsRestart = true;
}
@GuardedBy("this")
private void executeTask(@NonNull InstrumentedTask newTask) {
try {
executor.execute(newTask);
running++;
}
catch (RejectedExecutionException e) {
dumpExecutorState(newTask, e);
throw e;
}
}
// Note that this is only accurate at the moment it is called...
private boolean spaceAvailable() { return executor.getQueue().remainingCapacity() > MIN_CAPACITY; }
// This shouldn't happen. Checking `spaceAvailable` should guarantee that the
// underlying executor always has resources when we attempt to execute something.
private void dumpExecutorState(@Nullable InstrumentedTask current, @Nullable RejectedExecutionException ex) {
if (throttled()) { return; }
dumpServiceState(executor, "size: " + running, ex);
Log.w(DOMAIN, "==== Concurrent Executor status: " + this);
if (needsRestart) { Log.w(DOMAIN, "= stalled"); }
if (current != null) { Log.w(DOMAIN, "== Current task: " + current, current.origin); }
final ArrayList<InstrumentedTask> waiting = new ArrayList<>(pendingTasks);
Log.w(DOMAIN, "== Pending tasks: " + waiting.size());
int n = 0;
for (InstrumentedTask t : waiting) { Log.w(DOMAIN, "@" + (++n) + ": " + t, t.origin); }
}
}
/**
* Serial execution, patterned after AsyncTask's executor.
* Tasks are queued on an unbounded queue and executed one at a time
* on an underlying executor: the head of the queue is the currently running task.
* Since this executor can have at most two tasks scheduled on the underlying
* executor, ensuring space on that executor makes it unlikely that
* a serial executor will refuse a task for execution.
*/
private static class SerialExecutor implements CloseableExecutor {
@NonNull
private final ThreadPoolExecutor executor;
@GuardedBy("this")
@NonNull
private final Queue<InstrumentedTask> pendingTasks = new LinkedList<>();
// a non-null stop latch is the flag that this executor has been stopped
@GuardedBy("this")
@Nullable
private CountDownLatch stopLatch;
@GuardedBy("this")
private boolean needsRestart;
SerialExecutor(@NonNull ThreadPoolExecutor executor) {
Preconditions.checkArgNotNull(executor, "executor");
this.executor = executor;
}
/**
* Schedule a task for in-order execution.
*
* @param task a task to be executed after all currently pending tasks.
* @throws ExecutorClosedException if the executor has been stopped
* @throws RejectedExecutionException if the underlying executor rejects the task
*/
@Override
public void execute(@NonNull Runnable task) {
Preconditions.checkArgNotNull(task, "task");
synchronized (this) {
if (stopLatch != null) { throw new ExecutorClosedException("Executor has been stopped"); }
pendingTasks.add(new InstrumentedTask(task, this::scheduleNext));
if (needsRestart || (pendingTasks.size() == 1)) { executeTask(null); }
}
}
/**
* Stop the executor.
* If this call returns false, the executor has *not* yet stopped.
* It will continue to run tasks from its queue until all have completed.
*
* @param timeout time to wait for shutdown
* @param unit time unit for shutdown wait
* @return true if all currently scheduled tasks completed before the shutdown
*/
@Override
public boolean stop(long timeout, @NonNull TimeUnit unit) {
Preconditions.testArg(timeout, "timeout must be >= 0", x -> x >= 0);
Preconditions.checkArgNotNull(unit, "time unit");
final CountDownLatch latch;
synchronized (this) {
if (stopLatch == null) { stopLatch = new CountDownLatch(1); }
if (pendingTasks.size() <= 0) { return true; }
latch = stopLatch;
}
try { return latch.await(timeout, unit); }
catch (InterruptedException ignore) { }
return false;
}
// Called on completion of the task at the head of the pending queue.
private void scheduleNext() {
final CountDownLatch latch;
synchronized (this) {
executeTask(pendingTasks.remove());
latch = (pendingTasks.size() > 0) ? null : stopLatch;
}
if (latch != null) { latch.countDown(); }
}
@GuardedBy("this")
private void executeTask(@Nullable InstrumentedTask prevTask) {
final InstrumentedTask nextTask = pendingTasks.peek();
if (nextTask == null) { return; }
try {
executor.execute(nextTask);
needsRestart = false;
}
catch (RejectedExecutionException e) {
needsRestart = true;
dumpExecutorState(e, prevTask);
}
}
private void dumpExecutorState(@NonNull RejectedExecutionException ex, @Nullable InstrumentedTask prev) {
if (throttled()) { return; }
dumpServiceState(executor, "size: " + pendingTasks.size(), ex);
Log.w(DOMAIN, "==== Serial Executor status: " + this);
if (needsRestart) { Log.w(DOMAIN, "= stalled"); }
if (prev != null) { Log.w(DOMAIN, "== Previous task: " + prev, prev.origin); }
if (pendingTasks.isEmpty()) { Log.w(DOMAIN, "== Queue is empty"); }
else {
final ArrayList<InstrumentedTask> waiting = new ArrayList<>(pendingTasks);
final InstrumentedTask current = waiting.remove(0);
Log.w(DOMAIN, "== Current task: " + current, current.origin);
Log.w(DOMAIN, "== Pending tasks: " + waiting.size());
int n = 0;
for (InstrumentedTask t : waiting) { Log.w(DOMAIN, "@" + (++n) + ": " + t, t.origin); }
}
}
}
//---------------------------------------------
// Class methods
//---------------------------------------------
// check `throttled()` before calling.
static void dumpServiceState(@NonNull Executor ex, @NonNull String msg, @Nullable Exception e) {
Log.w(LogDomain.DATABASE, "====== Catastrophic failure of executor " + ex + ": " + msg, e);
final Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
Log.w(DOMAIN, "==== Threads: " + stackTraces.size());
for (Map.Entry<Thread, StackTraceElement[]> stack : stackTraces.entrySet()) {
Log.w(DOMAIN, "== Thread: " + stack.getKey());
for (StackTraceElement frame : stack.getValue()) { Log.w(DOMAIN, " at " + frame); }
}
if (!(ex instanceof ThreadPoolExecutor)) { return; }
final ArrayList<Runnable> waiting = new ArrayList<>(((ThreadPoolExecutor) ex).getQueue());
Log.w(DOMAIN, "==== Executor queue: " + waiting.size());
int n = 0;
for (Runnable r : waiting) {
final Exception orig = (!(r instanceof InstrumentedTask)) ? null : ((InstrumentedTask) r).origin;
Log.w(DOMAIN, "@" + (n++) + ": " + r, orig);
}
}
static boolean throttled() {
final long now = System.currentTimeMillis();
synchronized (DUMP_LOCK) {
if ((now - lastDump) < DUMP_INTERVAL_MS) { return true; }
lastDump = now;
}
return false;
}
//---------------------------------------------
// Instance members
//---------------------------------------------
@NonNull
private final ThreadPoolExecutor baseExecutor;
@NonNull
private final ConcurrentExecutor concurrentExecutor;
//---------------------------------------------
// Constructor
//---------------------------------------------
protected AbstractExecutionService(@NonNull ThreadPoolExecutor baseExecutor) {
this.baseExecutor = baseExecutor;
concurrentExecutor = new ConcurrentExecutor(baseExecutor);
}
//---------------------------------------------
// Public methods
//---------------------------------------------
@NonNull
@Override
public CloseableExecutor getSerialExecutor() { return new SerialExecutor(baseExecutor); }
@NonNull
@Override
public CloseableExecutor getConcurrentExecutor() { return concurrentExecutor; }
//---------------------------------------------
// Package-private methods
//---------------------------------------------
@VisibleForTesting
void dumpExecutorState() { concurrentExecutor.dumpExecutorState(null, new RejectedExecutionException()); }
}