Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-master only run single startMirror at a time #3003

Merged
merged 1 commit into from
Dec 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions cmd/mirror-main.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,13 @@ EXAMPLES:
const uaMirrorAppName = "mc-mirror"

type mirrorJob struct {

// the channel to trap SIGKILL signals
trapCh <-chan bool
stopCh chan struct{}

// mutex for shutdown, this prevents the shutdown
// to be initiated multiple times
m *sync.Mutex
m sync.Mutex

// the global watcher object, which receives notifications of created
// and deleted files
Expand All @@ -204,10 +203,11 @@ type mirrorJob struct {
sourceURL string
targetURL string

isFake, isRemove, isOverwrite, isWatch, isPreserve bool
olderThan, newerThan string
storageClass string
userMetadata map[string]string
isFake, isRemove, isOverwrite bool
isWatch, isPreserve bool
olderThan, newerThan string
storageClass string
userMetadata map[string]string

excludeOptions []string
encKeyDB map[string][]prefixSSEPair
Expand Down Expand Up @@ -560,6 +560,10 @@ func (mj *mirrorJob) watchURL(sourceClient Client) *probe.Error {

// Fetch urls that need to be mirrored
func (mj *mirrorJob) startMirror(ctx context.Context, cancelMirror context.CancelFunc, stopParallel func()) {
// Do not run multiple startMirror's
mj.m.Lock()
defer mj.m.Unlock()

isMetadata := len(mj.userMetadata) > 0 || mj.isPreserve
URLsCh := prepareMirrorURLs(mj.sourceURL, mj.targetURL, mj.isFake, mj.isOverwrite, mj.isRemove, isMetadata, mj.excludeOptions, mj.encKeyDB)

Expand Down Expand Up @@ -644,6 +648,7 @@ func (mj *mirrorJob) mirror(ctx context.Context, cancelMirror context.CancelFunc
close(mj.queueCh)
mj.parallel.wait()
}
// startMirror locks and blocks itself.
mj.startMirror(ctx, cancelMirror, stopParallel)
}()

Expand All @@ -668,7 +673,10 @@ func (mj *mirrorJob) mirror(ctx context.Context, cancelMirror context.CancelFunc
case <-ticker.C:
// Start with random sleep time, so as to avoid
// "synchronous checks" between servers
time.Sleep(time.Duration(rand.Float64() * float64(time.Second*5)))
r := rand.New(rand.NewSource(time.Now().Unix()))
time.Sleep(time.Duration(r.Float64() * float64(time.Second*5)))
// startMirror blocks if there is already
// another mirror running.
mj.startMirror(ctx, cancelMirror, nil)
}
}
Expand All @@ -691,7 +699,6 @@ func newMirrorJob(srcURL, dstURL string, isFake, isRemove, isOverwrite, isWatch,
mj := mirrorJob{
trapCh: signalTrap(os.Interrupt, syscall.SIGTERM, syscall.SIGKILL),
stopCh: make(chan struct{}),
m: new(sync.Mutex),

sourceURL: srcURL,
targetURL: dstURL,
Expand Down