Skip to content

Commit

Permalink
mongo: add JournalEnabled function
Browse files Browse the repository at this point in the history
Utility function for detecting at runtime
whether the Mongo connected to has journaling
enabled.
  • Loading branch information
axw committed Jul 29, 2014
1 parent 2761ba5 commit 62c08a9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/juju/utils"
"github.com/juju/utils/apt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"

"github.com/juju/juju/network"
"github.com/juju/juju/replicaset"
Expand Down Expand Up @@ -89,6 +90,18 @@ func IsMaster(session *mgo.Session, obj WithAddresses) (bool, error) {
return machinePeerAddr == masterAddr, nil
}

// JournalEnabled reports whether mongo has journaling enabled.
func JournalEnabled(session *mgo.Session) (bool, error) {
var result bson.M
if err := session.Run("serverStatus", &result); err != nil {
return false, err
}
// The Journaling (dur) document is present if journaling is enabled.
// http://docs.mongodb.org/manual/reference/server-status/
_, ok := result["dur"]
return ok, nil
}

// SelectPeerAddress returns the address to use as the
// mongo replica set peer address by selecting it from the given addresses.
func SelectPeerAddress(addrs []network.Address) string {
Expand Down
21 changes: 21 additions & 0 deletions mongo/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,27 @@ func (s *MongoSuite) TestAddPPAInQuantal(c *gc.C) {
}})
}

func (s *MongoSuite) TestJournalEnabledDetected(c *gc.C) {
s.testJournalEnabled(c, true)
}

func (s *MongoSuite) TestJournalDisabledDetected(c *gc.C) {
s.testJournalEnabled(c, false)
}

func (s *MongoSuite) testJournalEnabled(c *gc.C, enabled bool) {
inst := &testing.MgoInstance{EnableJournal: enabled}
err := inst.Start(coretesting.Certs)
c.Assert(err, gc.IsNil)
defer inst.DestroyWithLog()
session, err := inst.Dial()
c.Assert(err, gc.IsNil)
defer session.Close()
isEnabled, err := mongo.JournalEnabled(session)
c.Assert(err, gc.IsNil)
c.Assert(isEnabled, gc.Equals, enabled)
}

// mockShellCommand creates a new command with the given
// name and contents, and patches $PATH so that it will be
// executed by preference. It returns the name of a file
Expand Down

0 comments on commit 62c08a9

Please sign in to comment.