forked from CiscoDevNet/bigmuddy-network-telemetry-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replay.go
292 lines (255 loc) · 6.2 KB
/
replay.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
//
// November 2016, cisco
//
// Copyright (c) 2016 by cisco Systems, Inc.
// All rights reserved.
//
//
// Input node used to replay streaming telemetry archives. Archives
// can be recoded using 'tap' output module with "raw = true" set.
//
// Tests for replay module are in tap_test.go
//
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
log "github.com/Sirupsen/logrus"
"io"
"os"
"time"
)
const (
REPLAY_DELAY_DEFAULT_USEC = 200000
)
//
// Module implementing inputNodeModule interface allowing REPLAY to read
// binary dump for replay.
type replayInputModule struct {
name string
logctx *log.Entry
filename string
logData bool
firstN int
loop bool
delayUsec int
done bool
count int
ctrlChan chan *ctrlMsg
dataChans []chan<- dataMsg
}
func replayInputModuleNew() inputNodeModule {
return &replayInputModule{}
}
func (t *replayInputModule) String() string {
return fmt.Sprintf("%s:%s", t.name, t.filename)
}
func (t *replayInputModule) replayInputFeederLoop() error {
var stats msgStats
var tickIn time.Duration
var tick <-chan time.Time
err, parser := getNewEncapParser(t.name, "st", t)
if err != nil {
t.logctx.WithError(err).Error(
"Failed to open get parser, STOP")
t.done = true
}
f, err := os.Open(t.filename)
if err != nil {
t.logctx.WithError(err).Error(
"Failed to open file with binary dump of telemetry. " +
"Dump should be produced with 'tap' output, 'raw=true', STOP")
t.done = true
} else {
defer f.Close()
if t.delayUsec != 0 {
tickIn = time.Duration(t.delayUsec) * time.Microsecond
} else {
//
// We still tick to get control channel a look in.
tickIn = time.Nanosecond
}
tick = time.Tick(tickIn)
}
for {
select {
case <-tick:
if t.done {
// Waiting for exit, we're done here.
continue
}
// iterate until a message is produced (header, payload)
var i int
for {
i = i + 1
err, buffer := parser.nextBlockBuffer()
if err != nil {
t.logctx.WithError(err).WithFields(
log.Fields{
"iteration": i,
"nth_msg": t.count,
}).Error("Failed to fetch buffer, STOP")
t.done = true
return err
}
readn, err := io.ReadFull(f, *buffer)
if err != nil {
if err != io.EOF {
t.logctx.WithError(err).WithFields(
log.Fields{
"iteration": i,
"nth_msg": t.count,
}).Error("Failed to read next buffer, STOP")
t.done = true
return err
}
if !t.loop {
//
// We're done.
return nil
}
t.logctx.Debug("restarting from start of message archive")
_, err := f.Seek(0, 0)
if err != nil {
t.logctx.WithError(err).WithFields(
log.Fields{
"iteration": i,
"nth_msg": t.count,
}).Error("Failed to go back to start, STOP")
t.done = true
return err
}
//
// Because we're starting from scratch, and we
// need to get a new parser to restart parser
// state machine along with data.
err, parser = getNewEncapParser(t.name, "st", t)
continue
}
err, msgs := parser.nextBlock(*buffer, nil)
if err != nil {
t.logctx.WithError(err).WithFields(
log.Fields{
"iteration": i,
"nth_msg": t.count,
"read_in": readn,
"len": len(*buffer),
"msg": hex.Dump(*buffer),
}).Error(
"Failed to decode next block, STOP")
t.done = true
return err
}
if t.logData {
t.logctx.WithFields(log.Fields{
"iteration": i,
"nth_msg": t.count,
"dataMsgCount": len(msgs),
"len": len(*buffer),
"msg": hex.Dump(*buffer),
}).Debug("REPLAY input logdata")
}
if msgs == nil {
//
// We probably just read a header
continue
}
for _, msg := range msgs {
for _, dataChan := range t.dataChans {
if len(dataChan) == cap(dataChan) {
t.logctx.Error("Input overrun (replace with counter)")
continue
}
dataChan <- msg
}
}
t.count = t.count + 1
if t.count == t.firstN && t.firstN != 0 {
t.logctx.Debug("dumped all messages expected")
t.done = true
}
break
}
case msg := <-t.ctrlChan:
switch msg.id {
case REPORT:
t.logctx.Debug("report request")
content, _ := json.Marshal(stats)
resp := &ctrlMsg{
id: ACK,
content: content,
respChan: nil,
}
msg.respChan <- resp
case SHUTDOWN:
t.logctx.Info("REPLAY input loop, rxed SHUTDOWN, shutting down")
resp := &ctrlMsg{
id: ACK,
respChan: nil,
}
msg.respChan <- resp
return nil
default:
t.logctx.Error("REPLAY input loop, unknown ctrl message")
}
}
}
}
func (t *replayInputModule) replayInputFeederLoopSticky() {
t.logctx.Debug("Starting REPLAY feeder loop")
for {
err := t.replayInputFeederLoop()
if err == nil {
t.logctx.Debug("REPLAY feeder loop done, exit")
break
} else {
// retry
time.Sleep(time.Second)
if t.done {
t.logctx.WithFields(log.Fields{
"nth_msg": t.count,
}).Debug("idle, waiting for shutdown")
} else {
t.logctx.Debug("Restarting REPLAY feeder loop")
}
}
}
}
func (t *replayInputModule) configure(
name string,
nc nodeConfig,
dataChans []chan<- dataMsg) (error, chan<- *ctrlMsg) {
var err error
t.filename, err = nc.config.GetString(name, "file")
if err != nil {
return err, nil
}
t.name = name
t.logData, _ = nc.config.GetBool(name, "logdata")
t.firstN, _ = nc.config.GetInt(name, "firstn")
if t.firstN == 0 {
t.loop, _ = nc.config.GetBool(name, "loop")
}
t.delayUsec, err = nc.config.GetInt(name, "delayusec")
if err != nil {
//
// Default to a sensible-ish value for replay to
// avoid overwhelming output stages. Note that
// we can still overwhelm output stages if we
// want to do so explicitly i.e. set to zero.
t.delayUsec = REPLAY_DELAY_DEFAULT_USEC
}
t.ctrlChan = make(chan *ctrlMsg)
t.dataChans = dataChans
t.logctx = logger.WithFields(log.Fields{
"name": t.name,
"file": t.filename,
"logdata": t.logData,
"firstN": t.firstN,
"delayUsec": t.delayUsec,
"loop": t.loop,
})
go t.replayInputFeederLoopSticky()
return nil, t.ctrlChan
}