Skip to content

Commit

Permalink
Fix a bug that broke keepalives and check TTL (#3377)
Browse files Browse the repository at this point in the history
As a result of refactoring, keepalive leases were being stored in
the wrong key prefix.

Signed-off-by: Eric Chlebek <[email protected]>
  • Loading branch information
echlebek authored and Nikki Attea committed Nov 4, 2019
1 parent 424e51e commit b8fd84e
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions backend/liveness/liveness.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func EtcdFactory(ctx context.Context, client *clientv3.Client) Factory {
type SwitchSet struct {
client *clientv3.Client
prefix string
leaseKey string
leasePrefix string
notifyDead EventFunc
notifyAlive EventFunc
logger logrus.FieldLogger
Expand Down Expand Up @@ -132,7 +132,7 @@ func NewSwitchSet(client *clientv3.Client, name string, dead, alive EventFunc, l
return &SwitchSet{
client: client,
prefix: path.Join(SwitchPrefix, name),
leaseKey: path.Join(SwitchPrefix, "lease", name),
leasePrefix: path.Join(SwitchPrefix, "lease", name),
notifyDead: dead,
notifyAlive: alive,
logger: logger,
Expand Down Expand Up @@ -220,7 +220,8 @@ func (t *SwitchSet) ping(ctx context.Context, id string, ttl int64, alive bool)
}

func (t *SwitchSet) getLeaseID(ctx context.Context, ttl int64, switchID string) (clientv3.LeaseID, []clientv3.Op, error) {
resp, err := t.client.Get(ctx, t.leaseKey)
leaseKey := path.Join(t.leasePrefix, switchID)
resp, err := t.client.Get(ctx, leaseKey)
if err != nil {
return 0, nil, err
}
Expand All @@ -239,7 +240,7 @@ func (t *SwitchSet) getLeaseID(ctx context.Context, ttl int64, switchID string)
return 0, nil, err
}
leaseID = lease.ID
ops = append(ops, clientv3.OpPut(t.leaseKey, fmt.Sprintf("%x", lease.ID)))
ops = append(ops, clientv3.OpPut(leaseKey, fmt.Sprintf("%x", lease.ID)))
}
return leaseID, ops, nil
}
Expand Down Expand Up @@ -355,7 +356,7 @@ func (t *SwitchSet) handleEvent(ctx context.Context, event *clientv3.Event) {
}

t.logger.Debugf("creating a lease for %s with TTL %d", key, leaseTTL)
leaseID, leaseOps, err := t.getLeaseID(ctx, leaseTTL, path.Base(key))
leaseID, leaseOps, err := t.getLeaseID(ctx, leaseTTL, strings.TrimPrefix(key, t.prefix))
if err != nil {
t.logger.WithError(err).Errorf("error while granting lease for %s", key)
return
Expand Down

0 comments on commit b8fd84e

Please sign in to comment.