blob: bc4652599a7d3c3a2aee8daf5000171f5be9310a [file] [log] [blame]
Serge Bazanskic00318e2021-03-03 12:39:24 +01001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17// Package event defines and implements Event Values, a mechanism in which
18// multiple consumers can watch a value for updates in a reliable way.
19//
20// Values currently are kept in memory (see: MemoryValue), but a future
21// implementation might exist for other storage backends, eg. etcd.
22//
23// Background and intended use
24//
25// The Event Value library is intended to be used within Metropolis'
26// supervisor-based runnables to communicate state changes to other runnables,
27// while permitting both sides to restart if needed. It grew out of multiple
28// codebases reimplementing an ad-hoc observer pattern, and from the
29// realization that implementing all possible edge cases of such patterns is
30// non-trivial and subject to programming errors. As such, it was turned into a
31// self-standing library.
32//
33// Why not just channels?
34//
35// Plain channels have multiple deficiencies for this usecase:
36// - Strict FIFO behaviour: all values sent to a channel must be received, and
37// historic and newest data must be treated in the same way. This means that
38// a consumer of state changes must process all updates to the value as if
39// they are the newest, and unable to skip rapid updates when a system is
40// slowly settling due to a cascading state change.
41// - Implementation overhead: implementing an observer
42// registration/unregistration pattern is prone to programming bugs,
43// especially for features like always first sending the current state to a
44// new observer.
45// - Strict buffer size: due to their FIFO nature and the possibility of
46// consumers not receiving actively, channels would have to buffer all
47// existing updates, requiring some arbitrary best-guess channel buffer
48// sizing that would still not prevent blocking writes or data loss in a
49// worst case scenario.
50//
51// Or, in other words: Go channels are a synchronization primitive, not a
52// ready-made solution to this problem. The Event Value implementation in fact
53// extensively uses Go channels within its implementation as a building block.
54//
55// Why not just condition variables (sync.Cond)?
56//
57// Go's condition variable implementation doesn't fully address our needs
58// either:
59// - No context/canceling support: once a condition is being Wait()ed on,
60// this cannot be interrupted. This is especially painful and unwieldy when
61// dealing with context-heavy code, such as Metropolis.
62// - Spartan API: expecting users to plainly use sync.Cond is risky, as the API
63// is fairly low-level.
64// - No solution for late consumers: late consumers (ones that missed the value
65// being set by a producer) would still have to implement logic in order to
66// find out such a value, as sync.Cond only supports what amounts to
67// edge-level triggers as part of its Broadcast/Signal system.
68//
69// It would be possible to implement MemoryValue using a sync.Cond internally,
70// but such an implementation would likely be more complex than the current
71// implementation based on channels and mutexes, as it would have to work
72// around issues like lack of canceling, etc.
73//
74// Type safety
75//
76// The Value/Watcher interfaces are, unfortunately, implemented using
77// interface{}. There was an attempt to use Go's existing generic types facility
78// (interfaces) to solve this problem. However, with Type Parameters likely soon
79// appearing in mainline Go, this was not a priority, as that will fully solve
80// this problem without requiring mental gymnastics. For now, users of this
81// library will have to write some boilerplate code to allow consumers/watchers
82// to access the data in a a typesafe manner without assertions. See
83// ExampleValue_full for one possible approach to this.
84package event
85
86import (
87 "context"
88)
89
90// A Value is an 'Event Value', some piece of data that can be updated ('Set')
91// by Producers and retrieved by Consumers.
92type Value interface {
93 // Set updates the Value to the given data. It is safe to call this from
94 // multiple goroutines, including concurrently.
95 //
96 // Any time Set is called, any consumers performing a Watch on this Value
97 // will be notified with the new data - even if the Set data is the same as
98 // the one that was already stored.
99 //
100 // A Value will initially have no data set. This 'no data' state is seen by
101 // consumers by the first .Get() call on the Watcher blocking until data is Set.
102 //
103 // All updates will be serialized in an arbitrary order - if multiple
104 // producers wish to perform concurrent actions to update the Value partially,
105 // this should be negotiated and serialized externally by the producers.
106 Set(val interface{})
107
Serge Bazanskifac8b2e2021-05-04 12:23:26 +0200108 // ValueWatch implements the Watch method. It is split out into another
109 // interface to allow some 'Event Values' to implement only the watch/read
110 // part, with the write side being implicit or defined by a more complex
111 // interface then a simple Set().
112 ValueWatch
113}
114
115// ValueWatch is the read side of an 'Event Value', witch can by retrieved by
116// Consumers by performing a Watch operation on it.
117type ValueWatch interface {
Serge Bazanskic00318e2021-03-03 12:39:24 +0100118 // Watch retrieves a Watcher that keeps track on the version of the data
119 // contained within the Value that was last seen by a consumer. Once a
120 // Watcher is retrieved, it can be used to then get the actual data stored
121 // within the Value, and to reliably retrieve updates to it without having
122 // to poll for changes.
123 Watch() Watcher
124}
125
126// A Watcher keeps track of the last version of data seen by a consumer for a
127// given Value. Each consumer should use an own Watcher instance, and it is not
128// safe to use this type concurrently. However, it is safe to move/copy it
129// across different goroutines, as long as no two goroutines access it
130// simultaneously.
131type Watcher interface {
132 // Get blocks until a Value's data is available:
133 // - On first use of a Watcher, Get will return the data contained in the
134 // value at the time of calling .Watch(), or block if no data has been
135 // .Set() on it yet. If a value has been Set() since the the initial
136 // creation of the Watch() but before Get() is called for the first
137 // time, the first Get() call will immediately return the new value.
138 // - On subsequent uses of a Watcher, Get will block until the given Value
139 // has been Set with new data. This does not necessarily mean that the
140 // new data is different - consumers should always perform their own
141 // checks on whether the update is relevant to them (ie., the data has
142 // changed in a significant way), unless specified otherwise by a Value
143 // publisher.
144 //
145 // Get() will always return the current newest data that has been Set() on
146 // the Value, and not a full log of historical events. This is geared
147 // towards event values where consumers only care about changes to data
148 // since last retrieval, not every value that has been Set along the way.
149 // Thus, consumers need not make sure that they actively .Get() on a
150 // watcher all the times.
151 //
152 // If the context is canceled before data is available to be returned, the
153 // context's error will be returned. However, the Watcher will still need to be
154 // Closed, as it is still fully functional after the context has been canceled.
155 //
156 // Concurrent requests to Get result in an error. The reasoning to return
157 // an error instead of attempting to serialize the requests is that any
158 // concurrent access from multiple goroutines would cause a desync in the
159 // next usage of the Watcher. For example:
160 // 1) w.Get() (in G0) and w.Get(G1) start. They both block waiting for an
161 // initial value.
162 // 2) v.Set(0)
163 // 3) w.Get() in G0 returns 0,
164 // 4) v.Set(1)
165 // 4) w.Get() in G1 returns 1,
166 // This would cause G0 and G1 to become desynchronized between eachother
167 // (both have different value data) and subsequent updates will also
168 // continue skipping some updates.
169 // If multiple goroutines need to access the Value, they should each use
170 // their own Watcher.
Serge Bazanski8d45a052021-10-18 17:24:24 +0200171 Get(context.Context, ...GetOption) (interface{}, error)
Serge Bazanskic00318e2021-03-03 12:39:24 +0100172
173 // Close must be called if the Watcher is not going to be used anymore -
174 // otherwise, a goroutine will leak.
175 Close() error
176}
Serge Bazanski8d45a052021-10-18 17:24:24 +0200177
178type GetOption interface{}