Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 1 | // 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. |
| 84 | package event |
| 85 | |
| 86 | import ( |
| 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. |
| 92 | type 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 | |
| 108 | // Watch retrieves a Watcher that keeps track on the version of the data |
| 109 | // contained within the Value that was last seen by a consumer. Once a |
| 110 | // Watcher is retrieved, it can be used to then get the actual data stored |
| 111 | // within the Value, and to reliably retrieve updates to it without having |
| 112 | // to poll for changes. |
| 113 | Watch() Watcher |
| 114 | } |
| 115 | |
| 116 | // A Watcher keeps track of the last version of data seen by a consumer for a |
| 117 | // given Value. Each consumer should use an own Watcher instance, and it is not |
| 118 | // safe to use this type concurrently. However, it is safe to move/copy it |
| 119 | // across different goroutines, as long as no two goroutines access it |
| 120 | // simultaneously. |
| 121 | type Watcher interface { |
| 122 | // Get blocks until a Value's data is available: |
| 123 | // - On first use of a Watcher, Get will return the data contained in the |
| 124 | // value at the time of calling .Watch(), or block if no data has been |
| 125 | // .Set() on it yet. If a value has been Set() since the the initial |
| 126 | // creation of the Watch() but before Get() is called for the first |
| 127 | // time, the first Get() call will immediately return the new value. |
| 128 | // - On subsequent uses of a Watcher, Get will block until the given Value |
| 129 | // has been Set with new data. This does not necessarily mean that the |
| 130 | // new data is different - consumers should always perform their own |
| 131 | // checks on whether the update is relevant to them (ie., the data has |
| 132 | // changed in a significant way), unless specified otherwise by a Value |
| 133 | // publisher. |
| 134 | // |
| 135 | // Get() will always return the current newest data that has been Set() on |
| 136 | // the Value, and not a full log of historical events. This is geared |
| 137 | // towards event values where consumers only care about changes to data |
| 138 | // since last retrieval, not every value that has been Set along the way. |
| 139 | // Thus, consumers need not make sure that they actively .Get() on a |
| 140 | // watcher all the times. |
| 141 | // |
| 142 | // If the context is canceled before data is available to be returned, the |
| 143 | // context's error will be returned. However, the Watcher will still need to be |
| 144 | // Closed, as it is still fully functional after the context has been canceled. |
| 145 | // |
| 146 | // Concurrent requests to Get result in an error. The reasoning to return |
| 147 | // an error instead of attempting to serialize the requests is that any |
| 148 | // concurrent access from multiple goroutines would cause a desync in the |
| 149 | // next usage of the Watcher. For example: |
| 150 | // 1) w.Get() (in G0) and w.Get(G1) start. They both block waiting for an |
| 151 | // initial value. |
| 152 | // 2) v.Set(0) |
| 153 | // 3) w.Get() in G0 returns 0, |
| 154 | // 4) v.Set(1) |
| 155 | // 4) w.Get() in G1 returns 1, |
| 156 | // This would cause G0 and G1 to become desynchronized between eachother |
| 157 | // (both have different value data) and subsequent updates will also |
| 158 | // continue skipping some updates. |
| 159 | // If multiple goroutines need to access the Value, they should each use |
| 160 | // their own Watcher. |
| 161 | Get(context.Context) (interface{}, error) |
| 162 | |
| 163 | // Close must be called if the Watcher is not going to be used anymore - |
| 164 | // otherwise, a goroutine will leak. |
| 165 | Close() error |
| 166 | } |