Skip to content

Commit

Permalink
support store status forever
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Nov 9, 2020
1 parent 16bf528 commit 35bd9fc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
14 changes: 9 additions & 5 deletions store/etcdv3/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,15 @@ func (m *Mercury) SetContainerStatus(ctx context.Context, container *types.Conta
}
val := string(data)
statusKey := filepath.Join(containerStatusPrefix, appname, entrypoint, container.Nodename, container.ID)
lease, err := m.cliv3.Grant(ctx, ttl)
if err != nil {
return err
updateStatus := []clientv3.Op{clientv3.OpPut(statusKey, val)}
lease := &clientv3.LeaseGrantResponse{}
if ttl != 0 {
lease, err = m.cliv3.Grant(ctx, ttl)
if err != nil {
return err
}
updateStatus = []clientv3.Op{clientv3.OpPut(statusKey, val, clientv3.WithLease(lease.ID))}
}
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)).
Then( // 保证有容器
Expand All @@ -106,7 +110,7 @@ func (m *Mercury) SetContainerStatus(ctx context.Context, container *types.Conta
return nil
}
tr3 := tr2.Responses[0].GetResponseTxn()
if tr3.Succeeded {
if tr3.Succeeded && ttl != 0 { // 有 status 并且内容还跟之前一样
oldLeaseID := clientv3.LeaseID(tr3.Responses[0].GetResponseRange().Kvs[0].Lease) // 拿到 status 绑定的 leaseID
_, err := m.cliv3.KeepAliveOnce(ctx, oldLeaseID) // 刷新 lease
return err
Expand Down
3 changes: 3 additions & 0 deletions store/etcdv3/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ func TestSetContainerStatus(t *testing.T) {
container.StatusMeta.Running = true
err = m.SetContainerStatus(ctx, container, 10)
assert.NoError(t, err)
// status not changed, ttl = 0
err = m.SetContainerStatus(ctx, container, 0)
assert.NoError(t, err)
}

func TestListContainers(t *testing.T) {
Expand Down

0 comments on commit 35bd9fc

Please sign in to comment.