func init() { stateWorkerDialOpts = mongo.DefaultDialOpts() stateWorkerDialOpts.PostDial = func(session *mgo.Session) error { safe := mgo.Safe{} if ProductionMongoWriteConcern { safe.J = true _, err := replicaset.CurrentConfig(session) if err == nil { // set mongo to write-majority (writes only returned after // replicated to a majority of replica-set members). safe.WMode = "majority" } } session.SetSafe(&safe) return nil } }
func (r *RestoreSuite) TestReplicasetIsReset(c *gc.C) { server := &gitjujutesting.MgoInstance{Params: []string{"--replSet", "juju"}} err := server.Start(coretesting.Certs) c.Assert(err, jc.ErrorIsNil) defer server.DestroyWithLog() mgoAddr := server.Addr() dialInfo := server.DialInfo() var cfg *replicaset.Config dialInfo = server.DialInfo() dialInfo.Addrs = []string{mgoAddr} err = resetReplicaSet(dialInfo, mgoAddr) session := server.MustDial() defer session.Close() cfg, err = replicaset.CurrentConfig(session) c.Assert(err, jc.ErrorIsNil) c.Assert(cfg.Members, gc.HasLen, 1) c.Assert(cfg.Members[0].Address, gc.Equals, mgoAddr) }
// attemptInitiateMongoServer attempts to initiate the replica set. func attemptInitiateMongoServer(dialInfo *mgo.DialInfo, memberHostPort string, force bool) error { session, err := mgo.DialWithInfo(dialInfo) if err != nil { return errors.Annotatef(err, "cannot dial mongo to initiate replicaset") } defer session.Close() session.SetSocketTimeout(mongo.SocketTimeout) if !force { bInfo, err := session.BuildInfo() if err != nil && isNotUnreachableError(err) { return errors.Annotate(err, "cannot determine mongo build information") } var cfg *replicaset.Config if err != nil || !bInfo.VersionAtLeast(3) { cfg, err = replicaset.CurrentConfig(session) if err != nil && err != mgo.ErrNotFound { return errors.Errorf("cannot get replica set configuration: %v", err) } } if cfg != nil && len(cfg.Members) > 0 { logger.Infof("replica set configuration found: %#v", cfg) return ErrReplicaSetAlreadyInitiated } } return replicaset.Initiate( session, memberHostPort, mongo.ReplicaSetName, map[string]string{ jujuMachineKey: agent.BootstrapMachineId, }, ) }