-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathregistrar.go
309 lines (255 loc) · 7.95 KB
/
registrar.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
308
309
// 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 registrar
import (
"fmt"
"strings"
"sync"
"time"
"github.com/pkg/errors"
"github.com/elastic/beats/v7/filebeat/input/file"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/libbeat/monitoring"
"github.com/elastic/beats/v7/libbeat/statestore"
"github.com/elastic/beats/v7/libbeat/statestore/backend"
)
type Registrar struct {
log *logp.Logger
// registrar event input and output
Channel chan []file.State
out successLogger
bufferedStateUpdates int
// shutdown handling
done chan struct{}
wg sync.WaitGroup
// state storage
states *file.States // Map with all file paths inside and the corresponding state
store *statestore.Store // Store keeps states in memory and on disk
flushTimeout time.Duration
gcEnabled, gcRequired bool
}
type successLogger interface {
Published(n int) bool
}
type StateStore interface {
Access() (*statestore.Store, error)
}
var (
statesUpdate = monitoring.NewInt(nil, "registrar.states.update")
statesCleanup = monitoring.NewInt(nil, "registrar.states.cleanup")
statesCurrent = monitoring.NewInt(nil, "registrar.states.current")
registryWrites = monitoring.NewInt(nil, "registrar.writes.total")
registryFails = monitoring.NewInt(nil, "registrar.writes.fail")
registrySuccess = monitoring.NewInt(nil, "registrar.writes.success")
)
const fileStatePrefix = "filebeat::logs::"
// New creates a new Registrar instance, updating the registry file on
// `file.State` updates. New fails if the file can not be opened or created.
func New(stateStore StateStore, out successLogger, flushTimeout time.Duration) (*Registrar, error) {
store, err := stateStore.Access()
if err != nil {
return nil, err
}
r := &Registrar{
log: logp.NewLogger("registrar"),
Channel: make(chan []file.State, 1),
out: out,
done: make(chan struct{}),
wg: sync.WaitGroup{},
states: file.NewStates(),
store: store,
flushTimeout: flushTimeout,
}
return r, nil
}
// GetStates return the registrar states
func (r *Registrar) GetStates() []file.State {
return r.states.GetStates()
}
// loadStates fetches the previous reading state from the configure RegistryFile file
// The default file is `registry` in the data path.
func (r *Registrar) loadStates() error {
states, err := readStatesFrom(r.store)
if err != nil {
return errors.Wrap(err, "can not load filebeat registry state")
}
r.states.SetStates(states)
r.log.Infof("States Loaded from registrar: %+v", len(states))
return nil
}
func (r *Registrar) Start() error {
// Load the previous log file locations now, for use in input
err := r.loadStates()
if err != nil {
return fmt.Errorf("error loading state: %v", err)
}
r.wg.Add(1)
go func() {
defer r.wg.Done()
r.Run()
}()
return nil
}
// Stop stops the registry. It waits until Run function finished.
func (r *Registrar) Stop() {
r.log.Info("Stopping Registrar")
defer r.log.Info("Registrar stopped")
close(r.done)
r.wg.Wait()
}
func (r *Registrar) Run() {
r.log.Debug("Starting Registrar")
defer r.log.Debug("Stopping Registrar")
defer r.store.Close()
defer func() {
writeStates(r.store, r.states.GetStates())
}()
var (
timer *time.Timer
flushC <-chan time.Time
directIn chan []file.State
collectIn chan []file.State
)
if r.flushTimeout <= 0 {
directIn = r.Channel
} else {
collectIn = r.Channel
}
for {
select {
case <-r.done:
r.log.Info("Ending Registrar")
return
case states := <-directIn:
// no flush timeout configured. Directly update registry
r.onEvents(states)
r.commitStateUpdates()
case states := <-collectIn:
// flush timeout configured. Only update internal state and track pending
// updates to be written to registry.
r.onEvents(states)
r.gcStates()
if flushC == nil && len(states) > 0 {
timer = time.NewTimer(r.flushTimeout)
flushC = timer.C
}
case <-flushC:
timer.Stop()
r.commitStateUpdates()
flushC = nil
timer = nil
}
}
}
func (r *Registrar) commitStateUpdates() {
// First clean up states
r.gcStates()
states := r.states.GetStates()
statesCurrent.Set(int64(len(states)))
registryWrites.Inc()
if err := writeStates(r.store, states); err != nil {
r.log.Errorf("Error writing registrar state to statestore: %v", err)
registryFails.Inc()
}
r.log.Debugf("Registry file updated. %d active states.", len(states))
registrySuccess.Inc()
if r.out != nil {
r.out.Published(r.bufferedStateUpdates)
}
r.bufferedStateUpdates = 0
}
// onEvents processes events received from the publisher pipeline
func (r *Registrar) onEvents(states []file.State) {
r.processEventStates(states)
r.bufferedStateUpdates += len(states)
// check if we need to enable state cleanup
if !r.gcEnabled {
for i := range states {
if states[i].TTL >= 0 || states[i].Finished {
r.gcEnabled = true
break
}
}
}
r.log.Debugf("Registrar state updates processed. Count: %v", len(states))
// new set of events received -> mark state registry ready for next
// cleanup phase in case gc'able events are stored in the registry.
r.gcRequired = r.gcEnabled
}
// gcStates runs a registry Cleanup. The method check if more event in the
// registry can be gc'ed in the future. If no potential removable state is found,
// the gcEnabled flag is set to false, indicating the current registrar state being
// stable. New registry update events can re-enable state gc'ing.
func (r *Registrar) gcStates() {
if !r.gcRequired {
return
}
beforeCount := r.states.Count()
cleanedStates, pendingClean := r.states.CleanupWith(func(id string) {
// TODO: report error
r.store.Remove(fileStatePrefix + id)
})
statesCleanup.Add(int64(cleanedStates))
r.log.Debugf(
"Registrar states cleaned up. Before: %d, After: %d, Pending: %d",
beforeCount, beforeCount-cleanedStates, pendingClean)
r.gcRequired = false
r.gcEnabled = pendingClean > 0
}
// processEventStates gets the states from the events and writes them to the registrar state
func (r *Registrar) processEventStates(states []file.State) {
r.log.Debugf("Processing %d events", len(states))
ts := time.Now()
for i := range states {
r.states.UpdateWithTs(states[i], ts)
statesUpdate.Add(1)
}
}
func readStatesFrom(store *statestore.Store) ([]file.State, error) {
var states []file.State
err := store.Each(func(key string, dec statestore.ValueDecoder) (bool, error) {
if !strings.HasPrefix(key, fileStatePrefix) {
return true, nil
}
// try to decode. Ingore faulty/incompatible values.
var st file.State
if err := dec.Decode(&st); err != nil {
// XXX: Do we want to log here? In case we start to store other
// state types in the registry, then this operation will likely fail
// quite often, producing some false-positives in the logs...
return true, nil
}
st.Id = key[len(fileStatePrefix):]
states = append(states, st)
return true, nil
})
if err != nil {
return nil, err
}
states = fixStates(states)
states = resetStates(states)
return states, nil
}
func writeStates(store backend.Store, states []file.State) error {
for i := range states {
key := fileStatePrefix + states[i].Id
if err := store.Set(key, states[i]); err != nil {
return err
}
}
return nil
}