forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_fan_in.go
281 lines (244 loc) · 7.94 KB
/
dynamic_fan_in.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
// Copyright (c) 2018 Ashley Jeffs
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package broker
import (
"sync/atomic"
"time"
"github.com/Jeffail/benthos/lib/types"
"github.com/Jeffail/benthos/lib/util/service/log"
"github.com/Jeffail/benthos/lib/util/service/metrics"
)
//------------------------------------------------------------------------------
// DynamicInput is an interface of input types that must be closable.
type DynamicInput interface {
types.Transactor
types.Closable
}
// wrappedInput is a struct that wraps a DynamicInput with an identifying name.
type wrappedInput struct {
Name string
Input DynamicInput
Timeout time.Duration
ResChan chan<- error
}
//------------------------------------------------------------------------------
// DynamicFanIn is a broker that implements types.Producer and manages a map of
// inputs to unique string identifiers, routing them through a single message
// channel. Inputs can be added and removed dynamically as the broker runs.
type DynamicFanIn struct {
running int32
stats metrics.Type
log log.Modular
transactionChan chan types.Transaction
internalTsChan chan types.Transaction
onAdd func(label string)
onRemove func(label string)
newInputChan chan wrappedInput
inputs map[string]DynamicInput
inputClosedChans map[string]chan struct{}
closedChan chan struct{}
closeChan chan struct{}
}
// NewDynamicFanIn creates a new DynamicFanIn type by providing an initial map
// map of inputs.
func NewDynamicFanIn(
inputs map[string]DynamicInput,
logger log.Modular,
stats metrics.Type,
options ...func(*DynamicFanIn),
) (*DynamicFanIn, error) {
d := &DynamicFanIn{
running: 1,
stats: stats,
log: logger,
transactionChan: make(chan types.Transaction),
onAdd: func(l string) {},
onRemove: func(l string) {},
newInputChan: make(chan wrappedInput),
inputs: make(map[string]DynamicInput),
inputClosedChans: make(map[string]chan struct{}),
closedChan: make(chan struct{}),
closeChan: make(chan struct{}),
}
for _, opt := range options {
opt(d)
}
for key, input := range inputs {
if err := d.addInput(key, input); err != nil {
d.stats.Incr("broker.dynamic_fan_in.input.add.error", 1)
d.log.Errorf("Failed to start new dynamic input '%v': %v\n", key, err)
}
}
go d.managerLoop()
return d, nil
}
// SetInput attempts to add a new input to the dynamic input broker. If an input
// already exists with the same identifier it will be closed and removed. If
// either action takes longer than the timeout period an error will be returned.
//
// A nil input is safe and will simply remove the previous input under the
// indentifier, if there was one.
func (d *DynamicFanIn) SetInput(ident string, input DynamicInput, timeout time.Duration) error {
if atomic.LoadInt32(&d.running) != 1 {
return types.ErrTypeClosed
}
resChan := make(chan error)
select {
case d.newInputChan <- wrappedInput{
Name: ident,
Input: input,
ResChan: resChan,
Timeout: timeout,
}:
case <-d.closeChan:
return types.ErrTypeClosed
}
return <-resChan
}
// TransactionChan returns the channel used for consuming messages from this
// broker.
func (d *DynamicFanIn) TransactionChan() <-chan types.Transaction {
return d.transactionChan
}
//------------------------------------------------------------------------------
// OptDynamicFanInSetOnAdd sets the function that is called whenever a dynamic
// input is added.
func OptDynamicFanInSetOnAdd(onAddFunc func(label string)) func(*DynamicFanIn) {
return func(d *DynamicFanIn) {
d.onAdd = onAddFunc
}
}
// OptDynamicFanInSetOnRemove sets the function that is called whenever a
// dynamic input is removed.
func OptDynamicFanInSetOnRemove(onRemoveFunc func(label string)) func(*DynamicFanIn) {
return func(d *DynamicFanIn) {
d.onRemove = onRemoveFunc
}
}
//------------------------------------------------------------------------------
func (d *DynamicFanIn) addInput(ident string, input DynamicInput) error {
closedChan := make(chan struct{})
// Launch goroutine that async writes input into single channel
go func(in DynamicInput, cChan chan struct{}) {
defer func() {
d.onRemove(ident)
close(cChan)
}()
d.onAdd(ident)
for {
in, open := <-input.TransactionChan()
if !open {
// Race condition: This will be called when shutting down.
return
}
d.transactionChan <- in
}
}(input, closedChan)
// Add new input to our map
d.inputs[ident] = input
d.inputClosedChans[ident] = closedChan
return nil
}
func (d *DynamicFanIn) removeInput(ident string, timeout time.Duration) error {
input, exists := d.inputs[ident]
if !exists {
// Nothing to do
return nil
}
input.CloseAsync()
select {
case <-d.inputClosedChans[ident]:
case <-time.After(timeout):
// Do NOT remove inputs from our map unless we are sure they are
// closed.
return types.ErrTimeout
}
delete(d.inputs, ident)
delete(d.inputClosedChans, ident)
return nil
}
// managerLoop is an internal loop that monitors new and dead input types.
func (d *DynamicFanIn) managerLoop() {
defer func() {
for _, i := range d.inputs {
i.CloseAsync()
}
for key := range d.inputs {
if err := d.removeInput(key, time.Second); err != nil {
for err != nil {
err = d.removeInput(key, time.Second)
}
}
}
close(d.transactionChan)
close(d.closedChan)
}()
for {
select {
case wrappedInput, open := <-d.newInputChan:
if !open {
return
}
d.stats.Incr("broker.dynamic_fan_in.input.count", 1)
var err error
if _, exists := d.inputs[wrappedInput.Name]; exists {
if err = d.removeInput(wrappedInput.Name, wrappedInput.Timeout); err != nil {
d.stats.Incr("broker.dynamic_fan_in.input.remove.error", 1)
d.log.Errorf("Failed to stop old copy of dynamic input '%v': %v\n", wrappedInput.Name, err)
} else {
d.stats.Incr("broker.dynamic_fan_in.input.remove.success", 1)
}
}
if err == nil && wrappedInput.Input != nil {
// If the input is nil then we only wanted to remove the input.
if err = d.addInput(wrappedInput.Name, wrappedInput.Input); err != nil {
d.stats.Incr("broker.dynamic_fan_in.input.add.error", 1)
d.log.Errorf("Failed to start new dynamic input '%v': %v\n", wrappedInput.Name, err)
} else {
d.stats.Incr("broker.dynamic_fan_in.input.add.success", 1)
}
}
select {
case wrappedInput.ResChan <- err:
case <-d.closeChan:
close(wrappedInput.ResChan)
return
}
case <-d.closeChan:
return
}
}
}
// CloseAsync shuts down the DynamicFanIn broker and stops processing requests.
func (d *DynamicFanIn) CloseAsync() {
if atomic.CompareAndSwapInt32(&d.running, 1, 0) {
close(d.closeChan)
}
}
// WaitForClose blocks until the DynamicFanIn broker has closed down.
func (d *DynamicFanIn) WaitForClose(timeout time.Duration) error {
select {
case <-d.closedChan:
case <-time.After(timeout):
return types.ErrTimeout
}
return nil
}
//------------------------------------------------------------------------------