-
Notifications
You must be signed in to change notification settings - Fork 234
/
input_box.go
162 lines (136 loc) · 4.04 KB
/
input_box.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
package input
import (
"reflect"
"sync"
"github.com/childe/gohangout/field_setter"
"github.com/childe/gohangout/filter"
"github.com/childe/gohangout/output"
"github.com/childe/gohangout/topology"
"github.com/childe/gohangout/value_render"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
)
type InputBox struct {
config map[string]interface{} // whole config
input topology.Input
outputsInAllWorker [][]*topology.OutputBox
stop bool
once sync.Once
shutdownChan chan bool
promCounter prometheus.Counter
shutdownWhenNil bool
mainThreadExitChan chan struct{}
addFields map[field_setter.FieldSetter]value_render.ValueRender
}
// SetShutdownWhenNil is used for benchmark.
// Gohangout main thread would exit when one input box receive a nil message, such as Ctrl-D in Stdin input
func (box *InputBox) SetShutdownWhenNil(shutdownWhenNil bool) {
box.shutdownWhenNil = shutdownWhenNil
}
func NewInputBox(input topology.Input, inputConfig map[interface{}]interface{}, config map[string]interface{}, mainThreadExitChan chan struct{}) *InputBox {
b := &InputBox{
input: input,
config: config,
stop: false,
shutdownChan: make(chan bool, 1),
promCounter: topology.GetPromCounter(inputConfig),
mainThreadExitChan: mainThreadExitChan,
}
if add_fields, ok := inputConfig["add_fields"]; ok {
b.addFields = make(map[field_setter.FieldSetter]value_render.ValueRender)
for k, v := range add_fields.(map[interface{}]interface{}) {
fieldSetter := field_setter.NewFieldSetter(k.(string))
if fieldSetter == nil {
glog.Errorf("could build field setter from %s", k.(string))
return nil
}
b.addFields[fieldSetter] = value_render.GetValueRender(v.(string))
}
} else {
b.addFields = nil
}
return b
}
func (box *InputBox) beat(workerIdx int) {
var firstNode *topology.ProcessorNode = box.buildTopology(workerIdx)
var (
event map[string]interface{}
)
for !box.stop {
event = box.input.ReadOneEvent()
if box.promCounter != nil {
box.promCounter.Inc()
}
if event == nil {
glog.V(5).Info("received nil message.")
if box.stop {
break
}
if box.shutdownWhenNil {
glog.Info("received nil message. shutdown...")
box.mainThreadExitChan <- struct{}{}
break
} else {
continue
}
}
for fs, v := range box.addFields {
event = fs.SetField(event, v.Render(event), "", false)
}
firstNode.Process(event)
}
}
func (box *InputBox) buildTopology(workerIdx int) *topology.ProcessorNode {
outputs := topology.BuildOutputs(box.config, output.BuildOutput)
box.outputsInAllWorker[workerIdx] = outputs
var outputProcessor topology.Processor
if len(outputs) == 1 {
outputProcessor = outputs[0]
} else {
outputProcessor = (topology.OutputsProcessor)(outputs)
}
filterBoxes := topology.BuildFilterBoxes(box.config, filter.BuildFilter)
var firstNode *topology.ProcessorNode
for _, b := range filterBoxes {
firstNode = topology.AppendProcessorsToLink(firstNode, b)
}
firstNode = topology.AppendProcessorsToLink(firstNode, outputProcessor)
// Set BelongTo
var node *topology.ProcessorNode
node = firstNode
for _, b := range filterBoxes {
node = node.Next
v := reflect.ValueOf(b.Filter)
f := v.MethodByName("SetBelongTo")
if f.IsValid() {
f.Call([]reflect.Value{reflect.ValueOf(node)})
}
}
return firstNode
}
// Beat starts the processors and wait until shutdown
func (box *InputBox) Beat(worker int) {
box.outputsInAllWorker = make([][]*topology.OutputBox, worker)
for i := 0; i < worker; i++ {
go box.beat(i)
}
<-box.shutdownChan
}
func (box *InputBox) shutdown() {
box.once.Do(func() {
glog.Infof("try to shutdown input %T", box.input)
box.input.Shutdown()
for i, outputs := range box.outputsInAllWorker {
for _, o := range outputs {
glog.Infof("try to shutdown output %T in worker %d", o, i)
o.Output.Shutdown()
}
}
})
box.shutdownChan <- true
}
// Shutdown shutdowns the inputs and outputs
func (box *InputBox) Shutdown() {
box.shutdown()
box.stop = true
}