// newState creates an incomplete *State, with a configured watcher but no // pwatcher, leadershipManager, or serverTag. You must start() the returned // *State before it will function correctly. func newState(environTag names.EnvironTag, session *mgo.Session, mongoInfo *mongo.MongoInfo, policy Policy) (_ *State, resultErr error) { admin := session.DB("admin") if mongoInfo.Tag != nil { if err := admin.Login(mongoInfo.Tag.String(), mongoInfo.Password); err != nil { return nil, maybeUnauthorized(err, fmt.Sprintf("cannot log in to admin database as %q", mongoInfo.Tag)) } } else if mongoInfo.Password != "" { if err := admin.Login(mongo.AdminUser, mongoInfo.Password); err != nil { return nil, maybeUnauthorized(err, "cannot log in to admin database") } } // Set up database. rawDB := session.DB(jujuDB) database, err := allCollections().Load(rawDB, environTag.Id()) if err != nil { return nil, errors.Trace(err) } if err := InitDbLogs(session); err != nil { return nil, errors.Trace(err) } // Create State. st := &State{ environTag: environTag, mongoInfo: mongoInfo, session: session, database: database, policy: policy, watcher: watcher.New(rawDB.C(txnLogC)), } st.LeasePersistor = NewLeasePersistor(leaseC, st.run, st.getCollection) return st, nil }
func newState(session *mgo.Session, mongoInfo *mongo.MongoInfo, policy Policy) (_ *State, resultErr error) { admin := session.DB("admin") if mongoInfo.Tag != nil { if err := admin.Login(mongoInfo.Tag.String(), mongoInfo.Password); err != nil { return nil, maybeUnauthorized(err, fmt.Sprintf("cannot log in to admin database as %q", mongoInfo.Tag)) } } else if mongoInfo.Password != "" { if err := admin.Login(mongo.AdminUser, mongoInfo.Password); err != nil { return nil, maybeUnauthorized(err, "cannot log in to admin database") } } db := session.DB("juju") pdb := session.DB("presence") st := &State{ mongoInfo: mongoInfo, policy: policy, db: db, } log := db.C(txnLogC) logInfo := mgo.CollectionInfo{Capped: true, MaxBytes: logSize} // The lack of error code for this error was reported upstream: // https://jira.klmongodb.org/browse/SERVER-6992 err := log.Create(&logInfo) if err != nil && err.Error() != "collection already exists" { return nil, maybeUnauthorized(err, "cannot create log collection") } txns := db.C(txnsC) err = txns.Create(&mgo.CollectionInfo{}) if err != nil && err.Error() != "collection already exists" { return nil, maybeUnauthorized(err, "cannot create transaction collection") } st.watcher = watcher.New(log) defer func() { if resultErr != nil { if err := st.watcher.Stop(); err != nil { logger.Errorf("failed to stop watcher: %v", err) } } }() st.pwatcher = presence.NewWatcher(pdb.C(presenceC)) defer func() { if resultErr != nil { if err := st.pwatcher.Stop(); err != nil { logger.Errorf("failed to stop presence watcher: %v", err) } } }() for _, item := range indexes { index := mgo.Index{Key: item.key, Unique: item.unique} if err := db.C(item.collection).EnsureIndex(index); err != nil { return nil, errors.Annotate(err, "cannot create database index") } } return st, nil }
func newState(session *mgo.Session, mongoInfo *mongo.MongoInfo, policy Policy) (_ *State, resultErr error) { admin := session.DB("admin") if mongoInfo.Tag != nil { if err := admin.Login(mongoInfo.Tag.String(), mongoInfo.Password); err != nil { return nil, maybeUnauthorized(err, fmt.Sprintf("cannot log in to admin database as %q", mongoInfo.Tag)) } } else if mongoInfo.Password != "" { if err := admin.Login(mongo.AdminUser, mongoInfo.Password); err != nil { return nil, maybeUnauthorized(err, "cannot log in to admin database") } } db := session.DB("juju") // Create collections used to track client-side transactions (mgo/txn). txnLog := db.C(txnLogC) txnLogInfo := mgo.CollectionInfo{Capped: true, MaxBytes: txnLogSize} err := txnLog.Create(&txnLogInfo) if isCollectionExistsError(err) { return nil, maybeUnauthorized(err, "cannot create transaction log collection") } txns := db.C(txnsC) err = txns.Create(new(mgo.CollectionInfo)) if isCollectionExistsError(err) { return nil, maybeUnauthorized(err, "cannot create transaction collection") } // Create and set up State. st := &State{ mongoInfo: mongoInfo, policy: policy, db: db, watcher: watcher.New(txnLog), } defer func() { if resultErr != nil { if err := st.watcher.Stop(); err != nil { logger.Errorf("failed to stop watcher: %v", err) } } }() st.LeasePersistor = NewLeasePersistor(leaseC, st.run, st.getCollection) // Create DB indexes. for _, item := range indexes { index := mgo.Index{Key: item.key, Unique: item.unique, Sparse: item.sparse} if err := db.C(item.collection).EnsureIndex(index); err != nil { return nil, errors.Annotate(err, "cannot create database index") } } if err := InitDbLogs(session); err != nil { return nil, errors.Trace(err) } return st, nil }
func (s *FastPeriodSuite) TestIgnoreAncientHistory(c *gc.C) { s.insert(c, "test", "a") w := watcher.New(s.log) defer w.Stop() w.StartSync() w.Watch("test", "a", -1, s.ch) assertNoChange(c, s.ch) }
func (s *watcherSuite) SetUpTest(c *gc.C) { s.BaseSuite.SetUpTest(c) s.MgoSuite.SetUpTest(c) db := s.MgoSuite.Session.DB("juju") s.log = db.C("txnlog") s.log.Create(&mgo.CollectionInfo{ Capped: true, MaxBytes: 1000000, }) s.stash = db.C("txn.stash") s.runner = txn.NewRunner(db.C("txn")) s.runner.ChangeLog(s.log) s.w = watcher.New(s.log) s.ch = make(chan watcher.Change) }
// newState creates an incomplete *State, with a configured watcher but no // pwatcher, leadershipManager, or controllerTag. You must start() the returned // *State before it will function correctly. func newState(environTag names.EnvironTag, session *mgo.Session, mongoInfo *mongo.MongoInfo, policy Policy) (_ *State, resultErr error) { // Set up database. rawDB := session.DB(jujuDB) database, err := allCollections().Load(rawDB, environTag.Id()) if err != nil { return nil, errors.Trace(err) } if err := InitDbLogs(session); err != nil { return nil, errors.Trace(err) } // Create State. return &State{ environTag: environTag, mongoInfo: mongoInfo, session: session, database: database, policy: policy, watcher: watcher.New(rawDB.C(txnLogC)), }, nil }
func newState(session *mgo.Session, info *Info, policy Policy) (*State, error) { db := session.DB("juju") pdb := session.DB("presence") admin := session.DB("admin") if info.Tag != "" { if err := db.Login(info.Tag, info.Password); err != nil { return nil, maybeUnauthorized(err, fmt.Sprintf("cannot log in to juju database as %q", info.Tag)) } if err := pdb.Login(info.Tag, info.Password); err != nil { return nil, maybeUnauthorized(err, fmt.Sprintf("cannot log in to presence database as %q", info.Tag)) } if err := admin.Login(info.Tag, info.Password); err != nil { return nil, maybeUnauthorized(err, fmt.Sprintf("cannot log in to admin database as %q", info.Tag)) } } else if info.Password != "" { if err := admin.Login(AdminUser, info.Password); err != nil { return nil, maybeUnauthorized(err, "cannot log in to admin database") } } st := &State{ info: info, policy: policy, db: db, environments: db.C("environments"), charms: db.C("charms"), machines: db.C("machines"), containerRefs: db.C("containerRefs"), instanceData: db.C("instanceData"), relations: db.C("relations"), relationScopes: db.C("relationscopes"), services: db.C("services"), requestedNetworks: db.C("requestednetworks"), networks: db.C("networks"), networkInterfaces: db.C("networkinterfaces"), minUnits: db.C("minunits"), settings: db.C("settings"), settingsrefs: db.C("settingsrefs"), constraints: db.C("constraints"), units: db.C("units"), actions: db.C("actions"), actionresults: db.C("actionresults"), users: db.C("users"), presence: pdb.C("presence"), cleanups: db.C("cleanups"), annotations: db.C("annotations"), statuses: db.C("statuses"), stateServers: db.C("stateServers"), } log := db.C("txns.log") logInfo := mgo.CollectionInfo{Capped: true, MaxBytes: logSize} // The lack of error code for this error was reported upstream: // https://jira.klmongodb.org/browse/SERVER-6992 err := log.Create(&logInfo) if err != nil && err.Error() != "collection already exists" { return nil, maybeUnauthorized(err, "cannot create log collection") } st.runner = txn.NewRunner(db.C("txns")) st.runner.ChangeLog(db.C("txns.log")) st.watcher = watcher.New(db.C("txns.log")) st.pwatcher = presence.NewWatcher(pdb.C("presence")) for _, item := range indexes { index := mgo.Index{Key: item.key, Unique: item.unique} if err := db.C(item.collection).EnsureIndex(index); err != nil { return nil, fmt.Errorf("cannot create database index: %v", err) } } st.transactionHooks = make(chan ([]transactionHook), 1) st.transactionHooks <- nil // TODO(rog) delete this when we can assume there are no // pre-1.18 environments running. if err := st.createStateServersDoc(); err != nil { return nil, fmt.Errorf("cannot create state servers document: %v", err) } if err := st.createAPIAddressesDoc(); err != nil { return nil, fmt.Errorf("cannot create API addresses document: %v", err) } if err := st.createStateServingInfoDoc(); err != nil { return nil, fmt.Errorf("cannot create state serving info document: %v", err) } return st, nil }
func (wf workersFactory) NewTxnLogWorker() (workers.TxnLogWorker, error) { coll := wf.st.getTxnLogCollection() worker := watcher.New(coll) return worker, nil }
func newState(session *mgo.Session, mongoInfo *authentication.MongoInfo, policy Policy) (*State, error) { db := session.DB("juju") pdb := session.DB("presence") admin := session.DB("admin") authenticated := false if mongoInfo.Tag != nil { if err := db.Login(mongoInfo.Tag.String(), mongoInfo.Password); err != nil { return nil, maybeUnauthorized(err, fmt.Sprintf("cannot log in to juju database as %q", mongoInfo.Tag)) } if err := pdb.Login(mongoInfo.Tag.String(), mongoInfo.Password); err != nil { return nil, maybeUnauthorized(err, fmt.Sprintf("cannot log in to presence database as %q", mongoInfo.Tag)) } if err := admin.Login(mongoInfo.Tag.String(), mongoInfo.Password); err != nil { return nil, maybeUnauthorized(err, fmt.Sprintf("cannot log in to admin database as %q", mongoInfo.Tag)) } authenticated = true } else if mongoInfo.Password != "" { if err := admin.Login(AdminUser, mongoInfo.Password); err != nil { return nil, maybeUnauthorized(err, "cannot log in to admin database") } authenticated = true } st := &State{ mongoInfo: mongoInfo, policy: policy, authenticated: authenticated, db: db, } log := db.C(txnLogC) logInfo := mgo.CollectionInfo{Capped: true, MaxBytes: logSize} // The lack of error code for this error was reported upstream: // https://jira.klmongodb.org/browse/SERVER-6992 err := log.Create(&logInfo) if err != nil && err.Error() != "collection already exists" { return nil, maybeUnauthorized(err, "cannot create log collection") } txns := db.C(txnsC) err = txns.Create(&mgo.CollectionInfo{}) if err != nil && err.Error() != "collection already exists" { return nil, maybeUnauthorized(err, "cannot create transaction collection") } st.watcher = watcher.New(log) st.pwatcher = presence.NewWatcher(pdb.C(presenceC)) for _, item := range indexes { index := mgo.Index{Key: item.key, Unique: item.unique} if err := db.C(item.collection).EnsureIndex(index); err != nil { return nil, fmt.Errorf("cannot create database index: %v", err) } } // TODO(rog) delete this when we can assume there are no // pre-1.18 environments running. if err := st.createStateServersDoc(); err != nil { return nil, fmt.Errorf("cannot create state servers document: %v", err) } if err := st.createAPIAddressesDoc(); err != nil { return nil, fmt.Errorf("cannot create API addresses document: %v", err) } if err := st.createStateServingInfoDoc(); err != nil { return nil, fmt.Errorf("cannot create state serving info document: %v", err) } return st, nil }