summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Erf <erf@mongodb.com>2014-11-05 13:16:26 -0500
committerKyle Erf <erf@mongodb.com>2014-11-05 13:16:29 -0500
commitd43d1d4d66f023fce2292f75a44ebbbea0afafae (patch)
treefcaabc30d081df425c3ffd896b7aad1c2b371b9e
parent00cfb2d2762e4657b91c856eecf8b20471b61e8d (diff)
downloadmongo-d43d1d4d66f023fce2292f75a44ebbbea0afafae.tar.gz
TOOLS-333 mongodump defaults to w=majority
Former-commit-id: da1728787d55f7f0ce9fb702800bbcba11ecd21f
-rw-r--r--mongorestore/mongorestore.go37
-rw-r--r--mongorestore/options/options.go10
2 files changed, 38 insertions, 9 deletions
diff --git a/mongorestore/mongorestore.go b/mongorestore/mongorestore.go
index f44332f378b..06ab1b5f28e 100644
--- a/mongorestore/mongorestore.go
+++ b/mongorestore/mongorestore.go
@@ -10,6 +10,7 @@ import (
"github.com/mongodb/mongo-tools/mongorestore/options"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
+ "strconv"
)
type MongoRestore struct {
@@ -62,13 +63,41 @@ func (restore *MongoRestore) ParseAndValidateOptions() error {
var err error
restore.oplogLimit, err = ParseTimestampFlag(restore.InputOptions.OplogLimit)
if err != nil {
- return fmt.Errorf("error parsing timestamp argument to --oplogLimit: %v", err) //TODO help text?
+ return fmt.Errorf("error parsing timestamp argument to --oplogLimit: %v", err)
}
}
- if restore.OutputOptions.WriteConcern > 0 {
- restore.safety = &mgo.Safe{W: restore.OutputOptions.WriteConcern} //TODO, audit extra steps
- log.Logf(log.DebugHigh, "\tdumping with w=%v", restore.safety.W)
+ if restore.OutputOptions.WriteConcern == "" || restore.OutputOptions.WriteConcern == "majority" {
+ log.Logf(log.DebugLow, "\tdumping with w=majority")
+
+ // check if we are using a replica set and fall back to w=1 if we aren't (for <= 2.4)
+ isRepl, err := restore.SessionProvider.IsReplicaSet()
+ if err != nil {
+ return fmt.Errorf("error determining if connected to replica set: %v", err)
+ }
+
+ if isRepl {
+ restore.safety = &mgo.Safe{WMode: "majority"}
+ } else {
+ log.Logf(log.DebugHigh, "\t\tnot connected to a replset, using equivalent w=1 for backward compatibility")
+ restore.safety = &mgo.Safe{W: 1}
+ }
+ } else {
+ intWriteConcern, err := strconv.Atoi(restore.OutputOptions.WriteConcern)
+ if err != nil {
+ return fmt.Errorf("error parsing --w value: %v", err)
+ }
+ if intWriteConcern < 0 {
+ return fmt.Errorf("cannot use a negative write concern")
+ }
+
+ log.Logf(log.DebugLow, "\tdumping with w=%v", intWriteConcern)
+ if intWriteConcern == 0 {
+ // we must set safety to nil for true fire-and-forget behavior
+ restore.safety = nil
+ } else {
+ restore.safety = &mgo.Safe{W: intWriteConcern}
+ }
}
if restore.tempUsersCol == "" {
diff --git a/mongorestore/options/options.go b/mongorestore/options/options.go
index 694a19bf292..091ea3ab6b3 100644
--- a/mongorestore/options/options.go
+++ b/mongorestore/options/options.go
@@ -14,11 +14,11 @@ func (self *InputOptions) Name() string {
}
type OutputOptions struct {
- Drop bool `long:"drop" description:"Drop each collection before import"`
- WriteConcern int `long:"w" description:"Minimum number of replicas per write"`
- NoIndexRestore bool `long:"noIndexRestore" description:"Don't restore indexes"`
- NoOptionsRestore bool `long:"noOptionsRestore" description:"Don't restore options"`
- KeepIndexVersion bool `long:"keepIndexVersion" description:"Don't update index version"`
+ Drop bool `long:"drop" description:"Drop each collection before import"`
+ WriteConcern string `long:"w" description:"Minimum number of replicas per write (default=majority)"`
+ NoIndexRestore bool `long:"noIndexRestore" description:"Don't restore indexes"`
+ NoOptionsRestore bool `long:"noOptionsRestore" description:"Don't restore options"`
+ KeepIndexVersion bool `long:"keepIndexVersion" description:"Don't update index version"`
JobThreads int `long:"numParallelCollections" short:"j" description:"Number of collections to restore in parallel" default:"4"`
BulkWriters int `long:"numInsertionWorkersPerCollection" description:"Number of insert connections per collection" default:"1"`