forked from alibaba/MongoShake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc_syncer.go
387 lines (337 loc) · 10.5 KB
/
doc_syncer.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
package docsyncer
import (
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"mongoshake/collector/configure"
"mongoshake/collector/filter"
"mongoshake/collector/transform"
"mongoshake/common"
"github.com/gugemichael/nimo4go"
LOG "github.com/vinllen/log4go"
"github.com/vinllen/mgo"
"github.com/vinllen/mgo/bson"
)
const (
MAX_BUFFER_BYTE_SIZE = 16 * 1024 * 1024
)
func IsShardingToSharding(fromIsSharding bool, toConn *utils.MongoConn) bool {
toIsSharding := utils.IsSharding(toConn.Session)
if fromIsSharding && toIsSharding {
LOG.Info("replication from sharding to sharding")
return true
} else if fromIsSharding && !toIsSharding {
LOG.Info("replication from sharding to replica")
return false
} else if !fromIsSharding && toIsSharding {
LOG.Info("replication from replica to sharding")
return false
} else {
LOG.Info("replication from replica to replica")
return false
}
}
func StartDropDestCollection(nsSet map[utils.NS]bool, toConn *utils.MongoConn,
nsTrans *transform.NamespaceTransform) (map[string]bool, error) {
nsExistedSet := make(map[string]bool)
for ns := range nsSet {
toNS := utils.NewNS(nsTrans.Transform(ns.Str()))
if !conf.Options.ReplayerCollectionDrop {
colNames, err := toConn.Session.DB(toNS.Database).CollectionNames()
if err != nil {
return nil, LOG.Critical("Get collection names of db %v of dest mongodb failed. %v", toNS.Database, err)
}
for _, colName := range colNames {
if colName == ns.Collection {
LOG.Warn("ns %v to be synced already exists in dest mongodb, collection and index info will not be synced", toNS)
nsExistedSet[ns.Str()] = true
break
}
}
} else {
err := toConn.Session.DB(toNS.Database).C(toNS.Collection).DropCollection()
if err != nil && err.Error() != "ns not found" {
return nil, LOG.Critical("Drop collection ns %v of dest mongodb failed. %v", toNS, err)
}
}
}
return nsExistedSet, nil
}
func StartNamespaceSpecSyncForSharding(csUrl string, toConn *utils.MongoConn,
nsExistedSet map[string]bool, nsTrans *transform.NamespaceTransform) error {
LOG.Info("document syncer namespace spec for sharding begin")
var fromConn *utils.MongoConn
var err error
if fromConn, err = utils.NewMongoConn(csUrl, utils.ConnectModePrimary, true); err != nil {
return err
}
defer fromConn.Close()
filterList := filter.NewDocFilterList()
dbTrans := transform.NewDBTransform(conf.Options.TransformNamespace)
type dbSpec struct {
Db string `bson:"_id"`
Partitioned bool `bson:"partitioned"`
}
var dbSpecDoc dbSpec
// enable sharding for db
dbSpecIter := fromConn.Session.DB("config").C("databases").Find(bson.M{}).Iter()
for dbSpecIter.Next(&dbSpecDoc) {
if dbSpecDoc.Partitioned {
if filterList.IterateFilter(dbSpecDoc.Db + ".$cmd") {
LOG.Debug("DB is filtered. %v", dbSpecDoc.Db)
continue
}
var todbSpecDoc dbSpec
todbList := dbTrans.Transform(dbSpecDoc.Db)
for _, todb := range todbList {
err = toConn.Session.DB("config").C("databases").
Find(bson.D{{"_id", todb}}).One(&todbSpecDoc)
if err == nil && todbSpecDoc.Partitioned {
continue
}
err = toConn.Session.DB("admin").Run(bson.D{{"enablesharding", todb}}, nil)
if err != nil {
return LOG.Critical("Enable sharding for db %v of dest mongodb failed. %v", todb, err)
}
LOG.Info("Enable sharding for db %v of dest mongodb successful", todb)
}
}
}
if err := dbSpecIter.Close(); err != nil {
return LOG.Critical("Close iterator of config.database failed. %v", err)
}
type colSpec struct {
Ns string `bson:"_id"`
Key *bson.Raw `bson:"key"`
Unique bool `bson:"unique"`
Dropped bool `bson:"dropped"`
}
var colSpecDoc colSpec
// enable sharding for db
colSpecIter := fromConn.Session.DB("config").C("collections").Find(bson.M{}).Iter()
for colSpecIter.Next(&colSpecDoc) {
if _, ok := nsExistedSet[colSpecDoc.Ns]; ok {
LOG.Debug("Namespace spec sync is skipped. %v", colSpecDoc.Ns)
continue
}
if !colSpecDoc.Dropped {
if filterList.IterateFilter(colSpecDoc.Ns) {
LOG.Debug("Namespace spec sync is filtered. %v", colSpecDoc.Ns)
continue
}
toNs := nsTrans.Transform(colSpecDoc.Ns)
err = toConn.Session.DB("admin").Run(bson.D{{"shardCollection", toNs},
{"key", colSpecDoc.Key}, {"unique", colSpecDoc.Unique}}, nil)
if err != nil {
return LOG.Critical("Shard collection for ns %v of dest mongodb failed. %v", toNs, err)
}
LOG.Info("Shard collection for ns %v of dest mongodb successful", toNs)
}
}
if err = colSpecIter.Close(); err != nil {
return LOG.Critical("Close iterator of config.collections failed. %v", err)
}
LOG.Info("document syncer namespace spec for sharding successful")
return nil
}
func StartIndexSync(indexMap map[utils.NS][]mgo.Index, toUrl string,
nsExistedSet map[string]bool, nsTrans *transform.NamespaceTransform) (syncError error) {
type IndexNS struct {
ns utils.NS
indexList []mgo.Index
}
LOG.Info("document syncer sync index begin")
if len(indexMap) == 0 {
LOG.Info("document syncer sync index finish, but no data")
return nil
}
var indexNeedSync int
for ns := range indexMap {
if _, ok := nsExistedSet[ns.Str()]; ok {
LOG.Info("document syncer index sync of ns[%v] is skipped", ns.Str())
continue
}
indexNeedSync++
}
collExecutorParallel := conf.Options.ReplayerCollectionParallel
namespaces := make(chan *IndexNS, collExecutorParallel)
nimo.GoRoutine(func() {
for ns, indexList := range indexMap {
if _, ok := nsExistedSet[ns.Str()]; ok {
continue
}
namespaces <- &IndexNS{ns: ns, indexList: indexList}
}
})
var conn *utils.MongoConn
var err error
if conn, err = utils.NewMongoConn(toUrl, utils.ConnectModePrimary, false); err != nil {
return err
}
defer conn.Close()
if indexNeedSync > 0 {
var wg sync.WaitGroup
wg.Add(indexNeedSync)
for i := 0; i < collExecutorParallel; i++ {
nimo.GoRoutine(func() {
session := conn.Session.Clone()
defer session.Close()
for {
indexNs, ok := <-namespaces
if !ok {
break
}
ns := indexNs.ns
toNS := utils.NewNS(nsTrans.Transform(ns.Str()))
for _, index := range indexNs.indexList {
index.Background = false
if err = session.DB(toNS.Database).C(toNS.Collection).EnsureIndex(index); err != nil {
LOG.Warn("Create indexes for ns %v of dest mongodb failed. %v", ns, err)
}
}
LOG.Info("Create indexes for ns %v of dest mongodb finish", toNS)
wg.Done()
}
})
}
wg.Wait()
}
close(namespaces)
LOG.Info("document syncer sync index finish")
return syncError
}
type DBSyncer struct {
replset string
// source mongodb url
FromMongoUrl string
// destination mongodb url
ToMongoUrl string
// index of namespace
indexMap map[utils.NS][]mgo.Index
// start time of sync
startTime time.Time
// namespace transform
nsTrans *transform.NamespaceTransform
// filter orphan duplicate record
orphanFilter *filter.OrphanFilter
mutex sync.Mutex
replMetric *utils.ReplicationMetric
}
func NewDBSyncer(
replset string,
fromMongoUrl string,
toMongoUrl string,
nsTrans *transform.NamespaceTransform,
orphanFilter *filter.OrphanFilter) *DBSyncer {
syncer := &DBSyncer{
replset: replset,
FromMongoUrl: fromMongoUrl,
ToMongoUrl: toMongoUrl,
indexMap: make(map[utils.NS][]mgo.Index),
nsTrans: nsTrans,
orphanFilter: orphanFilter,
}
return syncer
}
func (syncer *DBSyncer) Start() (syncError error) {
syncer.startTime = time.Now()
var wg sync.WaitGroup
nsList, err := getDbNamespace(syncer.FromMongoUrl)
if err != nil {
return err
}
if len(nsList) == 0 {
LOG.Info("document syncer %v finish, but no data", syncer.replset)
}
collExecutorParallel := conf.Options.ReplayerCollectionParallel
namespaces := make(chan utils.NS, collExecutorParallel)
wg.Add(len(nsList))
nimo.GoRoutine(func() {
for _, ns := range nsList {
namespaces <- ns
}
})
var nsDoneCount int32 = 0
for i := 0; i < collExecutorParallel; i++ {
collExecutorId := GenerateCollExecutorId()
nimo.GoRoutine(func() {
for {
ns, ok := <-namespaces
if !ok {
break
}
toNS := utils.NewNS(syncer.nsTrans.Transform(ns.Str()))
LOG.Info("document syncer %v collExecutor-%d sync ns %v to %v begin",
syncer.replset, collExecutorId, ns, toNS)
err := syncer.collectionSync(collExecutorId, ns, toNS)
atomic.AddInt32(&nsDoneCount, 1)
if err != nil {
syncError = LOG.Critical("document syncer %v collExecutor-%d sync ns %v to %v failed. %v",
syncer.replset, collExecutorId, ns, toNS, err)
} else {
process := int(atomic.LoadInt32(&nsDoneCount)) * 100 / len(nsList)
LOG.Info("document syncer %v collExecutor-%d sync ns %v to %v successful. db syncer %v progress %v%%",
syncer.replset, collExecutorId, ns, toNS, syncer.replset, process)
}
wg.Done()
}
LOG.Info("document syncer %v collExecutor-%d finish", syncer.replset, collExecutorId)
})
}
wg.Wait()
close(namespaces)
return syncError
}
func (syncer *DBSyncer) collectionSync(collExecutorId int, ns utils.NS,
toNS utils.NS) error {
reader := NewDocumentReader(syncer.FromMongoUrl, ns)
colExecutor := NewCollectionExecutor(collExecutorId, syncer.ToMongoUrl, toNS)
if err := colExecutor.Start(); err != nil {
return err
}
bufferSize := conf.Options.ReplayerDocumentBatchSize
buffer := make([]*bson.Raw, 0, bufferSize)
bufferByteSize := 0
for {
var doc *bson.Raw
var err error
if doc, err = reader.NextDoc(); err != nil {
return errors.New(fmt.Sprintf("Get next document from ns %v of src mongodb failed. %v", ns, err))
} else if doc == nil {
colExecutor.Sync(buffer)
if err := colExecutor.Wait(); err != nil {
return err
}
break
}
if bufferByteSize+len(doc.Data) > MAX_BUFFER_BYTE_SIZE || len(buffer) >= bufferSize {
colExecutor.Sync(buffer)
buffer = make([]*bson.Raw, 0, bufferSize)
bufferByteSize = 0
}
// filter orphan document of chunk
if conf.Options.FilterOrphanDocument && syncer.orphanFilter.Filter(doc, ns.Str()) {
continue
}
// transform dbref
if len(conf.Options.TransformNamespace) > 0 && conf.Options.DBRef {
doc = transform.TransformDBRef(doc, ns.Database, syncer.nsTrans)
}
buffer = append(buffer, doc)
bufferByteSize += len(doc.Data)
}
if indexes, err := reader.GetIndexes(); err != nil {
return errors.New(fmt.Sprintf("Get indexes from ns %v of src mongodb failed. %v", ns, err))
} else {
syncer.mutex.Lock()
defer syncer.mutex.Unlock()
syncer.indexMap[ns] = indexes
}
reader.Close()
return nil
}
func (syncer *DBSyncer) GetIndexMap() map[utils.NS][]mgo.Index {
return syncer.indexMap
}