Skip to content

Commit

Permalink
Enable replicasets unconditionally
Browse files Browse the repository at this point in the history
Once juju#82 lands, we can enable replsets
unconditionally. This makes it possible
to do limited testing of HA on the local
provider currently, and gets rid of a
code burden that we don't need.
  • Loading branch information
axw committed Jun 12, 2014
1 parent ed3cd07 commit b118b14
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 57 deletions.
2 changes: 1 addition & 1 deletion cmd/jujud/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (s *agentSuite) SetUpSuite(c *gc.C) {
// a bit when some tests are restarting every 50ms for 10 seconds,
// so use a slightly more friendly delay.
worker.RestartDelay = 250 * time.Millisecond
s.PatchValue(&ensureMongoServer, func(string, string, params.StateServingInfo, bool) error {
s.PatchValue(&ensureMongoServer, func(string, string, params.StateServingInfo) error {
return nil
})
}
Expand Down
6 changes: 0 additions & 6 deletions cmd/jujud/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,14 @@ func (c *BootstrapCommand) startMongo(addrs []network.Address, agentConfig agent
}

logger.Debugf("calling ensureMongoServer")
withHA := shouldEnableHA(agentConfig)
err = ensureMongoServer(
agentConfig.DataDir(),
agentConfig.Value(agent.Namespace),
servingInfo,
withHA,
)
if err != nil {
return err
}
// If we are not doing HA, there is no need to set up replica set.
if !withHA {
return nil
}

peerAddr := mongo.SelectPeerAddress(addrs)
if peerAddr == "" {
Expand Down
6 changes: 2 additions & 4 deletions cmd/jujud/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,14 @@ type fakeEnsure struct {
initiateCount int
dataDir string
namespace string
withHA bool
info params.StateServingInfo
initiateParams peergrouper.InitiateMongoParams
err error
}

func (f *fakeEnsure) fakeEnsureMongo(dataDir, namespace string, info params.StateServingInfo, withHA bool) error {
func (f *fakeEnsure) fakeEnsureMongo(dataDir, namespace string, info params.StateServingInfo) error {
f.ensureCount++
f.dataDir, f.namespace, f.info, f.withHA = dataDir, namespace, info, withHA
f.dataDir, f.namespace, f.info = dataDir, namespace, info
return f.err
}

Expand Down Expand Up @@ -160,7 +159,6 @@ func (s *BootstrapSuite) TestInitializeEnvironment(c *gc.C) {
c.Assert(s.fakeEnsureMongo.initiateCount, gc.Equals, 1)
c.Assert(s.fakeEnsureMongo.ensureCount, gc.Equals, 1)
c.Assert(s.fakeEnsureMongo.dataDir, gc.Equals, s.dataDir)
c.Assert(s.fakeEnsureMongo.withHA, jc.IsTrue)

expectInfo, exists := machConf.StateServingInfo()
c.Assert(exists, jc.IsTrue)
Expand Down
22 changes: 4 additions & 18 deletions cmd/jujud/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,9 @@ func (a *MachineAgent) StateWorker() (worker.Worker, error) {
a.startWorkerAfterUpgrade(runner, "instancepoller", func() (worker.Worker, error) {
return instancepoller.NewWorker(st), nil
})
if shouldEnableHA(agentConfig) {
a.startWorkerAfterUpgrade(runner, "peergrouper", func() (worker.Worker, error) {
return peergrouperNew(st)
})
}
a.startWorkerAfterUpgrade(runner, "peergrouper", func() (worker.Worker, error) {
return peergrouperNew(st)
})
runner.StartWorker("apiserver", func() (worker.Worker, error) {
// If the configuration does not have the required information,
// it is currently not a recoverable error, so we kill the whole
Expand Down Expand Up @@ -508,7 +506,6 @@ func (a *MachineAgent) ensureMongoServer(agentConfig agent.Config) error {
return fmt.Errorf("state worker was started with no state serving info")
}
namespace := agentConfig.Value(agent.Namespace)
withHA := shouldEnableHA(agentConfig)

// When upgrading from a pre-HA-capable environment,
// we must add machine-0 to the admin database and
Expand Down Expand Up @@ -545,15 +542,14 @@ func (a *MachineAgent) ensureMongoServer(agentConfig agent.Config) error {
}
st.Close()
addrs = m.Addresses()
shouldInitiateMongoServer = withHA
shouldInitiateMongoServer = true
}

// ensureMongoServer installs/upgrades the upstart config as necessary.
if err := ensureMongoServer(
agentConfig.DataDir(),
namespace,
servingInfo,
withHA,
); err != nil {
return err
}
Expand Down Expand Up @@ -613,16 +609,6 @@ func isPreHAVersion(v version.Number) bool {
return v.Compare(version.MustParse("1.19.0")) < 0
}

// shouldEnableHA reports whether HA should be enabled.
//
// Eventually this should always be true, and ideally
// it should be true before 1.20 is released or we'll
// have more upgrade scenarios on our hands.
func shouldEnableHA(agentConfig agent.Config) bool {
providerType := agentConfig.Value(agent.ProviderType)
return providerType != provider.Local
}

func openState(agentConfig agent.Config) (_ *state.State, _ *state.Machine, err error) {
info, ok := agentConfig.StateInfo()
if !ok {
Expand Down
19 changes: 5 additions & 14 deletions mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,6 @@ func RemoveService(namespace string) error {
return upstartServiceStopAndRemove(svc)
}

const (
// WithHA is used when we want to start a mongo service with HA support.
WithHA = true
// WithoutHA is used when we want to start a mongo service without HA support.
WithoutHA = false
)

// EnsureMongoServer ensures that the correct mongo upstart script is installed
// and running.
//
Expand All @@ -150,7 +143,7 @@ const (
// The namespace is a unique identifier to prevent multiple instances of mongo
// on this machine from colliding. This should be empty unless using
// the local provider.
func EnsureServer(dataDir string, namespace string, info params.StateServingInfo, withHA bool) error {
func EnsureServer(dataDir string, namespace string, info params.StateServingInfo) error {
logger.Infof("Ensuring mongo server is running; data directory %s; port %d", dataDir, info.StatePort)
dbDir := filepath.Join(dataDir, "db")

Expand Down Expand Up @@ -187,7 +180,7 @@ func EnsureServer(dataDir string, namespace string, info params.StateServingInfo
return fmt.Errorf("cannot install mongod: %v", err)
}

upstartConf, mongoPath, err := upstartService(namespace, dataDir, dbDir, info.StatePort, withHA)
upstartConf, mongoPath, err := upstartService(namespace, dataDir, dbDir, info.StatePort)
if err != nil {
return err
}
Expand Down Expand Up @@ -268,7 +261,7 @@ func sharedSecretPath(dataDir string) string {
// upstartService returns the upstart config for the mongo state service.
// It also returns the path to the mongod executable that the upstart config
// will be using.
func upstartService(namespace, dataDir, dbDir string, port int, withHA bool) (*upstart.Conf, string, error) {
func upstartService(namespace, dataDir, dbDir string, port int) (*upstart.Conf, string, error) {
svc := upstart.NewService(ServiceName(namespace))

mongoPath, err := Path()
Expand All @@ -287,10 +280,8 @@ func upstartService(namespace, dataDir, dbDir string, port int, withHA bool) (*u
" --syslog" +
" --smallfiles" +
" --journal" +
" --keyFile " + utils.ShQuote(sharedSecretPath(dataDir))
if withHA {
mongoCmd += " --replSet " + ReplicaSetName
}
" --keyFile " + utils.ShQuote(sharedSecretPath(dataDir)) +
" --replSet " + ReplicaSetName
conf := &upstart.Conf{
Service: *svc,
Desc: "juju state database",
Expand Down
24 changes: 10 additions & 14 deletions mongo/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (s *MongoSuite) TestEnsureServer(c *gc.C) {

mockShellCommand(c, &s.CleanupSuite, "apt-get")

err := mongo.EnsureServer(dataDir, namespace, testInfo, mongo.WithHA)
err := mongo.EnsureServer(dataDir, namespace, testInfo)
c.Assert(err, gc.IsNil)

testJournalDirs(dbDir, c)
Expand Down Expand Up @@ -158,7 +158,7 @@ func (s *MongoSuite) TestEnsureServer(c *gc.C) {

s.installed = nil
// now check we can call it multiple times without error
err = mongo.EnsureServer(dataDir, namespace, testInfo, mongo.WithHA)
err = mongo.EnsureServer(dataDir, namespace, testInfo)
c.Assert(err, gc.IsNil)
assertInstalled()

Expand Down Expand Up @@ -194,7 +194,7 @@ func (s *MongoSuite) TestInstallMongod(c *gc.C) {

s.PatchValue(&version.Current.Series, test.series)

err := mongo.EnsureServer(dataDir, namespace, testInfo, mongo.WithHA)
err := mongo.EnsureServer(dataDir, namespace, testInfo)
c.Assert(err, gc.IsNil)

cmds := getMockShellCalls(c, output)
Expand All @@ -212,22 +212,18 @@ func (s *MongoSuite) TestInstallMongod(c *gc.C) {
}
}

func (s *MongoSuite) TestUpstartServiceWithHA(c *gc.C) {
func (s *MongoSuite) TestUpstartServiceWithReplSet(c *gc.C) {
dataDir := c.MkDir()

svc, _, err := mongo.UpstartService("", dataDir, dataDir, 1234, mongo.WithHA)
svc, _, err := mongo.UpstartService("", dataDir, dataDir, 1234)
c.Assert(err, gc.IsNil)
c.Assert(strings.Contains(svc.Cmd, "--replSet"), jc.IsTrue)

svc, _, err = mongo.UpstartService("", dataDir, dataDir, 1234, mongo.WithoutHA)
c.Assert(err, gc.IsNil)
c.Assert(strings.Contains(svc.Cmd, "--replSet"), jc.IsFalse)
}

func (s *MongoSuite) TestUpstartServiceWithJournal(c *gc.C) {
dataDir := c.MkDir()

svc, _, err := mongo.UpstartService("", dataDir, dataDir, 1234, mongo.WithHA)
svc, _, err := mongo.UpstartService("", dataDir, dataDir, 1234)
c.Assert(err, gc.IsNil)
journalPresent := strings.Contains(svc.Cmd, " --journal ") || strings.HasSuffix(svc.Cmd, " --journal")
c.Assert(journalPresent, jc.IsTrue)
Expand Down Expand Up @@ -265,11 +261,11 @@ func (s *MongoSuite) TestQuantalAptAddRepo(c *gc.C) {
// test that we call add-apt-repository only for quantal (and that if it
// fails, we return the error)
s.PatchValue(&version.Current.Series, "quantal")
err := mongo.EnsureServer(dir, "", testInfo, mongo.WithHA)
err := mongo.EnsureServer(dir, "", testInfo)
c.Assert(err, gc.ErrorMatches, "cannot install mongod: cannot add apt repository: exit status 1.*")

s.PatchValue(&version.Current.Series, "trusty")
err = mongo.EnsureServer(dir, "", testInfo, mongo.WithHA)
err = mongo.EnsureServer(dir, "", testInfo)
c.Assert(err, gc.IsNil)
}

Expand All @@ -278,7 +274,7 @@ func (s *MongoSuite) TestNoMongoDir(c *gc.C) {
// created.
mockShellCommand(c, &s.CleanupSuite, "apt-get")
dataDir := filepath.Join(c.MkDir(), "dir", "data")
err := mongo.EnsureServer(dataDir, "", testInfo, mongo.WithHA)
err := mongo.EnsureServer(dataDir, "", testInfo)
c.Check(err, gc.IsNil)

_, err = os.Stat(filepath.Join(dataDir, "db"))
Expand Down Expand Up @@ -344,7 +340,7 @@ func (s *MongoSuite) TestAddPPAInQuantal(c *gc.C) {
s.PatchValue(&version.Current.Series, "quantal")

dataDir := c.MkDir()
err := mongo.EnsureServer(dataDir, "", testInfo, mongo.WithHA)
err := mongo.EnsureServer(dataDir, "", testInfo)
c.Assert(err, gc.IsNil)

c.Assert(getMockShellCalls(c, addAptRepoOut), gc.DeepEquals, [][]string{{
Expand Down

0 comments on commit b118b14

Please sign in to comment.