-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3f7505
commit 957c7b6
Showing
6 changed files
with
158 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
sdk-core/src/main/java/dev/restate/sdk/core/AckStateMachine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH | ||
// | ||
// This file is part of the Restate Java SDK, | ||
// which is released under the MIT license. | ||
// | ||
// You can find a copy of the license in file LICENSE in the root | ||
// directory of this repository or package, or at | ||
// https://github.com/restatedev/sdk-java/blob/main/LICENSE | ||
package dev.restate.sdk.core; | ||
|
||
/** State machine tracking acks */ | ||
class AckStateMachine extends BaseSuspendableCallbackStateMachine<AckStateMachine.AckCallback> { | ||
|
||
interface AckCallback extends SuspendableCallback { | ||
void onAck(); | ||
} | ||
|
||
private int lastAcknowledgedEntry = -1; | ||
|
||
/** -1 means no side effect waiting to be acked. */ | ||
private int lastEntryToAck = -1; | ||
|
||
void waitLastAck(AckCallback callback) { | ||
if (lastEntryIsAcked()) { | ||
callback.onAck(); | ||
} else { | ||
this.setCallback(callback); | ||
} | ||
} | ||
|
||
void tryHandleAck(int entryIndex) { | ||
this.lastAcknowledgedEntry = Math.max(entryIndex, this.lastAcknowledgedEntry); | ||
if (lastEntryIsAcked()) { | ||
this.consumeCallback(AckCallback::onAck); | ||
} | ||
} | ||
|
||
void registerEntryToAck(int entryIndex) { | ||
this.lastEntryToAck = Math.max(entryIndex, this.lastEntryToAck); | ||
} | ||
|
||
private boolean lastEntryIsAcked() { | ||
return this.lastEntryToAck <= this.lastAcknowledgedEntry; | ||
} | ||
|
||
public int getLastEntryToAck() { | ||
return lastEntryToAck; | ||
} | ||
|
||
@Override | ||
void abort(Throwable cause) { | ||
super.abort(cause); | ||
// We can't do anything else if the input stream is closed, so we just fail the callback, if any | ||
this.tryFailCallback(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 0 additions & 57 deletions
57
sdk-core/src/main/java/dev/restate/sdk/core/SideEffectAckStateMachine.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.