Project

General

Profile

Websocket server » History » Version 6

Peter Amstutz, 08/19/2016 02:23 AM

1 1 Tom Clegg
h1. Websocket server
2
3
(early draft)
4
5
{{toc}}
6
7
h2. Background
8
9
The Rails API server can function as a websocket server. Clients (notably Workbench, arv-mount, arv-ws) use it to listen for events without polling.
10
11
Problems with current implementation:
12 3 Tom Clegg
* Unreliable. See #9427, #8277
13 1 Tom Clegg
* Resource-heavy (one postgres connection per connected client, uses lots of memory)
14
* Logging is not very good
15
* Updates look like database records instead of API responses (e.g., computed fields are missing, collection manifest_text has no signatures)
16 3 Tom Clegg
* Offers an API for catching up on missed events after disconnecting/reconnecting, but this API (let alone the code) isn't enough to offer a "don't miss any events, don't send any events twice" guarantee. See #9388
17 1 Tom Clegg
18 3 Tom Clegg
#8460
19
20
h2. Desired features
21
22
Monotonically increasing event IDs, so clients can (meaningfully) request "all matching events since X"
23
24 6 Peter Amstutz
h2. Design sketch (TC)
25 1 Tom Clegg
26
New server, written in Go.
27
28
One goroutine per connected client.
29
30
One database connection receiving notifications about new logs. (Possibly still N database connections serving "catch-up" messages to N clients.)
31 6 Peter Amstutz
32
h2. Design sketch (PA)
33
34
When client connects, it can request a new session (with event filter), or asks to resume an existing session from a given event id.
35
36
Each session has a session id and is associated with a user, an event channel, an event queue, event filters, and exactly one websocket connection.
37
38
Clients can create multiple sessions on the same websocket.  Resuming a session associates the session to a new websocket connection (must be same user).  Resuming a session replays any queued events occurring after the given event id.
39
40
Orderly websocket disconnects tear down the session.  Abrupt disconnects maintain the session for N minutes.
41
42
NOTIFY sends full json-encoded record.
43
44
Websocket server database LISTEN receives NOTIFY, deserializes record, gets @object_uuid@ and @object_owner_uuid@ and determines the set of users who have permission to view the log record using in-memory cache of permissions graph.
45
46
Intersect the set of users who can view the record with the users associated with active sessions and adds the record to the queue for each session with the associated user.
47
48
The goroutine for session gets a new event on the event channel.  It first applies the session filters to determine if the event should be propagated.  (This should be a Go-based implementation of Arvados query filters and not touch the database.)  If it passes filters, the event is assigned a session-specific sequence number, added to a queue, and can be sent to the websocket client.
49
50
The websocket client receives the event and sends an acknowledgement with the sequence number.  The server receives the acknowledgement and removes the message from the queue.
51 2 Tom Clegg
52
h2. Libraries
53
54
Websocket:
55
* https://godoc.org/golang.org/x/net/websocket
56
57
PostgreSQL:
58 1 Tom Clegg
* https://godoc.org/github.com/lib/pq via https://godoc.org/database/sql
59
* https://godoc.org/github.com/lib/pq#hdr-Notifications and https://godoc.org/github.com/lib/pq/listen_example
60 3 Tom Clegg
61
h2. Obstacles
62
63
#8565, #8566
64 4 Tom Clegg
65
h2. Related
66
67
It might be expedient to offload synchronization to some existing software that does this well.
68
* "Apache Zookeeper":https://zookeeper.apache.org/doc/trunk/zookeeperOver.html -- "Coordination services are notoriously hard to get right. They are especially prone to errors such as race conditions and deadlock. The motivation behind ZooKeeper is to relieve distributed applications the responsibility of implementing coordination services from scratch."
69
* Google "Chubby":http://static.googleusercontent.com/media/research.google.com/en//archive/chubby-osdi06.pdf paper
70 5 Nico César
* NSQ http://nsq.io/