-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.go
272 lines (231 loc) · 6 KB
/
utils.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
package scyllacdc
import (
"context"
"errors"
"fmt"
"io"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/gocql/gocql"
)
// PeriodicProgressReporter is a wrapper around ProgressReporter which can be
// used to save progress in regular periods of time.
type PeriodicProgressReporter struct {
reporter *ProgressReporter
interval time.Duration
refreshCh chan struct{}
stopCh chan struct{}
finishCh chan struct{}
mu *sync.Mutex
timeToReport gocql.UUID
logger Logger
}
// NewPeriodicProgressReporter creates a new PeriodicProgressReporter with
// given report interval.
func NewPeriodicProgressReporter(logger Logger, interval time.Duration, reporter *ProgressReporter) *PeriodicProgressReporter {
return &PeriodicProgressReporter{
reporter: reporter,
interval: interval,
refreshCh: make(chan struct{}, 1),
stopCh: make(chan struct{}),
finishCh: make(chan struct{}),
mu: &sync.Mutex{},
logger: logger,
}
}
// Start spawns an internal goroutine and starts the progress reporting loop.
func (ppr *PeriodicProgressReporter) Start(ctx context.Context) {
// Optimization: if the reporter is nil, or is NoProgressManager,
// then don't start the goroutine at all.
if _, ok := ppr.reporter.progressManager.(noProgressManager); ok {
close(ppr.finishCh)
return
}
go func() {
defer close(ppr.finishCh)
for {
// Wait for the duration period
select {
case <-time.After(ppr.interval):
case <-ctx.Done():
return
case <-ppr.stopCh:
return
}
// Wait for a signal to refresh
select {
case <-ppr.refreshCh:
ppr.mu.Lock()
timeToReport := ppr.timeToReport
ppr.mu.Unlock()
// TODO: Log errors?
err := ppr.reporter.MarkProgress(ctx, Progress{timeToReport})
if err != nil {
ppr.logger.Printf("failed to save progress for %s: %s", ppr.reporter.streamID, err)
}
case <-ctx.Done():
return
case <-ppr.stopCh:
return
}
}
}()
}
// Update tells the PeriodicProgressReporter that a row has been processed.
func (ppr *PeriodicProgressReporter) Update(newTime gocql.UUID) {
ppr.mu.Lock()
ppr.timeToReport = newTime
ppr.mu.Unlock()
// Fill the channel in a non-blocking manner
select {
case ppr.refreshCh <- struct{}{}:
default:
}
}
// Stop stops inner goroutine and waits until it finishes.
func (ppr *PeriodicProgressReporter) Stop() {
close(ppr.stopCh)
<-ppr.finishCh
}
// SaveAndStop stops inner goroutine, waits until it finishes, and then
// saves the most recent progress.
func (ppr *PeriodicProgressReporter) SaveAndStop(ctx context.Context) error {
close(ppr.stopCh)
<-ppr.finishCh
// No need to lock the mutex for timeToReport
if (ppr.timeToReport == gocql.UUID{}) {
return nil
}
err := ppr.reporter.MarkProgress(ctx, Progress{ppr.timeToReport})
if err != nil {
ppr.logger.Printf("failed to save progress for %s: %s", ppr.reporter.streamID, err)
} else {
ppr.logger.Printf("successfully saved final progress for %s: %s (%s)", ppr.reporter.streamID, ppr.timeToReport, ppr.timeToReport.Time())
}
return err
}
func CompareTimeUUID(u1, u2 gocql.UUID) int {
// Compare timestamps
t1 := u1.Timestamp()
t2 := u2.Timestamp()
if t1 < t2 {
return -1
}
if t1 > t2 {
return 1
}
// Lexicographically compare the second half as signed bytes
for i := 8; i < 16; i++ {
d := int(int8(u1[i])) - int(int8(u2[i]))
if d != 0 {
return int(d)
}
}
return 0
}
var validIDPattern = regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9_]*$")
func escapeColumnNameIfNeeded(s string) string {
if shouldEscape(s) {
return escapeColumnName(s)
}
return s
}
func shouldEscape(s string) bool {
// TODO: Check if it is a reserved keyword - for now, assume it's not
return !validIDPattern.MatchString(s)
}
func escapeColumnName(s string) string {
return "\"" + strings.ReplaceAll(s, "\"", "\\\"") + "\""
}
func fetchScyllaCDCExtensionTTL(
session *gocql.Session,
keyspaceName string,
tableName string,
) (int64, error) {
// Extensions are not available in the metadata,
// fetch and parse them manually until this is implemented in gocql
var exts map[string][]byte
err := session.Query(
"SELECT extensions FROM system_schema.tables "+
"WHERE keyspace_name = ? AND table_name = ?",
keyspaceName, tableName,
).Scan(&exts)
if err != nil {
return 0, fmt.Errorf("failed to query system tables: %w", err)
}
ext, ok := exts["cdc"]
if !ok {
return 0, errors.New("cdc extension not found")
}
m, err := newExtensionParser(ext).parseStringMap()
if err != nil {
return 0, fmt.Errorf("failed to parse the CDC extension: %w", err)
}
ttlS, ok := m["ttl"]
if !ok {
return 0, errors.New("ttl not set")
}
ttl, err := strconv.ParseInt(ttlS, 10, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse TTL from schema extension: %w", err)
}
return ttl, nil
}
type extensionParser struct {
raw []byte
}
func newExtensionParser(raw []byte) *extensionParser {
return &extensionParser{raw}
}
func (ep *extensionParser) parseStringMap() (map[string]string, error) {
l, err := ep.parseInt()
if err != nil {
return nil, err
}
if l < 0 {
return nil, errors.New("invalid map length")
}
m := make(map[string]string)
for i := int32(0); i < l; i++ {
k, err := ep.parseString()
if err != nil {
return nil, fmt.Errorf("failed to parse key #%d: %w", i, err)
}
v, err := ep.parseString()
if err != nil {
return nil, fmt.Errorf("failed to parse value #%d: %w", i, err)
}
m[k] = v
}
return m, nil
}
func (ep *extensionParser) parseInt() (int32, error) {
if len(ep.raw) < 4 {
return 0, io.EOF
}
// Little endian
x := int32(ep.raw[0]) |
(int32(ep.raw[1]) << 8) |
(int32(ep.raw[2]) << 16) |
(int32(ep.raw[3]) << 24)
ep.raw = ep.raw[4:]
return x, nil
}
func (ep *extensionParser) parseString() (string, error) {
l, err := ep.parseInt()
if err != nil {
return "", fmt.Errorf("failed to parse string length: %w", err)
}
if l < 0 {
return "", errors.New("invalid string length")
}
if len(ep.raw) < int(l) {
return "", io.EOF
}
s := string(ep.raw[:l])
ep.raw = ep.raw[l:]
return s, nil
}