-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
bulkwriter.go
346 lines (298 loc) · 11.1 KB
/
bulkwriter.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
343
344
345
346
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package firestore
import (
"context"
"errors"
"fmt"
"sync"
"time"
vkit "cloud.google.com/go/firestore/apiv1"
pb "cloud.google.com/go/firestore/apiv1/firestorepb"
"golang.org/x/time/rate"
"google.golang.org/api/support/bundler"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
// maxBatchSize is the max number of writes to send in a request
maxBatchSize = 20
// maxRetryAttempts is the max number of times to retry a write
maxRetryAttempts = 10
// defaultStartingMaximumOpsPerSecond is the starting max number of requests to the service per second
defaultStartingMaximumOpsPerSecond = 500
// maxWritesPerSecond is the starting limit of writes allowed to callers per second
maxWritesPerSecond = maxBatchSize * defaultStartingMaximumOpsPerSecond
)
// bulkWriterResult contains the WriteResult or error results from an individual
// write to the database.
type bulkWriterResult struct {
result *pb.WriteResult // (cached) result from the operation
err error // (cached) any errors that occurred
}
// BulkWriterJob provides read-only access to the results of a BulkWriter write attempt.
type BulkWriterJob struct {
resultChan chan bulkWriterResult // send errors and results to this channel
write *pb.Write // the writes to apply to the database
attempts int // number of times this write has been attempted
resultsLock sync.Mutex // guards the cached wr and e values for the job
result *WriteResult // (cached) result from the operation
err error // (cached) any errors that occurred
ctx context.Context // context for canceling/timing out results
}
// Results gets the results of the BulkWriter write attempt.
// This method blocks if the results for this BulkWriterJob haven't been
// received.
func (j *BulkWriterJob) Results() (*WriteResult, error) {
j.resultsLock.Lock()
defer j.resultsLock.Unlock()
if j.result == nil && j.err == nil {
j.result, j.err = j.processResults() // cache the results for additional calls
}
return j.result, j.err
}
// processResults checks for errors returned from send() and packages up the
// results as WriteResult objects
func (j *BulkWriterJob) processResults() (*WriteResult, error) {
select {
case <-j.ctx.Done():
return nil, j.ctx.Err()
case bwr := <-j.resultChan:
if bwr.err != nil {
return nil, bwr.err
}
return writeResultFromProto(bwr.result)
}
}
// setError ensures that an error is returned on the error channel of BulkWriterJob.
func (j *BulkWriterJob) setError(e error) {
bwr := bulkWriterResult{
err: e,
result: nil,
}
j.resultChan <- bwr
close(j.resultChan)
}
// A BulkWriter supports concurrent writes to multiple documents. The BulkWriter
// submits document writes in maximum batches of 20 writes per request. Each
// request can contain many different document writes: create, delete, update,
// and set are all supported.
//
// Only one operation (create, set, update, delete) per document is allowed.
// BulkWriter cannot promise atomicity: individual writes can fail or succeed
// independent of each other. Bulkwriter does not apply writes in any set order;
// thus a document can't have set on it immediately after creation.
type BulkWriter struct {
database string // the database as resource name: projects/[PROJECT]/databases/[DATABASE]
start time.Time // when this BulkWriter was started; used to calculate qps and rate increases
vc *vkit.Client // internal client
maxOpsPerSecond int // number of requests that can be sent per second
docUpdatePaths map[string]bool // document paths with corresponding writes in the queue
limiter rate.Limiter // limit requests to server to <= 500 qps
bundler *bundler.Bundler // handle bundling up writes to Firestore
ctx context.Context // context for canceling all BulkWriter operations
isOpenLock sync.RWMutex // guards against setting isOpen concurrently
isOpen bool // flag that the BulkWriter is closed
}
// newBulkWriter creates a new instance of the BulkWriter.
func newBulkWriter(ctx context.Context, c *Client, database string) *BulkWriter {
// Although typically we shouldn't store Context objects, in this case we
// need to pass this Context through to the Bundler handler.
ctx = withResourceHeader(ctx, c.path())
bw := &BulkWriter{
database: database,
start: time.Now(),
vc: c.c,
isOpen: true,
maxOpsPerSecond: defaultStartingMaximumOpsPerSecond,
docUpdatePaths: make(map[string]bool),
ctx: ctx,
limiter: *rate.NewLimiter(rate.Limit(maxWritesPerSecond), 1),
}
// can't initialize within struct above; need instance reference to BulkWriter.send()
bw.bundler = bundler.NewBundler(&BulkWriterJob{}, bw.send)
bw.bundler.HandlerLimit = bw.maxOpsPerSecond
bw.bundler.BundleCountThreshold = maxBatchSize
return bw
}
// End sends all enqueued writes in parallel and closes the BulkWriter to new requests.
// After calling End(), calling any additional method automatically returns
// with an error. This method completes when there are no more pending writes
// in the queue.
func (bw *BulkWriter) End() {
bw.isOpenLock.Lock()
bw.isOpen = false
bw.isOpenLock.Unlock()
bw.Flush()
}
// Flush commits all writes that have been enqueued up to this point in parallel.
// This method blocks execution.
func (bw *BulkWriter) Flush() {
bw.bundler.Flush()
}
// Create adds a document creation write to the queue of writes to send.
// Note: You cannot write to (Create, Update, Set, or Delete) the same document more than once.
func (bw *BulkWriter) Create(doc *DocumentRef, datum interface{}) (*BulkWriterJob, error) {
bw.isOpenLock.RLock()
defer bw.isOpenLock.RUnlock()
err := bw.checkWriteConditions(doc)
if err != nil {
return nil, err
}
w, err := doc.newCreateWrites(datum)
if err != nil {
return nil, fmt.Errorf("firestore: cannot create %v with %v", doc.ID, datum)
}
if len(w) > 1 {
return nil, fmt.Errorf("firestore: too many document writes sent to bulkwriter")
}
j := bw.write(w[0])
return j, nil
}
// Delete adds a document deletion write to the queue of writes to send.
// Note: You cannot write to (Create, Update, Set, or Delete) the same document more than once.
func (bw *BulkWriter) Delete(doc *DocumentRef, preconds ...Precondition) (*BulkWriterJob, error) {
bw.isOpenLock.RLock()
defer bw.isOpenLock.RUnlock()
err := bw.checkWriteConditions(doc)
if err != nil {
return nil, err
}
w, err := doc.newDeleteWrites(preconds)
if err != nil {
return nil, fmt.Errorf("firestore: cannot delete doc %v", doc.ID)
}
if len(w) > 1 {
return nil, fmt.Errorf("firestore: too many document writes sent to bulkwriter")
}
j := bw.write(w[0])
return j, nil
}
// Set adds a document set write to the queue of writes to send.
// Note: You cannot write to (Create, Update, Set, or Delete) the same document more than once.
func (bw *BulkWriter) Set(doc *DocumentRef, datum interface{}, opts ...SetOption) (*BulkWriterJob, error) {
bw.isOpenLock.RLock()
defer bw.isOpenLock.RUnlock()
err := bw.checkWriteConditions(doc)
if err != nil {
return nil, err
}
w, err := doc.newSetWrites(datum, opts)
if err != nil {
return nil, fmt.Errorf("firestore: cannot set %v on doc %v", datum, doc.ID)
}
if len(w) > 1 {
return nil, fmt.Errorf("firestore: too many writes sent to bulkwriter")
}
j := bw.write(w[0])
return j, nil
}
// Update adds a document update write to the queue of writes to send.
// Note: You cannot write to (Create, Update, Set, or Delete) the same document more than once.
func (bw *BulkWriter) Update(doc *DocumentRef, updates []Update, preconds ...Precondition) (*BulkWriterJob, error) {
bw.isOpenLock.RLock()
defer bw.isOpenLock.RUnlock()
err := bw.checkWriteConditions(doc)
if err != nil {
return nil, err
}
w, err := doc.newUpdatePathWrites(updates, preconds)
if err != nil {
return nil, fmt.Errorf("firestore: cannot update doc %v", doc.ID)
}
if len(w) > 1 {
return nil, fmt.Errorf("firestore: too many writes sent to bulkwriter")
}
j := bw.write(w[0])
return j, nil
}
// checkConditions determines whether this write attempt is valid. It returns
// an error if either the BulkWriter has already been closed or if it
// receives a nil document reference.
func (bw *BulkWriter) checkWriteConditions(doc *DocumentRef) error {
if !bw.isOpen {
return errors.New("firestore: BulkWriter has been closed")
}
if doc == nil {
return errors.New("firestore: nil document contents")
}
_, havePath := bw.docUpdatePaths[doc.shortPath]
if havePath {
return fmt.Errorf("firestore: BulkWriter received duplicate write for path: %v", doc.shortPath)
}
bw.docUpdatePaths[doc.shortPath] = true
return nil
}
// write packages up write requests into bulkWriterJob objects.
func (bw *BulkWriter) write(w *pb.Write) *BulkWriterJob {
j := &BulkWriterJob{
resultChan: make(chan bulkWriterResult, 1),
write: w,
ctx: bw.ctx,
}
bw.limiter.Wait(bw.ctx)
// ignore operation size constraints and related errors; can't be inferred at compile time
// Bundler is set to accept an unlimited amount of bytes
_ = bw.bundler.Add(j, 0)
return j
}
// send transmits writes to the service and matches response results to job channels.
func (bw *BulkWriter) send(i interface{}) {
bwj := i.([]*BulkWriterJob)
if len(bwj) == 0 {
return
}
var ws []*pb.Write
for _, w := range bwj {
ws = append(ws, w.write)
}
bwr := &pb.BatchWriteRequest{
Database: bw.database,
Writes: ws,
Labels: map[string]string{},
}
select {
case <-bw.ctx.Done():
return
default:
resp, err := bw.vc.BatchWrite(bw.ctx, bwr)
if err != nil {
// Do we need to be selective about what kind of errors we send?
for _, j := range bwj {
j.setError(err)
}
return
}
// Match write results with BulkWriterJob objects
for i, res := range resp.WriteResults {
s := resp.Status[i]
c := s.GetCode()
if c != 0 { // Should we do an explicit check against rpc.Code enum?
j := bwj[i]
j.attempts++
// Do we need separate retry bundler?
if j.attempts < maxRetryAttempts {
// ignore operation size constraints and related errors; job size can't be inferred at compile time
// Bundler is set to accept an unlimited amount of bytes
_ = bw.bundler.Add(j, 0)
} else {
j.setError(status.Error(codes.Code(s.Code), s.Message))
}
continue
}
bwj[i].resultChan <- bulkWriterResult{err: nil, result: res}
close(bwj[i].resultChan)
}
}
}