Skip to content

Commit 55eaed7

Browse files
Small things
1 parent d3cbed2 commit 55eaed7

3 files changed

Lines changed: 38 additions & 25 deletions

File tree

sdk-core/src/main/java/dev/restate/sdk/core/AsyncResults.java

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ non-sealed interface AsyncResultInternal<T> extends AsyncResult<T>, UnresolvedFu
5050

5151
void tryCancel();
5252

53-
void tryComplete(NotificationReader reader);
53+
/**
54+
* Attempts to complete this result from available notifications; returns whether it is now
55+
* done.
56+
*/
57+
boolean tryComplete(NotificationReader reader);
5458

5559
CompletableFuture<T> publicFuture();
5660

@@ -121,19 +125,21 @@ public void tryCancel() {
121125
}
122126

123127
@Override
124-
public void tryComplete(NotificationReader reader) {
125-
if (this.isDone()) return;
126-
reader
127-
.take(handle)
128-
.ifPresent(
129-
value -> {
130-
try {
131-
completer.complete(value, publicFuture);
132-
} catch (Throwable e) {
133-
contextInternal.fail(e);
134-
publicFuture.completeExceptionally(AbortedExecutionException.INSTANCE);
135-
}
136-
});
128+
public boolean tryComplete(NotificationReader reader) {
129+
if (this.isDone()) {
130+
return true;
131+
}
132+
Optional<NotificationValue> value = reader.take(handle);
133+
if (value.isEmpty()) {
134+
return false;
135+
}
136+
try {
137+
completer.complete(value.get(), publicFuture);
138+
} catch (Throwable e) {
139+
contextInternal.fail(e);
140+
publicFuture.completeExceptionally(AbortedExecutionException.INSTANCE);
141+
}
142+
return true;
137143
}
138144

139145
@Override
@@ -174,9 +180,12 @@ public void tryCancel() {
174180
}
175181

176182
@Override
177-
public void tryComplete(NotificationReader reader) {
178-
if (this.isDone()) return;
183+
public boolean tryComplete(NotificationReader reader) {
184+
if (this.isDone()) return true;
179185
asyncResult.tryComplete(reader);
186+
// This is defensive, because the async result might not compelte immediately if there's a
187+
// mapper?!
188+
return this.isDone();
180189
}
181190

182191
@Override
@@ -293,15 +302,16 @@ public void tryCancel() {
293302
}
294303

295304
@Override
296-
public void tryComplete(NotificationReader reader) {
297-
if (this.isDone()) return;
305+
public boolean tryComplete(NotificationReader reader) {
306+
if (this.isDone()) return true;
298307
asyncResults.forEach(ar -> ar.tryComplete(reader));
299308
for (int i = 0; i < asyncResults.size(); i++) {
300309
if (asyncResults.get(i).isDone()) {
301310
publicFuture.complete(i);
302-
return;
311+
return true;
303312
}
304313
}
314+
return this.isDone();
305315
}
306316

307317
@Override
@@ -343,8 +353,8 @@ public void tryCancel() {
343353
}
344354

345355
@Override
346-
public void tryComplete(NotificationReader reader) {
347-
if (this.isDone()) return;
356+
public boolean tryComplete(NotificationReader reader) {
357+
if (this.isDone()) return true;
348358
asyncResults.forEach(ar -> ar.tryComplete(reader));
349359
asyncResults.stream()
350360
.filter(ar -> ar.publicFuture().isCompletedExceptionally())
@@ -357,6 +367,7 @@ public void tryComplete(NotificationReader reader) {
357367
this.publicFuture.completeExceptionally(e.getCause());
358368
}
359369
});
370+
return this.isDone();
360371
}
361372

362373
@Override

sdk-core/src/main/java/dev/restate/sdk/core/HandlerContextImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,17 +424,19 @@ private void pollAsyncResultInner(AsyncResultInternal<?> asyncResult) {
424424
return;
425425
}
426426

427-
// Let's start by trying to complete it
427+
// Try to complete it; tryComplete returns whether it is now done, fusing the completeness
428+
// check so we don't walk the async-result tree twice.
429+
boolean done;
428430
try {
429-
asyncResult.tryComplete(this::takeNotification);
431+
done = asyncResult.tryComplete(this::takeNotification);
430432
} catch (Throwable e) {
431433
// This can happen if the state machine was closed in the meantime.
432434
failWithoutContextSwitch(e);
433435
asyncResult.publicFuture().completeExceptionally(AbortedExecutionException.INSTANCE);
434436
return;
435437
}
436438
// If done, nothing more to do here.
437-
if (asyncResult.isDone()) {
439+
if (done) {
438440
return;
439441
}
440442

sdk-core/src/main/java/dev/restate/sdk/core/StateMachine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ record CancelSignalReceived() implements AwaitResult {}
8787
* AbortedExecutionException}, which should be treated as a clean abort of the user code, not a
8888
* failure to re-report.
8989
*/
90-
@Nullable AwaitResult doAwait(UnresolvedFuture future);
90+
AwaitResult doAwait(UnresolvedFuture future);
9191

9292
sealed interface NotificationValue {
9393

0 commit comments

Comments
 (0)