-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongomove.go
483 lines (415 loc) · 12.3 KB
/
mongomove.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package mongomove
import (
"context"
"fmt"
"log"
"os"
"regexp"
"strings"
"sync"
"time"
"github.com/AlecAivazis/survey/v2"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"golang.org/x/sync/errgroup"
)
const (
defaultPingTimeout = 5 * time.Second
)
// Importer is a type that performs data import operations between two MongoDB
// instances, handling database filtering, index management, and parallel
// processing for efficient data transfers. It provides options to customize the
// import process, such as specifying filters for databases, ensuring indexes
// are created on the target instance, and configuring batch sizes and parallel
// processing.
type Importer struct {
source *mongo.Client
target *mongo.Client
}
// ImportOption is a configuration function that modifies the importConfig
// struct, allowing users to customize the behavior of the Importer when
// importing data from one MongoDB instance to another. It provides options for
// filtering databases, ensuring indexes, dropping existing data, skipping
// confirmation prompts, and adjusting parallelism and batch sizes for
// performance tuning.
type ImportOption func(*importConfig)
type importConfig struct {
dbFilter []func(string) bool
ensureIndexes bool
drop bool
skipConfirm bool
verbose bool
pingTimeout time.Duration
parallel int
batchSize int
}
// FilterDatabases returns an ImportOption that appends one or more filter
// functions to the importConfig's dbFilter list. These filter functions are
// used to determine which databases should be imported.
func FilterDatabases(filter ...func(string) bool) ImportOption {
return func(cfg *importConfig) {
cfg.dbFilter = append(cfg.dbFilter, filter...)
}
}
// EnsureIndexes sets the ensureIndexes field in the importConfig struct to the
// provided boolean value. If set to true, the function ensures that indexes are
// created on the target database during the import process.
func EnsureIndexes(ensure bool) ImportOption {
return func(cfg *importConfig) {
cfg.ensureIndexes = ensure
}
}
// HasPrefix returns a function that checks if a given string has the specified
// prefix. The returned function accepts a string and returns true if the input
// string starts with the specified prefix, otherwise false.
func HasPrefix(prefix string) func(string) bool {
return func(s string) bool {
return strings.HasPrefix(s, prefix)
}
}
func Exclude(exprs ...*regexp.Regexp) ImportOption {
return FilterDatabases(func(db string) bool {
for _, expr := range exprs {
if expr.MatchString(db) {
return false
}
}
return true
})
}
// Drop sets the drop option for the importConfig. If true, the target database
// will be dropped before importing data.
func Drop(drop bool) ImportOption {
return func(cfg *importConfig) {
cfg.drop = drop
}
}
// SkipConfirm sets whether to skip the confirmation prompt during the import
// process. If set to true, it will bypass the confirmation and proceed with the
// import without user interaction.
func SkipConfirm(skip bool) ImportOption {
return func(cfg *importConfig) {
cfg.skipConfirm = skip
}
}
// PingTimeout sets the duration for the ping timeout when establishing a
// connection to the source and target MongoDB instances.
func PingTimeout(d time.Duration) ImportOption {
return func(cfg *importConfig) {
cfg.pingTimeout = d
}
}
// Verbose sets the verbosity of the import process. If set to true, additional
// log messages will be printed during the import process.
func Verbose(v bool) ImportOption {
return func(cfg *importConfig) {
cfg.verbose = v
}
}
// Parallel sets the number of parallel import operations to be performed by the
// Importer. It takes an integer value as an input and returns an ImportOption
// function.
func Parallel(p int) ImportOption {
return func(cfg *importConfig) {
cfg.parallel = p
}
}
// BatchSize sets the size of the batches used when importing data from the
// source to the target database.
func BatchSize(size int) ImportOption {
return func(cfg *importConfig) {
cfg.batchSize = size
}
}
// New creates a new Importer instance with the specified source and target
// MongoDB clients. The source and target clients must not be nil; otherwise, it
// panics.
func New(source, target *mongo.Client) *Importer {
if source == nil {
panic("<nil> client (source)")
}
if target == nil {
panic("<nil> client (target)")
}
return &Importer{
source: source,
target: target,
}
}
// Import imports the specified databases from the source MongoDB client to the
// target MongoDB client using the provided import options. It handles database
// filtering, dropping existing databases, ensuring indexes, and parallelizing
// import operations.
func (i *Importer) Import(ctx context.Context, opts ...ImportOption) error {
cfg := importConfig{
pingTimeout: defaultPingTimeout,
ensureIndexes: true,
}
for _, opt := range opts {
opt(&cfg)
}
if cfg.parallel < 1 {
cfg.parallel = 1
}
if cfg.batchSize < 1 {
cfg.batchSize = 1
}
if err := i.ping(ctx, cfg.pingTimeout); err != nil {
return fmt.Errorf("ping: %w", err)
}
names, err := i.source.ListDatabaseNames(ctx, bson.M{})
if err != nil {
return fmt.Errorf("list database names: %w", err)
}
log.Printf("Found databases: %v", names)
names = cfg.filterDatabases(names...)
log.Printf("Filtered databases: %v", names)
confirmed, err := cfg.confirm()
if err != nil {
return fmt.Errorf("confirm: %w", err)
}
if !confirmed {
fmt.Println("Import aborted.")
os.Exit(1)
}
jobs := make(chan string)
var wg sync.WaitGroup
wg.Add(cfg.parallel)
errors := make(chan error)
for index := 0; index < cfg.parallel; index++ {
go func() {
defer wg.Done()
for name := range jobs {
db := i.source.Database(name)
if err := cfg.dropDB(ctx, i.target.Database(name)); err != nil {
select {
case <-ctx.Done():
return
case errors <- fmt.Errorf("drop %q database: %w", name, err):
}
}
if err := i.importDatabase(ctx, cfg, db); err != nil {
select {
case <-ctx.Done():
return
case errors <- fmt.Errorf("import %q database: %w", name, err):
}
}
}
}()
}
go func() {
for _, name := range names {
select {
case <-ctx.Done():
return
case jobs <- name:
}
}
close(jobs)
}()
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-ctx.Done():
return ctx.Err()
case err := <-errors:
return err
case <-done:
return nil
}
}
func (i *Importer) ping(ctx context.Context, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
if err := i.source.Ping(ctx, nil); err != nil {
return fmt.Errorf("ping source: %w", err)
}
if err := i.target.Ping(ctx, nil); err != nil {
return fmt.Errorf("ping target: %w", err)
}
return nil
}
func (i *Importer) importDatabase(ctx context.Context, cfg importConfig, db *mongo.Database) error {
cfg.log(fmt.Sprintf("Import database: %v", db.Name()))
names, err := db.ListCollectionNames(ctx, bson.M{})
if err != nil {
return fmt.Errorf("list collection names: %w", err)
}
cfg.log(fmt.Sprintf("[%s]: Found collections: %v", db.Name(), names))
importCtx, cancel := context.WithCancel(ctx)
defer cancel()
indexCtx, cancel := context.WithCancel(ctx)
defer cancel()
group, ctx := errgroup.WithContext(importCtx)
for _, name := range names {
name := name
group.Go(func() error {
if err := i.importCollection(ctx, cfg, db.Collection(name)); err != nil {
return fmt.Errorf("import %q collection: %w", name, err)
}
return nil
})
}
if err := group.Wait(); err != nil {
return err
}
if cfg.ensureIndexes {
if err := i.ensureIndexes(indexCtx, cfg, db, names); err != nil {
return fmt.Errorf("ensure indexes: %w", err)
}
} else {
cfg.log("Skipping index creation.")
}
return nil
}
func (i *Importer) importCollection(ctx context.Context, cfg importConfig, col *mongo.Collection) error {
cfg.log(fmt.Sprintf("[%s]: Import collection: %v", col.Database().Name(), col.Name()))
target := i.target.Database(col.Database().Name()).Collection(col.Name())
cur, err := col.Find(ctx, bson.M{}, options.Find().SetNoCursorTimeout(true))
if err != nil {
return fmt.Errorf("find all documents: %w", err)
}
defer cur.Close(ctx)
buf := make([]interface{}, 0, cfg.batchSize)
var batchIter int
insertBatch := func() error {
if len(buf) == 0 {
return nil
}
batchIter++
start := (batchIter - 1) * cfg.batchSize
qty := cfg.batchSize
if l := len(buf); l < qty {
qty = l
}
end := start + qty - 1
cfg.log(fmt.Sprintf("[%s/%s]: Inserting documents (%d - %d)...", col.Database().Name(), col.Name(), start, end))
if _, err := target.InsertMany(ctx, buf); err != nil {
return fmt.Errorf("insert documents: %w", err)
}
cfg.log(fmt.Sprintf("[%s/%s]: Inserted documents (%d - %d).", col.Database().Name(), col.Name(), start, end))
return nil
}
for cur.Next(ctx) {
doc := make(bson.M)
if err := cur.Decode(&doc); err != nil {
return fmt.Errorf("decode document: %w", err)
}
buf = append(buf, doc)
if len(buf) >= cfg.batchSize {
if err := insertBatch(); err != nil {
return err
}
buf = make([]interface{}, 0, cfg.batchSize)
}
}
if err := cur.Err(); err != nil {
return fmt.Errorf("cursor: %w", err)
}
if err := insertBatch(); err != nil {
return err
}
cfg.log(fmt.Sprintf("[%s/%s]: Import done.", col.Database().Name(), col.Name()))
return nil
}
func (i *Importer) ensureIndexes(ctx context.Context, cfg importConfig, db *mongo.Database, names []string) error {
cfg.log(fmt.Sprintf("[%s]: Ensure indexes: %v", db.Name(), names))
for _, name := range names {
if err := i.ensureColIndexes(ctx, cfg, db.Collection(name)); err != nil {
return fmt.Errorf("ensure indexes for %q collection: %w", name, err)
}
}
return nil
}
func (i *Importer) ensureColIndexes(ctx context.Context, cfg importConfig, col *mongo.Collection) error {
cfg.log(fmt.Sprintf("[%s/%s]: Ensure Collection indexes...", col.Database().Name(), col.Name()))
target := i.target.Database(col.Database().Name()).Collection(col.Name())
cur, err := col.Indexes().List(ctx)
if err != nil {
return fmt.Errorf("list indexes: %w", err)
}
var models []bson.M
if err := cur.All(ctx, &models); err != nil {
return fmt.Errorf("cursor: %w", err)
}
cfg.log(fmt.Sprintf("[%s/%s]: Found indexes: %v", col.Database().Name(), col.Name(), models))
if err := cur.Err(); err != nil {
return fmt.Errorf("cursor: %w", err)
}
idxModels := make([]mongo.IndexModel, 0, len(models))
for i := range models {
keym := models[i]["key"].(bson.M)
keys := make(bson.D, 0)
for k, v := range keym {
if strings.HasPrefix(k, "_") {
continue
}
keys = append(keys, bson.E{Key: k, Value: v})
}
if len(keys) == 0 {
continue
}
name, _ := models[i]["name"].(string)
unique, _ := models[i]["unique"].(bool)
opts := options.Index().SetName(name).SetUnique(unique)
idxModels = append(idxModels, mongo.IndexModel{
Keys: keys,
Options: opts,
})
}
if len(idxModels) > 0 {
if _, err := target.Indexes().CreateMany(ctx, idxModels); err != nil {
return fmt.Errorf("create indexes: %w", err)
}
}
cfg.log(fmt.Sprintf("[%s/%s]: Indexes created.", col.Database().Name(), col.Name()))
return nil
}
func (cfg importConfig) filterDatabases(names ...string) []string {
if len(cfg.dbFilter) == 0 {
return names
}
var filtered []string
L:
for _, name := range names {
for _, filter := range cfg.dbFilter {
if !filter(name) {
cfg.log(fmt.Sprintf("Database %q excluded from import.", name))
continue L
}
}
filtered = append(filtered, name)
}
return filtered
}
func (cfg importConfig) dropDB(ctx context.Context, db *mongo.Database) error {
if !cfg.drop {
return nil
}
cfg.log(fmt.Sprintf("Dropping target database: %v", db.Name()))
return db.Drop(ctx)
}
func (cfg importConfig) confirm() (bool, error) {
if cfg.skipConfirm {
return true, nil
}
fmt.Println()
var confirmed bool
if err := survey.AskOne(&survey.Confirm{
Message: "Do you really want to import the above databases?",
Default: false,
}, &confirmed); err != nil {
return confirmed, fmt.Errorf("survey: %w", err)
}
return confirmed, nil
}
func (cfg importConfig) log(v ...interface{}) {
if cfg.verbose {
log.Println(v...)
}
}