-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmigrator.go
327 lines (300 loc) · 8.93 KB
/
migrator.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
// Copyright Kuei-chun Chen, 2022-present. All rights reserved.
package hummingbird
import (
"context"
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
"time"
"github.com/simagix/keyhole/mdb"
"github.com/simagix/gox"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
// Migrator stores migration configurations
type Migrator struct {
Block int `bson:"block,omitempty"`
Command string `bson:"command"`
Includes Includes `bson:"includes,omitempty"`
IsDrop bool `bson:"drop,omitempty"`
License string `bson:"license,omitempty"`
Port int `bson:"port,omitempty"`
Source string `bson:"source"`
Spool string `bson:"spool,omitempty"`
Target string `bson:"target"`
Verbose bool `bson:"verbose,omitempty"`
Workers int `bson:"workers,omitempty"`
Yes bool `bson:"yes,omitempty"`
genesis time.Time
isExit bool
included map[string]*Include
mutex sync.Mutex
replicas map[string]string
sourceStats *mdb.ClusterStats
streamers []*OplogStreamer
targetStats *mdb.ClusterStats
workspace Workspace
}
var migratorInstance *Migrator
var once sync.Once
// NewMigratorInstance sets and returns a migrator instance
func NewMigratorInstance(filename string) (*Migrator, error) {
inst, err := ReadMigratorConfig(filename)
if err != nil {
return inst, err
}
if err = ValidateMigratorConfig(inst); err != nil {
return inst, err
}
if inst.Verbose {
gox.GetLogger().SetLoggerLevel(gox.Debug)
}
inst.genesis = time.Now()
// establish work space
os.Mkdir(inst.Spool, 0755)
inst.workspace = Workspace{dbName: MetaDBName, dbURI: inst.Target, spool: inst.Spool}
// get clusters stats
inst.sourceStats = mdb.NewClusterStats("")
client, err := GetMongoClient(inst.Source)
if err != nil {
return migratorInstance, fmt.Errorf("source GetMongoClient failed: %v", err)
}
if err = inst.SourceStats().GetClusterStatsSummary(client); err != nil {
return migratorInstance, err
}
inst.targetStats = mdb.NewClusterStats("")
if client, err = GetMongoClient(inst.Target); err != nil {
return migratorInstance, fmt.Errorf("target GetMongoClient failed: %v", err)
}
if err = inst.TargetStats().GetClusterStatsSummary(client); err != nil {
return migratorInstance, err
}
// create includes map
inst.included = map[string]*Include{}
for _, include := range inst.Includes {
inst.included[include.Namespace] = include
}
// set uri map
inst.replicas = map[string]string{}
list, err := GetAllReplicas(inst.Source)
if err != nil {
return nil, fmt.Errorf("GetAllReplicas failed: %v", err)
}
for _, replica := range list {
cs, err := mdb.ParseURI(replica)
if err != nil {
return nil, fmt.Errorf("ParseURI failed: %v", err)
}
inst.Replicas()[cs.ReplicaSet] = replica
}
migratorInstance = inst
return migratorInstance, nil
}
// GetMigratorInstance returns Migratro migratorInstance
func GetMigratorInstance() *Migrator {
return migratorInstance
}
// IsExit returns isExit
func (inst *Migrator) IsExit() bool {
inst.mutex.Lock()
defer inst.mutex.Unlock()
return inst.isExit
}
// NotifyWorkerExit set isExit to true
func (inst *Migrator) NotifyWorkerExit() {
inst.mutex.Lock()
defer inst.mutex.Unlock()
inst.isExit = true
}
// AddOplogStreamer returns isExit
func (inst *Migrator) AddOplogStreamer(streamer *OplogStreamer) {
inst.mutex.Lock()
defer inst.mutex.Unlock()
inst.streamers = append(inst.streamers, streamer)
}
// LiveStreamingOplogs set isExit to true
func (inst *Migrator) LiveStreamingOplogs() {
inst.mutex.Lock()
defer inst.mutex.Unlock()
for _, streamer := range inst.streamers {
streamer.LiveStream()
}
}
// ResetIncludesTo is a convenient function for go tests
func (inst *Migrator) ResetIncludesTo(includes Includes) {
migratorInstance.Includes = includes
migratorInstance.included = map[string]*Include{}
if includes != nil {
for _, include := range includes {
dbName, _ := mdb.SplitNamespace(include.Namespace)
if include.To != "" {
dbName, _ = mdb.SplitNamespace(include.To)
}
migratorInstance.included[dbName] = include
}
}
}
// DropCollections drops all qualified collections
func (inst *Migrator) DropCollections() error {
logger := gox.GetLogger("DropCollections")
ctx := context.Background()
client, err := GetMongoClient(inst.Target)
if err != nil {
return err
}
if len(inst.Includes) == 0 { // drop all
dbNames, err := GetQualifiedDBs(client, MetaDBName)
if err != nil {
return err
}
for _, dbName := range dbNames {
logger.Debug("drop database " + dbName)
if err = client.Database(dbName).Drop(ctx); err != nil {
return err
}
}
} else {
for _, include := range inst.Included() {
dbName, collName := mdb.SplitNamespace(include.Namespace)
if collName == "" || collName == "*" {
logger.Debug("drop database " + dbName)
if err = client.Database(dbName).Drop(ctx); err != nil {
return err
}
} else {
if include.To != "" {
dbName, collName = mdb.SplitNamespace(include.To)
}
logger.Infof("drop namespace %v.%v", dbName, collName)
if err = client.Database(dbName).Collection(collName).Drop(ctx); err != nil {
return err
}
}
}
}
return nil
}
// CheckIfBalancersDisabled check if both source and target balancers are disabled
func (inst *Migrator) CheckIfBalancersDisabled() error {
var err error
if inst.SourceStats().Cluster == mdb.Sharded {
if err = checkIfBalancerDisabled(inst.Source); err != nil {
return fmt.Errorf("source cluster error: %v", err)
}
}
if inst.TargetStats().Cluster == mdb.Sharded {
if err = checkIfBalancerDisabled(inst.Target); err != nil {
return fmt.Errorf("target cluster error: %v", err)
}
}
return nil
}
// Workspace returns Workspace
func (inst *Migrator) Workspace() Workspace {
return inst.workspace
}
// Included returns includes
func (inst *Migrator) Included() map[string]*Include {
return inst.included
}
// Replicas returns replica URI map
func (inst *Migrator) Replicas() map[string]string {
return inst.replicas
}
// SourceStats returns stats
func (inst *Migrator) SourceStats() *mdb.ClusterStats {
return inst.sourceStats
}
// TargetStats returns stats
func (inst *Migrator) TargetStats() *mdb.ClusterStats {
return inst.targetStats
}
// SkipNamespace skips namespace
func (inst *Migrator) SkipNamespace(namespace string) bool {
if len(inst.included) == 0 {
return false
}
dbName, collName := mdb.SplitNamespace(namespace)
allCollsInDB := dbName + ".*"
if inst.included[allCollsInDB] != nil || inst.included[namespace] != nil {
return false
}
collInAllDB := "*." + collName
if inst.included[collInAllDB] != nil || inst.included[namespace] != nil {
return false
}
return true
}
// GetToNamespace returns target namespace
func (inst *Migrator) GetToNamespace(ns string) string {
if len(inst.included) == 0 {
return ns
}
if inst.included[ns] != nil && inst.included[ns].To != "" {
return inst.included[ns].To
}
return ns
}
// ReadMigratorConfig validates configuration from a file
func ReadMigratorConfig(filename string) (*Migrator, error) {
var err error
var data []byte
migrator := Migrator{}
if data, err = ioutil.ReadFile(filename); err != nil {
return nil, err
} else if err = bson.UnmarshalExtJSON(data, false, &migrator); err != nil {
return nil, err
}
return &migrator, ValidateMigratorConfig(&migrator)
}
// ValidateMigratorConfig validates configuration from a file
func ValidateMigratorConfig(migrator *Migrator) error {
if migrator.Command == "" {
return fmt.Errorf(`command is required`)
} else if migrator.Source == "" || migrator.Target == "" {
return fmt.Errorf(`source and target must have validate connection strings`)
} else if migrator.Workers > MaxNumberWorkers {
return fmt.Errorf("number of workers must be between 1 and %v", MaxNumberWorkers)
} else if migrator.IsDrop && (migrator.Command == CommandData || migrator.Command == CommandDataOnly) {
return fmt.Errorf(`cannot set {"drop": true} when command is %v`, migrator.Command)
}
var logger = gox.GetLogger("ValidateMigratorConfig")
var values []string
if migrator.Block <= 0 {
values = append(values, fmt.Sprintf(`"block":%v`, MaxBlockSize))
migrator.Block = MaxBlockSize
}
if migrator.Port <= 0 {
values = append(values, fmt.Sprintf(`"port":%v`, Port))
migrator.Port = Port
}
if migrator.Spool == "" {
values = append(values, fmt.Sprintf(`"spool":"%v"`, DefaultSpool))
migrator.Spool = DefaultSpool
}
if migrator.Workers < 1 {
values = append(values, fmt.Sprintf(`"workers":%v`, NumberWorkers))
migrator.Workers = NumberWorkers
}
if len(values) > 0 {
logger.Info("set default {", strings.Join(values, ","), "}")
}
return nil
}
// checkIfBalancerDisabled returns balancer state
func checkIfBalancerDisabled(uri string) error {
var err error
var client *mongo.Client
var enabled bool
if client, err = GetMongoClient(uri); err != nil {
return err
}
if enabled, err = IsBalancerEnabled(client); err != nil {
return err
} else if enabled {
return fmt.Errorf("balancer is enabled")
}
return nil
}