-
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.
Some refactor of internal state machines (#112)
- Loading branch information
1 parent
8b99612
commit b608f9e
Showing
15 changed files
with
356 additions
and
313 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...ore-impl/src/main/java/dev/restate/sdk/core/impl/BaseSuspendableCallbackStateMachine.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,54 @@ | ||
package dev.restate.sdk.core.impl; | ||
|
||
import java.util.function.Consumer; | ||
|
||
// Implements the base logic for state machines containing suspensable callbacks. | ||
abstract class BaseSuspendableCallbackStateMachine<CB extends SuspendableCallback> { | ||
|
||
private final CallbackHandle<CB> callbackHandle; | ||
private final InputPublisherState inputPublisherState; | ||
|
||
BaseSuspendableCallbackStateMachine() { | ||
this.callbackHandle = new CallbackHandle<>(); | ||
this.inputPublisherState = new InputPublisherState(); | ||
} | ||
|
||
void abort(Throwable cause) { | ||
this.inputPublisherState.notifyClosed(cause); | ||
} | ||
|
||
public void tryFailCallback() { | ||
callbackHandle.consume( | ||
cb -> { | ||
if (inputPublisherState.isSuspended()) { | ||
cb.onSuspend(); | ||
} else if (inputPublisherState.isClosed()) { | ||
cb.onError(inputPublisherState.getCloseCause()); | ||
} | ||
}); | ||
} | ||
|
||
public void consumeCallback(Consumer<CB> consumer) { | ||
this.callbackHandle.consume(consumer); | ||
} | ||
|
||
public void consumeCallbackOrElse(Consumer<CB> consumer, Runnable elseRunnable) { | ||
this.callbackHandle.consumeOrElse(consumer, elseRunnable); | ||
} | ||
|
||
public void assertCallbackNotSet(String reason) { | ||
if (!this.callbackHandle.isEmpty()) { | ||
throw new IllegalStateException(reason); | ||
} | ||
} | ||
|
||
void setCallback(CB callback) { | ||
if (inputPublisherState.isSuspended()) { | ||
callback.onSuspend(); | ||
} else if (inputPublisherState.isClosed()) { | ||
callback.onError(inputPublisherState.getCloseCause()); | ||
} else { | ||
callbackHandle.set(callback); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
sdk-core-impl/src/main/java/dev/restate/sdk/core/impl/CallbackHandle.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,38 @@ | ||
package dev.restate.sdk.core.impl; | ||
|
||
import java.util.function.Consumer; | ||
import javax.annotation.Nullable; | ||
|
||
/** Handle for callbacks. */ | ||
final class CallbackHandle<T> { | ||
|
||
private @Nullable T cb = null; | ||
|
||
public void set(T t) { | ||
this.cb = t; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return this.cb == null; | ||
} | ||
|
||
public void consume(Consumer<T> consumer) { | ||
if (this.cb != null) { | ||
consumer.accept(pop()); | ||
} | ||
} | ||
|
||
public void consumeOrElse(Consumer<T> consumer, Runnable elseRunnable) { | ||
if (this.cb != null) { | ||
consumer.accept(pop()); | ||
} else { | ||
elseRunnable.run(); | ||
} | ||
} | ||
|
||
private @Nullable T pop() { | ||
T temp = this.cb; | ||
this.cb = null; | ||
return temp; | ||
} | ||
} |
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
66 changes: 0 additions & 66 deletions
66
sdk-core-impl/src/main/java/dev/restate/sdk/core/impl/EntriesQueue.java
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
sdk-core-impl/src/main/java/dev/restate/sdk/core/impl/IncomingEntriesStateMachine.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,46 @@ | ||
package dev.restate.sdk.core.impl; | ||
|
||
import com.google.protobuf.MessageLite; | ||
import java.util.ArrayDeque; | ||
import java.util.Queue; | ||
|
||
class IncomingEntriesStateMachine | ||
extends BaseSuspendableCallbackStateMachine<IncomingEntriesStateMachine.OnEntryCallback> { | ||
|
||
interface OnEntryCallback extends SuspendableCallback { | ||
void onEntry(MessageLite msg); | ||
} | ||
|
||
private final Queue<MessageLite> unprocessedMessages; | ||
|
||
IncomingEntriesStateMachine() { | ||
this.unprocessedMessages = new ArrayDeque<>(); | ||
} | ||
|
||
void offer(MessageLite msg) { | ||
Util.assertIsEntry(msg); | ||
this.consumeCallbackOrElse(cb -> cb.onEntry(msg), () -> this.unprocessedMessages.offer(msg)); | ||
} | ||
|
||
void read(OnEntryCallback msgCallback) { | ||
this.assertCallbackNotSet("Two concurrent reads were requested."); | ||
|
||
MessageLite popped = this.unprocessedMessages.poll(); | ||
if (popped != null) { | ||
msgCallback.onEntry(popped); | ||
} else { | ||
this.setCallback(msgCallback); | ||
} | ||
} | ||
|
||
boolean isEmpty() { | ||
return this.unprocessedMessages.isEmpty(); | ||
} | ||
|
||
@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(); | ||
} | ||
} |
48 changes: 0 additions & 48 deletions
48
sdk-core-impl/src/main/java/dev/restate/sdk/core/impl/InputChannelState.java
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
sdk-core-impl/src/main/java/dev/restate/sdk/core/impl/InputPublisherState.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,25 @@ | ||
package dev.restate.sdk.core.impl; | ||
|
||
import dev.restate.sdk.core.SuspendedException; | ||
import javax.annotation.Nullable; | ||
|
||
class InputPublisherState { | ||
|
||
private @Nullable Throwable closeCause = null; | ||
|
||
void notifyClosed(Throwable cause) { | ||
closeCause = cause; | ||
} | ||
|
||
boolean isSuspended() { | ||
return this.closeCause == SuspendedException.INSTANCE; | ||
} | ||
|
||
boolean isClosed() { | ||
return this.closeCause != null; | ||
} | ||
|
||
public Throwable getCloseCause() { | ||
return closeCause; | ||
} | ||
} |
Oops, something went wrong.