Skip to content

Commit

Permalink
Merge pull request #86 from Scalingo/fix/lease-time-duration-per-ip
Browse files Browse the repository at this point in the history
Fix lease time duration, it is now set according to the keepalive duration of the IP if set
  • Loading branch information
Soulou authored Mar 6, 2020
2 parents 63f01d5 + f33b3e2 commit c9e63b1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
10 changes: 8 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ type Config struct {
FailCountBeforeFailover int `envconfig:"FAIL_COUNT_BEFORE_FAILOVER" default:"3"`
}

func (c Config) LeaseTime() time.Duration {
return 5 * c.KeepAliveInterval
// LeaseTime is either 5* the global keepalive interval, or 5 times the one
// which is given as argument which can be speicific to the current IP.
func (c Config) LeaseTime(ipKeepalive int) time.Duration {
duration := time.Duration(ipKeepalive) * time.Second
if duration == 0 {
duration = c.KeepAliveInterval
}
return 5 * duration
}

// RandomDurationAround returns a duration (1.0 - percent) * duration < n < (1.0 + percent) * duration
Expand Down
4 changes: 2 additions & 2 deletions ip/event_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (m *manager) singleEventRun(ctx context.Context) bool {
// Sleeping twice the lease time will ensure that we've lost our lease and another node was elected MASTER.
// So after this sleep, we can safely remove our IP.

log.Infof("Stop order received, waiting %s to remove IP", (2 * m.config.LeaseTime()).String())
log.Infof("Stop order received, waiting %s to remove IP", (2 * m.config.LeaseTime(m.ip.KeepaliveInterval)).String())
m.waitTwiceLeaseTimeOrReallocation(ctx)
if m.stopOrder(ctx) {
log.Infof("Removing IP %s", m.ip.IP)
Expand All @@ -110,7 +110,7 @@ func (m *manager) singleEventRun(ctx context.Context) bool {

func (m *manager) waitTwiceLeaseTimeOrReallocation(ctx context.Context) {
log := logger.Get(ctx)
timer := time.NewTimer(2 * m.config.LeaseTime())
timer := time.NewTimer(2 * m.config.LeaseTime(m.ip.KeepaliveInterval))
defer timer.Stop()

ticker := time.NewTicker(500 * time.Millisecond)
Expand Down
10 changes: 7 additions & 3 deletions locker/etcd_locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (l *etcdLocker) Refresh(ctx context.Context) error {
log := logger.Get(ctx)

if l.leaseID == 0 {
grant, err := l.leaseEtcd.Grant(ctx, int64(l.config.LeaseTime().Seconds()))
grant, err := l.leaseEtcd.Grant(ctx, int64(l.config.LeaseTime(l.ip.KeepaliveInterval).Seconds()))
if err != nil {
return errors.Wrap(err, "fail to generate grant")
}
Expand All @@ -56,7 +56,11 @@ func (l *etcdLocker) Refresh(ctx context.Context) error {
// The goal of this transaction is to create the key with our leaseID only if this key does not exist
// We use a transaction to make sure that concurrent tries wont interfere with each others.

transactionCtx, cancel := context.WithTimeout(ctx, l.config.KeepAliveInterval)
transactionTimeout := time.Duration(l.ip.KeepaliveInterval) * time.Second
if transactionTimeout != 0 {
transactionTimeout = l.config.KeepAliveInterval
}
transactionCtx, cancel := context.WithTimeout(ctx, transactionTimeout)
defer cancel()

_, err := l.kvEtcd.Txn(transactionCtx).
Expand Down Expand Up @@ -96,7 +100,7 @@ func (l *etcdLocker) Refresh(ctx context.Context) error {
}

func (l *etcdLocker) leaseExpired() bool {
return l.lastLeaseRefresh.IsZero() || time.Now().After(l.lastLeaseRefresh.Add(l.config.LeaseTime()))
return l.lastLeaseRefresh.IsZero() || time.Now().After(l.lastLeaseRefresh.Add(l.config.LeaseTime(l.ip.KeepaliveInterval)))
}

func (l *etcdLocker) Unlock(ctx context.Context) error {
Expand Down

0 comments on commit c9e63b1

Please sign in to comment.