forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replay.go
230 lines (209 loc) · 5.14 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
package kapacitor
import (
"bufio"
"encoding/json"
"fmt"
"io"
"time"
dbmodels "github.com/influxdata/influxdb/models"
"github.com/influxdata/kapacitor/clock"
"github.com/influxdata/kapacitor/models"
)
// Replay engine that can replay static data sets against a specific executor and its tasks.
type Replay struct {
clck clock.Clock
Setter clock.Setter
}
// Create a new replay engine.
func NewReplay(c clock.Clock) *Replay {
return &Replay{
clck: c,
Setter: c,
}
}
// Replay a data set against an executor.
func (r *Replay) ReplayStream(data io.ReadCloser, stream StreamCollector, recTime bool, precision string) <-chan error {
src := newReplayStreamSource(data, r.clck)
go src.replayStream(stream, recTime, precision)
return src.Err()
}
type replayStreamSource struct {
data io.Closer
in *bufio.Scanner
clck clock.Clock
err chan error
}
func newReplayStreamSource(data io.ReadCloser, clck clock.Clock) *replayStreamSource {
return &replayStreamSource{
data: data,
in: bufio.NewScanner(data),
clck: clck,
err: make(chan error, 1),
}
}
func (r *replayStreamSource) Err() <-chan error {
return r.err
}
func (r *replayStreamSource) replayStream(stream StreamCollector, recTime bool, precision string) {
defer stream.Close()
defer r.data.Close()
start := time.Time{}
var diff time.Duration
zero := r.clck.Zero()
for r.in.Scan() {
db := r.in.Text()
if !r.in.Scan() {
r.err <- fmt.Errorf("invalid replay file format, expected another line")
return
}
rp := r.in.Text()
if !r.in.Scan() {
r.err <- fmt.Errorf("invalid replay file format, expected another line")
return
}
points, err := dbmodels.ParsePointsWithPrecision(
r.in.Bytes(),
zero,
precision,
)
if err != nil {
r.err <- err
return
}
if start.IsZero() {
start = points[0].Time()
diff = zero.Sub(start)
}
var t time.Time
waitTime := points[0].Time().Add(diff).UTC()
if !recTime {
t = waitTime
} else {
t = points[0].Time().UTC()
}
mp := points[0]
p := models.Point{
Database: db,
RetentionPolicy: rp,
Name: mp.Name(),
Group: models.NilGroup,
Tags: models.Tags(mp.Tags()),
Fields: models.Fields(mp.Fields()),
Time: t,
}
r.clck.Until(waitTime)
err = stream.CollectPoint(p)
if err != nil {
r.err <- err
return
}
}
r.err <- r.in.Err()
}
// Replay a data set against an executor.
// If source time is true then the replay will use the times stored in the
// recording instead of the clock time.
func (r *Replay) ReplayBatch(data []io.ReadCloser, batches []BatchCollector, recTime bool) <-chan error {
src := newReplayBatchSource(data, r.clck)
src.replayBatch(batches, recTime)
return src.Err()
}
type replayBatchSource struct {
data []io.ReadCloser
clck clock.Clock
allErrs chan error
err chan error
}
func newReplayBatchSource(data []io.ReadCloser, clck clock.Clock) *replayBatchSource {
return &replayBatchSource{
data: data,
clck: clck,
allErrs: make(chan error, len(data)),
err: make(chan error, 1),
}
}
func (r *replayBatchSource) Err() <-chan error {
return r.err
}
func (r *replayBatchSource) replayBatch(batches []BatchCollector, recTime bool) {
if e, g := len(r.data), len(batches); e != g {
r.err <- fmt.Errorf("unexpected number of batch collectors. exp %d got %d", e, g)
return
}
for i := range r.data {
go r.replayBatchFromData(r.data[i], batches[i], recTime)
}
go func() {
// Wait for each one to finish and report first error if any
for range r.data {
err := <-r.allErrs
if err != nil {
r.err <- err
return
}
}
r.err <- nil
}()
}
// Replay the batch data from a single source
func (r *replayBatchSource) replayBatchFromData(data io.ReadCloser, batch BatchCollector, recTime bool) {
defer batch.Close()
defer data.Close()
in := bufio.NewScanner(data)
// Find relative times
start := time.Time{}
var diff time.Duration
zero := r.clck.Zero()
for in.Scan() {
var b models.Batch
err := json.Unmarshal(in.Bytes(), &b)
if err != nil {
r.allErrs <- err
return
}
if len(b.Points) == 0 {
// do nothing
continue
}
b.Group = models.TagsToGroupID(models.SortedKeys(b.Tags), b.Tags)
if start.IsZero() {
start = b.Points[0].Time
diff = zero.Sub(start)
}
// Add tags to all points
if len(b.Tags) > 0 {
for i := range b.Points {
if len(b.Points[i].Tags) == 0 {
b.Points[i].Tags = b.Tags
}
}
}
var lastTime time.Time
if !recTime {
for i := range b.Points {
b.Points[i].Time = b.Points[i].Time.Add(diff).UTC()
}
lastTime = b.Points[len(b.Points)-1].Time
} else {
lastTime = b.Points[len(b.Points)-1].Time.Add(diff).UTC()
}
r.clck.Until(lastTime)
b.TMax = b.Points[len(b.Points)-1].Time
batch.CollectBatch(b)
}
r.allErrs <- in.Err()
}
func WritePointForRecording(w io.Writer, p models.Point, precision string) error {
fmt.Fprintf(w, "%s\n%s\n", p.Database, p.RetentionPolicy)
w.Write(p.Bytes(precision))
w.Write([]byte("\n"))
return nil
}
func WriteBatchForRecording(w io.Writer, b models.Batch) error {
enc := json.NewEncoder(w)
err := enc.Encode(b)
if err != nil {
return err
}
return nil
}