-
Notifications
You must be signed in to change notification settings - Fork 12
/
consumer_test.go
295 lines (240 loc) · 8.87 KB
/
consumer_test.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
package scyllacdc_test
import (
"context"
"log"
"os"
"sync"
"testing"
"time"
"github.com/gocql/gocql"
scyllacdc "github.com/scylladb/scylla-cdc-go"
"github.com/scylladb/scylla-cdc-go/internal/testutils"
)
type recordingConsumer struct {
mu *sync.Mutex
emptyTimestamps []gocql.UUID
}
func (rc *recordingConsumer) CreateChangeConsumer(_ context.Context, _ scyllacdc.CreateChangeConsumerInput) (scyllacdc.ChangeConsumer, error) {
return rc, nil
}
func (rc *recordingConsumer) Consume(ctx context.Context, change scyllacdc.Change) error {
return nil
}
func (rc *recordingConsumer) End() error {
return nil
}
func (rc *recordingConsumer) Empty(ctx context.Context, ackTime gocql.UUID) error {
rc.mu.Lock()
rc.emptyTimestamps = append(rc.emptyTimestamps, ackTime)
rc.mu.Unlock()
return nil
}
func (rc *recordingConsumer) GetTimestamps() []gocql.UUID {
rc.mu.Lock()
ret := append([]gocql.UUID{}, rc.emptyTimestamps...)
rc.mu.Unlock()
return ret
}
func TestConsumerCallsEmptyCallback(t *testing.T) {
consumer := &recordingConsumer{mu: &sync.Mutex{}}
adv := scyllacdc.AdvancedReaderConfig{
ChangeAgeLimit: -time.Millisecond,
PostNonEmptyQueryDelay: 100 * time.Millisecond,
PostEmptyQueryDelay: 100 * time.Millisecond,
PostFailedQueryDelay: 100 * time.Millisecond,
QueryTimeWindowSize: 100 * time.Millisecond,
ConfidenceWindowSize: time.Millisecond,
}
// Configure a session
address := testutils.GetSourceClusterContactPoint()
keyspaceName := testutils.CreateUniqueKeyspace(t, address)
cluster := gocql.NewCluster(address)
cluster.Keyspace = keyspaceName
cluster.PoolConfig.HostSelectionPolicy = gocql.TokenAwareHostPolicy(gocql.RoundRobinHostPolicy())
session, err := cluster.CreateSession()
if err != nil {
t.Fatal(err)
}
defer session.Close()
execQuery(t, session, "CREATE TABLE tbl (pk int PRIMARY KEY, v int) WITH cdc = {'enabled': true}")
cfg := &scyllacdc.ReaderConfig{
Session: session,
ChangeConsumerFactory: consumer,
TableNames: []string{keyspaceName + ".tbl"},
Advanced: adv,
Logger: log.New(os.Stderr, "", log.Ldate|log.Lmicroseconds|log.Lshortfile),
}
startTime := time.Now()
reader, err := scyllacdc.NewReader(context.Background(), cfg)
if err != nil {
t.Fatal(err)
}
errC := make(chan error)
go func() { errC <- reader.Run(context.Background()) }()
time.Sleep(time.Second)
endTime := startTime.Add(5 * time.Second)
reader.StopAt(endTime)
if err := <-errC; err != nil {
t.Fatal(err)
}
// All timestamps should be roughly between startTime and endTime
// To adjust for different clock on the scylla node, allow the time
// to exceed one second
acceptableStart := startTime.Add(-time.Second)
acceptableEnd := endTime.Add(time.Second)
timestamps := consumer.GetTimestamps()
if len(timestamps) == 0 {
t.Fatal("no empty event timestamps recorded")
}
for _, tstp := range timestamps {
early := !acceptableStart.Before(tstp.Time())
late := !tstp.Time().Before(acceptableEnd)
if early || late {
t.Errorf("timestamp of empty event %s not in expected range %s, %s",
tstp.Time(), acceptableStart, acceptableEnd)
}
}
}
func TestConsumerResumesWithTableBackedProgressReporter(t *testing.T) {
// Makes sure that the table backed progress consumer is able to resume correctly
// when StartGeneration was called, but no SaveProgress has been called
// so far.
// Configure a session
address := testutils.GetSourceClusterContactPoint()
keyspaceName := testutils.CreateUniqueKeyspace(t, address)
cluster := gocql.NewCluster(address)
cluster.Keyspace = keyspaceName
cluster.PoolConfig.HostSelectionPolicy = gocql.TokenAwareHostPolicy(gocql.RoundRobinHostPolicy())
session, err := cluster.CreateSession()
if err != nil {
t.Fatal(err)
}
defer session.Close()
execQuery(t, session, "CREATE TABLE tbl (pk int PRIMARY KEY, v int) WITH cdc = {'enabled': true}")
runWithProgressReporter := func(consumerFactory scyllacdc.ChangeConsumerFactory, endTime time.Time, adv scyllacdc.AdvancedReaderConfig) {
progressManager, err := scyllacdc.NewTableBackedProgressManager(session, "progress", "test")
if err != nil {
t.Fatalf("failed to create progress manager: %v", err)
}
cfg := &scyllacdc.ReaderConfig{
Session: session,
ChangeConsumerFactory: consumerFactory,
TableNames: []string{keyspaceName + ".tbl"},
ProgressManager: progressManager,
Advanced: adv,
Logger: log.New(os.Stderr, "", log.Ldate|log.Lmicroseconds|log.Lshortfile),
}
reader, err := scyllacdc.NewReader(context.Background(), cfg)
if err != nil {
t.Fatal(err)
}
errC := make(chan error)
go func() { errC <- reader.Run(context.Background()) }()
time.Sleep(500 * time.Millisecond)
reader.StopAt(endTime)
if err := <-errC; err != nil {
t.Fatal(err)
}
}
startTime := time.Now()
adv := scyllacdc.AdvancedReaderConfig{
PostNonEmptyQueryDelay: 100 * time.Millisecond,
PostEmptyQueryDelay: 100 * time.Millisecond,
PostFailedQueryDelay: 100 * time.Millisecond,
QueryTimeWindowSize: 100 * time.Millisecond,
ConfidenceWindowSize: time.Millisecond,
}
// Create and start the first consumer which will not call SaveProgress
// Start reading from ~now and stop after two seconds
// We should record that we started now but recorded no progress for
// any stream
adv.ChangeAgeLimit = -time.Millisecond
consumer := &recordingConsumer{mu: &sync.Mutex{}}
runWithProgressReporter(consumer, startTime.Add(2*time.Second), adv)
// Create and start the second consumer
// The progress manager should resume reading from the time
// when the previous run was started, not 1 minute ago
adv.ChangeAgeLimit = 10 * time.Second
consumer = &recordingConsumer{mu: &sync.Mutex{}}
runWithProgressReporter(consumer, startTime.Add(4*time.Second), adv)
// All timestamps should be roughly between startTime and endTime
// To adjust for different clock on the scylla node, allow the time
// to exceed one second
acceptableStart := startTime.Add(-time.Second)
acceptableEnd := startTime.Add(4 * time.Second).Add(time.Second)
timestamps := consumer.GetTimestamps()
if len(timestamps) == 0 {
t.Fatal("no empty event timestamps recorded")
}
for _, tstp := range timestamps {
early := !acceptableStart.Before(tstp.Time())
late := !tstp.Time().Before(acceptableEnd)
if early || late {
t.Errorf("timestamp of empty event %s not in expected range %s, %s",
tstp.Time(), acceptableStart, acceptableEnd)
}
}
}
func TestConsumerHonorsTableTTL(t *testing.T) {
// Make sure that the library doesn't attempt to read earlier than
// the table TTL
// Configure a session
address := testutils.GetSourceClusterContactPoint()
keyspaceName := testutils.CreateUniqueKeyspace(t, address)
cluster := gocql.NewCluster(address)
cluster.Keyspace = keyspaceName
cluster.PoolConfig.HostSelectionPolicy = gocql.TokenAwareHostPolicy(gocql.RoundRobinHostPolicy())
session, err := cluster.CreateSession()
if err != nil {
t.Fatal(err)
}
defer session.Close()
// Create a table with a very short TTL
execQuery(t, session, "CREATE TABLE tbl (pk int PRIMARY KEY, v int) WITH cdc = {'enabled': true, 'ttl': 2}")
startTime := time.Now()
endTime := startTime.Add(2 * time.Second)
adv := scyllacdc.AdvancedReaderConfig{
PostNonEmptyQueryDelay: 100 * time.Millisecond,
PostEmptyQueryDelay: 100 * time.Millisecond,
PostFailedQueryDelay: 100 * time.Millisecond,
QueryTimeWindowSize: 500 * time.Millisecond,
ConfidenceWindowSize: time.Millisecond,
ChangeAgeLimit: time.Minute, // should be overridden by the TTL
}
consumer := &recordingConsumer{mu: &sync.Mutex{}}
cfg := &scyllacdc.ReaderConfig{
Session: session,
ChangeConsumerFactory: consumer,
TableNames: []string{keyspaceName + ".tbl"},
Advanced: adv,
Logger: log.New(os.Stderr, "", log.Ldate|log.Lmicroseconds|log.Lshortfile),
}
reader, err := scyllacdc.NewReader(context.Background(), cfg)
if err != nil {
t.Fatal(err)
}
errC := make(chan error)
go func() { errC <- reader.Run(context.Background()) }()
time.Sleep(500 * time.Millisecond)
reader.StopAt(endTime)
if err := <-errC; err != nil {
t.Fatal(err)
}
// All timestamps should be roughly between startTime-TTL and endTime
// To adjust for different clock on the scylla node, allow the time
// to exceed one second
acceptableStart := startTime.Add(-time.Second).Add(-2 * time.Second)
acceptableEnd := startTime.Add(2 * time.Second).Add(time.Second)
timestamps := consumer.GetTimestamps()
if len(timestamps) == 0 {
t.Fatal("no empty event timestamps recorded")
}
for _, tstp := range timestamps {
early := !acceptableStart.Before(tstp.Time())
late := !tstp.Time().Before(acceptableEnd)
if early || late {
t.Errorf("timestamp of empty event %s not in expected range %s, %s",
tstp.Time(), acceptableStart, acceptableEnd)
}
}
}