This repository was archived by the owner on Mar 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManagedAppendSession.java
More file actions
436 lines (376 loc) · 15.2 KB
/
ManagedAppendSession.java
File metadata and controls
436 lines (376 loc) · 15.2 KB
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
package s2.client;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningScheduledExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.SettableFuture;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import s2.config.AppendRetryPolicy;
import s2.types.AppendInput;
import s2.types.AppendOutput;
import s2.v1alpha.AppendSessionRequest;
import s2.v1alpha.AppendSessionResponse;
public class ManagedAppendSession implements AutoCloseable {
static final int ACQUIRE_QUANTUM_MS = 50;
private static final Logger logger =
LoggerFactory.getLogger(ManagedAppendSession.class.getName());
final ListeningScheduledExecutorService executor;
final StreamClient client;
final Integer bufferCapacityBytes;
final Semaphore inflightBytes;
final AtomicInteger remainingAttempts;
final AtomicReference<Optional<Long>> nextDeadlineSystemNanos =
new AtomicReference<>(Optional.empty());
final AtomicBoolean acceptingAppends = new AtomicBoolean(true);
final LinkedBlockingQueue<InflightRecord> inflightQueue = new LinkedBlockingQueue<>();
final LinkedBlockingQueue<Notification> notificationQueue = new LinkedBlockingQueue<>();
final ListenableFuture<Void> daemon;
ManagedAppendSession(StreamClient client) {
this.executor = MoreExecutors.listeningDecorator(client.executor);
this.client = client;
this.daemon = retryingDaemon();
this.bufferCapacityBytes = client.config.maxAppendInflightBytes;
this.inflightBytes = new Semaphore(this.bufferCapacityBytes);
this.remainingAttempts = new AtomicInteger(this.client.config.maxRetries);
}
public Integer remainingBufferCapacityBytes() {
return this.inflightBytes.availablePermits();
}
private ListenableFuture<Void> retryingDaemon() {
return Futures.catchingAsync(
executor.submit(this::daemon),
Throwable.class,
err -> {
var status = Status.fromThrowable(err);
var currentRemainingAttempts = this.remainingAttempts.getAndDecrement();
if (client.config.appendRetryPolicy == AppendRetryPolicy.ALL
&& BaseClient.retryableStatus(status)
&& currentRemainingAttempts > 0) {
logger.debug(
"Retrying error with (original err={}) status={}, after {} delay.",
err,
status,
client.config.retryDelay);
return Futures.scheduleAsync(
this::retryingDaemon, client.config.retryDelay, this.executor);
} else {
logger.warn(
"Not retrying error with status={}. Cleaning up append session.", status.getCode());
return executor.submit(() -> cleanUp(status));
}
},
this.executor);
}
public ListenableFuture<AppendOutput> submit(AppendInput input, Duration maxWait)
throws InterruptedException {
long meteredBytes = input.meteredBytes();
if (!acquirePermits((int) meteredBytes, maxWait)) {
throw new RuntimeException("Unable to acquire permits within deadline.");
}
var record = InflightRecord.construct(input, meteredBytes);
this.notificationQueue.put(new Batch(record));
return record.callback;
}
private boolean acquirePermits(int permits, Duration maxWait) throws InterruptedException {
var millisToWait = maxWait.toMillis();
do {
if (millisToWait < 0) {
// maxWait has expired, and we haven't acquired permits.
return false;
}
millisToWait -= ACQUIRE_QUANTUM_MS;
// Make sure appends are still welcome before trying to acquire permits.
if (!acceptingAppends.get()) {
throw new RuntimeException("AppendSession has been shutdown.");
}
} while (!this.inflightBytes.tryAcquire(permits, ACQUIRE_QUANTUM_MS, TimeUnit.MILLISECONDS));
// We've acquired permits, but should check once more to verify that appends
// are still welcome.
if (!acceptingAppends.get()) {
this.inflightBytes.release(permits);
throw new RuntimeException("AppendSession has been shutdown.");
}
return true;
}
/// Note that this will NOT resolve any outstanding futures issued by this session.
public ListenableFuture<Void> closeImmediately() throws InterruptedException {
this.acceptingAppends.set(false);
this.notificationQueue.put(new ClientClose(false));
return daemon;
}
private void performInflightRecovery() throws InterruptedException {
final ArrayBlockingQueue<Notification> recoveryNotificationQueue =
new ArrayBlockingQueue<>(inflightQueue.size());
final var recoveryObserver =
this.client.asyncStub.appendSession(
new StreamObserver<>() {
@Override
public void onNext(AppendSessionResponse value) {
recoveryNotificationQueue.add(new Ack(AppendOutput.fromProto(value.getOutput())));
}
@Override
public void onError(Throwable t) {
recoveryNotificationQueue.add(new Error(t));
}
@Override
public void onCompleted() {
recoveryNotificationQueue.add(new Error(new Throwable("unexpected server close")));
}
});
logger.debug("resending inflight recovery");
// Retransmit all entries in the queue.
inflightQueue.forEach(
record -> {
recoveryObserver.onNext(
AppendSessionRequest.newBuilder()
.setInput(record.input.toProto(this.client.streamName))
.build());
});
logger.debug("inflight recovery finished");
while (!inflightQueue.isEmpty()) {
final long nanosToWait =
this.nextDeadlineSystemNanos
.get()
.orElseThrow(
() ->
Status.INTERNAL
.withDescription("internal corruption; expected next deadline")
.asRuntimeException());
final Notification notification =
nanosToWait > 0
? recoveryNotificationQueue.poll(nanosToWait, TimeUnit.NANOSECONDS)
: null;
if (notification == null) {
throw Status.CANCELLED
.withDescription("hit deadline while retransmitting")
.asRuntimeException();
} else if (notification instanceof Ack) {
final Ack ack = (Ack) notification;
this.remainingAttempts.set(this.client.config.maxRetries);
var correspondingInflight = inflightQueue.poll();
if (correspondingInflight == null) {
throw Status.INTERNAL.withDescription("inflight queue is empty").asRuntimeException();
} else {
validate(correspondingInflight, ack.output);
correspondingInflight.callback.set(ack.output);
this.inflightBytes.release((int) correspondingInflight.meteredBytes);
}
} else if (notification instanceof Error) {
final Error error = (Error) notification;
throw new RuntimeException(error.throwable);
} else {
throw Status.INTERNAL
.withDescription(
String.format(
"received unexpected %s notification during inflight recovery", notification))
.asRuntimeException();
}
}
}
private synchronized Void cleanUp(Status fatal) throws InterruptedException {
this.acceptingAppends.set(false);
this.inflightBytes.drainPermits();
// Cancel all inflight entrants using the throwable.
while (!inflightQueue.isEmpty()) {
var entry = inflightQueue.poll();
entry.callback.setException(fatal.asRuntimeException());
}
// Take care of all the blocked threads awaiting permits.
Thread.sleep(ACQUIRE_QUANTUM_MS * 2);
// Now that we are satisfied no more notifications are coming, loop
// through them and cancel any pending batches via their callback.
while (!notificationQueue.isEmpty()) {
var entry = notificationQueue.poll();
if (entry instanceof Batch) {
final Batch batch = (Batch) entry;
batch.input.callback.setException(fatal.asRuntimeException());
}
}
// Rethrow.
throw fatal.asRuntimeException();
}
private void validate(InflightRecord record, AppendOutput output) {
var numRecordsForAcknowledgement = output.endSeqNum - output.startSeqNum;
if (numRecordsForAcknowledgement != record.input.records.size()) {
throw Status.INTERNAL
.withDescription(
"number of acknowledged records from S2 does not equal amount from first inflight batch")
.asRuntimeException();
}
}
private synchronized Void daemon() throws InterruptedException {
logger.debug("append session daemon started");
final var clientObserver =
this.client.asyncStub.appendSession(
new StreamObserver<>() {
@Override
public void onNext(AppendSessionResponse value) {
notificationQueue.add(new Ack(AppendOutput.fromProto(value.getOutput())));
}
@Override
public void onError(Throwable t) {
notificationQueue.add(new Error(t));
}
@Override
public void onCompleted() {
notificationQueue.add(new ServerClose());
}
});
if (!inflightQueue.isEmpty()) {
logger.debug("Performing retransmission of {} batches.", inflightQueue.size());
performInflightRecovery();
}
if (!inflightQueue.isEmpty()) {
throw new RuntimeException("Corrupted inflight queue.");
}
while (this.acceptingAppends.get()
|| (!this.notificationQueue.isEmpty() || !this.inflightQueue.isEmpty())) {
final long nanosToWait =
this.nextDeadlineSystemNanos
.get()
.map(deadline -> deadline - System.nanoTime())
.orElse(TimeUnit.NANOSECONDS.convert(Duration.ofHours(1)));
final Notification notification =
nanosToWait > 0 ? this.notificationQueue.poll(nanosToWait, TimeUnit.NANOSECONDS) : null;
if (notification == null) {
logger.debug("notification=NONE");
if (!inflightQueue.isEmpty()) {
var elapsed = Duration.ofNanos(System.nanoTime() - inflightQueue.peek().entryNanos);
logger.warn("deadline hit, waited for {} ms", elapsed.toMillis());
clientObserver.onError(Status.CANCELLED.asRuntimeException());
throw Status.CANCELLED.asRuntimeException();
}
} else if (notification instanceof Batch) {
final Batch batch = (Batch) notification;
logger.debug("notification=BATCH");
if (!inflightQueue.offer(batch.input)) {
throw Status.INTERNAL.asRuntimeException();
} else {
clientObserver.onNext(
AppendSessionRequest.newBuilder()
.setInput(batch.input.input.toProto(this.client.streamName))
.build());
// Reset the next deadline.
this.nextDeadlineSystemNanos.set(
Optional.of(
System.nanoTime()
+ TimeUnit.NANOSECONDS.convert(this.client.config.requestTimeout)));
}
} else if (notification instanceof Ack) {
final Ack ack = (Ack) notification;
logger.debug("notification=ACK");
this.remainingAttempts.set(this.client.config.maxRetries);
var correspondingInflight = inflightQueue.poll();
if (correspondingInflight == null) {
throw Status.INTERNAL.asRuntimeException();
} else {
validate(correspondingInflight, ack.output);
correspondingInflight.callback.set(ack.output);
this.inflightBytes.release((int) correspondingInflight.meteredBytes);
// Reset the next deadline.
this.nextDeadlineSystemNanos.set(
Optional.ofNullable(this.inflightQueue.peek())
.map(
entry ->
entry.entryNanos
+ TimeUnit.NANOSECONDS.convert(this.client.config.requestTimeout)));
}
} else if (notification instanceof Error) {
final Error error = (Error) notification;
logger.debug("notification=ERROR");
clientObserver.onError(Status.CANCELLED.asRuntimeException());
throw new RuntimeException(error.throwable);
} else if (notification instanceof ClientClose) {
final ClientClose close = (ClientClose) notification;
logger.debug("notification=CLIENT_CLOSE,gracefully={}", close.gracefully);
clientObserver.onCompleted();
if (!close.gracefully) {
return null;
}
} else if (notification instanceof ServerClose) {
logger.debug("notification=SERVER_CLOSE");
if (acceptingAppends.get() || !inflightQueue.isEmpty() || !notificationQueue.isEmpty()) {
throw Status.INTERNAL
.withDescription("server closed without error while work remains")
.asRuntimeException();
}
}
}
return null;
}
@Override
public void close() {
try {
this.closeGracefully().get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
public ListenableFuture<Void> closeGracefully() {
this.acceptingAppends.set(false);
this.notificationQueue.add(new ClientClose(true));
return daemon;
}
interface Notification {}
static class InflightRecord {
final AppendInput input;
final long entryNanos;
final SettableFuture<AppendOutput> callback;
final long meteredBytes;
InflightRecord(
AppendInput input,
long entryNanos,
SettableFuture<AppendOutput> callback,
long meteredBytes) {
this.input = input;
this.entryNanos = entryNanos;
this.callback = callback;
this.meteredBytes = meteredBytes;
}
static InflightRecord construct(AppendInput input, Long meteredBytes) {
return new InflightRecord(input, System.nanoTime(), SettableFuture.create(), meteredBytes);
}
}
class Ack implements Notification {
final AppendOutput output;
Ack(AppendOutput output) {
this.output = output;
}
}
class Batch implements Notification {
final InflightRecord input;
Batch(InflightRecord input) {
this.input = input;
}
}
class ClientClose implements Notification {
final boolean gracefully;
ClientClose(boolean gracefully) {
this.gracefully = gracefully;
}
}
class Error implements Notification {
final Throwable throwable;
Error(Throwable throwable) {
this.throwable = throwable;
}
}
class ServerClose implements Notification {
ServerClose() {}
}
}