forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modelworkermanager.go
264 lines (235 loc) · 6.91 KB
/
modelworkermanager.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package modelworkermanager
import (
"fmt"
"time"
"github.com/juju/loggo"
"github.com/juju/clock"
"github.com/juju/errors"
"gopkg.in/juju/names.v3"
"gopkg.in/juju/worker.v1"
"gopkg.in/juju/worker.v1/catacomb"
"github.com/juju/juju/controller"
"github.com/juju/juju/state"
)
// ModelWatcher provides an interface for watching the additiona and
// removal of models.
type ModelWatcher interface {
WatchModels() state.StringsWatcher
}
// Controller provides an interface for getting models by UUID,
// and other details needed to pass into the function to start workers for a model.
// Once a model is no longer required, the returned function must
// be called to dispose of the model.
type Controller interface {
Config() (controller.Config, error)
Model(modelUUID string) (Model, func(), error)
DBLogger(modelUUID string) (DBLogger, error)
}
// Model represents a model.
type Model interface {
MigrationMode() state.MigrationMode
Type() state.ModelType
Name() string
Owner() names.UserTag
}
// DBLogger writes into the log collections.
type DBLogger interface {
// Log writes the given log records to the logger's storage.
Log([]state.LogRecord) error
Close()
}
// ModelLogger is a database backed loggo Writer.
type ModelLogger interface {
loggo.Writer
Close() error
}
// NewModelConfig holds the information required by the NewModelWorkerFunc
// to start the workers for the specified model
type NewModelConfig struct {
ModelName string // Use a fully qualified name "<namespace>-<name>"
ModelUUID string
ModelType state.ModelType
ModelLogger ModelLogger
ControllerConfig controller.Config
}
// NewModelWorkerFunc should return a worker responsible for running
// all a model's required workers; and for returning nil when there's
// no more model to manage.
type NewModelWorkerFunc func(config NewModelConfig) (worker.Worker, error)
// Config holds the dependencies and configuration necessary to run
// a model worker manager.
type Config struct {
Clock clock.Clock
Logger Logger
MachineID string
ModelWatcher ModelWatcher
Controller Controller
NewModelWorker NewModelWorkerFunc
ErrorDelay time.Duration
}
// Validate returns an error if config cannot be expected to drive
// a functional model worker manager.
func (config Config) Validate() error {
if config.Clock == nil {
return errors.NotValidf("nil Clock")
}
if config.Logger == nil {
return errors.NotValidf("nil Logger")
}
if config.MachineID == "" {
return errors.NotValidf("empty MachineID")
}
if config.ModelWatcher == nil {
return errors.NotValidf("nil ModelWatcher")
}
if config.Controller == nil {
return errors.NotValidf("nil Controller")
}
if config.NewModelWorker == nil {
return errors.NotValidf("nil NewModelWorker")
}
if config.ErrorDelay <= 0 {
return errors.NotValidf("non-positive ErrorDelay")
}
return nil
}
// New starts a new model worker manager.
func New(config Config) (worker.Worker, error) {
if err := config.Validate(); err != nil {
return nil, errors.Trace(err)
}
m := &modelWorkerManager{
config: config,
}
err := catacomb.Invoke(catacomb.Plan{
Site: &m.catacomb,
Work: m.loop,
})
if err != nil {
return nil, errors.Trace(err)
}
return m, nil
}
type modelWorkerManager struct {
catacomb catacomb.Catacomb
config Config
runner *worker.Runner
}
// Kill satisfies the Worker interface.
func (m *modelWorkerManager) Kill() {
m.catacomb.Kill(nil)
}
// Wait satisfies the Worker interface.
func (m *modelWorkerManager) Wait() error {
return m.catacomb.Wait()
}
func (m *modelWorkerManager) loop() error {
controllerConfig, err := m.config.Controller.Config()
if err != nil {
return errors.Annotate(err, "unable to get controller config")
}
m.runner = worker.NewRunner(worker.RunnerParams{
IsFatal: neverFatal,
MoreImportant: neverImportant,
RestartDelay: m.config.ErrorDelay,
})
if err := m.catacomb.Add(m.runner); err != nil {
return errors.Trace(err)
}
watcher := m.config.ModelWatcher.WatchModels()
if err := m.catacomb.Add(watcher); err != nil {
return errors.Trace(err)
}
modelChanged := func(modelUUID string) error {
model, release, err := m.config.Controller.Model(modelUUID)
if errors.IsNotFound(err) {
// Model was removed, ignore it.
// The reason we ignore it here is that one of the embedded
// workers is also responding to the model life changes and
// when it returns a NotFound error, which is determined as a
// fatal error for the model worker engine. This causes it to be
// removed from the runner above. However since the runner itself
// has neverFatal as an error handler, the runner itself doesn't
// propagate the error.
return nil
} else if err != nil {
return errors.Trace(err)
}
defer release()
if !isModelActive(model) {
// Ignore this model until it's activated - we
// never want to run workers for an importing
// model.
// https://bugs.launchpad.net/juju/+bug/1646310
return nil
}
cfg := NewModelConfig{
ModelName: fmt.Sprintf("%s-%s", model.Owner().Id(), model.Name()),
ModelUUID: modelUUID,
ModelType: model.Type(),
ControllerConfig: controllerConfig,
}
return errors.Trace(m.ensure(cfg))
}
for {
select {
case <-m.catacomb.Dying():
return m.catacomb.ErrDying()
case uuids, ok := <-watcher.Changes():
if !ok {
return errors.New("changes stopped")
}
for _, modelUUID := range uuids {
if err := modelChanged(modelUUID); err != nil {
return errors.Trace(err)
}
}
}
}
}
func (m *modelWorkerManager) ensure(cfg NewModelConfig) error {
starter := m.starter(cfg)
if err := m.runner.StartWorker(cfg.ModelUUID, starter); err != nil {
return errors.Trace(err)
}
return nil
}
func (m *modelWorkerManager) starter(cfg NewModelConfig) func() (worker.Worker, error) {
return func() (worker.Worker, error) {
modelUUID := cfg.ModelUUID
modelName := fmt.Sprintf("%q (%s)", cfg.ModelName, cfg.ModelUUID)
m.config.Logger.Debugf("starting workers for model %s", modelName)
dbLogger, err := m.config.Controller.DBLogger(modelUUID)
if err != nil {
return nil, errors.Annotatef(err, "unable to create db logger for %s", modelName)
}
cfg.ModelLogger = newModelLogger(
"controller-"+m.config.MachineID,
modelUUID,
dbLogger,
m.config.Clock,
m.config.Logger,
)
worker, err := m.config.NewModelWorker(cfg)
if err != nil {
cfg.ModelLogger.Close()
return nil, errors.Annotatef(err, "cannot manage model %s", modelName)
}
return worker, nil
}
}
func neverFatal(error) bool {
return false
}
func neverImportant(error, error) bool {
return false
}
func isModelActive(m Model) bool {
return m.MigrationMode() != state.MigrationModeImporting
}
// Report shows up in the dependency engine report.
func (m *modelWorkerManager) Report() map[string]interface{} {
return m.runner.Report()
}