forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reload.go
295 lines (239 loc) · 6.58 KB
/
reload.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
package cfgfile
import (
"path/filepath"
"sync"
"time"
"github.com/joeshaw/multierror"
"github.com/mitchellh/hashstructure"
"github.com/pkg/errors"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/cfgwarn"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/monitoring"
"github.com/elastic/beats/libbeat/paths"
)
var (
DefaultDynamicConfig = DynamicConfig{
Reload: Reload{
Period: 10 * time.Second,
Enabled: false,
},
}
debugf = logp.MakeDebug("cfgfile")
configReloads = monitoring.NewInt(nil, "libbeat.config.reloads")
moduleStarts = monitoring.NewInt(nil, "libbeat.config.module.starts")
moduleStops = monitoring.NewInt(nil, "libbeat.config.module.stops")
moduleRunning = monitoring.NewInt(nil, "libbeat.config.module.running")
)
// DynamicConfig loads config files from a given path, allowing to reload new changes
// while running the beat
type DynamicConfig struct {
// If path is a relative path, it is relative to the ${path.config}
Path string `config:"path"`
Reload Reload `config:"reload"`
}
type Reload struct {
Period time.Duration `config:"period"`
Enabled bool `config:"enabled"`
}
type RunnerFactory interface {
Create(config *common.Config, meta *common.MapStrPointer) (Runner, error)
}
type Runner interface {
Start()
Stop()
}
// Reloader is used to register and reload modules
type Reloader struct {
registry *Registry
config DynamicConfig
path string
done chan struct{}
wg sync.WaitGroup
}
// NewReloader creates new Reloader instance for the given config
func NewReloader(cfg *common.Config) *Reloader {
config := DefaultDynamicConfig
cfg.Unpack(&config)
path := config.Path
if !filepath.IsAbs(path) {
path = paths.Resolve(paths.Config, path)
}
if config.Reload.Enabled {
cfgwarn.Beta("Dynamic config reload is enabled.")
}
return &Reloader{
registry: NewRegistry(),
config: config,
path: path,
done: make(chan struct{}),
}
}
// Check configs are valid (only if reload is disabled)
func (rl *Reloader) Check(runnerFactory RunnerFactory) error {
// If config reload is enabled we ignore errors (as they may be fixed afterwards)
if rl.config.Reload.Enabled {
return nil
}
debugf("Checking module configs from: %s", rl.path)
gw := NewGlobWatcher(rl.path)
files, _, err := gw.Scan()
if err != nil {
return errors.Wrap(err, "fetching config files")
}
// Load all config objects
configs, err := rl.loadConfigs(files)
if err != nil {
return err
}
debugf("Number of module configs found: %v", len(configs))
// Initialize modules
for _, c := range configs {
// Only add configs to startList which are enabled
if !c.Enabled() {
continue
}
_, err := runnerFactory.Create(c, nil)
if err != nil {
return err
}
}
return nil
}
// Run runs the reloader
func (rl *Reloader) Run(runnerFactory RunnerFactory) {
logp.Info("Config reloader started")
rl.wg.Add(1)
defer rl.wg.Done()
// Stop all running modules when method finishes
defer rl.stopRunners(rl.registry.CopyList())
gw := NewGlobWatcher(rl.path)
// If reloading is disable, config files should be loaded immediately
if !rl.config.Reload.Enabled {
rl.config.Reload.Period = 0
}
overwriteUpdate := true
for {
select {
case <-rl.done:
logp.Info("Dynamic config reloader stopped")
return
case <-time.After(rl.config.Reload.Period):
debugf("Scan for new config files")
configReloads.Add(1)
files, updated, err := gw.Scan()
if err != nil {
// In most cases of error, updated == false, so will continue
// to next iteration below
logp.Err("Error fetching new config files: %v", err)
}
// no file changes
if !updated && !overwriteUpdate {
overwriteUpdate = false
continue
}
// Load all config objects
configs, _ := rl.loadConfigs(files)
debugf("Number of module configs found: %v", len(configs))
startList := map[uint64]Runner{}
stopList := rl.registry.CopyList()
for _, c := range configs {
// Only add configs to startList which are enabled
if !c.Enabled() {
continue
}
rawCfg := map[string]interface{}{}
err := c.Unpack(rawCfg)
if err != nil {
logp.Err("Unable to unpack config file due to error: %v", err)
continue
}
hash, err := hashstructure.Hash(rawCfg, nil)
if err != nil {
// Make sure the next run also updates because some runners were not properly loaded
overwriteUpdate = true
debugf("Unable to generate hash for config file %v due to error: %v", c, err)
continue
}
debugf("Remove module from stoplist: %v", hash)
delete(stopList, hash)
// As module already exist, it must be removed from the stop list and not started
if !rl.registry.Has(hash) {
debugf("Add module to startlist: %v", hash)
runner, err := runnerFactory.Create(c, nil)
if err != nil {
logp.Err("Unable to create runner due to error: %v", err)
continue
}
startList[hash] = runner
}
}
rl.stopRunners(stopList)
rl.startRunners(startList)
}
// Path loading is enabled but not reloading. Loads files only once and then stops.
if !rl.config.Reload.Enabled {
logp.Info("Loading of config files completed.")
select {
case <-rl.done:
logp.Info("Dynamic config reloader stopped")
return
}
}
}
}
func (rl *Reloader) loadConfigs(files []string) ([]*common.Config, error) {
// Load all config objects
configs := []*common.Config{}
var errs multierror.Errors
for _, file := range files {
c, err := LoadList(file)
if err != nil {
errs = append(errs, err)
logp.Err("Error loading config: %s", err)
continue
}
configs = append(configs, c...)
}
return configs, errs.Err()
}
// Stop stops the reloader and waits for all modules to properly stop
func (rl *Reloader) Stop() {
close(rl.done)
rl.wg.Wait()
}
func (rl *Reloader) startRunners(list map[uint64]Runner) {
if len(list) == 0 {
return
}
logp.Info("Starting %v runners ...", len(list))
for id, runner := range list {
runner.Start()
rl.registry.Add(id, runner)
moduleStarts.Add(1)
moduleRunning.Add(1)
debugf("New runner started: %v", id)
}
}
func (rl *Reloader) stopRunners(list map[uint64]Runner) {
if len(list) == 0 {
return
}
logp.Info("Stopping %v runners ...", len(list))
wg := sync.WaitGroup{}
for hash, runner := range list {
wg.Add(1)
// Stop modules in parallel
go func(h uint64, run Runner) {
defer func() {
moduleStops.Add(1)
moduleRunning.Add(-1)
debugf("Runner stopped: %v", h)
wg.Done()
}()
run.Stop()
rl.registry.Remove(h)
}(hash, runner)
}
wg.Wait()
}