Elffuss BraidHub

kikocis/EDC public

main · 4.1 KiBRenderedRawPermalink
1# Add creation and update timestamp
2
3## Decision
4
5All objects in EDC, which can be persisted in a database, should have a `createdAt` timestamp, all objects that
6are
7_mutable_ should also have a `updatedAt` timestamp. In this document they will be referred to as "business
8objects"
9/"entities" and "mutable business objects"/"mutable entities" respectively
10
11## Rationale
12
13It should be possible to track the creation time and last-updated time for the aforementioned (mutable)
14entities, e.g. for auditing or for displaying purposes in a web frontend.
15
16The `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`
18operations that do not entail an actual change to the object.
19
20## Approach
21
22Both these timestamps are in Epoc milliseconds in Universal Coordinated Time (UTC), so obtaining the current time
23would be done using `Clock.systemUTC().millis()`. This is in compliance with EDC usage of `java.time.Clock`.
24
25The following entities are considered _immutable_:
26
27- `Policy`/`PolicyDefinition`
28- `ContractDefinition`
29- `Asset`
30
31The following entities are considered _mutable_
32
33- `ContractNegotiation`
34- `TransferProcess`
35
36All entities that are effectively immutable, even though the might get re-persisted multiple times, are still
37considered _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
40a copy.
41
42Similarly, once a `ContractNegotiation` is in the `CONFIRMED` state, i.e. there is a `ContractAgreement` attached to it,
43both it and the `ContractAgreement` become _immutable_.
44
45### Data model
46
47I 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`
49field is initialized with the current UTC epoch in milliseconds.
50
51In addition, the `StatefulEntity` can be extended with a `updatedAt` field:
52
53```java
54public 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
68And similar to the `Entity`, the `StatefulEntity`'s Builder would also have a new method that sets the last update. By
69default, `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
72purpose._
73
74### Mutating objects
75
76The `createdAt` field would always be initialized during object creation. The `updatedAt` timestamp would be
77updated by the _manipulating class_. That means, whichever class manipulates the `StatefulEntity` is also responsible
78for 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
88No action needs to be taken for the CosmosDB store as it is already document-based and persisting another field should
89be seamless. For the Postgres implementations the `createdAt` and `updatedAt` fields should be of type `BIGINT`
90, for example (`TransferProcess`):
91
92```postgresql
93
94CREATE 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```