forked from cockroachdb/replicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sink.go
333 lines (297 loc) · 8.43 KB
/
sink.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
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package main
import (
"bufio"
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"sort"
"strings"
"github.com/lib/pq"
)
// Sink holds all the info needed for a specific table.
type Sink struct {
originalTableName string
resultTableFullName string
sinkTableFullName string
primaryKeyColumns []string
endpoint string
}
// CreateSink creates all the required tables and returns a new Sink.
func CreateSink(
db *sql.DB, originalTable string, resultDB string, resultTable string, endpoint string,
) (*Sink, error) {
// Check to make sure the table exists.
resultTableFullName := fmt.Sprintf("%s.%s", resultDB, resultTable)
exists, err := TableExists(db, resultDB, resultTable)
if err != nil {
return nil, err
}
if !exists {
return nil, fmt.Errorf("Table %s could not be found", resultTableFullName)
}
sinkTableFullName := SinkTableFullName(resultDB, resultTable)
if err := CreateSinkTable(db, sinkTableFullName); err != nil {
return nil, err
}
columns, err := GetPrimaryKeyColumns(db, resultTableFullName)
if err != nil {
return nil, err
}
sink := &Sink{
originalTableName: originalTable,
resultTableFullName: resultTableFullName,
sinkTableFullName: sinkTableFullName,
primaryKeyColumns: columns,
endpoint: endpoint,
}
return sink, nil
}
const chunkSize = 1000
// HandleRequest is a handler used for this specific sink.
func (s *Sink) HandleRequest(db *sql.DB, w http.ResponseWriter, r *http.Request) {
scanner := bufio.NewScanner(r.Body)
defer r.Body.Close()
var lines []Line
for scanner.Scan() {
line, err := parseLine(scanner.Bytes())
if err != nil {
log.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
lines = append(lines, line)
if len(lines) >= chunkSize {
if err := WriteToSinkTable(db, s.sinkTableFullName, lines); err != nil {
log.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("%s: added %d operations", s.endpoint, chunkSize)
lines = []Line{}
}
}
if err := WriteToSinkTable(db, s.sinkTableFullName, lines); err != nil {
log.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("%s: added %d operations", s.endpoint, len(lines))
}
// deleteRows preforms all the deletes specified in lines.
func (s *Sink) deleteRows(tx *sql.Tx, lines []Line) error {
if len(lines) == 0 {
return nil
}
var chunks [][]Line
for i := 0; i < len(lines); i += chunkSize {
end := i + chunkSize
if end > len(lines) {
end = len(lines)
}
chunks = append(chunks, lines[i:end])
}
for _, chunk := range chunks {
// Build the statement.
var statement strings.Builder
fmt.Fprintf(&statement, "DELETE FROM %s WHERE (", s.resultTableFullName)
for i, column := range s.primaryKeyColumns {
if i > 0 {
fmt.Fprint(&statement, ",")
}
// Placeholder index always starts at 1.
fmt.Fprintf(&statement, "%s", column)
}
fmt.Fprintf(&statement, ") IN (")
var keys []interface{}
for i, line := range chunk {
if i > 0 {
fmt.Fprintf(&statement, ",")
}
fmt.Fprintf(&statement, "(")
for _, key := range line.Key {
keys = append(keys, key)
fmt.Fprintf(&statement, "$%d", len(keys))
}
fmt.Fprintf(&statement, ")")
}
fmt.Fprintf(&statement, ")")
// Upsert the line
if _, err := tx.Exec(statement.String(), keys...); err != nil {
return err
}
}
return nil
}
// cleanValue will check the type of the value being upserted to ensure it
// can be handled by pq.
func cleanValue(value interface{}) (interface{}, error) {
switch t := value.(type) {
case bool:
// bool
return value, nil
case string:
// bit, date, inet, interval, string, time, timestamp, timestamptz, uuid,
// collated strings
return value, nil
case json.Number:
// decimal, float, int, serial
return value, nil
case []interface{}:
// array
// These must be converted using the specialized pq function.
return pq.Array(value.([]interface{})), nil
case map[string]interface{}:
// jsonb
// This must be marshalled or pq won't be able to insert it.
marshalled, err := json.Marshal(value)
return marshalled, err
case nil:
// null values, regardless of sql type
return nil, nil
default:
log.Printf("Type: %T, value: %s", t, value)
return nil, fmt.Errorf("unsupported type %T", t)
}
}
// upsertRows performs all upserts specified in lines.
func (s *Sink) upsertRows(tx *sql.Tx, lines []Line) error {
if len(lines) == 0 {
return nil
}
// Get all the column names and order them alphabetically.
line0 := lines[0]
if err := line0.parseAfter(); err != nil {
return err
}
var columnNames []string
for name := range line0.After {
columnNames = append(columnNames, name)
}
sort.Strings(columnNames)
var chunks [][]Line
for i := 0; i < len(lines); i += chunkSize {
end := i + chunkSize
if end > len(lines) {
end = len(lines)
}
chunks = append(chunks, lines[i:end])
}
for _, chunk := range chunks {
// Build the statement.
var statement strings.Builder
// TODO: This first part can be memoized as long as there are no schema
// changes.
fmt.Fprintf(&statement, "UPSERT INTO %s (", s.resultTableFullName)
for i, name := range columnNames {
if i > 0 {
fmt.Fprintf(&statement, ",")
}
fmt.Fprintf(&statement, name)
}
fmt.Fprint(&statement, ") VALUES ")
var values []interface{}
for i, line := range chunk {
if err := line.parseAfter(); err != nil {
return nil
}
if i == 0 {
fmt.Fprintf(&statement, "(")
} else {
fmt.Fprintf(&statement, ",(")
}
for j, name := range columnNames {
insertableValue, err := cleanValue(line.After[name])
if err != nil {
return err
}
values = append(values, insertableValue)
if j == 0 {
fmt.Fprintf(&statement, "$%d", len(values))
} else {
fmt.Fprintf(&statement, ",$%d", len(values))
}
}
fmt.Fprintf(&statement, ")")
}
// Upsert the line
if _, err := tx.Exec(statement.String(), values...); err != nil {
return err
}
}
return nil
}
// UpdateRows updates all changed rows.
func (s *Sink) UpdateRows(tx *sql.Tx, prev ResolvedLine, next ResolvedLine) error {
// First, gather all the rows to update.
lines, err := FindAllRowsToUpdate(tx, s.sinkTableFullName, prev, next)
if err != nil {
return err
}
if len(lines) == 0 {
return nil
}
log.Printf("%s: %s executed %d operations", s.endpoint, s.sinkTableFullName, len(lines))
// TODO: Batch these by 100 rows? Not sure what the max should be.
var upserts []Line
var deletes []Line
// This must happen in reverse order and all keys must be kept track of.
// This way, we can ensure that more recent changes overwrite earlier ones
// without having to perform multiple upserts/deletes to the db.
usedKeys := make(map[string]struct{})
for i := len(lines) - 1; i >= 0; i-- {
line := lines[i]
// Did we updates this line already? If so, don't perform this update.
if _, exist := usedKeys[line.key]; exist {
continue
}
usedKeys[line.key] = struct{}{}
// Parse the key into columns
// Large numbers are not turned into strings, so the UseNumber option for
// the decoder is required.
dec := json.NewDecoder(strings.NewReader(line.key))
dec.UseNumber()
if err := dec.Decode(&(line.Key)); err != nil {
return err
}
// Is this needed? What if we have 2 primary key columns but the 2nd one
// nullable or has a default? Does CDC send it?
if len(line.Key) != len(s.primaryKeyColumns) {
return fmt.Errorf(
"table %s has %d primary key columns %v, but only got %d keys %v",
s.resultTableFullName,
len(s.primaryKeyColumns),
s.primaryKeyColumns,
len(line.Key),
line.Key,
)
}
// Is this a delete?
if line.after == "null" {
deletes = append(deletes, line)
} else {
// This must be an upsert statement.
upserts = append(upserts, line)
}
}
// Delete all rows
if err := s.deleteRows(tx, deletes); err != nil {
return err
}
// Upsert all rows
if err := s.upsertRows(tx, upserts); err != nil {
return err
}
// Delete the rows in the sink table.
return DeleteSinkTableLines(tx, s.sinkTableFullName, prev, next)
}