| 1 | package main |
| 2 | |
| 3 | // The controller's own wake directory. watch --woken-by DIR starts a pass |
| 4 | // soon after a writer marks DIR due, instead of at the end of its interval. |
| 5 | // It holds at most one file, whoever writes and however often: due, an |
| 6 | // empty file a writer creates with O_CREAT|O_EXCL, exactly as markDue |
| 7 | // (bus.go) creates the check worker's. A due that is already there is a |
| 8 | // mark already made, so any number of moves leave one file. The host's |
| 9 | // post-receive hook and the controller that lands the floor are the |
| 10 | // writers. Nothing reads due: it says only "read the refs again", and what |
| 11 | // a pass decides still comes from the refs. |
| 12 | // |
| 13 | // The wake directory carries marks and nothing else. Which controller |
| 14 | // serves a repository is the repository's own lock (own_unix.go), taken |
| 15 | // before the controller sweeps or passes and checked before every pass. So |
| 16 | // a wake directory that is missing, replaced or watched by nothing changes |
| 17 | // how soon a pass starts, and never which controller runs it. |
| 18 | // |
| 19 | // The controller watches the directory with inotify before it removes due, |
| 20 | // and removes due before a pass reads any ref. A mark made after the |
| 21 | // removal is a new due, which the controller finds when the pass ends and |
| 22 | // starts the next pass for. A mark made before the removal is covered by |
| 23 | // the pass, which reads the refs after it. So a mark is never lost, and a |
| 24 | // pass never removes a mark made since it started reading. |
| 25 | // |
| 26 | // Before it waits, the controller looks for due on disk, so a notification |
| 27 | // it was given is never what starts a pass, and a notification left over |
| 28 | // from a due that a pass already removed starts nothing. A pass that lands |
| 29 | // anything is followed at once by another, with no mark: a request judged |
| 30 | // against the tip before the landing is judged again against the tip after |
| 31 | // it. |
| 32 | // |
| 33 | // The interval stays the recovery pass. A mark that never came, a watch |
| 34 | // that failed, a directory that was missing, and a crash between a ref |
| 35 | // move and its mark are all covered by the next timer pass, and by the |
| 36 | // pass every start makes. A due that cannot be removed is ignored until |
| 37 | // the next timer pass, so it starts one pass, and not one after another. |
| 38 | |
| 39 | import ( |
| 40 | "errors" |
| 41 | "fmt" |
| 42 | "io" |
| 43 | "time" |
| 44 | ) |
| 45 | |
| 46 | // passReason says why a pass started, in the line that opens it. |
| 47 | type passReason string |
| 48 | |
| 49 | const ( |
| 50 | reasonStartup passReason = "startup" // the first pass of every start |
| 51 | reasonDue passReason = "due" // a writer marked the directory |
| 52 | reasonTimer passReason = "timer" // the interval ran out |
| 53 | reasonFollowUp passReason = "follow-up" // the pass before it landed something |
| 54 | reasonWatcher passReason = "watcher" // the watch was restored after a failure |
| 55 | ) |
| 56 | |
| 57 | const ( |
| 58 | // watchRetry is how long a controller whose watch failed waits before it |
| 59 | // tries to watch the directory again. Each try opens and locks the |
| 60 | // directory and adds two inotify watches, and a failure is said once |
| 61 | // until it changes. |
| 62 | watchRetry = 5 * time.Second |
| 63 | // failedPassHold is the least time from a pass that returned an error to |
| 64 | // the next pass, however many marks arrive meanwhile. It is never longer |
| 65 | // than the interval. |
| 66 | failedPassHold = 10 * time.Second |
| 67 | ) |
| 68 | |
| 69 | // errLockHeld is what taking a repository's lock returns when another |
| 70 | // controller holds it. |
| 71 | var errLockHeld = errors.New("another controller holds its lock") |
| 72 | |
| 73 | // errGone is what a watch ends with when the directory it holds is no |
| 74 | // longer the one the path names: it was removed, moved or replaced. |
| 75 | var errGone = errors.New("the directory was removed, moved or replaced") |
| 76 | |
| 77 | // watcher is a watch on a wake directory, as the platform provides one. It |
| 78 | // holds the directory it watches, and looks for due and removes it there. |
| 79 | type watcher interface { |
| 80 | // events is sent to, without blocking, when due is created in the |
| 81 | // directory or the kernel dropped events. Its capacity is one, so any |
| 82 | // number of notifications wait as one. |
| 83 | events() <-chan struct{} |
| 84 | // failed is sent the reason once the watch can no longer notify. |
| 85 | failed() <-chan error |
| 86 | // removeDue removes due from the directory watched. No due is no error. |
| 87 | removeDue() error |
| 88 | // hasDue says whether due is in the directory watched. |
| 89 | hasDue() bool |
| 90 | close() |
| 91 | } |
| 92 | |
| 93 | // wakeLoop runs passes over the refs: one at start, one soon after each |
| 94 | // mark, one after each pass that landed, and one when the interval runs |
| 95 | // out with nothing else having started one. |
| 96 | type wakeLoop struct { |
| 97 | dir string |
| 98 | interval time.Duration |
| 99 | stderr io.Writer |
| 100 | // pass runs one pass and says whether it landed anything. |
| 101 | pass func(reason passReason) (landed bool, err error) |
| 102 | // owned says whether the controller still holds the repository it |
| 103 | // serves (repoLock.check). It is asked before every pass, and an error |
| 104 | // ends the loop before the pass. Nil asks nothing. |
| 105 | owned func() error |
| 106 | |
| 107 | // The platform's watch (wake_linux.go). Tests may replace it. |
| 108 | watch func(dir string) (watcher, error) |
| 109 | // stop ends the loop when it is closed, for tests; nil never ends it. |
| 110 | stop <-chan struct{} |
| 111 | |
| 112 | w watcher // nil while the directory is not watched |
| 113 | retryAt time.Time // when to try watching again, while w is nil |
| 114 | failure string // the last failure said, so one is not said again |
| 115 | restored bool // the watch came back, and a pass is owed for it |
| 116 | } |
| 117 | |
| 118 | // run runs passes until stop is closed, or until the controller no longer |
| 119 | // holds the repository it serves. A watch that fails changes only how soon |
| 120 | // passes start. |
| 121 | func (l *wakeLoop) run() error { |
| 122 | // A directory that is missing or cannot be watched is said here, and |
| 123 | // tried again later. The start's pass runs either way. |
| 124 | l.arm() |
| 125 | defer l.disarm() |
| 126 | reason := reasonStartup |
| 127 | for { |
| 128 | if l.owned != nil { |
| 129 | if err := l.owned(); err != nil { |
| 130 | return err |
| 131 | } |
| 132 | } |
| 133 | claimed := l.claim() |
| 134 | landed, err := l.pass(reason) |
| 135 | end := time.Now() |
| 136 | next := end.Add(l.interval) |
| 137 | var earliest, holdDue time.Time |
| 138 | if err != nil { |
| 139 | fmt.Fprintf(l.stderr, "integrator: %v\n", err) |
| 140 | earliest = end.Add(min(failedPassHold, l.interval)) |
| 141 | } |
| 142 | if !claimed { |
| 143 | holdDue = next |
| 144 | } |
| 145 | if landed && err == nil { |
| 146 | reason = reasonFollowUp |
| 147 | continue |
| 148 | } |
| 149 | if reason = l.wait(next, earliest, holdDue); reason == "" { |
| 150 | return nil |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // claim removes due, before the pass reads any ref, and says whether the |
| 156 | // mark is taken care of. No due, or no watch to consume it through, is |
| 157 | // nothing to claim. A due that cannot be removed is said, and the caller |
| 158 | // ignores it until the next timer pass. |
| 159 | func (l *wakeLoop) claim() bool { |
| 160 | if l.w == nil { |
| 161 | return true |
| 162 | } |
| 163 | err := l.w.removeDue() |
| 164 | if err == nil { |
| 165 | return true |
| 166 | } |
| 167 | fmt.Fprintf(l.stderr, "integrator: could not remove the due in %s, so it starts no pass before the next timer pass: %v\n", |
| 168 | l.dir, err) |
| 169 | return false |
| 170 | } |
| 171 | |
| 172 | // due says whether a writer has marked the directory. It is the durable |
| 173 | // state, and what decides whether a pass starts. |
| 174 | func (l *wakeLoop) due() bool { |
| 175 | return l.w != nil && l.w.hasDue() |
| 176 | } |
| 177 | |
| 178 | // wait waits for the reason of the next pass: a due, once earliest has |
| 179 | // passed and holdDue has; a restored watch, once earliest has passed; or |
| 180 | // the timer at next. It returns "" when stop is closed. |
| 181 | func (l *wakeLoop) wait(next, earliest, holdDue time.Time) passReason { |
| 182 | for { |
| 183 | now := time.Now() |
| 184 | if !now.Before(next) { |
| 185 | return reasonTimer |
| 186 | } |
| 187 | if l.w == nil && !now.Before(l.retryAt) { |
| 188 | l.arm() |
| 189 | } |
| 190 | if !now.Before(earliest) { |
| 191 | if l.restored { |
| 192 | l.restored = false |
| 193 | return reasonWatcher |
| 194 | } |
| 195 | if !now.Before(holdDue) && l.due() { |
| 196 | return reasonDue |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | wake := next |
| 201 | if l.w == nil && l.retryAt.Before(wake) { |
| 202 | wake = l.retryAt |
| 203 | } |
| 204 | if earliest.After(now) && earliest.Before(wake) { |
| 205 | wake = earliest |
| 206 | } |
| 207 | timer := time.NewTimer(wake.Sub(now)) |
| 208 | var events <-chan struct{} |
| 209 | var failed <-chan error |
| 210 | if l.w != nil { |
| 211 | events, failed = l.w.events(), l.w.failed() |
| 212 | } |
| 213 | select { |
| 214 | case <-events: |
| 215 | case err := <-failed: |
| 216 | l.fail(fmt.Sprintf("the watch on %s ended: %v", l.dir, err)) |
| 217 | case <-timer.C: |
| 218 | case <-l.stop: |
| 219 | timer.Stop() |
| 220 | return "" |
| 221 | } |
| 222 | timer.Stop() |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // arm watches the directory. Watching starts before anything looks for |
| 227 | // due. A failure is said, and arm is tried again after watchRetry; until |
| 228 | // then, passes come from the timer. A watch restored after a failure owes |
| 229 | // one pass, since a mark may have been made where nothing watched. |
| 230 | func (l *wakeLoop) arm() { |
| 231 | w, err := l.watch(l.dir) |
| 232 | if err != nil { |
| 233 | l.fail(fmt.Sprintf("not watching %s: %v", l.dir, err)) |
| 234 | return |
| 235 | } |
| 236 | l.w = w |
| 237 | if l.failure != "" { |
| 238 | fmt.Fprintf(l.stderr, "integrator: watching %s again\n", l.dir) |
| 239 | l.failure = "" |
| 240 | l.restored = true |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // fail drops the watch, so the next try watches again. The repository's |
| 245 | // lock is not the watch's, and stays held. |
| 246 | func (l *wakeLoop) fail(why string) { |
| 247 | l.disarm() |
| 248 | l.retryAt = time.Now().Add(watchRetry) |
| 249 | if why != l.failure { |
| 250 | fmt.Fprintf(l.stderr, "integrator: %s; passes come every %s, and the watch is tried again every %s\n", |
| 251 | why, l.interval, watchRetry) |
| 252 | l.failure = why |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | func (l *wakeLoop) disarm() { |
| 257 | if l.w != nil { |
| 258 | l.w.close() |
| 259 | l.w = nil |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // passOutput is a pass's standard output. It opens with the line naming |
| 264 | // why the pass started. A timer pass that says nothing else says nothing |
| 265 | // at all, so an idle controller's journal is not one line per interval. |
| 266 | type passOutput struct { |
| 267 | w io.Writer |
| 268 | header string // the opening line, until it is written |
| 269 | } |
| 270 | |
| 271 | func newPassOutput(w io.Writer, reason passReason) *passOutput { |
| 272 | p := &passOutput{w: w, header: fmt.Sprintf("pass: %s\n", reason)} |
| 273 | if reason != reasonTimer { |
| 274 | p.Write(nil) |
| 275 | } |
| 276 | return p |
| 277 | } |
| 278 | |
| 279 | func (p *passOutput) Write(b []byte) (int, error) { |
| 280 | if p.header != "" { |
| 281 | io.WriteString(p.w, p.header) |
| 282 | p.header = "" |
| 283 | } |
| 284 | return p.w.Write(b) |
| 285 | } |