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

Fix binary backup of mongodb 4.2 #1364

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 13 additions & 9 deletions internal/databases/mongo/binary/mongod.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (mongodService *MongodService) GetBackupCursorExtended(backupCursorMeta *Ba
})
}

func (mongodService *MongodService) FixSystemDataAfterRestore(LastWriteTS primitive.Timestamp) error {
func (mongodService *MongodService) FixSystemDataAfterRestore(LastWriteTS primitive.Timestamp, fixOplog bool) error {
ctx := mongodService.Context
localDatabase := mongodService.MongoClient.Database("local")

Expand All @@ -145,14 +145,18 @@ func (mongodService *MongodService) FixSystemDataAfterRestore(LastWriteTS primit
return err
}

tracelog.DebugLogger.Printf("oplogTruncateAfterPoint: %v", LastWriteTS)
err = replaceData(ctx, localDatabase.Collection("replset.oplogTruncateAfterPoint"), true,
bson.M{
"_id": "oplogTruncateAfterPoint",
"oplogTruncateAfterPoint": LastWriteTS,
})
if err != nil {
return err
if fixOplog {
tracelog.DebugLogger.Printf("oplogTruncateAfterPoint: %v", LastWriteTS)
err = replaceData(ctx, localDatabase.Collection("replset.oplogTruncateAfterPoint"), true,
bson.M{
"_id": "oplogTruncateAfterPoint",
"oplogTruncateAfterPoint": LastWriteTS,
})
if err != nil {
return err
}
} else {
tracelog.InfoLogger.Printf("We are skipping fix replset.oplogTruncateAfterPoint because it is disabled")
}

err = replaceData(ctx, localDatabase.Collection("system.replset"), false, nil)
Expand Down
19 changes: 14 additions & 5 deletions internal/databases/mongo/binary/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,29 @@ func (restoreService *RestoreService) DoRestore(backupName, restoreMongodVersion
return err
}

err = restoreService.fixSystemData(sentinel)
if err != nil {
needFixOplog := NeedFixOplog(restoreMongodVersion)

if err = restoreService.fixSystemData(sentinel, needFixOplog); err != nil {
return err
}

return restoreService.recoverFromOplogAsStandalone()
if needFixOplog {
if err = restoreService.recoverFromOplogAsStandalone(); err != nil {
return err
}
} else {
tracelog.InfoLogger.Printf("We are skipping recoverFromOplogAsStandalone because it is disabled")
}

return nil
}

func (restoreService *RestoreService) downloadFromTarArchives(backupName string) error {
downloader := CreateConcurrentDownloader(restoreService.Uploader)
return downloader.Download(backupName, restoreService.LocalStorage.MongodDBPath)
}

func (restoreService *RestoreService) fixSystemData(sentinel *models.Backup) error {
func (restoreService *RestoreService) fixSystemData(sentinel *models.Backup, needFixOplog bool) error {
mongodProcess, err := StartMongodWithDisableLogicalSessionCacheRefresh(restoreService.minimalConfigPath)
if err != nil {
return errors.Wrap(err, "unable to start mongod in special mode")
Expand All @@ -80,7 +89,7 @@ func (restoreService *RestoreService) fixSystemData(sentinel *models.Backup) err
}

lastWriteTS := sentinel.MongoMeta.BackupLastTS
err = mongodService.FixSystemDataAfterRestore(lastWriteTS)
err = mongodService.FixSystemDataAfterRestore(lastWriteTS, needFixOplog)
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions internal/databases/mongo/binary/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"golang.org/x/mod/semver"
)

const minOplogRecoverSupportedVersion = "4.2"

// MajorMinorVersion return version in format '<MajorVersion>.<MinorVersion>' (without patch, prerelease, build, ...)
func MajorMinorVersion(version string) string {
if len(version) == 0 {
Expand All @@ -29,3 +31,7 @@ func EnsureCompatibilityToRestoreMongodVersions(backupMongodVersion, restoreMong
}
return nil
}

func NeedFixOplog(mongodVersion string) bool {
return semver.Compare(MajorMinorVersion(mongodVersion), MajorMinorVersion(minOplogRecoverSupportedVersion)) > 0
}
15 changes: 15 additions & 0 deletions internal/databases/mongo/binary/utilities_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package binary

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNeedFixOplog(t *testing.T) {
assert.False(t, NeedFixOplog("4.0"))
assert.False(t, NeedFixOplog("4.2"))
assert.True(t, NeedFixOplog("4.4"))
assert.True(t, NeedFixOplog("5.0"))
assert.True(t, NeedFixOplog("6.0"))
}