Skip to content

Commit

Permalink
make sure redundant leases when return
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Dec 17, 2019
1 parent 00e175e commit 994cb02
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19.11.09
19.11.10
26 changes: 15 additions & 11 deletions store/etcdv3/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,17 @@ func (m *Mercury) SetContainerStatus(ctx context.Context, container *types.Conta
}
val := string(data)
statusKey := filepath.Join(containerStatusPrefix, appname, entrypoint, container.Nodename, container.ID)
lease := &clientv3.LeaseGrantResponse{}
lease, err = m.cliv3.Grant(ctx, ttl)
lease, err := m.cliv3.Grant(ctx, ttl)
if err != nil {
return err
}
defer func() {
if lease.ID != 0 {
if _, err := m.cliv3.Revoke(ctx, lease.ID); err != nil {
log.Errorf("[SetContainerStatus] revoke lease failed %v", err) // ignore revoke lease error
}
}
}()
updateStatus := []clientv3.Op{clientv3.OpPut(statusKey, val, clientv3.WithLease(lease.ID))}
tr, err := m.cliv3.Txn(ctx).
If(clientv3.Compare(clientv3.Version(fmt.Sprintf(containerInfoKey, container.ID)), "!=", 0)).
Expand All @@ -103,18 +109,16 @@ func (m *Mercury) SetContainerStatus(ctx context.Context, container *types.Conta
}
tr2 := tr.Responses[0].GetResponseTxn()
if !tr2.Succeeded { // 没 status key 直接 put
lease.ID = 0
return nil
}
tr3 := tr2.Responses[0].GetResponseTxn()
leaseID := clientv3.LeaseID(tr3.Responses[0].GetResponseRange().Kvs[0].Lease)
revokeID := leaseID // 默认是 status 改变了 revoke 老的 lease 然后直接 put
if tr3.Succeeded { // status 没改变 revoke 新的 lease 然后刷新老的 lease
revokeID = lease.ID
_, err = m.cliv3.KeepAliveOnce(ctx, clientv3.LeaseID(leaseID))
}
if _, err := m.cliv3.Revoke(ctx, revokeID); err != nil {
log.Errorf("[SetContainerStatus] revoke lease failed %v", err) // ignore revoke lease error
}
oldLeaseID := clientv3.LeaseID(tr3.Responses[0].GetResponseRange().Kvs[0].Lease) // 拿到 status 绑定的 leaseID
if !tr3.Succeeded { // status 改变了 revoke 老的 lease
lease.ID = oldLeaseID
return nil
} // 没改变就还是 revoke 新的 ID
_, err = m.cliv3.KeepAliveOnce(ctx, clientv3.LeaseID(oldLeaseID)) // 刷新 lease
return err
}

Expand Down
14 changes: 9 additions & 5 deletions store/etcdv3/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ func TestSetContainerStatus(t *testing.T) {
nodename := "n1"
podname := "test"
container := &types.Container{
ID: ID,
Nodename: nodename,
Podname: podname,
ID: ID,
Nodename: nodename,
Podname: podname,
StatusMeta: &types.StatusMeta{},
}
// fail by no name
err := m.SetContainerStatus(ctx, container, 0)
Expand All @@ -153,8 +154,11 @@ func TestSetContainerStatus(t *testing.T) {
// no status key, put succ, err nil
err = m.SetContainerStatus(ctx, container, 10)
assert.NoError(t, err)
// status changed, update
// update lease
// status not changed, update old lease
err = m.SetContainerStatus(ctx, container, 10)
assert.NoError(t, err)
// status changed, revoke old lease
container.StatusMeta.Running = true
err = m.SetContainerStatus(ctx, container, 10)
assert.NoError(t, err)
}
Expand Down

0 comments on commit 994cb02

Please sign in to comment.