-
Notifications
You must be signed in to change notification settings - Fork 527
/
Copy pathreloader.go
214 lines (188 loc) · 6 KB
/
reloader.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package beatcmd
import (
"context"
"errors"
"fmt"
"sync"
"golang.org/x/sync/errgroup"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common/reload"
"github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/logp"
)
// NewRunnerFunc is a function type that constructs a new Runner with the given
// parameters.
type NewRunnerFunc func(RunnerParams) (Runner, error)
// RunnerParams holds parameters that will be passed by Reloader
// to its NewRunnerFunc.
type RunnerParams struct {
// Config holds the full, raw, configuration, including apm-server.*
// and output.* attributes.
Config *config.C
// Info holds information about the APM Server ("beat", for historical
// reasons) process.
Info beat.Info
// Logger holds a logger to use for logging throughout the APM Server.
Logger *logp.Logger
}
// Runner is an interface returned by NewRunnerFunc.
type Runner interface {
// Run runs until its context is cancelled.
Run(context.Context) error
}
// NewReloader returns a new Reloader which creates Runners using the provided
// beat.Info and NewRunnerFunc.
func NewReloader(info beat.Info, newRunner NewRunnerFunc) (*Reloader, error) {
r := &Reloader{
info: info,
logger: logp.NewLogger(""),
newRunner: newRunner,
stopped: make(chan struct{}),
}
if err := reload.RegisterV2.RegisterList(reload.InputRegName, reloadableListFunc(r.reloadInputs)); err != nil {
return nil, fmt.Errorf("failed to register inputs reloader: %w", err)
}
if err := reload.RegisterV2.Register(reload.OutputRegName, reload.ReloadableFunc(r.reloadOutput)); err != nil {
return nil, fmt.Errorf("failed to register output reloader: %w", err)
}
return r, nil
}
// Reloader responds to libbeat configuration changes by calling the given
// NewRunnerFunc to create a new Runner, and then stopping any existing one.
type Reloader struct {
info beat.Info
logger *logp.Logger
newRunner NewRunnerFunc
runner Runner
stopRunner func() error
mu sync.Mutex
inputConfig *config.C
outputConfig *config.C
stopped chan struct{}
}
// Run runs the Reloader, blocking until ctx is cancelled or a fatal error occurs.
//
// Run must be called once and only once.
func (r *Reloader) Run(ctx context.Context) error {
defer close(r.stopped)
<-ctx.Done()
r.mu.Lock()
defer r.mu.Unlock()
if r.runner == nil {
return nil
}
return r.stopRunner()
}
// reloadInput (re)loads input configuration.
//
// Note: reloadInputs may be called before the Reloader is running.
func (r *Reloader) reloadInputs(configs []*reload.ConfigWithMeta) error {
if n := len(configs); n != 1 {
return fmt.Errorf("only 1 input supported, got %d", n)
}
r.mu.Lock()
defer r.mu.Unlock()
cfg := configs[0].Config
// Input configuration is expected to have a monotonically
// increasing revision number.
revision, err := cfg.Int("revision", -1)
if err != nil {
return fmt.Errorf("failed to extract input config revision: %w", err)
}
if err := r.reload(cfg, r.outputConfig); err != nil {
return fmt.Errorf("failed to load input config: %w", err)
}
r.inputConfig = cfg
r.logger.With(logp.Int64("revision", revision)).Info("loaded input config")
return nil
}
// reloadOutput (re)loads output configuration.
//
// Note: reloadOutput may be called before the Reloader is running.
func (r *Reloader) reloadOutput(cfg *reload.ConfigWithMeta) error {
r.mu.Lock()
defer r.mu.Unlock()
if err := r.reload(r.inputConfig, cfg.Config); err != nil {
return fmt.Errorf("failed to load output config: %w", err)
}
r.outputConfig = cfg.Config
r.logger.Info("loaded output config")
return nil
}
func (r *Reloader) reload(inputConfig, outputConfig *config.C) error {
var outputNamespace config.Namespace
if outputConfig != nil {
if err := outputConfig.Unpack(&outputNamespace); err != nil {
return err
}
}
if inputConfig == nil || !outputNamespace.IsSet() {
// Wait until both input and output have been received.
return nil
}
select {
case <-r.stopped:
// The process is shutting down: ignore reloads.
return nil
default:
}
wrappedOutputConfig := config.MustNewConfigFrom(map[string]interface{}{
"output": outputConfig,
})
mergedConfig, err := config.MergeConfigs(inputConfig, wrappedOutputConfig)
if err != nil {
return err
}
// Create a new runner. We separate creation from starting to
// allow the runner to perform initialisations that must run
// synchronously.
newRunner, err := r.newRunner(RunnerParams{
Config: mergedConfig,
Info: r.info,
Logger: r.logger,
})
if err != nil {
return err
}
// Start the new runner.
var g errgroup.Group
ctx, cancel := context.WithCancel(context.Background())
g.Go(func() error {
if err := newRunner.Run(ctx); err != nil && !errors.Is(err, context.Canceled) {
r.logger.With(logp.Error(err)).Error("runner returned with an error")
return err
}
return nil
})
stopRunner := func() error {
cancel()
return g.Wait()
}
// Stop any existing runner.
if r.runner != nil {
_ = r.stopRunner() // logged above
}
r.runner = newRunner
r.stopRunner = stopRunner
return nil
}
type reloadableListFunc func(config []*reload.ConfigWithMeta) error
func (f reloadableListFunc) Reload(configs []*reload.ConfigWithMeta) error {
return f(configs)
}