-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
1441 lines (1295 loc) · 60.3 KB
/
main.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2018 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"os"
"os/signal"
"reflect"
"sync"
"sync/atomic"
"time"
"github.com/couchbase/gocb/v2"
xdcrBase "github.com/couchbase/goxdcr/v8/base"
xdcrParts "github.com/couchbase/goxdcr/v8/base/filter"
xdcrLog "github.com/couchbase/goxdcr/v8/log"
"github.com/couchbase/goxdcr/v8/metadata"
"github.com/couchbase/goxdcr/v8/metadata_svc"
"github.com/couchbase/goxdcr/v8/service_def"
service_def_mock "github.com/couchbase/goxdcr/v8/service_def/mocks"
"github.com/couchbase/goxdcr/v8/service_impl"
"github.com/couchbase/goxdcr/v8/streamApiWatcher"
xdcrUtils "github.com/couchbase/goxdcr/v8/utils"
"github.com/couchbase/xdcrDiffer/base"
"github.com/couchbase/xdcrDiffer/dcp"
"github.com/couchbase/xdcrDiffer/differ"
fdp "github.com/couchbase/xdcrDiffer/fileDescriptorPool"
"github.com/couchbase/xdcrDiffer/filterPool"
"github.com/couchbase/xdcrDiffer/utils"
"github.com/stretchr/testify/mock"
)
var done = make(chan bool)
type inputOptions struct {
sourceUrl string
sourceUsername string
sourcePassword string
sourceBucketName string
remoteClusterName string
sourceFileDir string
targetUrl string
targetUsername string
targetPassword string
targetBucketName string
targetFileDir string
numberOfSourceDcpClients uint64
numberOfWorkersPerSourceDcpClient uint64
numberOfTargetDcpClients uint64
numberOfWorkersPerTargetDcpClient uint64
numberOfWorkersForFileDiffer uint64
numberOfWorkersForMutationDiffer uint64
numberOfBins uint64
numberOfFileDesc uint64
// the duration that the tools should be run, in minutes
completeByDuration uint64
// whether tool should complete after processing all mutations at tool start time
completeBySeqno bool
// directory for checkpoint files
checkpointFileDir string
// name of source cluster checkpoint file to load from when tool starts
// if not specified, source cluster will start from 0
oldSourceCheckpointFileName string
// name of target cluster checkpoint file to load from when tool starts
// if not specified, target cluster will start from 0
oldTargetCheckpointFileName string
// name of new checkpoint file to write to when tool shuts down
// if not specified, tool will not save checkpoint files
newCheckpointFileName string
// directory for storing diffs generated by file differ
fileDifferDir string
// output directory for mutation differ
mutationDifferDir string
// size of batch used by mutation differ
mutationDifferBatchSize uint64
// timeout, in seconds, used by mutation differ
mutationDifferTimeout uint64
// size of source dcp handler channel
sourceDcpHandlerChanSize uint64
// size of target dcp handler channel
targetDcpHandlerChanSize uint64
// timeout for bucket for stats collection, in seconds
bucketOpTimeout uint64
// max number of retry for get stats
maxNumOfGetStatsRetry uint64
// max number of retry for send batch
maxNumOfSendBatchRetry uint64
// retry interval for get stats, in seconds
getStatsRetryInterval uint64
// retry interval for send batch, in milliseconds
sendBatchRetryInterval uint64
// max backoff for get stats, in seconds
getStatsMaxBackoff uint64
// max backoff for send batch, in seconds
sendBatchMaxBackoff uint64
// delay between source cluster start up and target cluster start up, in seconds
delayBetweenSourceAndTarget uint64
//interval for periodical checkpointing, in seconds
// value of 0 indicates no periodical checkpointing
checkpointInterval uint64
// whether to run data generation
runDataGeneration bool
// whether to run file differ
runFileDiffer bool
// whether to verify diff keys through aysnc Get on clusters
runMutationDiffer bool
// Whether or not to enforce secure communications for data retrieval
enforceTLS bool
// Number of items kept in memory per binary buffer bucket
bucketBufferCapacity int
// Compare metadata, or body, or both
compareType string
// Number of times for mutationsDiffer to retry to resolve doc differences
mutationDifferRetries int
// Number of secs to wait between retries
mutationDifferRetriesWaitSecs int
// Number of filters to be created for the filter pool to be shared
numOfFiltersInFilterPool int
// Enables DEBUG level logs for xdcrDiffer and gocb verbose logging
debugMode bool
// a common setup timeout duration - in seconds
setupTimeout int
//string denoting the xattrs that shouldn't be compared
fileContaingXattrKeysForNoComapre string
}
var options inputOptions = inputOptions{}
func (o inputOptions) String() string {
return fmt.Sprintf("Options{sourceUrl: %s, sourceUsername: %s, sourcePassword: REDACTED, sourceBucketName: %s, remoteClusterName: %s, sourceFileDir: %s, targetUrl: %s, targetUsername: %s, targetPassword: REDACTED, targetBucketName: %s, targetFileDir: %s, numberOfSourceDcpClients: %d, numberOfWorkersPerSourceDcpClient: %d, numberOfTargetDcpClients: %d, numberOfWorkersPerTargetDcpClient: %d, numberOfWorkersForFileDiffer: %d, numberOfWorkersForMutationDiffer: %d, numberOfBins: %d, numberOfFileDesc: %d, completeByDuration: %d, completeBySeqno: %t, checkpointFileDir: %s, oldSourceCheckpointFileName: %s, oldTargetCheckpointFileName: %s, newCheckpointFileName: %s, fileDifferDir: %s, mutationDifferDir: %s, mutationDifferBatchSize: %d, mutationDifferTimeout: %d, sourceDcpHandlerChanSize: %d, targetDcpHandlerChanSize: %d, bucketOpTimeout: %d, maxNumOfGetStatsRetry: %d, maxNumOfSendBatchRetry: %d, getStatsRetryInterval: %d, sendBatchRetryInterval: %d, getStatsMaxBackoff: %d, sendBatchMaxBackoff: %d, delayBetweenSourceAndTarget: %d, checkpointInterval: %d, runDataGeneration: %t, runFileDiffer: %t, runMutationDiffer: %t, enforceTLS: %t, bucketBufferCapacity: %d, compareType: %s, mutationDifferRetries: %d, mutationDifferRetriesWaitSecs: %d, numOfFiltersInFilterPool: %d, debugMode: %t, setupTimeout: %d, fileContaingXattrKeysForNoComapre: %s}",
o.sourceUrl, o.sourceUsername, o.sourceBucketName, o.remoteClusterName, o.sourceFileDir, o.targetUrl, o.targetUsername, o.targetBucketName, o.targetFileDir, o.numberOfSourceDcpClients, o.numberOfWorkersPerSourceDcpClient, o.numberOfTargetDcpClients, o.numberOfWorkersPerTargetDcpClient, o.numberOfWorkersForFileDiffer, o.numberOfWorkersForMutationDiffer, o.numberOfBins, o.numberOfFileDesc, o.completeByDuration, o.completeBySeqno, o.checkpointFileDir, o.oldSourceCheckpointFileName, o.oldTargetCheckpointFileName, o.newCheckpointFileName, o.fileDifferDir, o.mutationDifferDir, o.mutationDifferBatchSize, o.mutationDifferTimeout, o.sourceDcpHandlerChanSize, o.targetDcpHandlerChanSize, o.bucketOpTimeout, o.maxNumOfGetStatsRetry, o.maxNumOfSendBatchRetry, o.getStatsRetryInterval, o.sendBatchRetryInterval, o.getStatsMaxBackoff, o.sendBatchMaxBackoff, o.delayBetweenSourceAndTarget, o.checkpointInterval, o.runDataGeneration, o.runFileDiffer, o.runMutationDiffer, o.enforceTLS, o.bucketBufferCapacity, o.compareType, o.mutationDifferRetries, o.mutationDifferRetriesWaitSecs, o.numOfFiltersInFilterPool, o.debugMode, o.setupTimeout, o.fileContaingXattrKeysForNoComapre)
}
func argParse() {
flag.StringVar(&options.sourceUrl, "sourceUrl", "",
"url for source cluster")
flag.StringVar(&options.sourceUsername, "sourceUsername", "",
"username for source cluster")
flag.StringVar(&options.sourcePassword, "sourcePassword", "",
"password for source cluster")
flag.StringVar(&options.sourceBucketName, "sourceBucketName", "",
"bucket name for source cluster")
flag.StringVar(&options.remoteClusterName, "remoteClusterName", "",
"Remote cluster reference name used when creating it")
flag.StringVar(&options.sourceFileDir, "sourceFileDir", base.SourceFileDir,
"directory to store mutations in source cluster")
flag.StringVar(&options.targetUrl, "targetUrl", "",
"url for target cluster")
flag.StringVar(&options.targetUsername, "targetUsername", "",
"username for target cluster")
flag.StringVar(&options.targetPassword, "targetPassword", "",
"password for target cluster")
flag.StringVar(&options.targetBucketName, "targetBucketName", "",
"bucket name for target cluster")
flag.StringVar(&options.targetFileDir, "targetFileDir", base.TargetFileDir,
"directory to store mutations in target cluster")
flag.Uint64Var(&options.numberOfSourceDcpClients, "numberOfSourceDcpClients", 1,
"number of source dcp clients")
flag.Uint64Var(&options.numberOfWorkersPerSourceDcpClient, "numberOfWorkersPerSourceDcpClient", 64,
"number of workers for each source dcp client")
flag.Uint64Var(&options.numberOfTargetDcpClients, "numberOfTargetDcpClients", 1,
"number of target dcp clients")
flag.Uint64Var(&options.numberOfWorkersPerTargetDcpClient, "numberOfWorkersPerTargetDcpClient", 64,
"number of workers for each target dcp client")
flag.Uint64Var(&options.numberOfWorkersForFileDiffer, "numberOfWorkersForFileDiffer", 30,
"number of worker threads for file differ ")
flag.Uint64Var(&options.numberOfWorkersForMutationDiffer, "numberOfWorkersForMutationDiffer", 30,
"number of worker threads for mutation differ ")
flag.Uint64Var(&options.numberOfBins, "numberOfBins", 5,
"number of buckets per vbucket")
flag.Uint64Var(&options.numberOfFileDesc, "numberOfFileDesc", 500,
"number of file descriptors")
flag.Uint64Var(&options.completeByDuration, "completeByDuration", 0,
"duration that the tool should run")
flag.BoolVar(&options.completeBySeqno, "completeBySeqno", true,
"whether tool should automatically complete (after processing all mutations at start time)")
flag.StringVar(&options.checkpointFileDir, "checkpointFileDir", base.CheckpointFileDir,
"directory for checkpoint files")
flag.StringVar(&options.oldSourceCheckpointFileName, "oldSourceCheckpointFileName", "",
"old source checkpoint file to load from when tool starts")
flag.StringVar(&options.oldTargetCheckpointFileName, "oldTargetCheckpointFileName", "",
"old target checkpoint file to load from when tool starts")
flag.StringVar(&options.newCheckpointFileName, "newCheckpointFileName", "",
"new checkpoint file to write to when tool shuts down")
flag.StringVar(&options.fileDifferDir, "fileDifferDir", base.FileDifferDir,
" directory for storing diffs generated by file differ")
flag.StringVar(&options.mutationDifferDir, "mutationDifferDir", base.MutationDifferDir,
" output directory for mutation differ")
flag.Uint64Var(&options.mutationDifferBatchSize, "mutationDifferBatchSize", 100,
"size of batch used by mutation differ")
flag.Uint64Var(&options.mutationDifferTimeout, "mutationDifferTimeout", 30,
"timeout, in seconds, used by mutation differ")
flag.Uint64Var(&options.sourceDcpHandlerChanSize, "sourceDcpHandlerChanSize", base.DcpHandlerChanSize,
"size of source dcp handler channel")
flag.Uint64Var(&options.targetDcpHandlerChanSize, "targetDcpHandlerChanSize", base.DcpHandlerChanSize,
"size of target dcp handler channel")
flag.Uint64Var(&options.bucketOpTimeout, "bucketOpTimeout", base.BucketOpTimeout,
" timeout for bucket for stats collection, in seconds")
flag.Uint64Var(&options.maxNumOfGetStatsRetry, "maxNumOfGetStatsRetry", base.MaxNumOfGetStatsRetry,
"max number of retry for get stats")
flag.Uint64Var(&options.maxNumOfSendBatchRetry, "maxNumOfSendBatchRetry", base.MaxNumOfSendBatchRetry,
"max number of retry for send batch")
flag.Uint64Var(&options.getStatsRetryInterval, "getStatsRetryInterval", base.GetStatsRetryInterval,
" retry interval for get stats, in seconds")
flag.Uint64Var(&options.sendBatchRetryInterval, "sendBatchRetryInterval", base.SendBatchRetryInterval,
"retry interval for send batch, in milliseconds")
flag.Uint64Var(&options.getStatsMaxBackoff, "getStatsMaxBackoff", base.GetStatsMaxBackoff,
"max backoff for get stats, in seconds")
flag.Uint64Var(&options.sendBatchMaxBackoff, "sendBatchMaxBackoff", base.SendBatchMaxBackoff,
"max backoff for send batch, in seconds")
flag.Uint64Var(&options.delayBetweenSourceAndTarget, "delayBetweenSourceAndTarget", base.DelayBetweenSourceAndTarget,
"delay between source cluster start up and target cluster start up, in seconds")
flag.Uint64Var(&options.checkpointInterval, "checkpointInterval", base.CheckpointInterval,
"interval for periodical checkpointing, in seconds")
flag.BoolVar(&options.runDataGeneration, "runDataGeneration", true,
" whether to run data generation")
flag.BoolVar(&options.runFileDiffer, "runFileDiffer", true,
" whether to file differ")
flag.BoolVar(&options.runMutationDiffer, "runMutationDiffer", true,
" whether to verify diff keys through aysnc Get on clusters")
flag.BoolVar(&options.enforceTLS, "enforceTLS", false,
" stops executing if pre-requisites are not in place to ensure TLS communications")
flag.IntVar(&options.bucketBufferCapacity, "bucketBufferCapacity", base.BucketBufferCapacity,
" number of items kept in memory per binary buffer bucket")
flag.StringVar(&options.compareType, "compareType", base.MutationCompareTypeMetadata,
" whether to compare meta, body, or both. Default meta")
flag.IntVar(&options.mutationDifferRetries, "mutationRetries", 0,
"Additional number of times to retry to resolve the mutation differences")
flag.IntVar(&options.mutationDifferRetriesWaitSecs, "mutationRetriesWaitSecs", 60,
"Seconds to wait in between retries for mutation differences")
flag.IntVar(&options.numOfFiltersInFilterPool, "numOfFiltersInFilterPool", 32,
"Number of filters to be created and shared among all DCP handlers")
flag.BoolVar(&options.debugMode, "debugMode", false,
"The differ to be run with debug log level and the SDK/gocb logging will also be enabled.")
flag.IntVar(&options.setupTimeout, "setupTimeout", base.SetupTimeoutSeconds,
"Common setup timeout duration in seconds")
flag.StringVar(&options.fileContaingXattrKeysForNoComapre, "fileContaingXattrKeysForNoComapre", "",
"Path to the file containing the Xattr keys for NoCompare ")
flag.Parse()
}
func validateCompareType(method string) {
for _, str := range base.MutationDiffCompareType {
if method == str {
return
}
}
fmt.Fprintf(os.Stderr, "Invalid compareType '%v'. Accepted values are %v\n", options.compareType, base.MutationDiffCompareType)
os.Exit(1)
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage : %s [OPTIONS] \n", os.Args[0])
flag.PrintDefaults()
}
type diffToolStateType int
const (
StateInitial diffToolStateType = iota
StateDcpStarted diffToolStateType = iota
StateFinal diffToolStateType = iota
)
type difftoolState struct {
state diffToolStateType
mtx sync.Mutex
}
type vbInfo struct {
sourceNoOfVbuckets uint16
targetNoOfVbuckets uint16
isVariableVB bool
}
type xdcrDiffTool struct {
utils xdcrUtils.UtilsIface
metadataSvc service_def.MetadataSvc
remoteClusterSvc service_def.RemoteClusterSvc
replicationSpecSvc service_def.ReplicationSpecSvc
collectionsManifestsSvc service_def.CollectionsManifestSvc
bucketTopologySvc service_def.BucketTopologySvc
logger *xdcrLog.CommonLogger
xdcrTopologySvc service_def.XDCRCompTopologySvc
selfRef *metadata.RemoteClusterReference
selfRefPopulated uint32
specifiedRef *metadata.RemoteClusterReference
specifiedSpec *metadata.ReplicationSpecification
filter xdcrParts.Filter
selfDefaultPoolInfo map[string]interface{}
selfPoolsNodes map[string]interface{}
srcCapabilities metadata.Capability
tgtCapabilities metadata.Capability
srcClusterCompat int
srcBucketManifest *metadata.CollectionsManifest
tgtBucketManifest *metadata.CollectionsManifest
// If non-empty, just stream these collection IDs from each side's DCP
srcCollectionIds []uint32
tgtCollectionIds []uint32
// Logically there should only be 1-1 mapping, but make this flexible just in case
srcToTgtColIdsMap map[uint32][]uint32
// For collections migration mode, each filter should cause one or more target collection IDs
colFilterToTgtColIdsMap map[string][]uint32
// Each filter string above is translated into a consistent ordered list below. The *index* of each filter
// string will then be used for the remainder of the differ protocol, and used to determine if a source mutation
// has passed a certain filter or not
colFilterOrderedKeys []string
colFilterOrderedTargetNs []*xdcrBase.CollectionNamespace
colFilterOrderedTargetColId []uint32
// Used for migration mapping
migrationMapping metadata.CollectionNamespaceMapping
duplicatedMapping differ.DuplicatedHintMap
sourceDcpDriver *dcp.DcpDriver
targetDcpDriver *dcp.DcpDriver
curState difftoolState
legacyMode bool
// Xattr Keys to be excluded for comparison
xattrKeysForNoCompare map[string]bool
// Includes vBucket details for both the source and target buckets.
vbInfo *vbInfo
}
func staticHostAddr() string {
return "http://" + options.sourceUrl
}
func NewDiffTool(legacyMode bool) (*xdcrDiffTool, error) {
var err error
difftool := &xdcrDiffTool{
utils: xdcrUtils.NewUtilities(),
legacyMode: legacyMode,
srcToTgtColIdsMap: make(map[uint32][]uint32),
colFilterToTgtColIdsMap: map[string][]uint32{},
xattrKeysForNoCompare: map[string]bool{},
}
if options.fileContaingXattrKeysForNoComapre != "" {
readFile, er := os.Open(options.fileContaingXattrKeysForNoComapre)
if er != nil {
fmt.Printf("Error in reading the file %v. err=%v\n", options.fileContaingXattrKeysForNoComapre, err)
return nil, er
}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
difftool.xattrKeysForNoCompare[fileScanner.Text()] = true
}
}
// HLV and ImportCas needs to be stripped from the Xattrs
difftool.xattrKeysForNoCompare[xdcrBase.XATTR_HLV] = true
difftool.xattrKeysForNoCompare[xdcrBase.XATTR_MOU] = true
difftool.xattrKeysForNoCompare[xdcrBase.XATTR_MOBILE] = true
logCtx := xdcrLog.DefaultLoggerContext
difftool.logger = xdcrLog.NewLogger("xdcrDiffTool", xdcrLog.DefaultLoggerContext)
if options.debugMode {
logCtx.SetLogLevel(xdcrLog.LogLevelDebug)
gocb.SetLogger(gocb.VerboseStdioLogger())
}
var poolsInfo map[string]interface{}
var sourceClusterUUID string
err, statusCode := difftool.utils.QueryRestApi(staticHostAddr(), xdcrBase.PoolsPath, false, xdcrBase.MethodGet, "", nil, 0, &poolsInfo, difftool.logger)
if err != nil || statusCode != 200 {
return nil, fmt.Errorf("Failed on calling %v, err=%v, statusCode=%v\n", xdcrBase.PoolsPath, err, statusCode)
}
// note that xdcrBase.RemoteClusterUuid is purely "uuid" and can be used for local cluster UUID as well
uuidObj, ok := poolsInfo[xdcrBase.RemoteClusterUuid]
if !ok {
return nil, fmt.Errorf("Could not get uuid of local cluster.\n")
}
sourceClusterUUID = uuidObj.(string)
difftool.selfRef, _ = metadata.NewRemoteClusterReference(sourceClusterUUID, base.SelfReferenceName, options.sourceUrl, options.sourceUsername, options.sourcePassword,
"", false, "", nil, nil, nil, nil)
if !legacyMode {
difftool.metadataSvc, err = metadata_svc.NewMetaKVMetadataSvc(nil, difftool.utils, true /*readOnly*/)
if err != nil {
return nil, err
}
uiLogSvcMock := &service_def_mock.UILogSvc{}
uiLogSvcMock.On("Write", mock.Anything).Run(func(args mock.Arguments) { fmt.Printf("%v", args.Get(0).(string)) }).Return(nil)
xdcrTopologyMock := &service_def_mock.XDCRCompTopologySvc{}
xdcrTopologyMockSetupCb := func() {
setupXdcrToplogyMock(xdcrTopologyMock, difftool)
}
resolverSvcMock := &service_def_mock.ResolverSvcIface{}
checkpointSvcMock := &service_def_mock.CheckpointsService{}
manifestsSvcMock := &service_def_mock.ManifestsService{}
manifestsSvcMock.On("GetSourceManifests", mock.Anything).Return(nil, service_def.MetadataNotFoundErr)
manifestsSvcMock.On("GetTargetManifests", mock.Anything).Return(nil, service_def.MetadataNotFoundErr)
replicationSettingSvc := metadata_svc.NewReplicationSettingsSvc(difftool.metadataSvc, nil, xdcrTopologyMock)
difftool.remoteClusterSvc, err = metadata_svc.NewRemoteClusterService(uiLogSvcMock, difftool.metadataSvc, xdcrTopologyMock,
xdcrLog.DefaultLoggerContext, difftool.utils)
if err != nil {
return nil, err
}
if err = difftool.retrieveClustersCapabilities(legacyMode, xdcrTopologyMockSetupCb); err != nil {
return nil, err
}
difftool.replicationSpecSvc, err = metadata_svc.NewReplicationSpecService(uiLogSvcMock, difftool.remoteClusterSvc,
difftool.metadataSvc, xdcrTopologyMock, resolverSvcMock, difftool.logger.LoggerContext(), difftool.utils,
replicationSettingSvc)
if err != nil {
return nil, err
}
err = difftool.retrieveReplicationSpecInfo()
if err != nil {
return nil, err
}
securitySvc := &service_def_mock.SecuritySvc{}
setupSecuritySvcMock(securitySvc)
err = setupMyKVNodes(xdcrTopologyMock, difftool)
if err != nil {
return nil, err
}
difftool.bucketTopologySvc, err = service_impl.NewBucketTopologyService(xdcrTopologyMock, difftool.remoteClusterSvc,
difftool.utils, xdcrBase.TopologyChangeCheckInterval, difftool.logger.LoggerContext(),
difftool.replicationSpecSvc, securitySvc, streamApiWatcher.GetStreamApiWatcher)
if err != nil {
return nil, err
}
difftool.collectionsManifestsSvc, err = metadata_svc.NewCollectionsManifestService(difftool.remoteClusterSvc,
difftool.replicationSpecSvc, uiLogSvcMock, difftool.logger.LoggerContext(), difftool.utils, checkpointSvcMock,
xdcrTopologyMock, difftool.bucketTopologySvc, manifestsSvcMock)
if err != nil {
return nil, err
}
difftool.logger.Infof("Source cluster supports collections: %v Target cluster supports collections: %v\n",
difftool.srcCapabilities.HasCollectionSupport(), difftool.tgtCapabilities.HasCollectionSupport())
if difftool.srcCapabilities.HasCollectionSupport() || difftool.tgtCapabilities.HasCollectionSupport() {
err = difftool.populateCollectionsPreReq()
if err != nil {
return nil, err
}
}
} else {
// Need to do this outside of legacy mode
if err := difftool.retrieveClustersCapabilities(legacyMode, nil); err != nil {
return nil, err
}
}
difftool.vbInfo, err = difftool.getVbInfo()
if err != nil {
return nil, err
}
// Capture any Ctrl-C for continuing to next steps or cleanup
go difftool.monitorInterruptSignal()
return difftool, err
}
func setupSecuritySvcMock(securitySvc *service_def_mock.SecuritySvc) {
securitySvc.On("IsClusterEncryptionLevelStrict").Return(false)
}
// This may be re-set up once self-reference is populated
func setupXdcrToplogyMock(xdcrTopologyMock *service_def_mock.XDCRCompTopologySvc, diffTool *xdcrDiffTool) {
xdcrTopologyMock.On("IsMyClusterEnterprise").Return(true, nil)
xdcrTopologyMock.On("IsKVNode").Return(true, nil)
xdcrTopologyMock.On("IsMyClusterEncryptionLevelStrict").Return(false)
xdcrTopologyMock.On("MyClusterCompatibility").Return(diffTool.srcClusterCompat, nil)
xdcrTopologyMock.On("IsOrchestratorNode").Return(false, nil)
setupTopologyMockCredentials(xdcrTopologyMock, diffTool)
setupTopologyMockConnectionString(xdcrTopologyMock, diffTool)
}
func setupMyKVNodes(topologyMock *service_def_mock.XDCRCompTopologySvc, diffTool *xdcrDiffTool) error {
// As of XDCR v8, pools/nodes endpoint is gone so we need to do things the legacy way
nodesInfo := diffTool.selfPoolsNodes
if nodes, ok := nodesInfo[base.NodesKey]; !ok {
return fmt.Errorf("%v is not found from pools/nodes output", base.NodesKey)
} else if nodesList, ok := nodes.([]interface{}); !ok {
return fmt.Errorf("nodesList is not an interface list")
} else {
var found bool
for _, node := range nodesList {
nodeInfoMap, ok := node.(map[string]interface{})
if !ok {
// should never get here
return fmt.Errorf("node type is %v", reflect.TypeOf(node))
}
thisNode, ok := nodeInfoMap[xdcrBase.ThisNodeKey]
if ok {
thisNodeBool, ok := thisNode.(bool)
if !ok {
// should never get here
return fmt.Errorf("thisNode is %v", reflect.TypeOf(thisNode))
}
if thisNodeBool {
// found current node
found = true
}
}
if found {
ports := nodeInfoMap[xdcrBase.PortsKey]
portsMap := ports.(map[string]interface{})
directPort := portsMap[xdcrBase.DirectPortKey]
directPortFloat := directPort.(float64)
memcachedPort := uint16(directPortFloat)
hostAddr := nodeInfoMap[xdcrBase.HostNameKey]
hostAddrStr := hostAddr.(string)
hostName := xdcrBase.GetHostName(hostAddrStr)
memcachedAddr := xdcrBase.GetHostAddr(hostName, memcachedPort)
topologyMock.On("MyKVNodes").Return([]string{memcachedAddr}, nil)
break
}
}
if !found {
return fmt.Errorf("Unable to set memcached port")
}
}
return nil
}
func setupTopologyMockConnectionString(xdcrTopologyMock *service_def_mock.XDCRCompTopologySvc, diffTool *xdcrDiffTool) {
connFunc := func() string {
if atomic.LoadUint32(&diffTool.selfRefPopulated) == 1 {
connStr, _ := diffTool.selfRef.MyConnectionStr()
return connStr
} else {
return ""
}
}
errFunc := func() error {
if atomic.LoadUint32(&diffTool.selfRefPopulated) == 1 {
return nil
} else {
return fmt.Errorf("Not initialized yet")
}
}
xdcrTopologyMock.On("MyConnectionStr").Return(connFunc, errFunc)
}
func setupTopologyMockCredentials(xdcrTopologyMock *service_def_mock.XDCRCompTopologySvc, diffTool *xdcrDiffTool) {
getUserName := func() string {
if atomic.LoadUint32(&diffTool.selfRefPopulated) == 1 {
return diffTool.selfRef.UserName()
} else {
return ""
}
}
getPw := func() string {
if atomic.LoadUint32(&diffTool.selfRefPopulated) == 1 {
return diffTool.selfRef.Password()
} else {
return ""
}
}
getAuthMech := func() xdcrBase.HttpAuthMech {
if atomic.LoadUint32(&diffTool.selfRefPopulated) == 1 {
return diffTool.selfRef.HttpAuthMech()
} else {
return xdcrBase.HttpAuthMechPlain
}
}
getCert := func() []byte {
if atomic.LoadUint32(&diffTool.selfRefPopulated) == 1 {
return diffTool.selfRef.Certificates()
} else {
return nil
}
}
getSanCert := func() bool {
if atomic.LoadUint32(&diffTool.selfRefPopulated) == 1 {
return diffTool.selfRef.SANInCertificate()
} else {
return false
}
}
getClientCert := func() []byte {
if atomic.LoadUint32(&diffTool.selfRefPopulated) == 1 {
return diffTool.selfRef.ClientCertificate()
} else {
return nil
}
}
getClientKey := func() []byte {
if atomic.LoadUint32(&diffTool.selfRefPopulated) == 1 {
return diffTool.selfRef.ClientKey()
} else {
return nil
}
}
getErr := func() error {
if atomic.LoadUint32(&diffTool.selfRefPopulated) == 1 {
return nil
} else {
return fmt.Errorf("Not initialized yet")
}
}
xdcrTopologyMock.On("MyCredentials").Return(getUserName, getPw, getAuthMech, getCert, getSanCert, getClientCert, getClientKey, getErr)
}
func maybeSetEnv(key, value string) {
if os.Getenv(key) != "" {
return
}
os.Setenv(key, value)
}
func main() {
argParse()
base.SetupTimeoutSeconds = options.setupTimeout
validateCompareType(options.compareType)
fmt.Printf("differ is run with options: %+v\n", options)
legacyMode := len(options.targetUsername) > 0
if err := setupDirectories(); err != nil {
fmt.Printf("Unable to set up directory structure: %v\n", err)
os.Exit(1)
}
difftool, err := NewDiffTool(legacyMode)
if err != nil {
fmt.Printf("Error creating difftool: %v\n", err)
os.Exit(1)
}
if options.enforceTLS {
// For using certificates, the source cluster must be on a loopback device since we will be retrieving the
// source cluster's certificate to prevent sniffing
if !isURLLoopBack(options.sourceUrl) {
fmt.Printf("enforceTLS options requires that source addr %v to use loopback device\n", options.sourceUrl)
os.Exit(1)
}
}
if legacyMode {
if options.enforceTLS {
fmt.Printf("enforceTLS option is not compatible with legacyMode")
os.Exit(1)
}
// OK to ignore metakv err in manual mode
if err := difftool.populateTemporarySpecAndRef(); err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
}
if options.runDataGeneration {
err := difftool.generateDataFiles()
if err != nil {
fmt.Printf("Error generating data files. err=%v\n", err)
os.Exit(1)
}
} else {
fmt.Printf("Skipping generating data files since it has been disabled\n")
}
if options.runFileDiffer {
err := difftool.diffDataFiles()
if err != nil {
fmt.Printf("Error running file difftool. err=%v\n", err)
os.Exit(1)
}
} else {
fmt.Printf("Skipping file difftool since it has been disabled\n")
}
if options.runMutationDiffer {
difftool.runMutationDiffer()
} else {
fmt.Printf("Skipping mutation diff since it has been disabled\n")
}
}
func isURLLoopBack(url string) bool {
IPLoopbackCheck := net.ParseIP(xdcrBase.GetHostName(url))
hostNameIsLocalHost := xdcrBase.GetHostName(url) == "localhost"
return IPLoopbackCheck.IsLoopback() || hostNameIsLocalHost
}
func setupDirectories() error {
err := os.MkdirAll(options.sourceFileDir, 0777)
if err != nil {
fmt.Printf("Error mkdir sourceFileDir: %v\n", err)
}
err = os.MkdirAll(options.targetFileDir, 0777)
if err != nil {
fmt.Printf("Error mkdir targetFileDir: %v\n", err)
}
err = os.MkdirAll(options.checkpointFileDir, 0777)
if err != nil {
// it is ok for checkpoint dir to be existing, since we do not clean it up
fmt.Printf("Error mkdir checkpointFileDir: %v\n", err)
}
return nil
}
func (difftool *xdcrDiffTool) createFilter() error {
var ok bool
var expr string
expr, ok = difftool.specifiedSpec.Settings.Values[metadata.FilterExpressionKey].(string)
filterMode := difftool.specifiedSpec.Settings.GetExpDelMode()
if ok && len(expr) > 0 {
var filterVersion xdcrBase.FilterVersionType
if filterVersion, ok = difftool.specifiedSpec.Settings.Values[metadata.FilterVersionKey].(xdcrBase.FilterVersionType); !ok {
err := fmt.Errorf("Unable to find filter version given filter expression %v\nsettings:%v\n", expr, difftool.specifiedSpec.Settings)
return err
}
if filterVersion == xdcrBase.FilterVersionKeyOnly {
expr = xdcrBase.UpgradeFilter(expr)
}
difftool.logger.Infof("Found filtering expression: %v\n", expr)
}
mobileCompat := difftool.specifiedSpec.Settings.GetMobileCompatible()
filter, err := filterPool.NewFilterPool(options.numOfFiltersInFilterPool, expr, difftool.utils, filterMode, mobileCompat)
difftool.filter = filter
return err
}
func (difftool *xdcrDiffTool) generateDataFiles() error {
difftool.logger.Infof("GenerateDataFiles routine started\n")
defer difftool.logger.Infof("GenerateDataFiles routine completed\n")
if options.completeByDuration == 0 && !options.completeBySeqno {
difftool.logger.Infof("completeByDuration is required when completeBySeqno is false\n")
os.Exit(1)
}
errChan := make(chan error, 1)
waitGroup := &sync.WaitGroup{}
var fileDescPool fdp.FdPoolIface
if options.numberOfFileDesc > 0 {
fileDescPool = fdp.NewFileDescriptorPool(int(options.numberOfFileDesc))
}
if err := difftool.createFilter(); err != nil {
difftool.logger.Errorf("Error creating filter: %v", err.Error())
os.Exit(1)
}
difftool.sourceDcpDriver = startDcpDriver(difftool.logger, base.SourceClusterName, options.sourceUrl, difftool.specifiedSpec.SourceBucketName,
difftool.selfRef, options.sourceFileDir, options.checkpointFileDir,
options.oldSourceCheckpointFileName, options.newCheckpointFileName, options.numberOfSourceDcpClients,
options.numberOfWorkersPerSourceDcpClient, options.numberOfBins, options.sourceDcpHandlerChanSize,
options.bucketOpTimeout, options.maxNumOfGetStatsRetry, options.getStatsRetryInterval,
options.getStatsMaxBackoff, options.checkpointInterval, errChan, waitGroup, options.completeBySeqno, fileDescPool, difftool.filter,
difftool.srcCapabilities, difftool.srcCollectionIds, difftool.colFilterOrderedKeys, difftool.utils, options.bucketBufferCapacity,
difftool.migrationMapping, difftool.specifiedSpec.Settings.GetMobileCompatible(), difftool.specifiedSpec.Settings.GetExpDelMode(), difftool.xattrKeysForNoCompare, difftool.vbInfo.sourceNoOfVbuckets, difftool.vbInfo.isVariableVB)
delayDurationBetweenSourceAndTarget := time.Duration(options.delayBetweenSourceAndTarget) * time.Second
difftool.logger.Infof("Waiting for %v before starting target dcp clients\n", delayDurationBetweenSourceAndTarget)
time.Sleep(delayDurationBetweenSourceAndTarget)
difftool.logger.Infof("Starting target dcp clients\n")
difftool.targetDcpDriver = startDcpDriver(difftool.logger, base.TargetClusterName, difftool.specifiedRef.HostName_,
difftool.specifiedSpec.TargetBucketName, difftool.specifiedRef,
options.targetFileDir, options.checkpointFileDir, options.oldTargetCheckpointFileName, options.newCheckpointFileName,
options.numberOfTargetDcpClients, options.numberOfWorkersPerTargetDcpClient, options.numberOfBins, options.targetDcpHandlerChanSize,
options.bucketOpTimeout, options.maxNumOfGetStatsRetry, options.getStatsRetryInterval, options.getStatsMaxBackoff,
options.checkpointInterval, errChan, waitGroup, options.completeBySeqno, fileDescPool, difftool.filter,
difftool.tgtCapabilities, difftool.tgtCollectionIds, difftool.colFilterOrderedKeys, difftool.utils, options.bucketBufferCapacity,
difftool.migrationMapping, difftool.specifiedSpec.Settings.GetMobileCompatible(), difftool.specifiedSpec.Settings.GetExpDelMode(), difftool.xattrKeysForNoCompare, difftool.vbInfo.targetNoOfVbuckets, difftool.vbInfo.isVariableVB)
difftool.curState.mtx.Lock()
difftool.curState.state = StateDcpStarted
difftool.curState.mtx.Unlock()
var err error
if options.completeBySeqno {
err = difftool.waitForCompletion(difftool.sourceDcpDriver, difftool.targetDcpDriver, errChan, waitGroup)
} else {
err = difftool.waitForDuration(difftool.sourceDcpDriver, difftool.targetDcpDriver, errChan, options.completeByDuration, delayDurationBetweenSourceAndTarget)
}
return err
}
func (difftool *xdcrDiffTool) diffDataFiles() error {
difftool.logger.Infof("DiffDataFiles routine started\n")
defer difftool.logger.Infof("DiffDataFiles routine completed\n")
err := os.RemoveAll(options.fileDifferDir)
if err != nil {
difftool.logger.Errorf("Error removing fileDifferDir: %v\n", err)
}
err = os.MkdirAll(options.fileDifferDir, 0777)
if err != nil {
return fmt.Errorf("Error mkdir fileDifferDir: %v\n", err)
}
var numberOfVbuckets uint16 = difftool.vbInfo.sourceNoOfVbuckets
if difftool.vbInfo.isVariableVB { // numOfVbs at source != numOfVbs at target
numberOfVbuckets = base.TraditionalNumberOfVbuckets
}
difftoolDriver := differ.NewDifferDriver(options.sourceFileDir, options.targetFileDir, options.fileDifferDir,
base.DiffKeysFileName, int(options.numberOfWorkersForFileDiffer), int(options.numberOfBins),
int(options.numberOfFileDesc), difftool.srcToTgtColIdsMap, difftool.colFilterOrderedKeys, difftool.colFilterOrderedTargetColId, difftool.selfRef.Uuid_, difftool.specifiedRef.Uuid_, difftool.specifiedSpec.SourceBucketUUID, difftool.specifiedSpec.TargetBucketUUID, difftool.bucketTopologySvc, difftool.specifiedSpec, difftool.logger, numberOfVbuckets)
err = difftoolDriver.Run()
if err != nil {
difftool.logger.Errorf("Error from diffDataFiles = %v\n", err)
}
difftoolDriver.MapLock.RLock()
if difftool.colFilterOrderedKeys == nil {
difftool.logger.Infof("Source vb to item count map: %v", difftoolDriver.SrcVbItemCntMap)
}
difftool.logger.Infof("Target vb to item count map: %v", difftoolDriver.TgtVbItemCntMap)
difftoolDriver.MapLock.RUnlock()
if difftool.colFilterOrderedKeys == nil {
difftool.logger.Infof("Source bucket item count including tombstones is %v (excluding %v filtered mutations)", difftoolDriver.SourceItemCount, difftool.sourceDcpDriver.FilteredCount())
} else {
difftool.logger.Infof("Replication is in migration mode from the source bucket")
}
difftool.logger.Infof("Target bucket item count including tombstones is %v (excluding %v filtered mutations)", difftoolDriver.TargetItemCount, difftool.targetDcpDriver.FilteredCount())
if difftool.colFilterOrderedKeys == nil && difftoolDriver.SourceItemCount != difftoolDriver.TargetItemCount {
difftool.logger.Infof("Here are the vbuckets with different item counts:")
for vb, c1 := range difftoolDriver.SrcVbItemCntMap {
c2 := difftoolDriver.TgtVbItemCntMap[vb]
if c1 != c2 {
difftool.logger.Infof("vb:%v source count %v, target count %v", vb, c1, c2)
}
}
}
difftool.duplicatedMapping = difftoolDriver.DuplicatedHint
return err
}
func (difftool *xdcrDiffTool) runMutationDiffer() {
difftool.logger.Infof("runMutationDiffer started with compareBody=%v\n", options.compareType)
defer difftool.logger.Infof("runMutationDiffer completed\n")
err := os.RemoveAll(options.mutationDifferDir)
if err != nil {
difftool.logger.Errorf("Error removing mutationDifferDir: %v\n", err)
}
err = os.MkdirAll(options.mutationDifferDir, 0777)
if err != nil {
err = fmt.Errorf("Error mkdir mutationDifferDir: %v\n", err)
return
}
mutationDiffer := differ.NewMutationDiffer(difftool.selfRef.Uuid_, difftool.specifiedSpec.SourceBucketName, difftool.specifiedSpec.SourceBucketUUID,
difftool.selfRef, difftool.specifiedRef.Uuid_, difftool.specifiedSpec.TargetBucketName, difftool.specifiedSpec.TargetBucketUUID, difftool.specifiedRef,
options.fileDifferDir, options.mutationDifferDir, int(options.numberOfWorkersForMutationDiffer),
int(options.mutationDifferBatchSize), int(options.mutationDifferTimeout), int(options.maxNumOfSendBatchRetry),
time.Duration(options.sendBatchRetryInterval)*time.Millisecond,
time.Duration(options.sendBatchMaxBackoff)*time.Second, options.compareType, difftool.logger, difftool.srcToTgtColIdsMap,
difftool.srcCapabilities, difftool.tgtCapabilities, difftool.utils, options.mutationDifferRetries,
options.mutationDifferRetriesWaitSecs, difftool.duplicatedMapping)
err = mutationDiffer.Run()
if err != nil {
difftool.logger.Errorf("Error from runMutationDiffer = %v\n", err)
}
}
func startDcpDriver(logger *xdcrLog.CommonLogger, name, url, bucketName string, ref *metadata.RemoteClusterReference, fileDir, checkpointFileDir, oldCheckpointFileName, newCheckpointFileName string, numberOfDcpClients, numberOfWorkersPerDcpClient, numberOfBins, dcpHandlerChanSize, bucketOpTimeout, maxNumOfGetStatsRetry, getStatsRetryInterval, getStatsMaxBackoff, checkpointInterval uint64, errChan chan error, waitGroup *sync.WaitGroup, completeBySeqno bool, fdPool fdp.FdPoolIface, filter xdcrParts.Filter, capabilities metadata.Capability, collectionIDs []uint32, colMigrationFilters []string, utils xdcrUtils.UtilsIface, bucketBufferCap int, migrationMapping metadata.CollectionNamespaceMapping, mobileCompat int, expDelMode xdcrBase.FilterExpDelType, xattrKeysForNoCompare map[string]bool, numberOfVbuckets uint16, isVariableVB bool) *dcp.DcpDriver {
waitGroup.Add(1)
dcpDriver := dcp.NewDcpDriver(logger, name, url, bucketName, ref, fileDir, checkpointFileDir, oldCheckpointFileName,
newCheckpointFileName, int(numberOfDcpClients), int(numberOfWorkersPerDcpClient), int(numberOfBins),
int(dcpHandlerChanSize), time.Duration(bucketOpTimeout)*time.Second, int(maxNumOfGetStatsRetry),
time.Duration(getStatsRetryInterval)*time.Second, time.Duration(getStatsMaxBackoff)*time.Second,
int(checkpointInterval), errChan, waitGroup, completeBySeqno, fdPool, filter, capabilities, collectionIDs, colMigrationFilters,
utils, bucketBufferCap, migrationMapping, mobileCompat, expDelMode, xattrKeysForNoCompare, numberOfVbuckets, isVariableVB)
// dcp driver startup may take some time. Do it asynchronously
go startDcpDriverAysnc(dcpDriver, errChan, logger)
return dcpDriver
}
func startDcpDriverAysnc(dcpDriver *dcp.DcpDriver, errChan chan error, logger *xdcrLog.CommonLogger) {
err := dcpDriver.Start()
if err != nil {
logger.Errorf("Error starting dcp driver %v. err=%v\n", dcpDriver.Name, err)
utils.AddToErrorChan(errChan, err)
}
}
func (difftool *xdcrDiffTool) waitForCompletion(sourceDcpDriver, targetDcpDriver *dcp.DcpDriver, errChan chan error, waitGroup *sync.WaitGroup) error {
doneChan := make(chan bool, 1)
go utils.WaitForWaitGroup(waitGroup, doneChan)
select {
case err := <-errChan:
difftool.logger.Errorf("Stop diff generation due to error from dcp client %v\n", err)
err1 := sourceDcpDriver.Stop()
if err1 != nil {
difftool.logger.Errorf("Error stopping source dcp client. err=%v\n", err1)
}
err1 = targetDcpDriver.Stop()
if err1 != nil {
difftool.logger.Errorf("Error stopping target dcp client. err=%v\n", err1)
}
return err
case <-doneChan:
difftool.logger.Infof("Source cluster and target cluster have completed\n")
return nil
}
}
func (difftool *xdcrDiffTool) waitForDuration(sourceDcpDriver, targetDcpDriver *dcp.DcpDriver, errChan chan error, duration uint64, delayDurationBetweenSourceAndTarget time.Duration) (err error) {
timer := time.NewTimer(time.Duration(duration) * time.Second)
select {
case err = <-errChan:
difftool.logger.Errorf("Stop diff generation due to error from dcp client %v\n", err)
case <-timer.C:
difftool.logger.Infof("Stop diff generation after specified processing duration\n")
}
err1 := sourceDcpDriver.Stop()
if err1 != nil {
difftool.logger.Errorf("Error stopping source dcp client. err=%v\n", err1)
}
time.Sleep(delayDurationBetweenSourceAndTarget)
err1 = targetDcpDriver.Stop()
if err1 != nil {
difftool.logger.Errorf("Error stopping target dcp client. err=%v\n", err1)
}
return err
}
func (difftool *xdcrDiffTool) retrieveReplicationSpecInfo() error {
// CBAUTH has already been setup
var err error
if options.enforceTLS && !difftool.specifiedRef.IsHttps() {
err = fmt.Errorf("enforceTLS requires that the remote cluster reference %v to use Full-Encryption mode", difftool.specifiedRef.Name())
difftool.logger.Errorf(err.Error())
return err
}
if options.targetUsername != "" && options.targetUsername != difftool.specifiedRef.UserName() && options.targetPassword != "" && options.targetPassword != difftool.specifiedRef.Password() {
err = fmt.Errorf("user-specified username and password is different from that of the credentials from reference %v", difftool.specifiedRef.Name())
difftool.logger.Errorf(err.Error())
return err
}
specMap, err := difftool.replicationSpecSvc.AllReplicationSpecs()
if err != nil {
difftool.logger.Errorf("Error retrieving specs: %v\n", err)
return err
}
for _, spec := range specMap {
if spec.SourceBucketName == options.sourceBucketName && spec.TargetBucketName == options.targetBucketName && spec.TargetClusterUUID == difftool.specifiedRef.Uuid() {
difftool.specifiedSpec = spec
break
}
}
if difftool.specifiedSpec == nil {
difftool.logger.Warnf("Unable to find Replication Spec with source %v target %v, attempting to create a temporary one\n", options.sourceBucketName, options.targetBucketName)
// Create a dummy spec
difftool.specifiedSpec, err = metadata.NewReplicationSpecification(options.sourceBucketName, "" /*sourceBucketUUID*/, difftool.specifiedRef.Uuid(), options.targetBucketName, "" /*targetBucketUUID*/)
if err != nil {
difftool.logger.Errorf(err.Error())
}
return err
}
difftool.logger.Infof("Found Remote Cluster: %v and Replication Spec: %v\n", difftool.specifiedRef.String(), difftool.specifiedSpec.String())
return nil
}
func (difftool *xdcrDiffTool) populateTemporarySpecAndRef() error {
var err error
difftool.specifiedSpec, err = metadata.NewReplicationSpecification(options.sourceBucketName, "", /*sourceBucketUUID*/
"" /*targetClusterUUID*/, options.targetBucketName, "" /*targetBucketUUID*/)