-
Notifications
You must be signed in to change notification settings - Fork 9
/
oplog_utils.go
281 lines (272 loc) · 8.61 KB
/
oplog_utils.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
// Copyright Kuei-chun Chen, 2022-present. All rights reserved.
package hummingbird
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/simagix/gox"
"github.com/simagix/keyhole/mdb"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// OplogWriteModel stores namespace and writeModel
type OplogWriteModel struct {
Namespace string
Operation string
WriteModel mongo.WriteModel
}
// GetTailableCursor returns a tailable cursor
func GetTailableCursor(client *mongo.Client, ts *primitive.Timestamp) (*mongo.Cursor, error) {
coll := client.Database("local").Collection("oplog.rs")
opts := options.Find()
opts.SetNoCursorTimeout(true)
opts.SetCursorType(options.TailableAwait)
opts.SetBatchSize(OplogBatchSize)
opts.SetMaxAwaitTime(time.Second)
filter := bson.M{"ts": bson.M{"$gte": ts}}
return coll.Find(context.Background(), filter, opts)
}
// SkipOplog returns if oplog should be skipped
func SkipOplog(oplog Oplog) bool {
dbName, collName := mdb.SplitNamespace(oplog.Namespace)
if dbName == "" || dbName == "local" || dbName == "config" {
return true
}
var err error
inst := GetMigratorInstance()
if collName == "$cmd" {
for _, v := range oplog.Object {
if v.Key == "dropDatabase" {
return inst.SkipNamespace(dbName + ".*")
} else if v.Key == "create" || v.Key == "createIndexes" || v.Key == "drop" || v.Key == "renameCollection" {
collName = v.Value.(string)
return inst.SkipNamespace(dbName + "." + collName)
} else if v.Key == "applyOps" {
oplogs, ok := v.Value.(primitive.A)
if !ok {
continue
}
for _, inlog := range oplogs {
var doc Oplog
var data []byte
if data, err = bson.Marshal(inlog); err != nil {
continue
}
if err = bson.Unmarshal(data, &doc); err != nil {
continue
}
return inst.SkipNamespace(doc.Namespace)
}
return false // unknown, keep it for further investigation
}
}
return false // unknown, keep it for further investigation
}
return inst.SkipNamespace(oplog.Namespace)
}
// BulkWriteOplogsResult stores results
type BulkWriteOplogsResult struct {
DeletedCount int64
InsertedCount int64
ModifiedCount int64
UpsertedCount int64
TotalCount int64
}
// BulkWriteOplogs applies oplogs in bulk
func BulkWriteOplogs(oplogs []Oplog) (*BulkWriteOplogsResult, error) {
var results = BulkWriteOplogsResult{}
inst := GetMigratorInstance()
logger := gox.GetLogger("BulkWriteOplogs")
client, err := GetMongoClient(inst.Target)
if err != nil {
return &results, fmt.Errorf("GetMongoClient failed: %v", err)
}
opts := options.BulkWrite()
modelsMap := map[string][]OplogWriteModel{}
for _, oplog := range oplogs {
if SkipOplog(oplog) {
continue
}
writeModels := GetWriteModels(oplog)
for _, wmodel := range writeModels {
modelsMap[wmodel.Namespace] = append(modelsMap[wmodel.Namespace], wmodel)
}
}
ctx := context.Background()
var result *mongo.BulkWriteResult
missed := 0
for ns, wmodels := range modelsMap {
inserts := []mongo.WriteModel{}
others := []mongo.WriteModel{}
dbName, collName := mdb.SplitNamespace(ns)
coll := client.Database(dbName).Collection(collName)
for _, wmodel := range wmodels {
if wmodel.Operation == "i" {
if len(others) > 0 { // flush the others
opts.SetOrdered(true)
result, err = coll.BulkWrite(ctx, others, opts)
if result != nil {
results.DeletedCount += result.DeletedCount
results.ModifiedCount += result.ModifiedCount
results.UpsertedCount += result.UpsertedCount
}
updated := result.DeletedCount + result.ModifiedCount + result.UpsertedCount
if int(updated) != len(others) {
extra := 0
for _, other := range others[updated:] {
if result, err = coll.BulkWrite(ctx, []mongo.WriteModel{other}); err != nil {
logger.Warnf("BulkWrites exception: %v", err)
} else {
results.DeletedCount += result.DeletedCount
results.UpsertedCount += result.UpsertedCount
results.ModifiedCount += result.ModifiedCount
extra += int(result.DeletedCount + result.UpsertedCount + result.ModifiedCount)
}
}
missed += (len(others) - int(updated) - int(extra))
}
others = []mongo.WriteModel{}
}
inserts = append(inserts, wmodel.WriteModel)
} else {
if len(inserts) > 0 { // flush inserts
opts.SetOrdered(false)
if result, err = coll.BulkWrite(ctx, inserts, opts); err != nil {
if mdb.IsDuplicateKeyError(err) {
results.InsertedCount += int64(len(inserts))
} else {
logger.Warnf("BulkWrites exception: %v", err)
}
} else {
results.InsertedCount += result.InsertedCount
}
inserts = []mongo.WriteModel{}
}
others = append(others, wmodel.WriteModel)
}
}
if len(inserts) > 0 { // flush inserts
opts.SetOrdered(false)
if result, err = coll.BulkWrite(ctx, inserts, opts); err != nil {
if mdb.IsDuplicateKeyError(err) {
results.InsertedCount += int64(len(inserts))
} else {
logger.Warnf("BulkWrites exception: %v", err)
}
} else {
results.InsertedCount += result.InsertedCount
}
}
if len(others) > 0 { // flush the others
opts.SetOrdered(true)
result, err = coll.BulkWrite(ctx, others, opts)
if result != nil {
results.DeletedCount += result.DeletedCount
results.ModifiedCount += result.ModifiedCount
results.UpsertedCount += result.UpsertedCount
}
updated := result.DeletedCount + result.ModifiedCount + result.UpsertedCount
if int(updated) != len(others) {
extra := 0
for _, other := range others[updated:] {
if result, err = coll.BulkWrite(ctx, []mongo.WriteModel{other}); err != nil {
logger.Warnf("BulkWrites exception: %v", err)
} else {
results.DeletedCount += result.DeletedCount
results.UpsertedCount += result.UpsertedCount
results.ModifiedCount += result.ModifiedCount
extra += int(result.DeletedCount + result.UpsertedCount + result.ModifiedCount)
}
}
missed += (len(others) - int(updated) - int(extra))
}
others = []mongo.WriteModel{}
}
}
results.TotalCount = results.InsertedCount + results.ModifiedCount + results.DeletedCount + results.UpsertedCount
if int(results.TotalCount) < len(oplogs) {
logger.Debugf("oplogs:%v, missed:%v, inserted:%v, modified:%v, deleted:%v, upserted:%v",
len(oplogs), missed,
results.InsertedCount, results.ModifiedCount, results.DeletedCount, results.UpsertedCount)
}
return &results, nil
}
// GetWriteModels returns WriteModel from an oplog
func GetWriteModels(oplog Oplog) []OplogWriteModel {
inst := GetMigratorInstance()
ns := inst.GetToNamespace(oplog.Namespace)
switch oplog.Operation {
case "c":
var err error
wmodels := []OplogWriteModel{}
for _, v := range oplog.Object {
if v.Key != "applyOps" { // not supported yet
continue
}
oplogs, ok := v.Value.(primitive.A)
if !ok {
break
}
for _, log := range oplogs {
var oplog Oplog
var data []byte
if data, err = bson.Marshal(log); err != nil {
break
}
if err = bson.Unmarshal(data, &oplog); err != nil {
break
}
wmodels = append(wmodels, GetWriteModels(oplog)...)
}
gox.GetLogger().Debugf("c found applyOps %d", len(wmodels))
}
return wmodels
case "d":
op := mongo.NewDeleteOneModel()
op.SetFilter(oplog.Object)
return []OplogWriteModel{OplogWriteModel{ns, oplog.Operation, op}}
case "i":
op := mongo.NewInsertOneModel()
op.SetDocument(oplog.Object)
return []OplogWriteModel{OplogWriteModel{ns, oplog.Operation, op}}
case "n":
return nil
case "u":
o := oplog.Object
for _, v := range oplog.Object {
if v.Key == "diff" {
for _, doc := range v.Value.(bson.D) {
if doc.Key == "u" || doc.Key == "i" {
op := mongo.NewUpdateOneModel()
op.SetFilter(oplog.Query)
op.SetUpdate(bson.D{{"$set", doc.Value}})
return []OplogWriteModel{{ns, oplog.Operation, op}}
} else if doc.Key == "d" {
op := mongo.NewUpdateOneModel()
op.SetFilter(oplog.Query)
op.SetUpdate(bson.D{{"$unset", doc.Value}})
return []OplogWriteModel{{ns, oplog.Operation, op}}
}
}
return []OplogWriteModel{}
} else if v.Key != "$v" && strings.HasPrefix(v.Key, "$") {
o = bson.D{{v.Key, v.Value}}
op := mongo.NewUpdateOneModel()
op.SetFilter(oplog.Query)
op.SetUpdate(o)
return []OplogWriteModel{{ns, oplog.Operation, op}}
}
}
op := mongo.NewReplaceOneModel()
op.SetFilter(oplog.Query)
op.SetReplacement(o)
return []OplogWriteModel{{ns, oplog.Operation, op}}
default:
log.Println("unrecognized op", oplog.Operation)
}
return nil
}