forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifold.go
307 lines (268 loc) · 7.51 KB
/
manifold.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"sync"
"time"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/catacomb"
"github.com/juju/worker/v3/dependency"
"gopkg.in/tomb.v2"
coreagent "github.com/DavinZhang/juju/agent"
"github.com/DavinZhang/juju/state"
"github.com/DavinZhang/juju/wrench"
)
var logger = loggo.GetLogger("juju.worker.state")
// ManifoldConfig provides the dependencies for Manifold.
type ManifoldConfig struct {
AgentName string
StateConfigWatcherName string
OpenStatePool func(coreagent.Config) (*state.StatePool, error)
PingInterval time.Duration
// SetStatePool is called with the state pool when it is created,
// and called again with nil just before the state pool is closed.
// This is used for publishing the state pool to the agent's
// introspection worker, which runs outside of the dependency
// engine; hence the manifold's Output cannot be relied upon.
SetStatePool func(*state.StatePool)
}
// Validate validates the manifold configuration.
func (config ManifoldConfig) Validate() error {
if config.AgentName == "" {
return errors.NotValidf("empty AgentName")
}
if config.StateConfigWatcherName == "" {
return errors.NotValidf("empty StateConfigWatcherName")
}
if config.OpenStatePool == nil {
return errors.NotValidf("nil OpenStatePool")
}
if config.SetStatePool == nil {
return errors.NotValidf("nil SetStatePool")
}
return nil
}
const defaultPingInterval = 15 * time.Second
// Manifold returns a manifold whose worker which wraps a
// *state.State, which is in turn wrapper by a StateTracker. It will
// exit if the State's associated mongodb session dies.
func Manifold(config ManifoldConfig) dependency.Manifold {
return dependency.Manifold{
Inputs: []string{
config.AgentName,
config.StateConfigWatcherName,
},
Start: func(context dependency.Context) (worker.Worker, error) {
if err := config.Validate(); err != nil {
return nil, errors.Trace(err)
}
// Get the agent.
var agent coreagent.Agent
if err := context.Get(config.AgentName, &agent); err != nil {
return nil, err
}
// Confirm we're running in a state server by asking the
// stateconfigwatcher manifold.
var haveStateConfig bool
if err := context.Get(config.StateConfigWatcherName, &haveStateConfig); err != nil {
return nil, err
}
if !haveStateConfig {
return nil, errors.Annotate(dependency.ErrMissing, "no StateServingInfo in config")
}
pool, err := config.OpenStatePool(agent.CurrentConfig())
if err != nil {
return nil, errors.Trace(err)
}
stTracker := newStateTracker(pool)
pingInterval := config.PingInterval
if pingInterval == 0 {
pingInterval = defaultPingInterval
}
w := &stateWorker{
stTracker: stTracker,
pingInterval: pingInterval,
setStatePool: config.SetStatePool,
}
if err := catacomb.Invoke(catacomb.Plan{
Site: &w.catacomb,
Work: w.loop,
}); err != nil {
if err := stTracker.Done(); err != nil {
logger.Warningf("error releasing state: %v", err)
}
return nil, errors.Trace(err)
}
return w, nil
},
Output: outputFunc,
}
}
// outputFunc extracts a *StateTracker from a *stateWorker.
func outputFunc(in worker.Worker, out interface{}) error {
inWorker, _ := in.(*stateWorker)
if inWorker == nil {
return errors.Errorf("in should be a %T; got %T", inWorker, in)
}
switch outPointer := out.(type) {
case *StateTracker:
*outPointer = inWorker.stTracker
default:
return errors.Errorf("out should be *StateTracker; got %T", out)
}
return nil
}
type stateWorker struct {
catacomb catacomb.Catacomb
stTracker StateTracker
pingInterval time.Duration
setStatePool func(*state.StatePool)
cleanupOnce sync.Once
}
func (w *stateWorker) loop() error {
pool, err := w.stTracker.Use()
if err != nil {
return errors.Trace(err)
}
defer func() { _ = w.stTracker.Done() }()
systemState := pool.SystemState()
w.setStatePool(pool)
defer w.setStatePool(nil)
modelWatcher := systemState.WatchModelLives()
_ = w.catacomb.Add(modelWatcher)
wrenchTimer := time.NewTimer(30 * time.Second)
defer wrenchTimer.Stop()
modelStateWorkers := make(map[string]worker.Worker)
for {
select {
case <-w.catacomb.Dying():
return w.catacomb.ErrDying()
case modelUUIDs := <-modelWatcher.Changes():
for _, modelUUID := range modelUUIDs {
if err := w.processModelLifeChange(
modelUUID,
modelStateWorkers,
pool,
); err != nil {
return errors.Trace(err)
}
}
// Useful for tracking down some bugs that occur when
// mongo is overloaded.
case <-wrenchTimer.C:
if wrench.IsActive("state-worker", "io-timeout") {
return errors.Errorf("wrench simulating i/o timeout!")
}
wrenchTimer.Reset(30 * time.Second)
}
}
}
// Report conforms to the Dependency Engine Report() interface, giving an opportunity to introspect
// what is going on at runtime.
func (w *stateWorker) Report() map[string]interface{} {
return w.stTracker.Report()
}
func (w *stateWorker) processModelLifeChange(
modelUUID string,
modelStateWorkers map[string]worker.Worker,
pool *state.StatePool,
) error {
remove := func() {
if w, ok := modelStateWorkers[modelUUID]; ok {
w.Kill()
delete(modelStateWorkers, modelUUID)
}
_, _ = pool.Remove(modelUUID)
}
model, hp, err := pool.GetModel(modelUUID)
if err != nil {
if errors.IsNotFound(err) {
// Model has been removed from state.
logger.Debugf("model %q removed from state", modelUUID)
remove()
return nil
}
return errors.Trace(err)
}
defer hp.Release()
if model.Life() == state.Dead {
// Model is Dead, and will soon be removed from state.
logger.Debugf("model %q is dead", modelUUID)
remove()
return nil
}
if modelStateWorkers[modelUUID] == nil {
mw := newModelStateWorker(pool, modelUUID, w.pingInterval)
modelStateWorkers[modelUUID] = mw
_ = w.catacomb.Add(mw)
}
return nil
}
// Kill is part of the worker.Worker interface.
func (w *stateWorker) Kill() {
w.catacomb.Kill(nil)
}
// Wait is part of the worker.Worker interface.
func (w *stateWorker) Wait() error {
err := w.catacomb.Wait()
w.cleanupOnce.Do(func() {
// Make sure the worker has exited before closing state.
if err := w.stTracker.Done(); err != nil {
logger.Warningf("error releasing state: %v", err)
}
})
return err
}
type modelStateWorker struct {
tomb tomb.Tomb
pool *state.StatePool
modelUUID string
pingInterval time.Duration
}
func newModelStateWorker(
pool *state.StatePool,
modelUUID string,
pingInterval time.Duration,
) worker.Worker {
w := &modelStateWorker{
pool: pool,
modelUUID: modelUUID,
pingInterval: pingInterval,
}
w.tomb.Go(w.loop)
return w
}
func (w *modelStateWorker) loop() error {
st, err := w.pool.Get(w.modelUUID)
if err != nil {
if errors.IsNotFound(err) {
// ignore not found error here, because the pooledState has already been removed.
return nil
}
return errors.Trace(err)
}
defer func() {
st.Release()
_, _ = w.pool.Remove(w.modelUUID)
}()
for {
select {
case <-w.tomb.Dying():
return tomb.ErrDying
case <-time.After(w.pingInterval):
if err := st.Ping(); err != nil {
return errors.Annotate(err, "state ping failed")
}
}
}
}
// Kill is part of the worker.Worker interface.
func (w *modelStateWorker) Kill() {
w.tomb.Kill(nil)
}
// Wait is part of the worker.Worker interface.
func (w *modelStateWorker) Wait() error {
return w.tomb.Wait()
}