github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/state/lease/config.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package lease 5 6 import ( 7 "time" 8 9 "github.com/juju/errors" 10 jujutxn "github.com/juju/txn" 11 12 "github.com/juju/juju/core/lease" 13 "github.com/juju/juju/mongo" 14 ) 15 16 // Mongo exposes MongoDB operations for use by the lease package. 17 type Mongo interface { 18 19 // RunTransaction should probably delegate to a jujutxn.Runner's Run method. 20 RunTransaction(jujutxn.TransactionSource) error 21 22 // GetCollection should probably call the mongo.CollectionFromName func. 23 GetCollection(name string) (collection mongo.Collection, closer func()) 24 } 25 26 // LocalClock provides the writer-local wall clock interface required by 27 // the lease package. 28 type LocalClock interface { 29 30 // Now returns the current, writer-local wall-clock time. 31 // 32 // Now is required to return times with a monotonic component, 33 // as returned by Go 1.9 and onwards, such that local times 34 // can be safely compared in the face of wall clock jumps. 35 Now() time.Time 36 } 37 38 // GlobalClock provides the global clock interface required by the lease 39 // package. 40 type GlobalClock interface { 41 42 // Now returns the current global clock time. 43 // 44 // Now is required to return monotonically increasing times. 45 Now() (time.Time, error) 46 } 47 48 // StoreConfig contains the resources and information required to create 49 // a Store. Multiple stores can collaborate if they share a collection and 50 // namespace, so long as they do not share ids; but within a collection, 51 // stores for different namespaces will not interfere with one another, 52 // regardless of id. 53 type StoreConfig struct { 54 55 // Id uniquely identifies the store. Multiple stores with the same id 56 // running concurrently will cause undefined behaviour. 57 Id string 58 59 // ModelUUID identifies the model the leases will be stored in. 60 ModelUUID string 61 62 // Namespace identifies a group of stores which operate on the same data. 63 Namespace string 64 65 // Collection names the MongoDB collection in which lease data is stored. 66 Collection string 67 68 // Mongo exposes the mgo[/txn] capabilities required by a Store. 69 Mongo Mongo 70 71 // LocalClock exposes the writer-local wall-clock time to a Store. 72 LocalClock LocalClock 73 74 // GlobalClock exposes the global clock to a Store. 75 GlobalClock GlobalClock 76 } 77 78 // validate returns an error if the supplied config is not valid. 79 func (config StoreConfig) validate() error { 80 if err := lease.ValidateString(config.Id); err != nil { 81 return errors.Annotatef(err, "invalid id") 82 } 83 if err := lease.ValidateString(config.Namespace); err != nil { 84 return errors.Annotatef(err, "invalid namespace") 85 } 86 if err := lease.ValidateString(config.Collection); err != nil { 87 return errors.Annotatef(err, "invalid collection") 88 } 89 if config.Mongo == nil { 90 return errors.New("missing mongo") 91 } 92 if config.LocalClock == nil { 93 return errors.New("missing local clock") 94 } 95 if config.GlobalClock == nil { 96 return errors.New("missing global clock") 97 } 98 return nil 99 }