Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +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 supervisor |
| 18 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 19 | // The service supervision library allows for writing of reliable, |
| 20 | // service-style software within a Metropolis node. It builds upon the |
| 21 | // Erlang/OTP supervision tree system, adapted to be more Go-ish. For detailed |
| 22 | // design see go/supervision. |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 23 | |
| 24 | import ( |
| 25 | "context" |
Serge Bazanski | 26d5225 | 2022-02-07 15:57:54 +0100 | [diff] [blame] | 26 | "fmt" |
Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 27 | "io" |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 28 | "sync" |
| 29 | |
Serge Bazanski | 31370b0 | 2021-01-07 16:31:14 +0100 | [diff] [blame] | 30 | "source.monogon.dev/metropolis/pkg/logtree" |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 31 | ) |
| 32 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 33 | // A Runnable is a function that will be run in a goroutine, and supervised |
| 34 | // throughout its lifetime. It can in turn start more runnables as its |
| 35 | // children, and those will form part of a supervision tree. |
| 36 | // The context passed to a runnable is very important and needs to be handled |
| 37 | // properly. It will be live (non-errored) as long as the runnable should be |
| 38 | // running, and canceled (ctx.Err() will be non-nil) when the supervisor wants |
| 39 | // it to exit. This means this context is also perfectly usable for performing |
| 40 | // any blocking operations. |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 41 | type Runnable func(ctx context.Context) error |
| 42 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 43 | // RunGroup starts a set of runnables as a group. These runnables will run |
| 44 | // together, and if any one of them quits unexpectedly, the result will be |
| 45 | // canceled and restarted. |
| 46 | // The context here must be an existing Runnable context, and the spawned |
| 47 | // runnables will run under the node that this context represents. |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 48 | func RunGroup(ctx context.Context, runnables map[string]Runnable) error { |
| 49 | node, unlock := fromContext(ctx) |
| 50 | defer unlock() |
| 51 | return node.runGroup(runnables) |
| 52 | } |
| 53 | |
| 54 | // Run starts a single runnable in its own group. |
| 55 | func Run(ctx context.Context, name string, runnable Runnable) error { |
| 56 | return RunGroup(ctx, map[string]Runnable{ |
| 57 | name: runnable, |
| 58 | }) |
| 59 | } |
| 60 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 61 | // Signal tells the supervisor that the calling runnable has reached a certain |
| 62 | // state of its lifecycle. All runnables should SignalHealthy when they are |
| 63 | // ready with set up, running other child runnables and are now 'serving'. |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 64 | func Signal(ctx context.Context, signal SignalType) { |
| 65 | node, unlock := fromContext(ctx) |
| 66 | defer unlock() |
| 67 | node.signal(signal) |
| 68 | } |
| 69 | |
| 70 | type SignalType int |
| 71 | |
| 72 | const ( |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 73 | // The runnable is healthy, done with setup, done with spawning more |
| 74 | // Runnables, and ready to serve in a loop. The runnable needs to check |
| 75 | // the parent context and ensure that if that context is done, the runnable |
| 76 | // exits. |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 77 | SignalHealthy SignalType = iota |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 78 | // The runnable is done - it does not need to run any loop. This is useful |
| 79 | // for Runnables that only set up other child runnables. This runnable will |
| 80 | // be restarted if a related failure happens somewhere in the supervision |
| 81 | // tree. |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 82 | SignalDone |
| 83 | ) |
| 84 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 85 | // supervisor represents and instance of the supervision system. It keeps track |
| 86 | // of a supervision tree and a request channel to its internal processor |
| 87 | // goroutine. |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 88 | type supervisor struct { |
| 89 | // mu guards the entire state of the supervisor. |
| 90 | mu sync.RWMutex |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 91 | // root is the root node of the supervision tree, named 'root'. It |
| 92 | // represents the Runnable started with the supervisor.New call. |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 93 | root *node |
Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 94 | // logtree is the main logtree exposed to runnables and used internally. |
| 95 | logtree *logtree.LogTree |
| 96 | // ilogger is the internal logger logging to "supervisor" in the logtree. |
| 97 | ilogger logtree.LeveledLogger |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 98 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 99 | // pReq is an interface channel to the lifecycle processor of the |
| 100 | // supervisor. |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 101 | pReq chan *processorRequest |
Serge Bazanski | 19bb412 | 2020-05-04 17:57:50 +0200 | [diff] [blame] | 102 | |
| 103 | // propagate panics, ie. don't catch them. |
| 104 | propagatePanic bool |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Serge Bazanski | 19bb412 | 2020-05-04 17:57:50 +0200 | [diff] [blame] | 107 | // SupervisorOpt are runtime configurable options for the supervisor. |
| 108 | type SupervisorOpt func(s *supervisor) |
| 109 | |
| 110 | var ( |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 111 | // WithPropagatePanic prevents the Supervisor from catching panics in |
| 112 | // runnables and treating them as failures. This is useful to enable for |
| 113 | // testing and local debugging. |
Serge Bazanski | 19bb412 | 2020-05-04 17:57:50 +0200 | [diff] [blame] | 114 | WithPropagatePanic = func(s *supervisor) { |
| 115 | s.propagatePanic = true |
| 116 | } |
| 117 | ) |
| 118 | |
Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 119 | func WithExistingLogtree(lt *logtree.LogTree) SupervisorOpt { |
| 120 | return func(s *supervisor) { |
| 121 | s.logtree = lt |
| 122 | } |
| 123 | } |
| 124 | |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 125 | // New creates a new supervisor with its root running the given root runnable. |
| 126 | // The given context can be used to cancel the entire supervision tree. |
Serge Bazanski | f8a8e65 | 2021-07-06 16:23:43 +0200 | [diff] [blame] | 127 | // |
| 128 | // For tests, we reccomend using TestHarness instead, which will also stream |
| 129 | // logs to stderr and take care of propagating root runnable errors to the test |
| 130 | // output. |
Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 131 | func New(ctx context.Context, rootRunnable Runnable, opts ...SupervisorOpt) *supervisor { |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 132 | sup := &supervisor{ |
Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 133 | logtree: logtree.New(), |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 134 | pReq: make(chan *processorRequest), |
| 135 | } |
Serge Bazanski | 19bb412 | 2020-05-04 17:57:50 +0200 | [diff] [blame] | 136 | |
| 137 | for _, o := range opts { |
| 138 | o(sup) |
| 139 | } |
| 140 | |
Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 141 | sup.ilogger = sup.logtree.MustLeveledFor("supervisor") |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 142 | sup.root = newNode("root", rootRunnable, sup, nil) |
| 143 | |
| 144 | go sup.processor(ctx) |
| 145 | |
| 146 | sup.pReq <- &processorRequest{ |
| 147 | schedule: &processorRequestSchedule{dn: "root"}, |
| 148 | } |
Serge Bazanski | ac6b644 | 2020-05-06 19:13:43 +0200 | [diff] [blame] | 149 | |
| 150 | return sup |
Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 151 | } |
Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 152 | |
| 153 | func Logger(ctx context.Context) logtree.LeveledLogger { |
| 154 | node, unlock := fromContext(ctx) |
| 155 | defer unlock() |
| 156 | return node.sup.logtree.MustLeveledFor(logtree.DN(node.dn())) |
| 157 | } |
| 158 | |
| 159 | func RawLogger(ctx context.Context) io.Writer { |
| 160 | node, unlock := fromContext(ctx) |
| 161 | defer unlock() |
| 162 | return node.sup.logtree.MustRawFor(logtree.DN(node.dn())) |
| 163 | } |
Serge Bazanski | 26d5225 | 2022-02-07 15:57:54 +0100 | [diff] [blame] | 164 | |
| 165 | // SubLogger returns a LeveledLogger for a given name. The name is used to |
| 166 | // placed that logger within the logtree hierarchy. For example, if the |
| 167 | // runnable `root.foo` requests a SubLogger for name `bar`, the returned logger |
| 168 | // will log to `root.foo.bar` in the logging tree. |
| 169 | // |
| 170 | // An error is returned if the given name is invalid or conflicts with a child |
| 171 | // runnable of the current runnable. In addition, whenever a node uses a |
| 172 | // sub-logger with a given name, that name also becomes unavailable for use as |
| 173 | // a child runnable (no runnable and sub-logger may ever log into the same |
| 174 | // logtree DN). |
| 175 | func SubLogger(ctx context.Context, name string) (logtree.LeveledLogger, error) { |
| 176 | node, unlock := fromContext(ctx) |
| 177 | defer unlock() |
| 178 | |
| 179 | if _, ok := node.children[name]; ok { |
| 180 | return nil, fmt.Errorf("name %q already in use by child runnable", name) |
| 181 | } |
| 182 | if !reNodeName.MatchString(name) { |
| 183 | return nil, fmt.Errorf("sub-logger name %q is invalid", name) |
| 184 | } |
| 185 | node.reserved[name] = true |
| 186 | |
| 187 | dn := fmt.Sprintf("%s.%s", node.dn(), name) |
| 188 | return node.sup.logtree.LeveledFor(logtree.DN(dn)) |
| 189 | } |
Serge Bazanski | 5a637b0 | 2022-02-18 12:18:04 +0100 | [diff] [blame] | 190 | |
| 191 | // MustSubLogger is a wrapper around SubLogger which panics on error. Errors |
| 192 | // should only happen due to invalid names, so as long as the given name is |
| 193 | // compile-time constant and valid, this function is safe to use. |
| 194 | func MustSubLogger(ctx context.Context, name string) logtree.LeveledLogger { |
| 195 | l, err := SubLogger(ctx, name) |
| 196 | if err != nil { |
| 197 | panic(err) |
| 198 | } |
| 199 | return l |
| 200 | } |