~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/worker/leadership/tracker.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 
9
9
        "github.com/juju/errors"
10
10
        "github.com/juju/loggo"
 
11
        "github.com/juju/utils/clock"
11
12
        "gopkg.in/juju/names.v2"
12
13
        "gopkg.in/tomb.v1"
13
14
 
21
22
        claimer         leadership.Claimer
22
23
        unitName        string
23
24
        applicationName string
 
25
        clock           clock.Clock
24
26
        duration        time.Duration
25
27
        isMinion        bool
26
28
 
41
43
// leadership for the duration supplied here without generating additional
42
44
// calls to the supplied manager (which may very well be on the other side of
43
45
// a network connection).
44
 
func NewTracker(tag names.UnitTag, claimer leadership.Claimer, duration time.Duration) *Tracker {
 
46
func NewTracker(tag names.UnitTag, claimer leadership.Claimer, clock clock.Clock, duration time.Duration) *Tracker {
45
47
        unitName := tag.Id()
46
48
        serviceName, _ := names.UnitApplication(unitName)
47
49
        t := &Tracker{
48
50
                unitName:          unitName,
49
51
                applicationName:   serviceName,
50
52
                claimer:           claimer,
 
53
                clock:             clock,
51
54
                duration:          duration,
52
55
                claimTickets:      make(chan chan bool),
53
56
                waitLeaderTickets: make(chan chan bool),
163
166
func (t *Tracker) refresh() error {
164
167
        logger.Debugf("checking %s for %s leadership", t.unitName, t.applicationName)
165
168
        leaseDuration := 2 * t.duration
166
 
        // TODO(fwereade): 2016-03-17 lp:1558657
167
 
        untilTime := time.Now().Add(leaseDuration)
 
169
        untilTime := t.clock.Now().Add(leaseDuration)
168
170
        err := t.claimer.ClaimLeadership(t.applicationName, t.unitName, leaseDuration)
169
171
        switch {
170
172
        case err == nil:
182
184
        logger.Infof("%s will renew %s leadership at %s", t.unitName, t.applicationName, renewTime)
183
185
        t.isMinion = false
184
186
        t.claimLease = nil
185
 
        // TODO(fwereade): 2016-03-17 lp:1558657
186
 
        t.renewLease = time.After(renewTime.Sub(time.Now()))
 
187
        t.renewLease = t.clock.After(renewTime.Sub(t.clock.Now()))
187
188
 
188
189
        for len(t.waitingLeader) > 0 {
189
190
                logger.Debugf("notifying %s ticket of impending %s leadership", t.unitName, t.applicationName)