diff options
author | Tim Fogarty <tim.fogarty@mongodb.com> | 2020-01-09 13:00:41 +0000 |
---|---|---|
committer | evergreen <evergreen@mongodb.com> | 2020-01-09 13:00:41 +0000 |
commit | bdbbb262ab3b730cde77cce0315014159840bcb7 (patch) | |
tree | 5ac3e0b37e3288d6cb81a5708a27cfc1ae4137ea | |
parent | 833cab9ee65fe218316abdff98383fc864b4e6b4 (diff) | |
download | mongo-bdbbb262ab3b730cde77cce0315014159840bcb7.tar.gz |
Import tools: 39676d8c639a98d4005e6ca08ba24fa1221cdff3 from branch v4.0
ref: 2aeced60be..39676d8c63
for: 4.0.15
TOOLS-2332 oplog_replay_local_rs.js fails on server latest
TOOLS-2422 admin.tempusers is not dropped by mongorestore
TOOLS-2423 mongorestore does not drop admin.tempusers if it exists in the dump
4 files changed, 111 insertions, 7 deletions
diff --git a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/import.data b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/import.data index 1f067e87b2b..a6b680a634b 100644 --- a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/import.data +++ b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/import.data @@ -1,5 +1,5 @@ { - "commit": "2aeced60bec4310f9d87fd0c2dd535945e253126", + "commit": "39676d8c639a98d4005e6ca08ba24fa1221cdff3", "github": "mongodb/mongo-tools.git", "vendor": "tools", "branch": "v4.0" diff --git a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/metadata.go b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/metadata.go index 7e644f3c047..33bd5e4d769 100644 --- a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/metadata.go +++ b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/metadata.go @@ -181,6 +181,15 @@ func (restore *MongoRestore) CollectionExists(intent *intents.Intent) (bool, err return exists, nil } +// addToKnownCollections will add a collection to the restore.knownCollections cache. +// This is used to update the cache after a collection has been created during a restore. +func (restore *MongoRestore) addToKnownCollections(intent *intents.Intent) { + restore.knownCollectionsMutex.Lock() + defer restore.knownCollectionsMutex.Unlock() + + restore.knownCollections[intent.DB] = append(restore.knownCollections[intent.DB], intent.C) +} + // CreateIndexes takes in an intent and an array of index documents and // attempts to create them using the createIndexes command. If that command // fails, we fall back to individual index creation. @@ -406,20 +415,20 @@ func (restore *MongoRestore) RestoreUsersOrRoles(users, roles *intents.Intent) e } // make sure we always drop the temporary collection - defer func() { + defer func(cleanupArg loopArg) { session, e := restore.SessionProvider.GetSession() if e != nil { // logging errors here because this has no way of returning that doesn't mask other errors - log.Logvf(log.Info, "error establishing connection to drop temporary collection admin.%v: %v", arg.tempCollectionName, e) + log.Logvf(log.Info, "error establishing connection to drop temporary collection admin.%v: %v", cleanupArg.tempCollectionName, e) return } defer session.Close() - log.Logvf(log.DebugHigh, "dropping temporary collection admin.%v", arg.tempCollectionName) - e = session.DB("admin").C(arg.tempCollectionName).DropCollection() + log.Logvf(log.DebugHigh, "dropping temporary collection admin.%v", cleanupArg.tempCollectionName) + e = session.DB("admin").C(cleanupArg.tempCollectionName).DropCollection() if e != nil { - log.Logvf(log.Info, "error dropping temporary collection admin.%v: %v", arg.tempCollectionName, e) + log.Logvf(log.Info, "error dropping temporary collection admin.%v: %v", cleanupArg.tempCollectionName, e) } - }() + }(arg) userTargetDB = arg.intent.DB } diff --git a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/mongorestore_test.go b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/mongorestore_test.go index bba951e4e58..5e23e240e5c 100644 --- a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/mongorestore_test.go +++ b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/mongorestore_test.go @@ -250,3 +250,97 @@ func TestMongorestorePreserveUUID(t *testing.T) { }) } + +func TestRestoreUsersOrRoles(t *testing.T) { + testtype.VerifyTestType(t, testtype.IntegrationTestType) + + nsOptions := &NSOptions{} + provider, toolOpts, err := testutil.GetBareSessionProvider() + if err != nil { + log.Logvf(log.Always, "error connecting to host: %v", err) + os.Exit(util.ExitError) + } + + Convey("With a test MongoRestore", t, func() { + inputOptions := &InputOptions{} + outputOptions := &OutputOptions{ + NumParallelCollections: 1, + NumInsertionWorkers: 1, + TempUsersColl: "tempusers", + TempRolesColl: "temproles", + } + restore := MongoRestore{ + ToolOptions: toolOpts, + OutputOptions: outputOptions, + InputOptions: inputOptions, + NSOptions: nsOptions, + SessionProvider: provider, + } + session, _ := provider.GetSession() + defer session.Close() + + db := session.DB("admin") + + Convey("Restoring users and roles should drop tempusers and temproles", func() { + restore.TargetDirectory = "testdata/usersdump" + err := restore.Restore() + So(err, ShouldBeNil) + + adminCollections, err := db.CollectionNames() + So(err, ShouldBeNil) + + for _, collName := range adminCollections { + So(collName, ShouldNotEqual, "tempusers") + So(collName, ShouldNotEqual, "temproles") + } + }) + }) +} + +func TestKnownCollections(t *testing.T) { + testtype.VerifyTestType(t, testtype.IntegrationTestType) + + Convey("With a test MongoRestore", t, func() { + inputOptions := &InputOptions{} + outputOptions := &OutputOptions{ + NumParallelCollections: 1, + NumInsertionWorkers: 1, + } + nsOptions := &NSOptions{} + provider, toolOpts, err := testutil.GetBareSessionProvider() + if err != nil { + log.Logvf(log.Always, "error connecting to host: %v", err) + os.Exit(util.ExitError) + } + restore := MongoRestore{ + ToolOptions: toolOpts, + OutputOptions: outputOptions, + InputOptions: inputOptions, + NSOptions: nsOptions, + SessionProvider: provider, + } + session, _ := provider.GetSession() + defer session.Close() + + db := session.DB("test") + defer func() { + db.C("foo").DropCollection() + }() + + Convey("Once collection foo has been restored, it should exist in restore.knownCollections", func() { + restore.TargetDirectory = "testdata/foodump" + err := restore.Restore() + So(err, ShouldBeNil) + + var namespaceExistsInCache bool + if cols, ok := restore.knownCollections["test"]; ok { + for _, collName := range cols { + if collName == "foo" { + namespaceExistsInCache = true + } + } + } + So(namespaceExistsInCache, ShouldBeTrue) + }) + }) +} diff --git a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/restore.go b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/restore.go index a0175807d37..b02e7530674 100644 --- a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/restore.go +++ b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/restore.go @@ -197,6 +197,7 @@ func (restore *MongoRestore) RestoreIntent(intent *intents.Intent) error { if err != nil { return fmt.Errorf("error creating collection %v: %v", intent.Namespace(), err) } + restore.addToKnownCollections(intent) } else { log.Logvf(log.Info, "collection %v already exists - skipping collection create", intent.Namespace()) } |