forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_dynamic_fan_out.go
306 lines (268 loc) · 7.25 KB
/
output_dynamic_fan_out.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
package io
import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/benthosdev/benthos/v4/internal/component"
"github.com/benthosdev/benthos/v4/internal/component/output"
"github.com/benthosdev/benthos/v4/internal/log"
"github.com/benthosdev/benthos/v4/internal/message"
"github.com/benthosdev/benthos/v4/internal/shutdown"
)
// wrappedOutput is a struct that wraps a DynamicOutput with an identifying
// name.
type wrappedOutput struct {
Ctx context.Context
Name string
Output output.Streamed
ResChan chan<- error
}
// outputWithTSChan is a struct containing both an output and the transaction
// chan it reads from.
type outputWithTSChan struct {
tsChan chan message.Transaction
output output.Streamed
ctx context.Context
done func()
}
type dynamicFanOutOutputBroker struct {
log log.Modular
onAdd func(label string)
onRemove func(label string)
transactions <-chan message.Transaction
outputsMut sync.RWMutex
newOutputChan chan wrappedOutput
outputs map[string]outputWithTSChan
shutSig *shutdown.Signaller
}
func newDynamicFanOutOutputBroker(
outputs map[string]output.Streamed,
logger log.Modular,
onAdd func(label string),
onRemove func(label string),
) (*dynamicFanOutOutputBroker, error) {
d := &dynamicFanOutOutputBroker{
log: logger,
transactions: nil,
newOutputChan: make(chan wrappedOutput),
outputs: make(map[string]outputWithTSChan, len(outputs)),
shutSig: shutdown.NewSignaller(),
onAdd: onAdd,
onRemove: onRemove,
}
if d.onAdd == nil {
d.onAdd = func(l string) {}
}
if d.onRemove == nil {
d.onRemove = func(l string) {}
}
for k, v := range outputs {
if err := d.addOutput(k, v); err != nil {
return nil, fmt.Errorf("failed to initialise dynamic output '%v': %v", k, err)
}
d.onAdd(k)
}
return d, nil
}
// SetOutput attempts to add a new output to the dynamic output broker. If an
// output 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 output argument is safe and will simply remove the previous output
// under the indentifier, if there was one.
func (d *dynamicFanOutOutputBroker) SetOutput(ctx context.Context, ident string, output output.Streamed) error {
resChan := make(chan error, 1)
select {
case d.newOutputChan <- wrappedOutput{
Name: ident,
Output: output,
ResChan: resChan,
Ctx: ctx,
}:
case <-ctx.Done():
return component.ErrTimeout
}
select {
case err := <-resChan:
return err
case <-ctx.Done():
}
return component.ErrTimeout
}
func (d *dynamicFanOutOutputBroker) Consume(transactions <-chan message.Transaction) error {
if d.transactions != nil {
return component.ErrAlreadyStarted
}
d.transactions = transactions
go d.loop()
return nil
}
func (d *dynamicFanOutOutputBroker) addOutput(ident string, output output.Streamed) error {
if _, exists := d.outputs[ident]; exists {
return fmt.Errorf("output key '%v' already exists", ident)
}
ow := outputWithTSChan{
tsChan: make(chan message.Transaction),
output: output,
}
if err := output.Consume(ow.tsChan); err != nil {
output.TriggerCloseNow()
return err
}
ow.ctx, ow.done = context.WithCancel(context.Background())
d.outputs[ident] = ow
return nil
}
func (d *dynamicFanOutOutputBroker) removeOutput(ctx context.Context, ident string) error {
ow, exists := d.outputs[ident]
if !exists {
return nil
}
ow.output.TriggerCloseNow()
err := ow.output.WaitForClose(ctx)
ow.done()
close(ow.tsChan)
delete(d.outputs, ident)
return err
}
func (d *dynamicFanOutOutputBroker) loop() {
apiWG := sync.WaitGroup{}
ackInterruptChan := make(chan struct{})
var ackPending int64
defer func() {
// Wait for pending acks to be resolved, or forceful termination
ackWaitLoop:
for atomic.LoadInt64(&ackPending) > 0 {
select {
case <-ackInterruptChan:
case <-time.After(time.Millisecond * 100):
// Just incase an interrupt doesn't arrive.
case <-d.shutSig.CloseAtLeisureChan():
break ackWaitLoop
}
}
for _, ow := range d.outputs {
ow.output.TriggerCloseNow()
close(ow.tsChan)
}
for _, ow := range d.outputs {
ow.output.TriggerCloseNow()
}
for _, ow := range d.outputs {
_ = ow.output.WaitForClose(context.Background())
}
d.shutSig.CloseNow()
apiWG.Wait()
d.outputs = map[string]outputWithTSChan{}
d.shutSig.ShutdownComplete()
}()
apiWG.Add(1)
go func() {
defer apiWG.Done()
for {
select {
case wrappedOutput, open := <-d.newOutputChan:
if !open {
return
}
func() {
d.outputsMut.Lock()
defer d.outputsMut.Unlock()
// First, always remove the previous output if it exists.
if _, exists := d.outputs[wrappedOutput.Name]; exists {
if err := d.removeOutput(wrappedOutput.Ctx, wrappedOutput.Name); err != nil {
d.log.Error("Failed to stop old copy of dynamic output '%v' in time: %v, the output will continue to shut down in the background.\n", wrappedOutput.Name, err)
}
d.onRemove(wrappedOutput.Name)
}
// Next, attempt to create a new output (if specified).
if wrappedOutput.Output == nil {
wrappedOutput.ResChan <- nil
} else {
err := d.addOutput(wrappedOutput.Name, wrappedOutput.Output)
if err != nil {
d.log.Error("Failed to start new dynamic output '%v': %v\n", wrappedOutput.Name, err)
} else {
d.onAdd(wrappedOutput.Name)
}
wrappedOutput.ResChan <- err
}
}()
case <-d.shutSig.CloseAtLeisureChan():
return
}
}
}()
for {
var ts message.Transaction
var open bool
select {
case ts, open = <-d.transactions:
if !open {
return
}
case <-d.shutSig.CloseAtLeisureChan():
return
}
d.outputsMut.RLock()
for len(d.outputs) == 0 {
// Assuming this isn't a common enough occurrence that it
// won't be busy enough to require a sync.Cond, looping with
// a sleep is fine for now.
d.outputsMut.RUnlock()
select {
case <-time.After(time.Millisecond * 10):
case <-d.shutSig.CloseAtLeisureChan():
return
}
d.outputsMut.RLock()
}
_ = atomic.AddInt64(&ackPending, 1)
pendingResponses := int64(len(d.outputs))
outputsLoop:
for _, output := range d.outputs {
select {
case output.tsChan <- message.NewTransactionFunc(ts.Payload.ShallowCopy(), func(ctx context.Context, err error) error {
if atomic.AddInt64(&pendingResponses, -1) == 0 || err != nil {
atomic.StoreInt64(&pendingResponses, 0)
ackErr := ts.Ack(ctx, err)
_ = atomic.AddInt64(&ackPending, -1)
select {
case ackInterruptChan <- struct{}{}:
default:
}
return ackErr
}
return nil
}):
case <-d.shutSig.CloseAtLeisureChan():
break outputsLoop // This signal will be caught again in the next loop
}
}
d.outputsMut.RUnlock()
}
}
func (d *dynamicFanOutOutputBroker) Connected() bool {
d.outputsMut.RLock()
defer d.outputsMut.RUnlock()
for _, out := range d.outputs {
if !out.output.Connected() {
return false
}
}
return true
}
func (d *dynamicFanOutOutputBroker) TriggerCloseNow() {
d.shutSig.CloseNow()
}
func (d *dynamicFanOutOutputBroker) WaitForClose(ctx context.Context) error {
select {
case <-d.shutSig.HasClosedChan():
case <-ctx.Done():
return ctx.Err()
}
return nil
}