/
applier.go
417 lines (355 loc) · 10.6 KB
/
applier.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package oplog
import (
"context"
"fmt"
"io"
"strings"
"github.com/mongodb/mongo-tools-common/db"
"github.com/mongodb/mongo-tools-common/txn"
"github.com/mongodb/mongo-tools-common/util"
"github.com/wal-g/tracelog"
"github.com/wal-g/wal-g/internal/databases/mongo/client"
"github.com/wal-g/wal-g/internal/databases/mongo/models"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type TypeAssertionError struct {
etype string
key string
value interface{}
}
func NewTypeAssertionError(etype, key string, value interface{}) *TypeAssertionError {
return &TypeAssertionError{etype, key, value}
}
func (e *TypeAssertionError) Error() string {
return fmt.Sprintf("assertion to %s failed, field %s: %+v", e.etype, e.key, e.value)
}
type OpHandleError struct {
op interface{}
err error
}
func NewOpHandleError(op interface{}, err error) *OpHandleError {
return &OpHandleError{op, err}
}
func (e *OpHandleError) Error() string {
return fmt.Sprintf("failed to apply with error: %+v\nop:\n%+v", e.err, e.op)
}
var (
jsonBegin = []byte("[\n")
jsonDelimiter = []byte(",\n")
jsonEnd = []byte("\n]\n")
_ = []Applier{&DBApplier{}, &JSONApplier{}, &BSONApplier{}, &BSONRawApplier{}}
)
// Applier defines interface to apply given oplog records.
type Applier interface {
Apply(ctx context.Context, opr models.Oplog) error
Close(ctx context.Context) error
}
// NewWriteApplier builds one of write appliers
func NewWriteApplier(format string, wc io.WriteCloser) (Applier, error) {
switch format {
case "json":
return NewJSONApplier(wc), nil
case "bson":
return NewBSONApplier(wc), nil
case "bson-raw":
return NewBSONRawApplier(wc), nil
}
return nil, fmt.Errorf("wrong write applier format: %s", format)
}
// DBApplier implements Applier interface for mongodb.
type DBApplier struct {
db client.MongoDriver
txnBuffer *txn.Buffer
preserveUUID bool
applyIgnoreErrorCodes map[string][]int32
}
// NewDBApplier builds DBApplier with given args.
func NewDBApplier(m client.MongoDriver, preserveUUID bool, ignoreErrCodes map[string][]int32) *DBApplier {
return &DBApplier{db: m, txnBuffer: txn.NewBuffer(), preserveUUID: preserveUUID, applyIgnoreErrorCodes: ignoreErrCodes}
}
func (ap *DBApplier) Apply(ctx context.Context, opr models.Oplog) error {
op := db.Oplog{}
if err := bson.Unmarshal(opr.Data, &op); err != nil {
return fmt.Errorf("can not unmarshal oplog entry: %w", err)
}
if err := ap.shouldSkip(op.Operation, op.Namespace); err != nil {
tracelog.DebugLogger.Printf("skipping op %+v due to: %+v", op, err)
return nil
}
meta, err := txn.NewMeta(op)
if err != nil {
return fmt.Errorf("can not extract op metadata: %w", err)
}
if meta.IsTxn() {
err = ap.handleTxnOp(ctx, meta, op)
} else {
err = ap.handleNonTxnOp(ctx, op)
}
if err != nil {
return err
}
return nil
}
func (ap *DBApplier) Close(ctx context.Context) error {
if err := ap.db.Close(ctx); err != nil {
return err
}
if err := ap.txnBuffer.Stop(); err != nil {
return err
}
return nil
}
func (ap *DBApplier) shouldSkip(op, ns string) error {
if op == "n" {
return fmt.Errorf("noop op")
}
// sharded clusters are not supported yet
if strings.HasPrefix(ns, "config.") {
return fmt.Errorf("config database op")
}
return nil
}
// shouldIgnore checks if error should be ignored
func (ap *DBApplier) shouldIgnore(op string, err error) bool {
ce, ok := err.(mongo.CommandError)
if !ok {
return false
}
ignoreErrorCodes, ok := ap.applyIgnoreErrorCodes[op]
if !ok {
return false
}
for i := range ignoreErrorCodes {
if ce.Code == ignoreErrorCodes[i] {
return true
}
}
return false
}
// handleNonTxnOp tries to apply given oplog record.
func (ap *DBApplier) handleNonTxnOp(ctx context.Context, op db.Oplog) error {
if !ap.preserveUUID {
var err error
op, err = filterUUIDs(op)
if err != nil {
return fmt.Errorf("can not filter UUIDs from op '%+v', error: %+v", op, err)
}
}
// TODO: wait for index building
if op.Operation == "c" && op.Object[0].Key == "commitIndexBuild" {
collName, indexes, err := indexSpecFromCommitIndexBuilds(op)
if err != nil {
return NewOpHandleError(op, err)
}
dbName, _ := util.SplitNamespace(op.Namespace)
return ap.db.CreateIndexes(ctx, dbName, collName, indexes)
}
if op.Operation == "c" && op.Object[0].Key == "createIndexes" {
collName, indexes, err := indexSpecsFromCreateIndexes(op)
if err != nil {
return NewOpHandleError(op, err)
}
dbName, _ := util.SplitNamespace(op.Namespace)
return ap.db.CreateIndexes(ctx, dbName, collName,
[]client.IndexDocument{indexes})
}
//tracelog.DebugLogger.Printf("applying op: %+v", op)
if err := ap.db.ApplyOp(ctx, op); err != nil {
// we ignore some errors (for example 'duplicate key error')
// TODO: check after TOOLS-2041
if !ap.shouldIgnore(op.Operation, err) {
return NewOpHandleError(op, err)
}
tracelog.WarningLogger.Printf("apply error is skipped: %+v\nop:\n%+v", err, op)
}
return nil
}
func indexSpecsFromCreateIndexes(op db.Oplog) (string, client.IndexDocument, error) {
index := client.IndexDocument{Options: bson.M{}}
var collName string
var elem bson.E
var ok bool
for i := range op.Object {
elem = op.Object[i]
switch elem.Key {
case "createIndexes":
if collName, ok = elem.Value.(string); !ok {
return "", client.IndexDocument{}, NewTypeAssertionError("string", "createIndexes", elem.Value)
}
case "key":
if index.Key, ok = elem.Value.(bson.D); !ok {
return "", client.IndexDocument{}, NewTypeAssertionError("bson.D", "key", elem.Value)
}
default:
index.Options[elem.Key] = elem.Value
}
}
return collName, index, nil
}
func indexSpecFromCommitIndexBuilds(op db.Oplog) (string, []client.IndexDocument, error) {
var collName string
var ok bool
var elemE bson.E
for i := range op.Object {
elemE = op.Object[i]
if elemE.Key == "commitIndexBuild" {
if collName, ok = elemE.Value.(string); !ok {
return "", nil, NewTypeAssertionError("string", "commitIndexBuild", elemE.Value)
}
}
}
for i := range op.Object {
elemE = op.Object[i]
if elemE.Key == "indexes" {
indexes, ok := elemE.Value.(bson.A)
if !ok {
return "", nil, NewTypeAssertionError("bson.A", "indexes", elemE.Value)
}
indexSpecs := make([]client.IndexDocument, len(indexes))
for i := range indexes {
indexSpecs[i].Options = bson.M{}
elements, ok := indexes[i].(bson.D)
if !ok {
return "", nil, NewTypeAssertionError("bson.D", fmt.Sprintf("indexes[%d]", i), elemE.Value)
}
for i := range elements {
elemE = elements[i]
if elemE.Key == "key" {
if indexSpecs[i].Key, ok = elemE.Value.(bson.D); !ok {
return "", nil, NewTypeAssertionError("bson.D", "key", elemE.Value)
}
} else {
indexSpecs[i].Options[elemE.Key] = elemE.Value
}
}
}
return collName, indexSpecs, nil
}
}
return "", nil, fmt.Errorf("can not find indexes key in op.Object: %+v", op)
}
// handleTxnOp handles oplog record with transaction attributes.
// TODO: unit test
func (ap *DBApplier) handleTxnOp(ctx context.Context, meta txn.Meta, op db.Oplog) error {
if meta.IsAbort() {
if err := ap.txnBuffer.PurgeTxn(meta); err != nil {
return fmt.Errorf("can not clean txn buffer after rollback cmd: %w", err)
}
}
if err := ap.txnBuffer.AddOp(meta, op); err != nil {
return fmt.Errorf("can not append command to txn buffer: %w", err)
}
if !meta.IsCommit() {
return nil
}
if err := ap.applyTxn(ctx, meta); err != nil {
return err
}
if err := ap.txnBuffer.PurgeTxn(meta); err != nil {
return fmt.Errorf("txn buffer failed to purge: %w", err)
}
return nil
}
func (ap *DBApplier) applyTxn(ctx context.Context, meta txn.Meta) error {
opc, errc := ap.txnBuffer.GetTxnStream(meta)
for {
select {
case op, ok := <-opc:
if !ok {
return nil
}
if err := ap.handleNonTxnOp(ctx, op); err != nil {
return err
}
case err, ok := <-errc:
if ok {
return err
}
case <-ctx.Done():
// opc and errc channels will be closed in PurgeTxn or Stop calls
return nil
}
}
}
// JSONApplier implements Applier interface for debugging.
type JSONApplier struct {
writer io.WriteCloser
started bool
}
// NewJSONApplier builds JSONApplier with given args.
func NewJSONApplier(w io.WriteCloser) *JSONApplier {
return &JSONApplier{writer: w, started: false}
}
func (ap *JSONApplier) Apply(ctx context.Context, opr models.Oplog) error {
op := db.Oplog{}
if err := bson.Unmarshal(opr.Data, &op); err != nil {
return fmt.Errorf("can not unmarshal oplog entry: %w", err)
}
jsonData, err := bson.MarshalExtJSON(op, true, true)
if err != nil {
return fmt.Errorf("can not convert to json: %w", err)
}
if !ap.started {
if _, err := ap.writer.Write(jsonBegin); err != nil {
return fmt.Errorf("can not write begin mark: %w", err)
}
ap.started = true
} else if _, err := ap.writer.Write(jsonDelimiter); err != nil {
return fmt.Errorf("can not write delimiter: %w", err)
}
if _, err := ap.writer.Write(jsonData); err != nil {
return fmt.Errorf("can not write json data: %w", err)
}
return nil
}
func (ap *JSONApplier) Close(ctx context.Context) error {
if ap.started {
if _, err := ap.writer.Write(jsonEnd); err != nil {
return fmt.Errorf("can not write end mark: %w", err)
}
}
return ap.writer.Close()
}
// BSONApplier implements Applier interface for debugging.
type BSONApplier struct {
writer io.WriteCloser
}
// NewBSONApplier builds BSONApplier with given args.
func NewBSONApplier(w io.WriteCloser) *BSONApplier {
return &BSONApplier{writer: w}
}
func (ap *BSONApplier) Apply(ctx context.Context, opr models.Oplog) error {
op := db.Oplog{}
if err := bson.Unmarshal(opr.Data, &op); err != nil {
return fmt.Errorf("can not unmarshal oplog entry: %w", err)
}
bsonBytes, err := bson.Marshal(op)
if err != nil {
return fmt.Errorf("can not marshal oplog entry: %w", err)
}
if _, err := ap.writer.Write(bsonBytes); err != nil {
return fmt.Errorf("can not write bson data: %w", err)
}
return nil
}
func (ap *BSONApplier) Close(ctx context.Context) error {
return ap.writer.Close()
}
// BSONRawApplier implements Applier interface for debugging.
type BSONRawApplier struct {
writer io.WriteCloser
}
// NewBSONRawApplier builds BSONRawApplier with given args.
func NewBSONRawApplier(w io.WriteCloser) *BSONRawApplier {
return &BSONRawApplier{writer: w}
}
func (ap *BSONRawApplier) Apply(ctx context.Context, opr models.Oplog) error {
if _, err := ap.writer.Write(opr.Data); err != nil {
return fmt.Errorf("can not write raw bson data: %w", err)
}
return nil
}
func (ap *BSONRawApplier) Close(ctx context.Context) error {
return ap.writer.Close()
}