| 1 | # Using the Command Queue |
| 2 | |
| 3 | In many situations, especially when there are is parallel processing in multiple threads involved, we may run into situations that could potentially cause |
| 4 | inconsistencies. For the purposes of this document we'll use the `TransferProcessManager` ("TPM") as example, although the concept applies to other components |
| 5 | as well. |
| 6 | |
| 7 | ## Problem statement |
| 8 | |
| 9 | The `TransferProcessManager` automatically updates `TransferProcess` ("TP") objects according to its state machine and depending on external influence, e.g. |
| 10 | provisioning. This is done on a separate thread. However, there are situations, where those TransferProcesses need to be modified from a different thread, for |
| 11 | example a watchdog, that retires timed-out or stale TPs. Simply fetching the TP from the store, updating it and storing it again may cause race conditions, and |
| 12 | it may cause the TP to be in an unexpected state. |
| 13 | |
| 14 | Thus, the `TransferProcessStore` should never be accessed directly, instead every action that modifies a TP, that happens _outside_ of the TPM should be |
| 15 | enqueued in the `CommandQueue`. That queue is drained at a specific time in the state machine, thus all actions are executed in a serial manner, which is |
| 16 | necessary to again avoid race conditions. |
| 17 | |
| 18 | ## When to use it |
| 19 | |
| 20 | Whenever access a shared resource like a TP is required, the respective piece of code be wrapped in a `CommandHandler` and enqueued in the `CommandQueue`. The |
| 21 | CommandQueue will then execute the code safely and without side effects. |
| 22 | |
| 23 | ## How to use it |
| 24 | |
| 25 | First it's important to note that `Commands` are mere POJOs, their contract is that they are fully serializable, so they cannot have references to any |
| 26 | non-serializable objects. |
| 27 | |
| 28 | Commands typically contain the data, that the `CommandHandler` needs to execute the code, for example the ID of a transfer process. `CommandHandler`s must be |
| 29 | registered at runtime start, which is typically done in a `ServiceExtension`: |
| 30 | |
| 31 | ```java |
| 32 | public class YourExtension { |
| 33 | @Inject |
| 34 | private CommandHandlerRegistry registry; |
| 35 | |
| 36 | @Override |
| 37 | public void initialize() { |
| 38 | var commandHandler = new HelloCommandHandler(); //handles HelloCommands commands |
| 39 | registry.register(commandHandler); |
| 40 | } |
| 41 | } |
| 42 | ``` |
| 43 | |
| 44 | Whenever a `HelloCommand` command needs to be issued in client code, a reference to the `CommandQueue` is required. For safety reasons the `CommandQueue` |
| 45 | is not exposed directly, but only accessible through e.g. a `TransferProcessManager`: |
| 46 | |
| 47 | ```java |
| 48 | public class ClientClass { |
| 49 | |
| 50 | private TransferProcessManager tpm; |
| 51 | |
| 52 | public void sayHello() { |
| 53 | var command = new HelloCommand(); |
| 54 | tpm.enqueueCommand(command); |
| 55 | } |
| 56 | } |
| 57 | ``` |
| 58 | |
| 59 | After that, the command will get executed eventually, but there is no guarantee with regard to latency, timeouts, ordering or resilience. The only certainty is |
| 60 | that the command will be executed once. If it fails, it gets re-enqueued, or discarded, if its retry cycles are exhausted. |
| 61 | |
| 62 | Note that there is no direct back channel to the issuer of the command. While it would have been easy to add a `CompletableFuture` as return type, it would |
| 63 | violate the serializability principle. |
| 64 | |
| 65 | ## Best practices |
| 66 | |
| 67 | - do not maintain any references to non-serializable objects in the `Command` object |
| 68 | - Commands should not have behaviour, only state |
| 69 | - Command handlers should not have state, only behaviour |
| 70 | - As command handlers must be stateless, they must determine their state, for example by executing a database `read()` directly in its `handle()` method. |
| 71 | - Command handlers must be defensive, for example they must check whether a transfer process can be modified or not. |
| 72 | - Command handler should not implement long-running operations, as they may hold up calling components such as the TPM. |
| 73 | - if command handlers throw an exception, the command may get retried, so proper cleanup must be done. |
| 74 | - wrap all `enqueue` operations in a `try-catch` block, because enqueuing may fail with an `IllegalStateException`, e.g. when the queue is full (bounded queue) |
| 75 | - do not expose the `CommandQueue` directly - clients should only be able to enqueue items. |
| 76 | |
| 77 | ## Further reading |
| 78 | |
| 79 | A complete sample with additional explanation can be found in the [samples](https://github.com/eclipse-edc/Samples/blob/main/transfer/transfer-03-modify-transferprocess/README.md). |