-
Notifications
You must be signed in to change notification settings - Fork 365
/
Copy pathbuffered_bulk.go
309 lines (266 loc) · 8.78 KB
/
buffered_bulk.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
// Copyright (C) MongoDB, Inc. 2014-present.
//
// 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
package db
import (
"context"
"fmt"
"strings"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// The default value of maxMessageSizeBytes
// See: https://docs.mongodb.com/manual/reference/command/hello/#mongodb-data-hello.maxMessageSizeBytes
const MAX_MESSAGE_SIZE_BYTES = 48000000
// BufferedBulkInserter implements a bufio.Writer-like design for queuing up
// documents and inserting them in bulk when the given doc limit (or max
// message size) is reached. Must be flushed at the end to ensure that all
// documents are written.
type BufferedBulkInserter struct {
collection *mongo.Collection
writeModels []mongo.WriteModel
docs []bson.D
docLimit int
docCount int
byteCount int
byteLimit int
bulkWriteOpts *options.BulkWriteOptions
upsert bool
}
func newBufferedBulkInserter(
collection *mongo.Collection,
docLimit int,
ordered bool,
) *BufferedBulkInserter {
bb := &BufferedBulkInserter{
collection: collection,
bulkWriteOpts: options.BulkWrite().SetOrdered(ordered),
docLimit: docLimit,
// We set the byte limit to be slightly lower than maxMessageSizeBytes so it can fit in one OP_MSG.
// This may not always be perfect, e.g. we don't count update selectors in byte totals, but it should
// be good enough to keep memory consumption in check.
byteLimit: MAX_MESSAGE_SIZE_BYTES - 100,
writeModels: make([]mongo.WriteModel, 0, docLimit),
}
return bb
}
// NewOrderedBufferedBulkInserter returns an initialized BufferedBulkInserter for performing ordered bulk writes.
func NewOrderedBufferedBulkInserter(
collection *mongo.Collection,
docLimit int,
) *BufferedBulkInserter {
return newBufferedBulkInserter(collection, docLimit, true)
}
// NewOrderedBufferedBulkInserter returns an initialized BufferedBulkInserter for performing unordered bulk writes.
func NewUnorderedBufferedBulkInserter(
collection *mongo.Collection,
docLimit int,
) *BufferedBulkInserter {
return newBufferedBulkInserter(collection, docLimit, false)
}
func (bb *BufferedBulkInserter) SetOrdered(ordered bool) *BufferedBulkInserter {
bb.bulkWriteOpts.SetOrdered(ordered)
return bb
}
func (bb *BufferedBulkInserter) SetBypassDocumentValidation(bypass bool) *BufferedBulkInserter {
bb.bulkWriteOpts.SetBypassDocumentValidation(bypass)
return bb
}
func (bb *BufferedBulkInserter) SetUpsert(upsert bool) *BufferedBulkInserter {
bb.upsert = upsert
return bb
}
// throw away the old bulk and init a new one.
func (bb *BufferedBulkInserter) ResetBulk() {
bb.writeModels = bb.writeModels[:0]
bb.docCount = 0
bb.byteCount = 0
bb.docs = bb.docs[:0]
}
// Insert adds a document to the buffer for bulk insertion. If the buffer becomes full, the bulk write is performed, returning
// any error that occurs.
func (bb *BufferedBulkInserter) Insert(doc bson.D) (*mongo.BulkWriteResult, error) {
rawBytes, err := bson.Marshal(doc)
if err != nil {
return nil, fmt.Errorf("bson encoding error: %v", err)
}
bb.docs = append(bb.docs, doc)
return bb.InsertRaw(rawBytes)
}
// Update adds a document to the buffer for bulk update. If the buffer becomes full, the bulk write is performed, returning
// any error that occurs.
func (bb *BufferedBulkInserter) Update(selector, update bson.D) (*mongo.BulkWriteResult, error) {
rawBytes, err := bson.Marshal(update)
if err != nil {
return nil, err
}
bb.byteCount += len(rawBytes)
return bb.addModel(
mongo.NewUpdateOneModel().SetFilter(selector).SetUpdate(rawBytes).SetUpsert(bb.upsert),
)
}
// Replace adds a document to the buffer for bulk replacement. If the buffer becomes full, the bulk write is performed, returning
// any error that occurs.
func (bb *BufferedBulkInserter) Replace(
selector, replacement bson.D,
) (*mongo.BulkWriteResult, error) {
rawBytes, err := bson.Marshal(replacement)
if err != nil {
return nil, err
}
bb.byteCount += len(rawBytes)
return bb.addModel(
mongo.NewReplaceOneModel().
SetFilter(selector).
SetReplacement(rawBytes).
SetUpsert(bb.upsert),
)
}
// InsertRaw adds a document, represented as raw bson bytes, to the buffer for bulk insertion. If the buffer becomes full,
// the bulk write is performed, returning any error that occurs.
func (bb *BufferedBulkInserter) InsertRaw(rawBytes []byte) (*mongo.BulkWriteResult, error) {
bb.byteCount += len(rawBytes)
return bb.addModel(mongo.NewInsertOneModel().SetDocument(rawBytes))
}
// Delete adds a document to the buffer for bulk removal. If the buffer becomes full, the bulk delete is performed, returning
// any error that occurs.
func (bb *BufferedBulkInserter) Delete(
selector, replacement bson.D,
) (*mongo.BulkWriteResult, error) {
return bb.addModel(mongo.NewDeleteOneModel().SetFilter(selector))
}
// addModel adds a WriteModel to the buffer. If the buffer becomes full, the bulk write is performed, returning any error
// that occurs.
func (bb *BufferedBulkInserter) addModel(model mongo.WriteModel) (*mongo.BulkWriteResult, error) {
bb.docCount++
bb.writeModels = append(bb.writeModels, model)
if bb.docCount >= bb.docLimit || bb.byteCount >= bb.byteLimit {
return bb.Flush()
}
return nil, nil
}
// Flush writes all buffered documents in one bulk write and then resets the buffer.
func (bb *BufferedBulkInserter) Flush() (*mongo.BulkWriteResult, error) {
defer bb.ResetBulk()
return bb.flush()
}
// TryFlush writes all buffered documents in one bulk write without resetting the buffer.
func (bb *BufferedBulkInserter) TryFlush() (*mongo.BulkWriteResult, error) {
return bb.flush()
}
func (bb *BufferedBulkInserter) flush() (*mongo.BulkWriteResult, error) {
ctx := context.Background()
if bb.docCount == 0 {
return nil, nil
}
res, bulkWriteErr := bb.collection.BulkWrite(ctx, bb.writeModels, bb.bulkWriteOpts)
if bulkWriteErr == nil {
return res, nil
}
bulkWriteException, ok := bulkWriteErr.(mongo.BulkWriteException)
if !ok {
return res, bulkWriteErr
}
var retryDocFilters []bson.D
for _, we := range bulkWriteException.WriteErrors {
if we.Code == ErrDuplicateKeyCode {
var errDetails map[string]bson.Raw
bson.Unmarshal(we.WriteError.Raw, &errDetails)
var filter bson.D
bson.Unmarshal(errDetails["keyValue"], &filter)
exists, err := checkDocumentExistence(ctx, bb.collection, filter)
if err != nil {
return nil, err
}
if !exists {
retryDocFilters = append(retryDocFilters, filter)
} else {
}
}
}
for _, filter := range retryDocFilters {
for _, doc := range bb.docs {
var exists bool
var err error
if compareDocumentWithKeys(filter, doc) {
for range(3) {
_, err = bb.collection.InsertOne(ctx, doc)
if err == nil {
break
}
}
exists, err = checkDocumentExistence(ctx, bb.collection, filter)
if err != nil {
return nil, err
}
if exists {
break
}
}
if !exists {
return nil, fmt.Errorf("could not insert document %+v", doc)
}
}
}
res.InsertedCount += int64(len(retryDocFilters))
return res, bulkWriteErr
}
// extractValueByPath digs into a bson.D using a dotted path to retrieve the value
func extractValueByPath(doc bson.D, path string) (interface{}, bool) {
parts := strings.Split(path, ".")
var current interface{} = doc
for _, part := range parts {
switch curr := current.(type) {
case bson.D:
found := false
for _, elem := range curr {
if elem.Key == part {
current = elem.Value
found = true
break
}
}
if !found {
return nil, false
}
default:
return nil, false
}
}
return current, true
}
// compareDocumentWithKeys checks if the key-value pairs in doc1 exist in doc2
func compareDocumentWithKeys(doc1 bson.D, doc2 bson.D) bool {
for _, elem := range doc1 {
value, exists := extractValueByPath(doc2, elem.Key)
if !exists || value != elem.Value {
return false
}
}
return true
}
func checkDocumentExistence(ctx context.Context, collection *mongo.Collection, document bson.D) (bool, error) {
findCmd := bson.D{
{Key: "find", Value: collection.Name()},
{Key: "filter", Value: document},
{Key: "readConcern", Value: bson.D{{Key: "level", Value: "majority"}}},
}
db := collection.Database()
var result bson.M
err := db.RunCommand(ctx, findCmd).Decode(&result)
if err != nil {
return false, err
}
if cursor, ok := result["cursor"].(bson.M); ok {
if firstBatch, ok := cursor["firstBatch"].(bson.A); ok && len(firstBatch) > 0 {
return true, nil
} else {
return false, nil
}
} else {
return false, err
}
}