/
utilities.go
77 lines (62 loc) · 2.4 KB
/
utilities.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
package binary
import (
"math"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/wal-g/tracelog"
"golang.org/x/mod/semver"
)
// MajorMinorVersion return version in format '<MajorVersion>.<MinorVersion>' (without patch, prerelease, build, ...)
func MajorMinorVersion(version string) string {
if len(version) == 0 {
return version
}
if version[0] != 'v' {
version = "v" + version
}
return semver.MajorMinor(version)
}
// MajorVersion return version in format '<MajorVersion>' (without minor, patch, prerelease, build, ...)
func MajorVersion(version string) string {
if len(version) == 0 {
return version
}
if version[0] != 'v' {
version = "v" + version
}
return semver.Major(version)
}
// NextMajorVersion return next major version in format '<MajorVersion> + 1' (without minor, patch, prerelease, build, ...)
func NextMajorVersion(version string) string {
if len(version) == 0 {
return version
}
minorVersion := MajorMinorVersion(version)
//For MongoDB 4.4 and previous, MongoDB versioning used a Production / Development versioning scheme,
// and had the form X.Y.Z where X.Y refers to either a release series or development series.
// Starting with MongoDB 5.0, MongoDB is released as two different release series: Major and Rapid releases
if minorVersion == "v4.2" {
return "v4"
}
majorVersionStr := MajorVersion(minorVersion)
majorVersionInt, _ := strconv.Atoi(majorVersionStr[1:])
return "v" + strconv.Itoa(majorVersionInt+1)
}
func EnsureCompatibilityToRestoreMongodVersions(backupMongodVersion, restoreMongodVersion string) error {
tracelog.InfoLogger.Printf("Check compatibility backup version %v and current mongod version %v",
backupMongodVersion, restoreMongodVersion)
var nextMajorBackup = NextMajorVersion(backupMongodVersion)
if semver.Compare(MajorMinorVersion(backupMongodVersion), MajorMinorVersion(restoreMongodVersion)) != 0 &&
semver.Compare(MajorVersion(restoreMongodVersion), nextMajorBackup) != 0 {
return errors.Errorf("Backup's Mongo version (%s) is not compatible with Mongo %s",
backupMongodVersion, restoreMongodVersion)
}
return nil
}
// ComputeMongoStartTimeout compute timeout with formula max(10min, UncompressedSizeTb * 40min)
func ComputeMongoStartTimeout(backupUncompressedSize int64) time.Duration {
var tb float64 = 1 << (10 * 4)
coef := float64(backupUncompressedSize) / tb
return time.Duration(math.Max(10, coef*40) * float64(time.Minute))
}