/
loop.go
321 lines (287 loc) · 9.98 KB
/
loop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package resolver
import (
"time"
jujucharm "github.com/juju/charm/v12"
"github.com/juju/charm/v12/hooks"
"github.com/juju/errors"
"github.com/juju/mutex/v2"
"github.com/juju/juju/core/lxdprofile"
"github.com/juju/juju/worker/fortress"
"github.com/juju/juju/worker/uniter/operation"
"github.com/juju/juju/worker/uniter/remotestate"
)
// ErrLoopAborted is used to signal that the loop is exiting because it
// received a value on its config's Abort chan.
var ErrLoopAborted = errors.New("resolver loop aborted")
// ErrDoNotProceed is used to distinguish behaviour from
// resolver.ErrNoOperation. i.e do not run any operations versus
// this resolver has no operations to run.
var ErrDoNotProceed = errors.New("do not proceed")
// Logger is here to stop the desire of creating a package level Logger.
// Don't do this, instead use the one passed into the LoopConfig.
type logger interface{}
var _ logger = struct{}{}
// Logger represents the logging methods used in this package.
type Logger interface {
Errorf(string, ...interface{})
Debugf(string, ...interface{})
Tracef(string, ...interface{})
Warningf(string, ...interface{})
}
// LoopConfig contains configuration parameters for the resolver loop.
type LoopConfig struct {
Resolver Resolver
Watcher remotestate.Watcher
Executor operation.Executor
Factory operation.Factory
Abort <-chan struct{}
OnIdle func() error
CharmDirGuard fortress.Guard
CharmDir string
Logger Logger
}
// Loop repeatedly waits for remote state changes, feeding the local and
// remote state to the provided Resolver to generate Operations which are
// then run with the provided Executor.
//
// The provided "onIdle" function will be called when the loop is waiting
// for remote state changes due to a lack of work to perform. It will not
// be called when a change is anticipated (i.e. due to ErrWaiting).
//
// The resolver loop can be controlled in the following ways:
// - if the "abort" channel is signalled, then the loop will
// exit with ErrLoopAborted
// - if the resolver returns ErrWaiting, then no operations
// will be executed until the remote state has changed
// again
// - if the resolver returns ErrNoOperation, then "onIdle"
// will be invoked and the loop will wait until the remote
// state has changed again
// - if the resolver, onIdle, or executor return some other
// error, the loop will exit immediately
func Loop(cfg LoopConfig, localState *LocalState) error {
rf := &resolverOpFactory{Factory: cfg.Factory, LocalState: localState}
// Initialize charmdir availability before entering the loop in case we're recovering from a restart.
err := updateCharmDir(cfg.Executor.State(), cfg.CharmDirGuard, cfg.Abort, cfg.Logger)
if err != nil {
return errors.Trace(err)
}
// If we're restarting the loop, ensure any pending charm upgrade is run
// before continuing.
err = checkCharmInstallUpgrade(cfg.Logger, cfg.CharmDir, cfg.Watcher.Snapshot(), rf, cfg.Executor)
if err != nil {
return errors.Trace(err)
}
fire := make(chan struct{}, 1)
for {
rf.RemoteState = cfg.Watcher.Snapshot()
rf.LocalState.State = cfg.Executor.State()
if localState.HookWasShutdown && rf.RemoteState.ContainerRunningStatus != nil {
agentShutdown := rf.RemoteState.Shutdown
if !agentShutdown {
agentShutdown = maybeAgentShutdown(cfg)
}
if !agentShutdown {
cfg.Logger.Warningf("last %q hook was killed, but agent still alive", localState.Hook.Kind)
}
}
op, err := cfg.Resolver.NextOp(*rf.LocalState, rf.RemoteState, rf)
for err == nil {
// Send remote state changes to running operations.
remoteStateChanged := make(chan remotestate.Snapshot)
done := make(chan struct{})
go func() {
var rs chan remotestate.Snapshot
for {
select {
case <-cfg.Watcher.RemoteStateChanged():
// We consumed a remote state change event
// so we need a way to trigger the select below
// in case it was a new operation.
select {
case fire <- struct{}{}:
default:
}
rs = remoteStateChanged
case rs <- cfg.Watcher.Snapshot():
rs = nil
case <-done:
return
}
}
}()
cfg.Logger.Tracef("running op: %v", op)
if err := cfg.Executor.Run(op, remoteStateChanged); err != nil {
close(done)
if errors.Cause(err) == mutex.ErrCancelled {
// If the lock acquisition was cancelled (such as when the
// migration-inactive flag drops) we do not want the
// resolver to surface that error. This puts the agent into
// the "failed" state, which causes the initial migration
// validation phase to fail.
// The safest thing to do is to bounce the loop and
// reevaluate our state, which is what happens upon a
// fortress error anyway (uniter.TranslateFortressErrors).
cfg.Logger.Warningf("executor lock acquisition cancelled")
return ErrRestart
}
return errors.Trace(err)
}
close(done)
// Refresh snapshot, in case remote state
// changed between operations.
rf.RemoteState = cfg.Watcher.Snapshot()
rf.LocalState.State = cfg.Executor.State()
err = updateCharmDir(rf.LocalState.State, cfg.CharmDirGuard, cfg.Abort, cfg.Logger)
if err != nil {
return errors.Trace(err)
}
op, err = cfg.Resolver.NextOp(*rf.LocalState, rf.RemoteState, rf)
}
switch errors.Cause(err) {
case nil:
case ErrWaiting:
// If a resolver is waiting for events to
// complete, the agent is not idle.
case ErrNoOperation:
if cfg.OnIdle != nil {
if err := cfg.OnIdle(); err != nil {
return errors.Trace(err)
}
}
default:
return err
}
select {
case <-cfg.Abort:
return ErrLoopAborted
case <-cfg.Watcher.RemoteStateChanged():
case <-fire:
}
}
}
// maybeAgentShutdown returns true if the agent was killed by a
// SIGTERM. If not true at the time of calling, it will wait a short
// time for the status to possibly be updated.
func maybeAgentShutdown(cfg LoopConfig) bool {
fire := make(chan struct{}, 1)
remoteStateChanged := make(chan remotestate.Snapshot)
done := make(chan struct{})
defer close(done)
go func() {
var rs chan remotestate.Snapshot
for {
select {
case <-cfg.Watcher.RemoteStateChanged():
// We consumed a remote state change event
// so we need a way to trigger the select below
// in case it was a new operation.
select {
case fire <- struct{}{}:
default:
}
rs = remoteStateChanged
case rs <- cfg.Watcher.Snapshot():
rs = nil
case <-done:
return
}
}
}()
for {
select {
case rs := <-remoteStateChanged:
if rs.Shutdown {
return true
}
case <-time.After(3 * time.Second):
return false
}
}
}
// updateCharmDir sets charm directory availability for sharing among
// concurrent workers according to local operation state.
func updateCharmDir(opState operation.State, guard fortress.Guard, abort fortress.Abort, logger Logger) error {
var changing bool
// Determine if the charm content is changing.
if opState.Kind == operation.Install || opState.Kind == operation.Upgrade {
changing = true
} else if opState.Kind == operation.RunHook && opState.Hook != nil && opState.Hook.Kind == hooks.UpgradeCharm {
changing = true
}
available := opState.Started && !opState.Stopped && !changing
logger.Tracef("charmdir: available=%v opState: started=%v stopped=%v changing=%v",
available, opState.Started, opState.Stopped, changing)
if available {
return guard.Unlock()
} else {
return guard.Lockdown(abort)
}
}
func checkCharmInstallUpgrade(logger Logger, charmDir string, remote remotestate.Snapshot, rf *resolverOpFactory, ex operation.Executor) error {
// If we restarted due to error with a pending charm upgrade available,
// do the upgrade now. There are cases (lp:1895040) where the error was
// caused because not all units were upgraded before relation-created
// hooks were attempted for peer relations. Do this before the remote
// state watcher is started. It will not trigger an upgrade, until the
// next applicationChanged event. Could get stuck in an error loop.
local := rf.LocalState
local.State = ex.State()
opFunc := rf.NewUpgrade
if !local.Installed && local.Hook != nil && local.Hook.Kind == hooks.Install && local.Step != operation.Done {
// We must have failed to run the install hook, restarted (possibly in a sidecar charm), so need to re-run the install op.
opFunc = rf.NewInstall
} else if !local.Installed || remote.CharmURL == "" {
// If the unit isn't installed, no need to start an upgrade.
return nil
}
_, err := jujucharm.ReadCharmDir(charmDir)
haveCharmDir := err == nil
if haveCharmDir {
// If the unit is installed and already upgrading and the charm dir
// exists no need to start an upgrade.
if local.Kind == operation.Upgrade || (local.Hook != nil && local.Hook.Kind == hooks.UpgradeCharm) {
return nil
}
}
if local.Started && remote.CharmProfileRequired {
if remote.LXDProfileName == "" {
return nil
}
rev, err := lxdprofile.ProfileRevision(remote.LXDProfileName)
if err != nil {
return errors.Trace(err)
}
curl, err := jujucharm.ParseURL(remote.CharmURL)
if err != nil {
return errors.Trace(err)
}
if rev != curl.Revision {
logger.Tracef("Charm profile required: current revision %d does not match new revision %d", rev, curl.Revision)
return nil
}
}
sameCharm := local.CharmURL == remote.CharmURL
if haveCharmDir && (!local.Started || sameCharm) {
return nil
}
if !haveCharmDir {
logger.Debugf("start to re-download charm %v because charm dir %q has gone which is usually caused by operator pod re-scheduling", remote.CharmURL, charmDir)
}
if !sameCharm {
logger.Debugf("execute pending upgrade from %s to %s after uniter loop restart", local.CharmURL, remote.CharmURL)
}
op, err := opFunc(remote.CharmURL)
if err != nil {
return errors.Trace(err)
}
if err = ex.Run(op, nil); err != nil {
return errors.Trace(err)
}
if local.Restart {
return ErrRestart
}
return nil
}