Project

General

Profile

Events API » History » Version 5

Tom Clegg, 11/04/2016 03:42 PM

1 1 Tom Clegg
h1. Events API
2
3
(draft)
4
5
{{toc}}
6
7
See also: [[Websocket server]]
8
9
h1. Purpose
10
11
The Events API serves to notify processes about events that interest them _as soon as possible after those events happen_.
12
13
(The history of events that have happened in the past is also interesting, but that's addressed by the Logs API, not the Events API.)
14
15
h1. Conceptual model
16
17
An event reports a change to the state of an object.
18
19
The fact that an object's state has changed is meaningful only when its previous state is known. For example, if a client asks "tell me the next time object X changes" at nearly the same time X changes, the response depends on whether the request arrives before or after the change occurs.
20
21
Therefore, the Events API should support operations like:
22
* "tell me the current state of X, and then notify me next time it changes"
23
* "tell me as soon as X differs from my cached copy that has Etag E"
24
25
An "event stream" is a sequence of events about an object, starting from an implicit or explicit known state.
26
27
h1. Essential features
28
29
h2. Multiple streams
30
31
The Events API supports multiplexing event streams on a single connection. The cost of setting up and maintaining an event channel can be non-trivial, and the sequence of events concerning multiple related objects may be significant.
32
33
It is possible to add and remove event streams on an existing connection, without interrupting other streams.
34
35
It is permitted to hold a connection open with no event streams, but the server may close such connections after some time threshold.
36
37
h2. Delivery guarantees
38
39
In general, the Events API cannot guarantee that every event will be delivered.
40
41 3 Tom Clegg
However, there are specific cases where it is beneficial (and practical) to detect missed events and notify the client.
42 1 Tom Clegg
43 3 Tom Clegg
If some events are dropped but the event stream is still open (for example, a server-side buffer overflows because a client is receiving data too slowly) the server must indicate this to the client no later than the next event delivery.
44
* The "missed events" signal may specify a single event stream (UUID); if not, the client must interpret this as "events may have been missed on all active streams".
45 1 Tom Clegg
* The "missed events" signal does not necessarily specify the number of missed events.
46
* The server is permitted to send a "missed events" signal even if no events were missed.
47
48
Depending on the application, a client might respond to a "missed events" signal by
49
* restarting the affected streams immediately
50
* restarting the affected streams only if they stay silent for some timeout period
51
* doing nothing
52
* hanging up
53
54
h2. Event message content
55
56 3 Tom Clegg
Each event includes, at minimum, the UUID and Etag of the changed object.
57 1 Tom Clegg
58 3 Tom Clegg
h2. Non-state-changing events (logs)
59 1 Tom Clegg
60 3 Tom Clegg
Container/job log messages (e.g., stderr) should be available through the Events API, even though they don't correspond to an etag change in any object.
61
62
Given that the etag does not change, the client is obviously interested in other attributes of the event itself (e.g., stderr text), so those attributes must be
63
* included with the event payload, or
64
* stored in a Log object whose UUID is included in the event payload, or
65
* both of the above.
66
67
(to be discussed)
68
69
Each non-state-changing event should include the UUID of the relevant Log object.
70
71
Each non-state-changing event should include the attributes of the relevant Log object itself.
72
73 1 Tom Clegg
h1. Additional features
74
75
h2. Event sequence
76
77
With the current API server, it may be possible to update an object twice in quick succession such that the modification timestamps are out of order: i.e., the current state of object X has modification time T1, even though the same object previously had modification time T2>T1. If this occurs, the Events API must return the T2 update before the T1 update (or not return the T2 update at all).
78
79
In order to support delivery mechanisms where messages are re-ordered in transit, the Events API should assign a strictly increasing integer ID to each event sent over a given connection. Client pseudocode:
80
81
<pre>
82
receiveEvent(id, uuid, newEtag):
83
  if lastID[uuid] > id:
84
    # already received a newer update for this object
85
    return
86
  currentEtag[uuid] = newEtag
87
  lastID[uuid] = id
88
</pre>
89
90
Note these IDs are connection-specific: they cannot be used to reconnect and resume an event stream.
91
92
h2. Server-side event filters
93
94
Some clients will only be interested in a subset of possible changes. For example, a pipeline runner wants to know as soon as a container's "state" attribute changes, but might not care about other changes like "priority" or "progress".
95
96
Possible API features for reducing unnecessary work and network traffic:
97
# Allow clients to describe which attributes are interesting, e.g., @"select":["state"]@
98
# With each event, provide the list of changed attributes, e.g., @"changed":["state","output","log"]@, but not the attribute values themselves
99
100
These features might be tricky to implement efficiently for attributes that are computed on the fly. 
101
102 3 Tom Clegg
h2. Including object attributes with events
103
104
Some clients perform a GET request in response to every event reveived. For the sake of efficiency and convenience, if desired by the client, the Events API should perform that request internally, and supply the response along with the event.
105
106
Clients should be able to control (separately for each stream) the list of object attributes to include with each event. This list corresponds to the "select" parameter for the "get object" REST API.
107
108
By default, only the "uuid" and "etag" attributes are included. It is not possible to un-select those attributes.
109
110
The values for any returned attributes must be identical to the values that would be returned in a GET response.
111
112 1 Tom Clegg
h2. Null stream
113
114
To simplify implementation of clients that subscribe to event streams but also retrieve some objects without listening for events, a client should be able to use the Events API to retrieve the current state of an object without subscribing to the object's event stream.
115 2 Tom Clegg
116
h2. Ownership-change events
117
118
Some clients need to know when an object is added or removed from a project.
119
120
When an object's owner_uuid changes, this event should be sent to:
121
# all clients subscribed to the object itself
122 1 Tom Clegg
# all clients subscribed to the old owner_uuid
123
# all clients subscribed to the new owner_uuid
124
125
Likewise, subscribing to stream X should cause clients to receive messages when a new object is created with owner_uuid=X, and when an object with owner_uuid=X is deleted.
126 3 Tom Clegg
127 5 Tom Clegg
h2. "Owner and children" subscription
128
129
Some clients need to know when any object in a project (or other type of group) changes.
130
131
When subscribing to a group or user X, clients should have the option to receive events about objects whose owner_uuid is X, even if the event does not change the owner_uuid.
132
133
In order to avoid races using etags, the client or server would have to send the initial/cached etag for a (sometimes large) number of child objects.
134
135
Alternatively, the server can send a message acknowledging the subscription, and guarantee that no events will be silently missed after the acknowledgement is sent. If a client needs to avoid races, it must invalidate its cache of child objects upon receiving the acknowledgement message.
136
137 3 Tom Clegg
h2. Event batches
138
139
When sending a sequence of events that differ only in etag (i.e., they refer to the same object UUID and the payload consists of just the new etag) the server is permitted to send just the last event, and silently skip the rest.
140 4 Tom Clegg
141
h1. Client and use case examples
142
143
Workbench
144
* "Object updated" events can trigger a page/section refresh.
145
* "Job/container stderr" events add text content to the page.
146
* Current implementation listens for all events, and filters them by UUID on the client side (inefficient!).
147
148
Future Workbench (single-page app)
149
* "Get object now, and again whenever updated; re-render whenever response arrives" is a likely pattern.
150
* During page transitions, connection will stay open but subscriptions will change (adjacent pages will often have overlapping subscriptions).
151
* Will still want to display stderr messages as they arrive, when a container/job log is on the screen.
152
153
arv-mount
154
* Current implementation listens for all events, and filters them by UUID on the client side (even when falling back to polling).
155
156
arv-ws (generic command line tool)
157
* Current default mode listens for all events.
158
* Offers "listen to events for given UUIDs" mode (compatible).
159
* Offers "listen to events with arbitrary filters" mode (incompatible).
160
161
arvados-cwl-runner
162
* Current implementation (2016-10-25) emulates a subscription by polling current state for a centrally tracked set of UUIDS, converting the responses to look like "update with new attributes" log entries, and passing them to an event handler.
163
164
h1. Comparison with initial websocket API
165
166
The approach described here differs from the initial puma-based websocket service:
167
* It is no longer possible to listen for events without filtering by UUID.
168
* By default, events are compact (previously, "update" events included contents of all old and new database columns).
169
* Races are addressed using etags. The server is not expected to replay an arbitrary set of past events in sequence.