| 1 | # State machine |
| 2 | |
| 3 | EDC provides a lightweight framework to facilitate the development of persistent state machines. |
| 4 | |
| 5 | The framework currently manages a single execution thread. |
| 6 | |
| 7 | ## Collaborators |
| 8 | |
| 9 | - The class which defines state machine instances. The `StatefulEntity` base class can be used to derive state machine |
| 10 | entity classes. For example, a `ContractNegotiation` is a `StatefulEntity` for an EDC contract negotiation. |
| 11 | - The `StateMachineManager` which manages an execution thread that periodically gives a chance to state machines to |
| 12 | progress their state. |
| 13 | - The state-machine specific Manager which instantiates the `StateMachineManager` and defines processors for each state |
| 14 | for a given state machine class. For example, `ConsumerContractNegotiationManagerImpl` manages `ContractNegotiation`s |
| 15 | in which the connector is a consumer. |
| 16 | - The `ServiceExtension` which manages the Manager's lifecycle. |
| 17 | - The Store which manages `StatefulEntity` persistence. `InMemoryStatefulEntityStore` provides a utility class to back |
| 18 | in-memory implementations for testing. |
| 19 | |
| 20 | ## State-machine specific Manager |
| 21 | |
| 22 | The Manager manages the `StateMachineManager`'s lifecycle and defines the state machine's behavior, while the `StatefulEntity` |
| 23 | is only concerned with the state machine's data. |
| 24 | |
| 25 | Here's a prototypical Manager implementation: |
| 26 | |
| 27 | ```java |
| 28 | public class EntityManager { |
| 29 | |
| 30 | public void start() { // Called from ServiceExtension start() method |
| 31 | stateMachineManager = StateMachineManager.Builder.newInstance("state-machine-name", monitor, executorInstrumentation, waitStrategy) |
| 32 | // Processors for non-terminal states |
| 33 | .processor(processEntitiesInState(STATE1, this::processState1)) |
| 34 | .processor(processEntitiesInState(STATE2, this::processState2)) |
| 35 | .processor(processEntitiesInState(STATE3, this::processState3)) |
| 36 | .build(); |
| 37 | |
| 38 | stateMachineManager.start(); |
| 39 | } |
| 40 | |
| 41 | public void stop() { // Called from ServiceExtension shutdown() method |
| 42 | if (stateMachineManager != null) { |
| 43 | stateMachineManager.stop(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | private Processor processEntitiesInState(State state, Function<StatefulEntityImpl, Boolean> function) { |
| 48 | var filter = new Criterion[] { hasState(state.code()), isNotPending() }; |
| 49 | return ProcessorImpl.Builder.newInstance(() -> transferProcessStore.nextNotLeased(batchSize, filter)) |
| 50 | .process(telemetry.contextPropagationMiddleware(function)) |
| 51 | .guard(pendingGuard, this::setPending) // a guard can be added to, e.g. put in pending certain entities based on the `pendingGuard` predicate |
| 52 | .build(); |
| 53 | } |
| 54 | |
| 55 | // Processor functions should return true only if the state machine has been updated |
| 56 | private boolean processState1(StatefulEntityImpl sm) { |
| 57 | if (conditionsForTransitionFromState1ToState2Apply(sm)) { |
| 58 | sm.transitionState2(); |
| 59 | store.save(sm); |
| 60 | return true; |
| 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | } |
| 66 | ``` |
| 67 | |
| 68 | ### Guards |
| 69 | On a state machine `Processor` a `Guard` can be specified. that's a way to have a custom flow based on a predicate that can |
| 70 | be extended, for example, to enable "external interactions" in the state machine, as user interactions. A `Guard` predicate |
| 71 | can be set on the processor with a specific process to be executed. This way when the predicate matches the entity can be |
| 72 | set to pending, making it "invisible" for the state machine, but still accessible and modifiable by users or external systems. |
| 73 | |
| 74 | `Guard` example: |
| 75 | ```java |
| 76 | class EntityPendingGuard implements PendingGuard<Entity> { |
| 77 | |
| 78 | // custom collaborators as other services |
| 79 | |
| 80 | boolean test(Entity entity) { |
| 81 | // additional logic |
| 82 | return entity.getState() = SPECIFIC_STATE.code() && otherCondition; // if true, the entity will be set as pending |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | ``` |
| 87 | |
| 88 | ## State-machine store |
| 89 | |
| 90 | The Store which manages `StatefulEntity` persistence must persist entities in a storage system. In-memory implementations |
| 91 | are provided for testing. |
| 92 | |
| 93 | Stores using persistent implementations must manage leases to support EDC clustered deployment. This ensures an entity is |
| 94 | processed by only one EDC instance at a time (assuming processing is quicker than lease expiration). |
| 95 | |
| 96 | ```java |
| 97 | class EntityStore { |
| 98 | public void save(StatefulEntityImpl instance) { |
| 99 | // persist instance |
| 100 | // release lease |
| 101 | } |
| 102 | |
| 103 | List<T> nextNotLeased(int max, Criterion... criteria); { |
| 104 | // retrieve and lease at most limit instances that satisfy criteria |
| 105 | } |
| 106 | } |
| 107 | ``` |