-
Notifications
You must be signed in to change notification settings - Fork 365
/
Copy pathbarrier.go
62 lines (59 loc) · 2 KB
/
barrier.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
// Copyright (C) MongoDB, Inc. 2025-present.
//
// 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
package mongodump
import (
"os"
"time"
"github.com/mongodb/mongo-tools/common/log"
"github.com/pkg/errors"
)
// Wait until a file exists and can be opened for reading
// This is used only for testing mongodump/mongorestore with resmoke
// test infrastructure. The tests will create the barrier file when they have
// finshed writes to the source cluster.
func waitForSourceWritesDoneBarrier(barrierName string) error {
// This code should only run in the resmoke testing environment. It's harmless and
// possibly useful to have verbose logging for testing; and if it accidentally runs
// in production, we want to see that in the logs so it's good to be verbose.
log.Logvf(
log.Always,
"waitForSourceWritesDoneBarrier: initial check for existence of file %#q",
barrierName,
)
start := time.Now()
logInterval := time.Minute
prevLogTime := start
for {
f, err := os.Open(barrierName)
if err == nil {
// We opened the file for reading, so it does exist.
f.Close()
log.Logvf(
log.Always,
"waitForSourceWritesDoneBarrier: barrier file %#q exists - proceed past the barrier",
barrierName,
)
return nil
}
if os.IsNotExist(err) {
if time.Since(prevLogTime) >= logInterval {
prevLogTime = time.Now()
log.Logvf(
log.Always,
"waitForSourceWritesDoneBarrier: still waiting for existence of file %#q after %.1f sec",
barrierName,
prevLogTime.Sub(start).Seconds(),
)
}
// Poll for existence of the barrier file every 500msec
time.Sleep(500 * time.Millisecond)
} else {
// Any other error implies that the resmoke test environment is
// irretrievably confused. The caller must check the error.
return errors.Wrapf(err, "failed to open barrier file %#q", barrierName)
}
}
}