| 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 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "errors" |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 22 | "fmt" |
| 23 | "runtime/debug" |
| Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 24 | "sort" |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 25 | "time" |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 26 | ) |
| 27 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 28 | // The processor maintains runnable goroutines - ie., when requested will start |
| Jan Schär | aa6b42a | 2024-12-18 18:03:26 +0100 | [diff] [blame] | 29 | // one, and then once it exits, it will record the result and act accordingly. |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 30 | // It is also responsible for detecting and acting upon supervision subtrees |
| 31 | // that need to be restarted after death (via a 'GC' process) |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 32 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 33 | // processorRequest is a request for the processor. Only one of the fields can |
| 34 | // be set. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 35 | type processorRequest struct { |
| Serge Bazanski | ac6b644 | 2020-05-06 19:13:43 +0200 | [diff] [blame] | 36 | schedule *processorRequestSchedule |
| 37 | died *processorRequestDied |
| 38 | waitSettled *processorRequestWaitSettled |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // processorRequestSchedule requests that a given node's runnable be started. |
| 42 | type processorRequestSchedule struct { |
| 43 | dn string |
| 44 | } |
| 45 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 46 | // processorRequestDied is a signal from a runnable goroutine that the runnable |
| 47 | // has died. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 48 | type processorRequestDied struct { |
| 49 | dn string |
| 50 | err error |
| 51 | } |
| 52 | |
| Serge Bazanski | ac6b644 | 2020-05-06 19:13:43 +0200 | [diff] [blame] | 53 | type processorRequestWaitSettled struct { |
| 54 | waiter chan struct{} |
| 55 | } |
| 56 | |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 57 | // processor is the main processing loop. |
| 58 | func (s *supervisor) processor(ctx context.Context) { |
| 59 | s.ilogger.Info("supervisor processor started") |
| 60 | |
| Serge Bazanski | ac6b644 | 2020-05-06 19:13:43 +0200 | [diff] [blame] | 61 | // Waiters waiting for the GC to be settled. |
| 62 | var waiters []chan struct{} |
| 63 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 64 | // The GC will run every millisecond if needed. Any time the processor |
| 65 | // requests a change in the supervision tree (ie a death or a new runnable) |
| 66 | // it will mark the state as dirty and run the GC on the next millisecond |
| 67 | // cycle. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 68 | gc := time.NewTicker(1 * time.Millisecond) |
| 69 | defer gc.Stop() |
| 70 | clean := true |
| 71 | |
| Serge Bazanski | ac6b644 | 2020-05-06 19:13:43 +0200 | [diff] [blame] | 72 | // How long has the GC been clean. This is used to notify 'settled' waiters. |
| 73 | cleanCycles := 0 |
| 74 | |
| 75 | markDirty := func() { |
| 76 | clean = false |
| 77 | cleanCycles = 0 |
| 78 | } |
| 79 | |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 80 | for { |
| 81 | select { |
| 82 | case <-ctx.Done(): |
| Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 83 | s.ilogger.Infof("supervisor processor exiting: %v", ctx.Err()) |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 84 | s.processKill() |
| Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 85 | s.ilogger.Info("supervisor exited, starting liquidator to clean up remaining runnables...") |
| 86 | go s.liquidator() |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 87 | return |
| 88 | case <-gc.C: |
| 89 | if !clean { |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 90 | s.processGC() |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 91 | } |
| 92 | clean = true |
| Serge Bazanski | ac6b644 | 2020-05-06 19:13:43 +0200 | [diff] [blame] | 93 | cleanCycles += 1 |
| 94 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 95 | // This threshold is somewhat arbitrary. It's a balance between |
| 96 | // test speed and test reliability. |
| Serge Bazanski | ac6b644 | 2020-05-06 19:13:43 +0200 | [diff] [blame] | 97 | if cleanCycles > 50 { |
| 98 | for _, w := range waiters { |
| 99 | close(w) |
| 100 | } |
| 101 | waiters = nil |
| 102 | } |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 103 | case r := <-s.pReq: |
| 104 | switch { |
| 105 | case r.schedule != nil: |
| 106 | s.processSchedule(r.schedule) |
| Serge Bazanski | ac6b644 | 2020-05-06 19:13:43 +0200 | [diff] [blame] | 107 | markDirty() |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 108 | case r.died != nil: |
| 109 | s.processDied(r.died) |
| Serge Bazanski | ac6b644 | 2020-05-06 19:13:43 +0200 | [diff] [blame] | 110 | markDirty() |
| 111 | case r.waitSettled != nil: |
| 112 | waiters = append(waiters, r.waitSettled.waiter) |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 113 | default: |
| 114 | panic(fmt.Errorf("unhandled request %+v", r)) |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 120 | // The liquidator is a context-free goroutine which the supervisor starts after |
| 121 | // its context has been canceled. Its job is to take over listening on the |
| 122 | // processing channels that the supervisor processor would usually listen on, |
| 123 | // and implement the minimum amount of logic required to mark existing runnables |
| 124 | // as DEAD. |
| 125 | // |
| 126 | // It exits when all runnables have exited one way or another, and the |
| 127 | // supervision tree is well and truly dead. This will also be reflected by |
| 128 | // liveRunnables returning an empty list. |
| 129 | func (s *supervisor) liquidator() { |
| 130 | for { |
| Tim Windelschmidt | 931b3a3 | 2024-04-18 23:39:20 +0200 | [diff] [blame] | 131 | r := <-s.pReq |
| 132 | switch { |
| 133 | case r.schedule != nil: |
| 134 | s.ilogger.Infof("liquidator: refusing to schedule %s", r.schedule.dn) |
| 135 | s.mu.Lock() |
| 136 | n := s.nodeByDN(r.schedule.dn) |
| Serge Bazanski | eca8ee3 | 2024-07-30 14:32:19 +0000 | [diff] [blame] | 137 | n.state = NodeStateDead |
| Tim Windelschmidt | 931b3a3 | 2024-04-18 23:39:20 +0200 | [diff] [blame] | 138 | s.mu.Unlock() |
| 139 | case r.died != nil: |
| 140 | s.ilogger.Infof("liquidator: %s exited", r.died.dn) |
| 141 | s.mu.Lock() |
| 142 | n := s.nodeByDN(r.died.dn) |
| Serge Bazanski | eca8ee3 | 2024-07-30 14:32:19 +0000 | [diff] [blame] | 143 | n.state = NodeStateDead |
| Tim Windelschmidt | 931b3a3 | 2024-04-18 23:39:20 +0200 | [diff] [blame] | 144 | s.mu.Unlock() |
| Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 145 | } |
| 146 | live := s.liveRunnables() |
| 147 | if len(live) == 0 { |
| 148 | s.ilogger.Infof("liquidator: complete, all runnables dead or done") |
| 149 | return |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| Jan Schär | aa6b42a | 2024-12-18 18:03:26 +0100 | [diff] [blame] | 154 | // liveRunnables returns a list of runnable DNs that aren't DONE/DEAD/CANCELED. |
| 155 | // This is used by the liquidator to figure out when its job is done, and by the |
| Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 156 | // TestHarness to know when to unblock the test cleanup function. |
| 157 | func (s *supervisor) liveRunnables() []string { |
| 158 | s.mu.RLock() |
| 159 | defer s.mu.RUnlock() |
| 160 | |
| Jan Schär | aa6b42a | 2024-12-18 18:03:26 +0100 | [diff] [blame] | 161 | // DFS through supervision tree, making note of live (non-DONE/DEAD/CANCELED |
| 162 | // runnables). |
| Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 163 | var live []string |
| 164 | seen := make(map[string]bool) |
| 165 | q := []*node{s.root} |
| 166 | for { |
| 167 | if len(q) == 0 { |
| 168 | break |
| 169 | } |
| 170 | |
| 171 | // Pop from DFS queue. |
| 172 | el := q[0] |
| 173 | q = q[1:] |
| 174 | |
| 175 | // Skip already visited runnables (this shouldn't happen because the supervision |
| 176 | // tree is, well, a tree - but better stay safe than get stuck in a loop). |
| 177 | eldn := el.dn() |
| 178 | if seen[eldn] { |
| 179 | continue |
| 180 | } |
| 181 | seen[eldn] = true |
| 182 | |
| Jan Schär | aa6b42a | 2024-12-18 18:03:26 +0100 | [diff] [blame] | 183 | if el.state != NodeStateDead && el.state != NodeStateDone && el.state != NodeStateCanceled { |
| Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 184 | live = append(live, eldn) |
| 185 | } |
| 186 | |
| 187 | // Recurse. |
| 188 | for _, child := range el.children { |
| 189 | q = append(q, child) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | sort.Strings(live) |
| 194 | return live |
| 195 | } |
| 196 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 197 | // processKill cancels all nodes in the supervision tree. This is only called |
| 198 | // right before exiting the processor, so they do not get automatically |
| 199 | // restarted. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 200 | func (s *supervisor) processKill() { |
| 201 | s.mu.Lock() |
| 202 | defer s.mu.Unlock() |
| 203 | |
| 204 | // Gather all context cancel functions. |
| 205 | var cancels []func() |
| 206 | queue := []*node{s.root} |
| 207 | for { |
| 208 | if len(queue) == 0 { |
| 209 | break |
| 210 | } |
| 211 | |
| 212 | cur := queue[0] |
| 213 | queue = queue[1:] |
| 214 | |
| 215 | cancels = append(cancels, cur.ctxC) |
| 216 | for _, c := range cur.children { |
| 217 | queue = append(queue, c) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // Call all context cancels. |
| 222 | for _, c := range cancels { |
| 223 | c() |
| 224 | } |
| 225 | } |
| 226 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 227 | // processSchedule starts a node's runnable in a goroutine and records its |
| 228 | // output once it's done. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 229 | func (s *supervisor) processSchedule(r *processorRequestSchedule) { |
| 230 | s.mu.Lock() |
| 231 | defer s.mu.Unlock() |
| 232 | |
| 233 | n := s.nodeByDN(r.dn) |
| Serge Bazanski | cf864da | 2024-07-31 11:23:34 +0000 | [diff] [blame] | 234 | if n.state != NodeStateNew { |
| 235 | panic("programming error: scheduled node not new") |
| 236 | } |
| 237 | s.metrics.NotifyNodeState(r.dn, n.state) |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 238 | go func() { |
| Serge Bazanski | 19bb412 | 2020-05-04 17:57:50 +0200 | [diff] [blame] | 239 | if !s.propagatePanic { |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 240 | defer func() { |
| 241 | if rec := recover(); rec != nil { |
| 242 | s.pReq <- &processorRequest{ |
| 243 | died: &processorRequestDied{ |
| 244 | dn: r.dn, |
| 245 | err: fmt.Errorf("panic: %v, stacktrace: %s", rec, string(debug.Stack())), |
| 246 | }, |
| 247 | } |
| 248 | } |
| 249 | }() |
| 250 | } |
| 251 | |
| 252 | res := n.runnable(n.ctx) |
| 253 | |
| 254 | s.pReq <- &processorRequest{ |
| 255 | died: &processorRequestDied{ |
| 256 | dn: r.dn, |
| 257 | err: res, |
| 258 | }, |
| 259 | } |
| 260 | }() |
| 261 | } |
| 262 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 263 | // processDied records the result from a runnable goroutine, and updates its |
| 264 | // node state accordingly. If the result is a death and not an expected exit, |
| 265 | // related nodes (ie. children and group siblings) are canceled accordingly. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 266 | func (s *supervisor) processDied(r *processorRequestDied) { |
| 267 | s.mu.Lock() |
| 268 | defer s.mu.Unlock() |
| 269 | |
| 270 | // Okay, so a Runnable has quit. What now? |
| 271 | n := s.nodeByDN(r.dn) |
| 272 | ctx := n.ctx |
| 273 | |
| 274 | // Simple case: it was marked as Done and quit with no error. |
| Serge Bazanski | eca8ee3 | 2024-07-30 14:32:19 +0000 | [diff] [blame] | 275 | if n.state == NodeStateDone && r.err == nil { |
| Serge Bazanski | cf864da | 2024-07-31 11:23:34 +0000 | [diff] [blame] | 276 | s.metrics.NotifyNodeState(r.dn, n.state) |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 277 | // Do nothing. This was supposed to happen. Keep the process as DONE. |
| 278 | return |
| 279 | } |
| 280 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 281 | // Simple case: the context was canceled and the returned error is the |
| 282 | // context error. |
| Tim Windelschmidt | 47d0344 | 2024-04-23 15:08:44 +0200 | [diff] [blame] | 283 | if r.err != nil && ctx.Err() != nil && errors.Is(r.err, ctx.Err()) { |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 284 | // Mark the node as canceled successfully. |
| Serge Bazanski | eca8ee3 | 2024-07-30 14:32:19 +0000 | [diff] [blame] | 285 | n.state = NodeStateCanceled |
| Serge Bazanski | cf864da | 2024-07-31 11:23:34 +0000 | [diff] [blame] | 286 | s.metrics.NotifyNodeState(r.dn, n.state) |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 287 | return |
| 288 | } |
| 289 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 290 | // Otherwise, the Runnable should not have died or quit. Handle |
| 291 | // accordingly. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 292 | err := r.err |
| 293 | // A lack of returned error is also an error. |
| 294 | if err == nil { |
| Serge Bazanski | 0164c71 | 2023-03-16 17:54:07 +0100 | [diff] [blame] | 295 | err = fmt.Errorf("returned nil when %s", n.state) |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 296 | } |
| 297 | |
| Serge Bazanski | 0164c71 | 2023-03-16 17:54:07 +0100 | [diff] [blame] | 298 | s.ilogger.Errorf("%s: %v", n.dn(), err) |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 299 | // Mark as dead. |
| Serge Bazanski | eca8ee3 | 2024-07-30 14:32:19 +0000 | [diff] [blame] | 300 | n.state = NodeStateDead |
| Serge Bazanski | cf864da | 2024-07-31 11:23:34 +0000 | [diff] [blame] | 301 | s.metrics.NotifyNodeState(r.dn, n.state) |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 302 | |
| 303 | // Cancel that node's context, just in case something still depends on it. |
| 304 | n.ctxC() |
| 305 | |
| 306 | // Cancel all siblings. |
| 307 | if n.parent != nil { |
| Tim Windelschmidt | 6b6428d | 2024-04-11 01:35:41 +0200 | [diff] [blame] | 308 | for name := range n.parent.groupSiblings(n.name) { |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 309 | if name == n.name { |
| 310 | continue |
| 311 | } |
| 312 | sibling := n.parent.children[name] |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 313 | // TODO(q3k): does this need to run in a goroutine, ie. can a |
| 314 | // context cancel block? |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 315 | sibling.ctxC() |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 320 | // processGC runs the GC process. It's not really Garbage Collection, as in, it |
| 321 | // doesn't remove unnecessary tree nodes - but it does find nodes that need to |
| 322 | // be restarted, find the subset that can and then schedules them for running. |
| 323 | // As such, it's less of a Garbage Collector and more of a Necromancer. |
| 324 | // However, GC is a friendlier name. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 325 | func (s *supervisor) processGC() { |
| 326 | s.mu.Lock() |
| 327 | defer s.mu.Unlock() |
| 328 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 329 | // The 'GC' serves is the main business logic of the supervision tree. It |
| 330 | // traverses a locked tree and tries to find subtrees that must be |
| 331 | // restarted (because of a DEAD/CANCELED runnable). It then finds which of |
| 332 | // these subtrees that should be restarted can be restarted, ie. which ones |
| 333 | // are fully recursively DEAD/CANCELED. It also finds the smallest set of |
| 334 | // largest subtrees that can be restarted, ie. if there's multiple DEAD |
| 335 | // runnables that can be restarted at once, it will do so. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 336 | |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 337 | // Phase one: Find all leaves. |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 338 | // This is a simple DFS that finds all the leaves of the tree, ie all nodes |
| 339 | // that do not have children nodes. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 340 | leaves := make(map[string]bool) |
| 341 | queue := []*node{s.root} |
| 342 | for { |
| 343 | if len(queue) == 0 { |
| 344 | break |
| 345 | } |
| 346 | cur := queue[0] |
| 347 | queue = queue[1:] |
| 348 | |
| 349 | for _, c := range cur.children { |
| 350 | queue = append([]*node{c}, queue...) |
| 351 | } |
| 352 | |
| 353 | if len(cur.children) == 0 { |
| 354 | leaves[cur.dn()] = true |
| 355 | } |
| 356 | } |
| 357 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 358 | // Phase two: traverse tree from node to root and make note of all subtrees |
| 359 | // that can be restarted. |
| 360 | // A subtree is restartable/ready iff every node in that subtree is either |
| 361 | // CANCELED, DEAD or DONE. Such a 'ready' subtree can be restarted by the |
| 362 | // supervisor if needed. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 363 | |
| 364 | // DNs that we already visited. |
| 365 | visited := make(map[string]bool) |
| 366 | // DNs whose subtrees are ready to be restarted. |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 367 | // These are all subtrees recursively - ie., root.a.a and root.a will both |
| 368 | // be marked here. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 369 | ready := make(map[string]bool) |
| 370 | |
| 371 | // We build a queue of nodes to visit, starting from the leaves. |
| 372 | queue = []*node{} |
| Tim Windelschmidt | 6b6428d | 2024-04-11 01:35:41 +0200 | [diff] [blame] | 373 | for l := range leaves { |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 374 | queue = append(queue, s.nodeByDN(l)) |
| 375 | } |
| 376 | |
| 377 | for { |
| 378 | if len(queue) == 0 { |
| 379 | break |
| 380 | } |
| 381 | |
| 382 | cur := queue[0] |
| 383 | curDn := cur.dn() |
| 384 | |
| 385 | queue = queue[1:] |
| 386 | |
| 387 | // Do we have a decision about our children? |
| 388 | allVisited := true |
| 389 | for _, c := range cur.children { |
| 390 | if !visited[c.dn()] { |
| 391 | allVisited = false |
| 392 | break |
| 393 | } |
| 394 | } |
| 395 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 396 | // If no decision about children is available, it means we ended up in |
| 397 | // this subtree through some shorter path of a shorter/lower-order |
| 398 | // leaf. There is a path to a leaf that's longer than the one that |
| 399 | // caused this node to be enqueued. Easy solution: just push back the |
| 400 | // current element and retry later. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 401 | if !allVisited { |
| 402 | // Push back to queue and wait for a decision later. |
| 403 | queue = append(queue, cur) |
| 404 | continue |
| 405 | } |
| 406 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 407 | // All children have been visited and we have an idea about whether |
| 408 | // they're ready/restartable. All of the node's children must be |
| 409 | // restartable in order for this node to be restartable. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 410 | childrenReady := true |
| Serge Bazanski | ba7bf7d | 2021-10-29 16:59:00 +0200 | [diff] [blame] | 411 | var childrenNotReady []string |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 412 | for _, c := range cur.children { |
| 413 | if !ready[c.dn()] { |
| Serge Bazanski | ba7bf7d | 2021-10-29 16:59:00 +0200 | [diff] [blame] | 414 | childrenNotReady = append(childrenNotReady, c.dn()) |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 415 | childrenReady = false |
| 416 | break |
| 417 | } |
| 418 | } |
| 419 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 420 | // In addition to children, the node itself must be restartable (ie. |
| 421 | // DONE, DEAD or CANCELED). |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 422 | curReady := false |
| 423 | switch cur.state { |
| Serge Bazanski | eca8ee3 | 2024-07-30 14:32:19 +0000 | [diff] [blame] | 424 | case NodeStateDone: |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 425 | curReady = true |
| Serge Bazanski | eca8ee3 | 2024-07-30 14:32:19 +0000 | [diff] [blame] | 426 | case NodeStateCanceled: |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 427 | curReady = true |
| Serge Bazanski | eca8ee3 | 2024-07-30 14:32:19 +0000 | [diff] [blame] | 428 | case NodeStateDead: |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 429 | curReady = true |
| Tim Windelschmidt | 9b2c156 | 2024-04-11 01:39:25 +0200 | [diff] [blame] | 430 | default: |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 431 | } |
| 432 | |
| Serge Bazanski | eca8ee3 | 2024-07-30 14:32:19 +0000 | [diff] [blame] | 433 | if cur.state == NodeStateDead && !childrenReady { |
| Serge Bazanski | ba7bf7d | 2021-10-29 16:59:00 +0200 | [diff] [blame] | 434 | s.ilogger.Warningf("Not restarting %s: children not ready to be restarted: %v", curDn, childrenNotReady) |
| 435 | } |
| 436 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 437 | // Note down that we have an opinion on this node, and note that |
| 438 | // opinion down. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 439 | visited[curDn] = true |
| 440 | ready[curDn] = childrenReady && curReady |
| 441 | |
| 442 | // Now we can also enqueue the parent of this node for processing. |
| 443 | if cur.parent != nil && !visited[cur.parent.dn()] { |
| 444 | queue = append(queue, cur.parent) |
| 445 | } |
| 446 | } |
| 447 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 448 | // Phase 3: traverse tree from root to find largest subtrees that need to |
| 449 | // be restarted and are ready to be restarted. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 450 | |
| 451 | // All DNs that need to be restarted by the GC process. |
| 452 | want := make(map[string]bool) |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 453 | // All DNs that need to be restarted and can be restarted by the GC process |
| 454 | // - a subset of 'want' DNs. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 455 | can := make(map[string]bool) |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 456 | // The set difference between 'want' and 'can' are all nodes that should be |
| 457 | // restarted but can't yet (ie. because a child is still in the process of |
| 458 | // being canceled). |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 459 | |
| 460 | // DFS from root. |
| 461 | queue = []*node{s.root} |
| 462 | for { |
| 463 | if len(queue) == 0 { |
| 464 | break |
| 465 | } |
| 466 | |
| 467 | cur := queue[0] |
| 468 | queue = queue[1:] |
| 469 | |
| 470 | // If this node is DEAD or CANCELED it should be restarted. |
| Serge Bazanski | eca8ee3 | 2024-07-30 14:32:19 +0000 | [diff] [blame] | 471 | if cur.state == NodeStateDead || cur.state == NodeStateCanceled { |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 472 | want[cur.dn()] = true |
| 473 | } |
| 474 | |
| Serge Bazanski | ac6b644 | 2020-05-06 19:13:43 +0200 | [diff] [blame] | 475 | // If it should be restarted and is ready to be restarted... |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 476 | if want[cur.dn()] && ready[cur.dn()] { |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 477 | // And its parent context is valid (ie hasn't been canceled), mark |
| 478 | // it as restartable. |
| Serge Bazanski | ac6b644 | 2020-05-06 19:13:43 +0200 | [diff] [blame] | 479 | if cur.parent == nil || cur.parent.ctx.Err() == nil { |
| 480 | can[cur.dn()] = true |
| 481 | continue |
| 482 | } |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 483 | } |
| 484 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 485 | // Otherwise, traverse further down the tree to see if something else |
| 486 | // needs to be done. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 487 | for _, c := range cur.children { |
| 488 | queue = append(queue, c) |
| 489 | } |
| 490 | } |
| 491 | |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 492 | // Reinitialize and reschedule all subtrees |
| Tim Windelschmidt | 6b6428d | 2024-04-11 01:35:41 +0200 | [diff] [blame] | 493 | for dn := range can { |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 494 | n := s.nodeByDN(dn) |
| Serge Bazanski | ac6b644 | 2020-05-06 19:13:43 +0200 | [diff] [blame] | 495 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 496 | // Only back off when the node unexpectedly died - not when it got |
| 497 | // canceled. |
| Serge Bazanski | ac6b644 | 2020-05-06 19:13:43 +0200 | [diff] [blame] | 498 | bo := time.Duration(0) |
| Serge Bazanski | eca8ee3 | 2024-07-30 14:32:19 +0000 | [diff] [blame] | 499 | if n.state == NodeStateDead { |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 500 | bo = n.bo.NextBackOff() |
| 501 | } |
| Serge Bazanski | ac6b644 | 2020-05-06 19:13:43 +0200 | [diff] [blame] | 502 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 503 | // Prepare node for rescheduling - remove its children, reset its state |
| 504 | // to new. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 505 | n.reset() |
| Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 506 | s.ilogger.Infof("rescheduling supervised node %s with backoff %s", dn, bo.String()) |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 507 | |
| 508 | // Reschedule node runnable to run after backoff. |
| 509 | go func(n *node, bo time.Duration) { |
| Jan Schär | 6560209 | 2024-12-19 10:37:34 +0100 | [diff] [blame^] | 510 | select { |
| 511 | case <-time.After(bo): |
| 512 | s.pReq <- &processorRequest{ |
| 513 | schedule: &processorRequestSchedule{dn: n.dn()}, |
| 514 | } |
| 515 | case <-n.ctx.Done(): |
| 516 | s.pReq <- &processorRequest{ |
| 517 | died: &processorRequestDied{ |
| 518 | dn: n.dn(), |
| 519 | err: n.ctx.Err(), |
| 520 | }, |
| 521 | } |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 522 | } |
| 523 | }(n, bo) |
| 524 | } |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 525 | } |