github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/state/leadership/interface.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package leadership 5 6 import ( 7 "time" 8 9 "github.com/juju/errors" 10 "gopkg.in/mgo.v2/txn" 11 12 "github.com/juju/juju/worker" 13 ) 14 15 // Token exposes mgo/txn operations that can be added to a transaction in order 16 // to abort it if the check that generated the Token would no longer pass. 17 type Token interface { 18 19 // AssertOps returns mgo/txn operations that will abort if some fact no 20 // longer holds true. 21 AssertOps() []txn.Op 22 } 23 24 // Manager allows units to claim service leadership; to verify a unit's continued 25 // leadership of a service; and to wait until a service has no leader. 26 type Manager interface { 27 28 // ClaimLeadership claims leadership of the named service on behalf of the 29 // named unit. If no error is returned, leadership will be guaranteed for 30 // at least the supplied duration from the point when the call was made. 31 ClaimLeadership(serviceName, unitName string, duration time.Duration) error 32 33 // CheckLeadership verifies that the named unit is leader of the named 34 // service, and returns a Token attesting to that fact for use building 35 // mgo/txn transactions that depend upon it. 36 CheckLeadership(serviceName, unitName string) (Token, error) 37 38 // BlockUntilLeadershipReleased blocks until the named service is known 39 // to have no leader, in which case it returns no error; or until the 40 // manager is stopped, in which case it will fail. 41 BlockUntilLeadershipReleased(serviceName string) error 42 } 43 44 // ManagerWorker implements Manager and worker.Worker. 45 type ManagerWorker interface { 46 worker.Worker 47 Manager 48 } 49 50 // errStopped is returned to clients when an operation cannot complete because 51 // the manager has started (and possibly finished) shutdown. 52 var errStopped = errors.New("leadership manager stopped")