-
Notifications
You must be signed in to change notification settings - Fork 22
/
buffered_event_source.go
345 lines (290 loc) · 9.08 KB
/
buffered_event_source.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
package broker
import (
"context"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"time"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/datanode/metrics"
"code.vegaprotocol.io/vega/logging"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
"github.com/golang/protobuf/proto"
)
type FileBufferedEventSource struct {
log *logging.Logger
lastBufferedSeqNum chan uint64
sendChannelBufferSize int
source EventReceiver
bufferFilePath string
config BufferedEventSourceConfig
}
const (
numberOfSeqNumBytes = 8
numberOfSizeBytes = 4
)
func NewBufferedEventSource(log *logging.Logger, config BufferedEventSourceConfig, source EventReceiver, bufferFilePath string) (*FileBufferedEventSource, error) {
err := os.RemoveAll(bufferFilePath)
if err != nil {
return nil, fmt.Errorf("failed to remove old buffer files:%w", err)
}
err = os.Mkdir(bufferFilePath, os.ModePerm)
if err != nil {
return nil, fmt.Errorf("failed to create buffer file directory:%w", err)
}
files, _ := ioutil.ReadDir(bufferFilePath)
for _, file := range files {
os.Remove(filepath.Join(bufferFilePath, file.Name()))
}
fb := &FileBufferedEventSource{
log: log.Named("buffered-event-source"),
source: source,
config: config,
lastBufferedSeqNum: make(chan uint64, config.MaxBufferedEvents),
bufferFilePath: bufferFilePath,
}
fb.log.Infof("Starting buffered event source with a max buffered event count of %d, and events per buffer file size %d",
config.MaxBufferedEvents, config.EventsPerFile)
return fb, nil
}
func (m *FileBufferedEventSource) Listen() error {
return m.source.Listen()
}
func (m *FileBufferedEventSource) Receive(ctx context.Context) (<-chan events.Event, <-chan error) {
sourceEventCh, sourceErrCh := m.source.Receive(ctx)
if m.config.EventsPerFile == 0 {
m.log.Info("events per file is set to 0, disabling event buffer")
return sourceEventCh, sourceErrCh
}
sinkEventCh := make(chan events.Event, m.sendChannelBufferSize)
sinkErrorCh := make(chan error, 1)
ctxWithCancel, cancel := context.WithCancel(ctx)
go func() {
m.writeEventsToBuffer(ctx, sourceEventCh, sourceErrCh, sinkErrorCh)
cancel()
}()
go func() {
m.readEventsFromBuffer(ctxWithCancel, sinkEventCh, sinkErrorCh)
}()
return sinkEventCh, sinkErrorCh
}
func (m *FileBufferedEventSource) writeEventsToBuffer(ctx context.Context, sourceEventCh <-chan events.Event,
sourceErrCh <-chan error, sinkErrorCh chan error,
) {
bufferSeqNum := uint64(0)
var bufferFile *os.File
defer func() {
if bufferFile != nil {
err := bufferFile.Close()
if err != nil {
m.log.Errorf("failed to close event buffer file:%w", err)
}
}
}()
var err error
for {
select {
case event, ok := <-sourceEventCh:
if !ok {
return
}
if bufferSeqNum%uint64(m.config.EventsPerFile) == 0 {
bufferFile, err = m.rollBufferFile(bufferFile, bufferSeqNum)
if err != nil {
sinkErrorCh <- fmt.Errorf("failed to roll buffer file:%w", err)
}
}
bufferSeqNum++
err = writeToBuffer(bufferFile, bufferSeqNum, event)
metrics.EventBufferWrittenCountInc()
if err != nil {
sinkErrorCh <- fmt.Errorf("failed to write events to buffer:%w", err)
}
select {
case m.lastBufferedSeqNum <- bufferSeqNum:
default:
loop:
for {
select {
case <-m.lastBufferedSeqNum:
case <-ctx.Done():
return
default:
break loop
}
}
m.lastBufferedSeqNum <- bufferSeqNum
}
case srcErr, ok := <-sourceErrCh:
if !ok {
return
}
sinkErrorCh <- srcErr
case <-ctx.Done():
return
}
}
}
func (m *FileBufferedEventSource) readEventsFromBuffer(ctx context.Context, sinkEventCh chan events.Event, sinkErrorCh chan error) {
var offset int64
var lastBufferSeqNum uint64
var lastSentBufferSeqNum uint64
var err error
var bufferFile *os.File
defer func() {
if bufferFile != nil {
err = bufferFile.Close()
if err != nil {
m.log.Errorf("failed to close event buffer file:%w", err)
}
}
close(sinkEventCh)
close(sinkErrorCh)
}()
for {
if ctx.Err() != nil {
return
}
if lastBufferSeqNum > lastSentBufferSeqNum {
if bufferFile == nil {
offset = 0
bufferFile, err = m.openBufferFile(lastSentBufferSeqNum+1, lastSentBufferSeqNum+uint64(m.config.EventsPerFile))
if err != nil {
sinkErrorCh <- fmt.Errorf("failed to open buffer file:%w", err)
return
}
}
event, bufferSeqNum, read, err := readEvent(bufferFile, offset)
if err != nil {
sinkErrorCh <- fmt.Errorf("error when reading event from buffer file:%w", err)
return
}
offset += int64(read)
if event != nil {
evt := toEvent(ctx, event)
sinkEventCh <- evt
metrics.EventBufferReadCountInc()
lastSentBufferSeqNum = bufferSeqNum
if lastSentBufferSeqNum%uint64(m.config.EventsPerFile) == 0 {
if err = m.removeBufferFile(bufferFile); err != nil {
sinkErrorCh <- fmt.Errorf("failed to remove buffer file:%w", err)
}
bufferFile = nil
}
} else {
// Time for the buffer write to complete if we were unable to read a complete event for a given seq num
time.Sleep(10 * time.Millisecond)
}
} else {
// Wait until told there is new data written to the buffer
select {
case <-ctx.Done():
return
case bufferSeqNum := <-m.lastBufferedSeqNum:
lastBufferSeqNum = bufferSeqNum
}
}
}
}
func (m *FileBufferedEventSource) rollBufferFile(currentBufferFile *os.File, seqNum uint64) (*os.File, error) {
if currentBufferFile != nil {
err := currentBufferFile.Close()
if err != nil {
return nil, fmt.Errorf("unable to create new buffer file, failed to close current events buffer file:%w", err)
}
}
newBufferFile, err := m.createFile(seqNum+1, seqNum+uint64(m.config.EventsPerFile))
if err != nil {
return nil, fmt.Errorf("failed to create events buffer file:%w", err)
}
return newBufferFile, nil
}
func writeToBuffer(bufferFile *os.File, bufferSeqNum uint64, event events.Event) error {
e := event.StreamMessage()
seqNumBytes := make([]byte, numberOfSeqNumBytes)
sizeBytes := make([]byte, numberOfSizeBytes)
size := numberOfSeqNumBytes + uint32(proto.Size(e))
protoBytes, err := proto.Marshal(e)
if err != nil {
return fmt.Errorf("failed to marshal bus event:%w", err)
}
binary.BigEndian.PutUint64(seqNumBytes, bufferSeqNum)
binary.BigEndian.PutUint32(sizeBytes, size)
allBytes := append([]byte{}, sizeBytes...)
allBytes = append(allBytes, seqNumBytes...)
allBytes = append(allBytes, protoBytes...)
_, err = bufferFile.Write(allBytes)
if err != nil {
return fmt.Errorf("failed to write to buffer file:%w", err)
}
return nil
}
func (m *FileBufferedEventSource) removeBufferFile(bufferFile *os.File) error {
err := bufferFile.Close()
if err != nil {
return fmt.Errorf("failed to close last event buffer file:%w", err)
}
err = os.Remove(bufferFile.Name())
if err != nil {
return fmt.Errorf("failed to remove event buffer file:%w", err)
}
return nil
}
func readEvent(eventFile *os.File, offset int64) (event *eventspb.BusEvent, seqNum uint64,
totalBytesRead uint32, err error,
) {
sizeBytes := make([]byte, numberOfSizeBytes)
read, err := eventFile.ReadAt(sizeBytes, offset)
if err == io.EOF {
return nil, 0, 0, nil
} else if err != nil {
return nil, 0, 0, fmt.Errorf("error reading message size from events file:%w", err)
}
if read < numberOfSizeBytes {
return nil, 0, 0, nil
}
messageOffset := offset + numberOfSizeBytes
msgSize := binary.BigEndian.Uint32(sizeBytes)
seqNumAndMsgBytes := make([]byte, msgSize)
read, err = eventFile.ReadAt(seqNumAndMsgBytes, messageOffset)
if err == io.EOF {
return nil, 0, 0, nil
} else if err != nil {
return nil, 0, 0, fmt.Errorf("error reading message bytes from events file:%w", err)
}
if read < int(msgSize) {
return nil, 0, 0, nil
}
seqNumBytes := seqNumAndMsgBytes[:numberOfSeqNumBytes]
seqNum = binary.BigEndian.Uint64(seqNumBytes)
event = &eventspb.BusEvent{}
msgBytes := seqNumAndMsgBytes[numberOfSeqNumBytes:]
err = proto.Unmarshal(msgBytes, event)
if err != nil {
return nil, 0, 0, fmt.Errorf("failed to unmarshal bus event: %w", err)
}
totalBytesRead = numberOfSizeBytes + msgSize
return event, seqNum, totalBytesRead, nil
}
func (m *FileBufferedEventSource) getBufferFileName(fromSeqNum uint64, toSeqNum uint64) string {
return fmt.Sprintf("%s/datanode-buffer-%d-%d", m.bufferFilePath, fromSeqNum, toSeqNum)
}
func (m *FileBufferedEventSource) createFile(fromSeqNum uint64, toSeqNum uint64) (*os.File, error) {
bufferFileName := m.getBufferFileName(fromSeqNum, toSeqNum)
bufferFile, err := os.Create(bufferFileName)
if err != nil {
return nil, fmt.Errorf("failed to create buffer file: %s :%w", bufferFileName, err)
}
return bufferFile, err
}
func (m *FileBufferedEventSource) openBufferFile(fromSeqNum uint64, toSeqNum uint64) (*os.File, error) {
bufferFileName := m.getBufferFileName(fromSeqNum, toSeqNum)
bufferFile, err := os.Open(bufferFileName)
if err != nil {
return nil, fmt.Errorf("failed to open buffer file: %s :%w", bufferFileName, err)
}
return bufferFile, nil
}