forked from jbott/gollum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coordinator.go
383 lines (326 loc) · 11.5 KB
/
coordinator.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright 2015-2018 trivago N.V.
//
// Licensed 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 main
import (
"os"
"os/signal"
"reflect"
"sync"
"time"
"github.com/sirupsen/logrus"
"github.com/trivago/gollum/core"
"github.com/trivago/gollum/logger"
"github.com/trivago/tgo"
)
const (
coordinatorStateConfigure = coordinatorState(iota)
coordinatorStateStartProducers = coordinatorState(iota)
coordinatorStateStartConsumers = coordinatorState(iota)
//coordinatorStateRunning = coordinatorState(iota)
coordinatorStateShutdown = coordinatorState(iota)
coordinatorStateStopConsumers = coordinatorState(iota)
coordinatorStateStopProducers = coordinatorState(iota)
coordinatorStateStopped = coordinatorState(iota)
)
const (
signalNone = signalType(iota)
signalExit = signalType(iota)
signalRoll = signalType(iota)
)
type coordinatorState byte
type signalType byte
// Coordinator is the main gollum instance taking care of starting and stopping
// plugins.
type Coordinator struct {
consumers []core.Consumer
producers []core.Producer
routers []core.Router
consumerWorker *sync.WaitGroup
producerWorker *sync.WaitGroup
logConsumer *core.LogConsumer
state coordinatorState
signal chan os.Signal
}
// NewCoordinator creates a new multplexer
func NewCoordinator() Coordinator {
return Coordinator{
consumerWorker: new(sync.WaitGroup),
producerWorker: new(sync.WaitGroup),
state: coordinatorStateConfigure,
}
}
// Configure processes the config and instantiates all valid plugins
func (co *Coordinator) Configure(conf *core.Config) error {
// Make sure the log is printed to the fallback device if we are stuck here
logFallback := time.AfterFunc(time.Duration(3)*time.Second, func() {
logrusHookBuffer.SetTargetWriter(logger.FallbackLogDevice)
logrusHookBuffer.Purge()
})
defer logFallback.Stop()
// Initialize the plugins in the order of routers > producers > consumers
// to match the order of reference between the different types.
errors := tgo.NewErrorStack()
errors.SetFormat(tgo.ErrorStackFormatCSV)
if !co.configureRouters(conf) {
errors.Pushf("At least one router failed to be configured")
}
if !co.configureProducers(conf) {
errors.Pushf("At least one producer failed to be configured")
}
if !co.configureConsumers(conf) {
errors.Pushf("At least one consumer failed to be configured")
}
if len(co.producers) == 0 {
errors.Pushf("No valid producers found")
}
if len(co.consumers) <= 1 {
errors.Pushf("No valid consumers found")
}
// As consumers might create new fallback router this is the first position
// where we can add the wildcard producers to all streams. No new routers
// created beyond this point must use StreamRegistry.AddWildcardProducersToRouter.
core.StreamRegistry.AddAllWildcardProducersToAllRouters()
return errors.OrNil()
}
// StartPlugins starts all plugins in the correct order.
func (co *Coordinator) StartPlugins() {
// Launch routers
for _, router := range co.routers {
logrus.Debug("Starting ", reflect.TypeOf(router))
if err := router.Start(); err != nil {
logrus.WithError(err).Errorf("Failed to start router of type '%s'", reflect.TypeOf(router))
}
}
// Launch producers
co.state = coordinatorStateStartProducers
for _, producer := range co.producers {
producer := producer
go tgo.WithRecoverShutdown(func() {
logrus.Debug("Starting ", reflect.TypeOf(producer))
producer.Produce(co.producerWorker)
})
}
// Set final log target and purge the intermediate buffer
if core.StreamRegistry.IsStreamRegistered(core.LogInternalStreamID) {
// The _GOLLUM_ stream has listeners, so use LogConsumer to write to it
if *flagLogColors == "always" {
logrus.SetFormatter(logger.NewConsoleFormatter())
}
logrusHookBuffer.SetTargetHook(co.logConsumer)
logrusHookBuffer.Purge()
} else {
logrusHookBuffer.SetTargetWriter(logger.FallbackLogDevice)
logrusHookBuffer.Purge()
}
// Launch consumers
co.state = coordinatorStateStartConsumers
for _, consumer := range co.consumers {
consumer := consumer
go tgo.WithRecoverShutdown(func() {
logrus.Debug("Starting ", reflect.TypeOf(consumer))
consumer.Consume(co.consumerWorker)
})
}
}
// Run is essentially the Coordinator main loop.
// It listens for shutdown signals and updates global metrics
func (co *Coordinator) Run() {
co.signal = newSignalHandler()
defer signal.Stop(co.signal)
logrus.Info("We be nice to them, if they be nice to us. (startup)")
for {
sig := <-co.signal
switch translateSignal(sig) {
case signalExit:
logrus.Info("Master betrayed us. Wicked. Tricksy, False. (signal)")
return // ### return, exit requested ###
case signalRoll:
for _, consumer := range co.consumers {
consumer.Control() <- core.PluginControlRoll
}
for _, producer := range co.producers {
producer.Control() <- core.PluginControlRoll
}
default:
}
}
}
// Shutdown all consumers and producers in a clean way.
// The internal log is flushed after the consumers have been shut down so that
// consumer related messages are still in the tlog.
// Producers are flushed after flushing the log, so producer related shutdown
// messages will be posted to stdout
func (co *Coordinator) Shutdown() {
logrus.Info("Filthy little hobbites. They stole it from us. (shutdown)")
stateAtShutdown := co.state
co.state = coordinatorStateShutdown
co.shutdownConsumers(stateAtShutdown)
// Make sure remaining warning / errors are written to stderr
logrus.Info("I'm not listening... I'm not listening... (flushing)")
logrusHookBuffer.SetTargetWriter(logger.FallbackLogDevice)
logrusHookBuffer.SetTargetHook(nil)
logrusHookBuffer.Purge()
// Shutdown producers
co.shutdownProducers(stateAtShutdown)
co.state = coordinatorStateStopped
}
func (co *Coordinator) configureRouters(conf *core.Config) bool {
allFine := true
routerConfigs := conf.GetRouters()
for _, config := range routerConfigs {
if _, hasStreams := config.Settings.Value("Stream"); !hasStreams {
logrus.Errorf("Router '%s' has no stream set", config.ID)
allFine = false
continue // ### continue ###
}
logrus.Debugf("Instantiating router '%s'", config.ID)
plugin, err := core.NewPluginWithConfig(config)
if err != nil {
logrus.WithError(err).Errorf("Failed to instantiate router '%s'", config.ID)
allFine = false
continue // ### continue ###
}
routerPlugin := plugin.(core.Router)
co.routers = append(co.routers, routerPlugin)
logrus.Debugf("Instantiated '%s' (%s) as '%s'", config.ID, core.StreamRegistry.GetStreamName(routerPlugin.GetStreamID()), config.Typename)
core.StreamRegistry.Register(routerPlugin, routerPlugin.GetStreamID())
}
return allFine
}
func (co *Coordinator) configureProducers(conf *core.Config) bool {
co.state = coordinatorStateStartProducers
allFine := true
// All producers are added to the wildcard stream so that consumers can send
// to all producers if required. The wildcard producer list is required
// to add producers listening to all routers to all streams that are used.
wildcardStream := core.StreamRegistry.GetRouterOrFallback(core.WildcardStreamID)
producerConfigs := conf.GetProducers()
for _, config := range producerConfigs {
if _, hasStreams := config.Settings.Value("Streams"); !hasStreams {
logrus.Errorf("Producer '%s' has no streams set", config.ID)
allFine = false
continue // ### continue ###
}
logrus.Debug("Instantiating ", config.ID)
plugin, err := core.NewPluginWithConfig(config)
if err != nil {
logrus.WithError(err).Errorf("Failed to instantiate producer '%s'", config.ID)
allFine = false
continue // ### continue ###
}
producer, _ := plugin.(core.Producer)
co.producers = append(co.producers, producer)
core.MetricProducers.Inc(1)
// Attach producer to streams
streams := producer.Streams()
for _, streamID := range streams {
if streamID == core.WildcardStreamID {
core.StreamRegistry.RegisterWildcardProducer(producer)
} else {
router := core.StreamRegistry.GetRouterOrFallback(streamID)
router.AddProducer(producer)
}
}
// Add producer to wildcard stream unless it only listens to internal streams
searchinternal:
for _, streamID := range streams {
switch streamID {
case core.LogInternalStreamID:
default:
wildcardStream.AddProducer(producer)
break searchinternal
}
}
}
return allFine
}
func (co *Coordinator) configureConsumers(conf *core.Config) bool {
co.state = coordinatorStateStartConsumers
allFine := co.configureLogConsumer()
consumerConfigs := conf.GetConsumers()
for _, config := range consumerConfigs {
if _, hasStreams := config.Settings.Value("Streams"); !hasStreams {
logrus.Errorf("Consumer '%s' has no streams set", config.ID)
allFine = false
continue
}
logrus.Debug("Instantiating ", config.ID)
plugin, err := core.NewPluginWithConfig(config)
if err != nil {
logrus.WithError(err).Errorf("Failed to instantiate producer '%s'", config.ID)
allFine = false
continue // ### continue ###
}
consumer, _ := plugin.(core.Consumer)
co.consumers = append(co.consumers, consumer)
core.MetricConsumers.Inc(1)
}
return allFine
}
func (co *Coordinator) configureLogConsumer() bool {
config := core.NewPluginConfig("", "core.LogConsumer")
configReader := core.NewPluginConfigReader(&config)
co.logConsumer = new(core.LogConsumer)
co.logConsumer.Configure(configReader)
if configReader.Errors.Len() == 0 {
co.consumers = append(co.consumers, co.logConsumer)
return true
}
return false
}
func (co *Coordinator) shutdownConsumers(stateAtShutdown coordinatorState) {
if stateAtShutdown >= coordinatorStateStartConsumers {
co.state = coordinatorStateStopConsumers
waitTimeout := time.Duration(0)
logrus.Debug("Telling consumers to stop")
for _, cons := range co.consumers {
timeout := cons.GetShutdownTimeout()
if timeout > waitTimeout {
waitTimeout = timeout
}
// Skip log consumer so we get clean log messages for all consumers
if cons != co.logConsumer {
cons.Control() <- core.PluginControlStopConsumer
}
}
waitTimeout *= 10
logrus.Debugf("Waiting for consumers to stop. Forced shutdown after %.2f seconds.", waitTimeout.Seconds())
if co.logConsumer != nil {
co.logConsumer.Control() <- core.PluginControlStopConsumer
// No logs in _GOLLUM_ after this point
}
if !tgo.ReturnAfter(waitTimeout, co.consumerWorker.Wait) {
logrus.Error("At least one consumer found to be blocking.")
}
}
}
func (co *Coordinator) shutdownProducers(stateAtShutdown coordinatorState) {
if stateAtShutdown >= coordinatorStateStartProducers {
co.state = coordinatorStateStopProducers
waitTimeout := time.Duration(0)
logrus.Debug("Telling producers to stop")
for _, prod := range co.producers {
timeout := prod.GetShutdownTimeout()
if timeout > waitTimeout {
waitTimeout = timeout
}
prod.Control() <- core.PluginControlStopProducer
}
waitTimeout *= 10
logrus.Debugf("Waiting for producers to stop. Forced shutdown after %.2f seconds.", waitTimeout.Seconds())
if !tgo.ReturnAfter(waitTimeout, co.producerWorker.Wait) {
logrus.Error("At least one producer found to be blocking.")
}
}
}