gunk-dev/the-valley

wake.go

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