-
Notifications
You must be signed in to change notification settings - Fork 1
/
subscriber.go
342 lines (289 loc) · 10 KB
/
subscriber.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
package spream
import (
"context"
"errors"
"fmt"
"sync"
"time"
"cloud.google.com/go/spanner"
"cloud.google.com/go/spanner/apiv1/spannerpb"
"golang.org/x/sync/errgroup"
)
// Subscriber subscribes change stream.
type Subscriber struct {
spannerClient *spanner.Client
streamName string
startTimestamp time.Time
endTimestamp time.Time
heartbeatInterval time.Duration
spannerRequestPriority spannerpb.RequestOptions_Priority
partitionStorage PartitionStorage
consumer Consumer
eg *errgroup.Group
mu sync.Mutex
}
// PartitionStorage is an interface for storing and reading PartitionMetadata.
type PartitionStorage interface {
GetUnfinishedMinWatermarkPartition(ctx context.Context) (*PartitionMetadata, error)
GetInterruptedPartitions(ctx context.Context) ([]*PartitionMetadata, error)
InitializeRootPartition(ctx context.Context, startTimestamp time.Time, endTimestamp time.Time, heartbeatInterval time.Duration) error
GetSchedulablePartitions(ctx context.Context, minWatermark time.Time) ([]*PartitionMetadata, error)
AddChildPartitions(ctx context.Context, parentPartition *PartitionMetadata, childPartitionsRecord *ChildPartitionsRecord) error
UpdateToScheduled(ctx context.Context, partitions []*PartitionMetadata) error
UpdateToRunning(ctx context.Context, partition *PartitionMetadata) error
UpdateToFinished(ctx context.Context, partition *PartitionMetadata) error
UpdateWatermark(ctx context.Context, partition *PartitionMetadata, watermark time.Time) error
}
type config struct {
startTimestamp time.Time
endTimestamp time.Time
heartbeatInterval time.Duration
spannerRequestPriority spannerpb.RequestOptions_Priority
}
type Option interface {
Apply(*config)
}
type withStartTimestamp time.Time
func (o withStartTimestamp) Apply(c *config) {
c.startTimestamp = time.Time(o)
}
// WithStartTimestamp set the start timestamp option for read change streams.
//
// The value must be within the retention period of the change stream and before the current time.
// Default value is current timestamp.
func WithStartTimestamp(startTimestamp time.Time) Option {
return withStartTimestamp(startTimestamp)
}
type withEndTimestamp time.Time
func (o withEndTimestamp) Apply(c *config) {
c.endTimestamp = time.Time(o)
}
// WithEndTimestamp set the end timestamp option for read change streams.
//
// The value must be within the retention period of the change stream and must be after the start timestamp.
// If not set, read latest changes until canceled.
func WithEndTimestamp(endTimestamp time.Time) Option {
return withEndTimestamp(endTimestamp)
}
type withHeartbeatInterval time.Duration
func (o withHeartbeatInterval) Apply(c *config) {
c.heartbeatInterval = time.Duration(o)
}
// WithHeartbeatInterval set the heartbeat interval for read change streams.
//
// Default value is 10 seconds.
func WithHeartbeatInterval(heartbeatInterval time.Duration) Option {
return withHeartbeatInterval(heartbeatInterval)
}
type withSpannerRequestPriotiry spannerpb.RequestOptions_Priority
func (o withSpannerRequestPriotiry) Apply(c *config) {
c.spannerRequestPriority = spannerpb.RequestOptions_Priority(o)
}
// WithSpannerRequestPriotiry set the request priority option for read change streams.
//
// Default value is unspecified, equivalent to high.
func WithSpannerRequestPriotiry(priority spannerpb.RequestOptions_Priority) Option {
return withSpannerRequestPriotiry(priority)
}
var (
defaultEndTimestamp = time.Date(9999, 12, 31, 23, 59, 59, 999999999, time.UTC) // Maximum value of Spanner TIMESTAMP type.
defaultHeartbeatInterval = 10 * time.Second
nowFunc = time.Now
)
// NewSubscriber creates a new subscriber of change streams.
func NewSubscriber(
client *spanner.Client,
streamName string,
partitionStorage PartitionStorage,
options ...Option,
) *Subscriber {
c := &config{
startTimestamp: nowFunc(),
endTimestamp: defaultEndTimestamp,
heartbeatInterval: defaultHeartbeatInterval,
}
for _, o := range options {
o.Apply(c)
}
return &Subscriber{
spannerClient: client,
streamName: streamName,
startTimestamp: c.startTimestamp,
endTimestamp: c.endTimestamp,
heartbeatInterval: c.heartbeatInterval,
spannerRequestPriority: c.spannerRequestPriority,
partitionStorage: partitionStorage,
}
}
// Consumer is the interface to consume the DataChangeRecord.
//
// Consume might be called from multiple goroutines and must be re-entrant safe.
type Consumer interface {
Consume(change *DataChangeRecord) error
}
// ConsumerFunc type is an adapter to allow the use of ordinary functions as Consumer.
type ConsumerFunc func(*DataChangeRecord) error
// Consume calls f(change).
func (f ConsumerFunc) Consume(change *DataChangeRecord) error {
return f(change)
}
// Subscribe starts subscribing to the change stream.
func (s *Subscriber) Subscribe(ctx context.Context, consumer Consumer) error {
eg, ctx := s.initErrGroup(ctx)
s.consumer = consumer
// Initialize root partition if this is the first run or if the previous run has already been completed.
minWatermarkPartition, err := s.partitionStorage.GetUnfinishedMinWatermarkPartition(ctx)
if err != nil {
return fmt.Errorf("failed to get unfinished min watermark partition on start subscribe: %w", err)
}
if minWatermarkPartition == nil {
if err := s.partitionStorage.InitializeRootPartition(ctx, s.startTimestamp, s.endTimestamp, s.heartbeatInterval); err != nil {
return fmt.Errorf("failed to initialize root partition: %w", err)
}
}
interruptedPartitions, err := s.partitionStorage.GetInterruptedPartitions(ctx)
if err != nil {
return fmt.Errorf("failed to get interrupted partitions: %w", err)
}
for _, p := range interruptedPartitions {
p := p
s.eg.Go(func() error {
return s.queryChangeStream(ctx, p)
})
}
eg.Go(func() error {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
err := s.detectNewPartitions(ctx)
switch err {
case errDone:
return nil
case nil:
// continue
default:
return err
}
case <-ctx.Done():
return ctx.Err()
}
}
})
return eg.Wait()
}
// SubscribeFunc is an adapter to allow the use of ordinary functions as Consumer.
//
// function might be called from multiple goroutines and must be re-entrant safe.
func (s *Subscriber) SubscribeFunc(ctx context.Context, f ConsumerFunc) error {
return s.Subscribe(ctx, f)
}
func (s *Subscriber) initErrGroup(ctx context.Context) (*errgroup.Group, context.Context) {
s.mu.Lock()
defer s.mu.Unlock()
if s.eg != nil {
panic("Subscriber has already started subscribe.")
}
eg, ctx := errgroup.WithContext(ctx)
s.eg = eg
return eg, ctx
}
var errDone = errors.New("all partitions have been processed")
func (s *Subscriber) detectNewPartitions(ctx context.Context) error {
minWatermarkPartition, err := s.partitionStorage.GetUnfinishedMinWatermarkPartition(ctx)
if err != nil {
return fmt.Errorf("failed to get unfinished min watarmark partition: %w", err)
}
if minWatermarkPartition == nil {
return errDone
}
// To make sure changes for a key is processed in timestamp order, wait until the records returned from all parents have been processed.
partitions, err := s.partitionStorage.GetSchedulablePartitions(ctx, minWatermarkPartition.Watermark)
if err != nil {
return fmt.Errorf("failed to get schedulable partitions: %w", err)
}
if len(partitions) == 0 {
return nil
}
if err := s.partitionStorage.UpdateToScheduled(ctx, partitions); err != nil {
return fmt.Errorf("failed to update to scheduled: %w", err)
}
for _, p := range partitions {
p := p
s.eg.Go(func() error {
return s.queryChangeStream(ctx, p)
})
}
return nil
}
func (s *Subscriber) queryChangeStream(ctx context.Context, p *PartitionMetadata) error {
if err := s.partitionStorage.UpdateToRunning(ctx, p); err != nil {
return fmt.Errorf("failed to update to running: %w", err)
}
stmt := spanner.Statement{
SQL: fmt.Sprintf("SELECT ChangeRecord FROM READ_%s (@startTimestamp, @endTimestamp, @partitionToken, @heartbeatMilliseconds)", s.streamName),
Params: map[string]interface{}{
"startTimestamp": p.Watermark,
"endTimestamp": p.EndTimestamp,
"partitionToken": p.PartitionToken,
"heartbeatMilliseconds": p.HeartbeatMillis,
},
}
if p.IsRootPartition() {
// Must be converted to NULL (root partition).
stmt.Params["partitionToken"] = nil
}
iter := s.spannerClient.Single().QueryWithOptions(ctx, stmt, spanner.QueryOptions{Priority: s.spannerRequestPriority})
if err := iter.Do(func(r *spanner.Row) error {
records := []*changeRecord{}
if err := r.Columns(&records); err != nil {
return err
}
if err := s.handle(ctx, p, records); err != nil {
return err
}
return nil
}); err != nil {
return err
}
if err := s.partitionStorage.UpdateToFinished(ctx, p); err != nil {
return fmt.Errorf("failed to update to finished: %w", err)
}
return nil
}
type watermarker struct {
watermark time.Time
}
func (w *watermarker) set(t time.Time) {
if t.After(w.watermark) {
w.watermark = t
}
}
func (w *watermarker) get() time.Time {
return w.watermark
}
func (s *Subscriber) handle(ctx context.Context, p *PartitionMetadata, records []*changeRecord) error {
var watermarker watermarker
for _, cr := range records {
for _, record := range cr.DataChangeRecords {
if err := s.consumer.Consume(record.decodeToNonSpannerType()); err != nil {
return err
}
watermarker.set(record.CommitTimestamp)
}
for _, record := range cr.HeartbeatRecords {
watermarker.set(record.Timestamp)
}
for _, record := range cr.ChildPartitionsRecords {
if err := s.partitionStorage.AddChildPartitions(ctx, p, record); err != nil {
return fmt.Errorf("failed to add child partitions: %w", err)
}
watermarker.set(record.StartTimestamp)
}
}
if err := s.partitionStorage.UpdateWatermark(ctx, p, watermarker.get()); err != nil {
return fmt.Errorf("failed to update watermark: %w", err)
}
return nil
}