oficina / alta Public

Clone
git clone https://amadan.net/oficina/alta

Plain git — no account needed to clone.

Download

Download this file

1// This file holds the one way adoption restarts an app: the canonical
2// host alone, through the narrowed restart door, after a dry run shows
3// that the door resolves exactly that host.
4package adopt
5
6import (
7 "context"
8 "errors"
9 "fmt"
10 "net/http"
11 "time"
12
13 "amadan.net/oficina/alta/carlosop"
14 "amadan.net/oficina/alta/provision"
15)
16
17// RestartAndWait's polling. Variables only so tests can shorten them.
18var (
19 pollEvery = 5 * time.Second
20 pollLimit = 5 * time.Minute
21)
22
23// RestartCanonical restarts app's canonical host, and nothing else. Every
24// restart adoption makes, a rollback's included, comes through here; it
25// is the only caller of carlosop.RestartHost.
26//
27// id must be app's canonical record: its app, and the canonical domain
28// claimed for its record host. The restart door is first asked for a
29// dry run, which must resolve exactly [id.RecordHost]; anything else is
30// "restart-scope-<n>", n the number of routes it resolved, and nothing is
31// restarted. A route the dry run reports as hibernating is accepted: it
32// will not cycle now, but the next request wakes it on the new config,
33// and the probe that follows makes that request.
34//
35// id may be a journalled baseline, so after the dry run, the last read
36// before the restart, the claim is resolved again (claimHolds). A claim
37// that no longer binds the canonical domain to id's record restarts
38// nothing, with or without apply.
39//
40// A console without the narrowed door (carlosop.ErrNoScopedRestart), a
41// 503 or any other refusal is returned as it is, and nothing falls back
42// to the app-wide door. Without apply, only the dry run is made.
43func RestartCanonical(ctx context.Context, c *carlosop.Client, app provision.App, id Identity, apply bool) error {
44 host := CanonicalHost(app)
45 switch {
46 case c == nil:
47 return errors.New("adopt: no console client")
48 case id.App != app.Slug:
49 return errors.New("adopt: the identity is another app's")
50 case id.RecordHost == "" || id.Domain != host || id.Route != id.RecordHost:
51 return errors.New("adopt: the identity is not the canonical host's")
52 }
53 routes, err := c.RestartHost(ctx, app.Slug, id.RecordHost, true)
54 if err != nil {
55 return err
56 }
57 if err := exactlyHost(routes, id.RecordHost); err != nil {
58 return err
59 }
60 if err := claimHolds(ctx, c, app, id); err != nil {
61 return err
62 }
63 if !apply {
64 return nil
65 }
66 routes, err = c.RestartHost(ctx, app.Slug, id.RecordHost, false)
67 if err != nil {
68 return err
69 }
70 // The request is made by now; a wider answer is still reported, so
71 // the run stops rather than carrying on as if one host cycled.
72 return exactlyHost(routes, id.RecordHost)
73}
74
75// claimHolds is nil when app's canonical domain still resolves to id's
76// record through id's route. Otherwise it is restart-binding-<code>, the
77// code canonicalRecord refused with, or restart-binding-changed for a
78// claim that now resolves elsewhere; a read that fails is its own error.
79// Whether the claim is live is not asked: a rollback has to reach the
80// same record while its domain is unwell.
81func claimHolds(ctx context.Context, c *carlosop.Client, app provision.App, id Identity) error {
82 ds, err := c.Domains(ctx, app.Slug)
83 if err != nil {
84 return err
85 }
86 all, err := c.Instances(ctx, app.Slug)
87 if err != nil {
88 return err
89 }
90 d, _, code, err := canonicalRecord(CanonicalHost(app), ds, all)
91 switch {
92 case err != nil:
93 return err
94 case code != "":
95 return errors.New("adopt: restart-binding-" + code)
96 case d.Route != id.Route:
97 return errors.New("adopt: restart-binding-changed")
98 }
99 return nil
100}
101
102// exactlyHost is nil when routes is the one route host.
103func exactlyHost(routes []carlosop.RestartRoute, host string) error {
104 if len(routes) != 1 || routes[0].Host != host {
105 return fmt.Errorf("restart-scope-%d", len(routes))
106 }
107 return nil
108}
109
110// RestartAndWait restarts app's canonical host through RestartCanonical,
111// then probes its public origin every 5 s, for at most 5 min, until
112// until accepts what the probe saw, and returns that observation. A probe
113// that fails (the app is between processes) is polled through. Running
114// out of time is "restart-timeout", with the last reason.
115//
116// Without apply, it makes RestartCanonical's dry run and one probe, and
117// returns what that probe saw: nothing restarted, so nothing is waited
118// for and until is not asked.
119func RestartAndWait(ctx context.Context, c *carlosop.Client, h *http.Client, app provision.App, id Identity, until func(Delivery) error, apply bool) (Delivery, error) {
120 if until == nil {
121 return Delivery{}, errors.New("adopt: nothing to wait for")
122 }
123 if err := RestartCanonical(ctx, c, app, id, apply); err != nil {
124 return Delivery{}, err
125 }
126 origin := provision.PublicOrigin(provision.AdoptionLabel, app.Slug)
127 if !apply {
128 return Probe(ctx, h, origin)
129 }
130 // Every wait and every probe runs under the limit's own context, so a
131 // stalled answer is cut off at the limit and nothing that finishes
132 // after it counts. Its expiry is told apart from the caller's.
133 poll, cancel := context.WithTimeout(ctx, pollLimit)
134 defer cancel()
135 var last Delivery
136 reason := errors.New("adopt: the app was not probed")
137 stopped := func() (Delivery, error) {
138 if err := ctx.Err(); err != nil {
139 return last, fmt.Errorf("adopt: stopped waiting for the restart: %w", err)
140 }
141 return last, fmt.Errorf("adopt: restart-timeout: %w", reason)
142 }
143 for {
144 // The restart is asynchronous, so the first probe waits too.
145 t := time.NewTimer(pollEvery)
146 select {
147 case <-poll.Done():
148 t.Stop()
149 return stopped()
150 case <-t.C:
151 }
152 // A probe that returns at all finished inside the limit: each of
153 // its requests ran under poll. One cut off by the limit is not a
154 // reason of its own, so the last real one is reported.
155 d, err := Probe(poll, h, origin)
156 if err != nil {
157 if poll.Err() != nil {
158 return stopped()
159 }
160 reason = err
161 continue
162 }
163 last = d
164 reason = until(d)
165 // Accepted only if the limit still holds once until has answered:
166 // a success reached after the deadline is a timeout.
167 if poll.Err() != nil {
168 if reason == nil {
169 reason = errors.New("adopt: the app came back only after the limit")
170 }
171 return stopped()
172 }
173 if reason == nil {
174 return d, nil
175 }
176 }
177}
178