| 1 | # Add creation and update timestamp |
| 2 | |
| 3 | ## Decision |
| 4 | |
| 5 | All objects in EDC, which can be persisted in a database, should have a `createdAt` timestamp, all objects that |
| 6 | are |
| 7 | _mutable_ should also have a `updatedAt` timestamp. In this document they will be referred to as "business |
| 8 | objects" |
| 9 | /"entities" and "mutable business objects"/"mutable entities" respectively |
| 10 | |
| 11 | ## Rationale |
| 12 | |
| 13 | It should be possible to track the creation time and last-updated time for the aforementioned (mutable) |
| 14 | entities, e.g. for auditing or for displaying purposes in a web frontend. |
| 15 | |
| 16 | The `createdAt` timestamp must be immutable. It cannot be changed after the initial object construction. The |
| 17 | `updatedAt` timestamp must be updated everytime the entity is put back into storage. This includes `save` |
| 18 | operations that do not entail an actual change to the object. |
| 19 | |
| 20 | ## Approach |
| 21 | |
| 22 | Both these timestamps are in Epoc milliseconds in Universal Coordinated Time (UTC), so obtaining the current time |
| 23 | would be done using `Clock.systemUTC().millis()`. This is in compliance with EDC usage of `java.time.Clock`. |
| 24 | |
| 25 | The following entities are considered _immutable_: |
| 26 | |
| 27 | - `Policy`/`PolicyDefinition` |
| 28 | - `ContractDefinition` |
| 29 | - `Asset` |
| 30 | |
| 31 | The following entities are considered _mutable_ |
| 32 | |
| 33 | - `ContractNegotiation` |
| 34 | - `TransferProcess` |
| 35 | |
| 36 | All entities that are effectively immutable, even though the might get re-persisted multiple times, are still |
| 37 | considered _immutable_. They typically cannot exist on their own. For instance a `DataRequest` is always tied to the |
| 38 | `TransferProcess` and cannot be changed once the transfer process has been created. By the same logic, a |
| 39 | `ContractOffer` - once received by either party - is considered immutable and any counter-offer triggers the creation of |
| 40 | a copy. |
| 41 | |
| 42 | Similarly, once a `ContractNegotiation` is in the `CONFIRMED` state, i.e. there is a `ContractAgreement` attached to it, |
| 43 | both it and the `ContractAgreement` become _immutable_. |
| 44 | |
| 45 | ### Data model |
| 46 | |
| 47 | I propose extracting the `createdTimestamp` (plus the Builder infrastructure) into a new abstract classes `Entity` |
| 48 | (and renaming it to `createdAt`) that is extended by _all_ entities, mutable and immutable. By default, the `createdAt` |
| 49 | field is initialized with the current UTC epoch in milliseconds. |
| 50 | |
| 51 | In addition, the `StatefulEntity` can be extended with a `updatedAt` field: |
| 52 | |
| 53 | ```java |
| 54 | public abstract class StatefulEntity<T extends StatefulEntity<T>> extends Entity implements TraceCarrier { |
| 55 | private long updatedAt; |
| 56 | |
| 57 | public long getUpdatedAt() { |
| 58 | return updatedAt; |
| 59 | } |
| 60 | |
| 61 | public void setUpdatedAt(long epochMillis) { |
| 62 | updatedAt = epochMillis; |
| 63 | } |
| 64 | // ... |
| 65 | } |
| 66 | ``` |
| 67 | |
| 68 | And similar to the `Entity`, the `StatefulEntity`'s Builder would also have a new method that sets the last update. By |
| 69 | default, `updatedAt` would be initialized with `createdAt`. |
| 70 | |
| 71 | _Note: the `StatefulEntity` class already has a `Clock` that can be moved up to the `Entity` and be re-used for this |
| 72 | purpose._ |
| 73 | |
| 74 | ### Mutating objects |
| 75 | |
| 76 | The `createdAt` field would always be initialized during object creation. The `updatedAt` timestamp would be |
| 77 | updated by the _manipulating class_. That means, whichever class manipulates the `StatefulEntity` is also responsible |
| 78 | for updating the `updatedAt` field. These are: |
| 79 | |
| 80 | - `TransferProcessManagerImpl`: right before `transferProcessStore.update()` is called |
| 81 | - `SingleTransferProcessCommandHandler`: could be done in the `else` path of the `handle` method |
| 82 | - `[Consumer|Provider]ContractNegotiationManagerImpl`: right before `negotiationStore.save()` is called. could be |
| 83 | extracted into the `AbstractContractNegotiationManager` |
| 84 | - `SingleContractNegotiationCommandHandler`: could be done in the `else` path of the `handle` method |
| 85 | |
| 86 | ### Persisting entities |
| 87 | |
| 88 | No action needs to be taken for the CosmosDB store as it is already document-based and persisting another field should |
| 89 | be seamless. For the Postgres implementations the `createdAt` and `updatedAt` fields should be of type `BIGINT` |
| 90 | , for example (`TransferProcess`): |
| 91 | |
| 92 | ```postgresql |
| 93 | |
| 94 | CREATE TABLE IF NOT EXISTS edc_transfer_process |
| 95 | ( |
| 96 | created_at BIGINT, -- already exists (albeit with a different name) |
| 97 | updated_at BIGINT -- this is new |
| 98 | |
| 99 | -- other columns omitted |
| 100 | ); |
| 101 | ``` |