forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reload.go
233 lines (188 loc) · 5.17 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
package cfgfile
import (
"path/filepath"
"sync"
"time"
"github.com/elastic/beats/libbeat/common"
"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(*common.Config) (Runner, error)
}
type Runner interface {
Start()
Stop()
ID() uint64
}
// Reloader is used to register and reload modules
type Reloader struct {
registry *Registry
config DynamicConfig
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)
return &Reloader{
registry: NewRegistry(),
config: config,
done: make(chan struct{}),
}
}
// 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())
path := rl.config.Path
if !filepath.IsAbs(path) {
path = paths.Resolve(paths.Config, path)
}
gw := NewGlobWatcher(path)
// If reloading is disable, config files should be loaded immidiately
if !rl.config.Reload.Enabled {
rl.config.Reload.Period = 0
}
overwriteUpate := 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 && !overwriteUpate {
overwriteUpate = false
continue
}
// Load all config objects
configs := []*common.Config{}
for _, file := range files {
c, err := LoadList(file)
if err != nil {
logp.Err("Error loading config: %s", err)
continue
}
configs = append(configs, c...)
}
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
}
runner, err := runnerFactory.Create(c)
if err != nil {
// Make sure the next run also updates because some runners were not properly loaded
overwriteUpate = true
// In case prospector already is running, do not stop it
if runner != nil && rl.registry.Has(runner.ID()) {
debugf("Remove module from stoplist: %v", runner.ID())
delete(stopList, runner.ID())
}
logp.Err("Error creating module: %s", err)
continue
}
debugf("Remove module from stoplist: %v", runner.ID())
delete(stopList, runner.ID())
// As module already exist, it must be removed from the stop list and not started
if !rl.registry.Has(runner.ID()) {
debugf("Add module to startlist: %v", runner.ID())
startList[runner.ID()] = runner
continue
}
}
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
}
}
}
}
// 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()
}