This repository has been archived by the owner on Feb 2, 2019. It is now read-only.
forked from kikinteractive/go-bqstreamer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync_worker.go
267 lines (230 loc) · 7.8 KB
/
sync_worker.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
// Here be a synchronous worker implementation for stream inserting into BigQuery.
package bqstreamer
import (
"net/http"
"time"
bigquery "google.golang.org/api/bigquery/v2"
"google.golang.org/api/googleapi"
)
// An estimated size for queued rows before inserting to BigQuery.
//
// https://cloud.google.com/bigquery/quota-policy#streaminginserts
//
// TODO make this configurable
const rowSize = 500
// SyncWorker streams rows to BigQuery in bulk using synchronous calls.
type SyncWorker struct {
// BigQuery client connection.
service *bigquery.Service
// Internal list to queue rows for stream insert.
rows []Row
// Sleep delay after a rejected insert and before a retry insert attempt.
retryInterval time.Duration
// Maximum retry insert attempts for non-rejected row insert
// e.g. GoogleAPI HTTP errors, generic HTTP errors, etc.
maxRetries int
// Accept rows that contain values that do not match the schema.
// The unknown values are ignored.
// Default is false, which treats unknown values as errors.
ignoreUnknownValues bool
// Insert all valid rows of a request, even if invalid rows exist.
// The default value is false, which causes the entire request
// to fail if any invalid rows exist.
skipInvalidRows bool
}
// NewSyncWorker returns a new SyncWorker.
func NewSyncWorker(client *http.Client, options ...SyncOptionFunc) (*SyncWorker, error) {
service, err := bigquery.New(client)
if err != nil {
return nil, err
}
w := SyncWorker{
service: service,
rows: make([]Row, 0, rowSize),
retryInterval: DefaultSyncRetryInterval,
maxRetries: DefaultSyncMaxRetries,
}
// Override defaults with options if given.
for _, option := range options {
if err := option(&w); err != nil {
return nil, err
}
}
return &w, nil
}
// Enqueue enqueues rows for insert in bulk.
func (w *SyncWorker) Enqueue(row Row) {
w.rows = append(w.rows, row)
}
// RowLen returns the number of enqueued rows in the worker,
// which haven't been inserted into BigQuery yet.
func (w *SyncWorker) RowLen() int {
return len(w.rows)
}
// Insert executes an insert operation in bulk.
// It sorts rows by tables, and inserts them using separate insert requests.
// It also splits rows for the same table if too many rows have been queued,
// according to BigQuery quota policy.
//
// The insert blocks until a response is returned.
// The response contains insert and row errors for the inserted tables.
func (w *SyncWorker) Insert() *InsertErrors {
insertErrs := w.insertAll(w.insertTable)
return insertErrs
}
// InsertWithRetry is similar to Insert(),
// but retries an insert operation multiple times on BigQuery server errors.
//
// See the following article for more info:
// https://cloud.google.com/bigquery/troubleshooting-errors
func (w *SyncWorker) InsertWithRetry() *InsertErrors {
insertErrs := w.insertAll(w.insertTableWithRetry)
return insertErrs
}
// insertAll takes all rows, sorts them across projects, datasets, and tables,
// then inserts them to their respectable tables in BigQuery using InsertAll().
func (w *SyncWorker) insertAll(insertFunc func(projectID, datasetID, tableID string, tbl table) *TableInsertErrors) *InsertErrors {
// Reset rows queue when finished.
defer func() { w.rows = w.rows[:0] }()
// Sort rows by project -> dataset -> table heirarchy.
// Necessary because each InsertAll() request has to be for a single table.
ps := projects{}
for _, r := range w.rows {
p, d, t := r.ProjectID, r.DatasetID, r.TableID
// Create project, dataset and table if uninitalized.
initTableIfNotExists(ps, p, d, t)
// Append row to table,
// and generate random row ID of 16 character length, for de-duplication purposes.
ps[p][d][t] = append(ps[p][d][t], &bigquery.TableDataInsertAllRequestRows{
InsertId: r.InsertID,
Json: r.Data,
})
}
// Stream insert each table to BigQuery.
//
// TODO insert concurrently
var insertErrs InsertErrors
for pID, p := range ps {
for dID, d := range p {
for tID := range d {
tableErrs := insertFunc(pID, dID, tID, d[tID])
insertErrs.Tables = append(insertErrs.Tables, tableErrs)
}
}
}
return &insertErrs
}
// insertTable inserts a single table to BigQuery using InsertAll().
//
// It returns tableErrors contains information about rows that were not inserted.
//
// TODO cache bigquery service instead of creating a new one every insertTable() call
// TODO add support for SkipInvalidRows, IgnoreUnknownValues
func (w *SyncWorker) insertTable(projectID, datasetID, tableID string, tbl table) *TableInsertErrors {
res, err := bigquery.NewTabledataService(w.service).
InsertAll(
projectID, datasetID, tableID,
&bigquery.TableDataInsertAllRequest{
Kind: "bigquery#tableDataInsertAllRequest",
Rows: tbl,
IgnoreUnknownValues: w.ignoreUnknownValues,
SkipInvalidRows: w.skipInvalidRows,
}).
Do()
var rows []*bigquery.TableDataInsertAllResponseInsertErrors
if res != nil {
rows = res.InsertErrors
}
insertIDs := make([]string, 0, len(tbl))
for _, row := range tbl {
insertIDs = append(insertIDs, row.InsertId)
}
// Return response as a single table insert attempt.
return &TableInsertErrors{
InsertAttempts: []*TableInsertAttemptErrors{
&TableInsertAttemptErrors{
err: err,
rows: rows,
insertIDs: insertIDs,
Table: tableID,
Dataset: datasetID,
Project: projectID,
},
},
}
}
// insertTableWithRetry is similar to insertTable,
// but also retries insert operations on certain conditions.
func (w *SyncWorker) insertTableWithRetry(projectID, datasetID, tableID string, tbl table) *TableInsertErrors {
var tableInsertErrs TableInsertErrors
numRetries := 0
for {
numRetries++
// Abort if retries and failed too many times.
if numRetries > w.maxRetries {
err := &TooManyFailedInsertRetriesError{
NumFailedRetries: numRetries,
Project: projectID,
Dataset: datasetID,
Table: tableID,
}
tableInsertErrs.InsertAttempts = append(tableInsertErrs.InsertAttempts, &TableInsertAttemptErrors{err: err})
return &tableInsertErrs
}
// Push this table's insert attempt as an additional one
// in insert attempts slice.
currTableInsertErrs := w.insertTable(projectID, datasetID, tableID, tbl)
currInsertAttempt := currTableInsertErrs.InsertAttempts[0]
tableInsertErrs.InsertAttempts = append(tableInsertErrs.InsertAttempts, currInsertAttempt)
// Retry on certain HTTP responses.
if w.shouldRetryInsert(currInsertAttempt.err) {
continue
}
// If we reached here, it means the insert operation was successful.
// Thus, it is not required to retry the insert operation.
//
// Return all accumulated errors.
break
}
return &tableInsertErrs
}
// shouldRetryInsert checks for given insert HTTP response error,
// and returns true if the insert should be retried.
//
// It also sleeps if the return value is true,
// as a backoff mechanism.
//
// There are various cases where a retry is or is not necessary.
// See the following article for more info:
// https://cloud.google.com/bigquery/troubleshooting-errors
//
// TODO add exponential backoff
func (w *SyncWorker) shouldRetryInsert(err error) bool {
if err != nil {
// Retry on GoogleAPI HTTP server errors 500, 503.
if gerr, ok := err.(*googleapi.Error); ok {
switch gerr.Code {
case 500, 503:
time.Sleep(w.retryInterval)
return true
}
}
}
return false
}
// initTableIfNotExists initializes given project, dataset, and table
// in project map if not yet initialized.
func initTableIfNotExists(ps map[string]project, p, d, t string) {
// Create table's project if non-existent.
if _, ok := ps[p]; !ok {
ps[p] = project{}
}
// Create table's dataset if non-existent.
if _, ok := ps[p][d]; !ok {
ps[p][d] = dataset{}
}
// Create table if non-existent.
if _, ok := ps[p][d][t]; !ok {
ps[p][d][t] = table{}
}
}