Skip to content

Commit b83ff93

Browse files
committed
SetNodeStatus will delete node status if ttl < 0
1 parent 0be8dba commit b83ff93

File tree

6 files changed

+32
-13
lines changed

6 files changed

+32
-13
lines changed

cluster/calcium/node.go

+6
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ func (c *Calcium) SetNode(ctx context.Context, opts *types.SetNodeOptions) (*typ
7373
n.Bypass = (opts.BypassOpt == types.TriTrue) || (opts.BypassOpt == types.TriKeep && n.Bypass)
7474
if n.IsDown() {
7575
logger.Errorf(ctx, "[SetNodeAvailable] node marked down: %s", opts.Nodename)
76+
// remove node status
77+
err := c.store.SetNodeStatus(ctx, node, -1)
78+
if err != nil {
79+
// don't return here
80+
log.Errorf(ctx, "[SetNode] failed to set node status, err: %v", err)
81+
}
7682
}
7783
if opts.WorkloadsDown {
7884
workloads, err := c.store.ListNodeWorkloads(ctx, opts.Nodename, nil)

cluster/calcium/node_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func TestSetNode(t *testing.T) {
140140
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
141141
lock.On("Lock", mock.Anything).Return(context.TODO(), nil)
142142
lock.On("Unlock", mock.Anything).Return(nil)
143+
store.On("SetNodeStatus", mock.Anything, mock.Anything, mock.Anything).Return(nil)
143144

144145
// fail by validating
145146
_, err := c.SetNode(ctx, &types.SetNodeOptions{Nodename: ""})

store/etcdv3/node.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,22 @@ func (m *Mercury) doGetNodes(ctx context.Context, kvs []*mvccpb.KeyValue, labels
290290
}
291291

292292
// SetNodeStatus sets status for a node, value will expire after ttl seconds
293-
// ttl should be larger than 0
293+
// ttl < 0 means to delete node status
294294
// this is heartbeat of node
295295
func (m *Mercury) SetNodeStatus(ctx context.Context, node *types.Node, ttl int64) error {
296-
if ttl <= 0 {
296+
if ttl == 0 {
297297
return types.ErrNodeStatusTTL
298298
}
299299

300+
// nodenames are unique
301+
statusKey := filepath.Join(nodeStatusPrefix, node.Name)
302+
entityKey := fmt.Sprintf(nodeInfoKey, node.Name)
303+
304+
if ttl < 0 {
305+
_, err := m.Delete(ctx, statusKey)
306+
return err
307+
}
308+
300309
data, err := json.Marshal(types.NodeStatus{
301310
Nodename: node.Name,
302311
Podname: node.Podname,
@@ -306,9 +315,6 @@ func (m *Mercury) SetNodeStatus(ctx context.Context, node *types.Node, ttl int64
306315
return err
307316
}
308317

309-
// nodenames are unique
310-
statusKey := filepath.Join(nodeStatusPrefix, node.Name)
311-
entityKey := fmt.Sprintf(nodeInfoKey, node.Name)
312318
return m.BindStatus(ctx, entityKey, statusKey, string(data), ttl)
313319
}
314320

store/etcdv3/node_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ RdCPRPt513WozkJZZAjUSP2U
141141
m.config.CertPath = "/tmp"
142142
node3, err := m.doAddNode(ctx, nodename3, endpoint3, podname, ca, cert, certkey, cpu, share, memory, storage, labels, nil, nil, nil)
143143
assert.NoError(t, err)
144-
engine3, err := m.makeClient(ctx, node3, true)
144+
engine3, err := m.makeClient(ctx, node3)
145145
assert.NoError(t, err)
146146
_, err = engine3.Info(ctx)
147147
assert.Error(t, err)
148148
// failed by get key
149149
node3.Name = "nokey"
150-
_, err = m.makeClient(ctx, node3, true)
150+
_, err = m.makeClient(ctx, node3)
151151
assert.NoError(t, err)
152152
}
153153

store/redis/node.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,21 @@ func (r *Rediaron) doGetNodes(ctx context.Context, kvs map[string]string, labels
288288
}
289289

290290
// SetNodeStatus sets status for a node, value will expire after ttl seconds
291-
// ttl should be larger than 0
291+
// ttl < 0 means delete node status
292292
// this is heartbeat of node
293293
func (r *Rediaron) SetNodeStatus(ctx context.Context, node *types.Node, ttl int64) error {
294-
if ttl <= 0 {
294+
if ttl == 0 {
295295
return types.ErrNodeStatusTTL
296296
}
297297

298+
// nodenames are unique
299+
key := filepath.Join(nodeStatusPrefix, node.Name)
300+
301+
if ttl < 0 {
302+
_, err := r.cli.Del(ctx, key).Result()
303+
return err
304+
}
305+
298306
data, err := json.Marshal(types.NodeStatus{
299307
Nodename: node.Name,
300308
Podname: node.Podname,
@@ -304,8 +312,6 @@ func (r *Rediaron) SetNodeStatus(ctx context.Context, node *types.Node, ttl int6
304312
return err
305313
}
306314

307-
// nodenames are unique
308-
key := filepath.Join(nodeStatusPrefix, node.Name)
309315
_, err = r.cli.Set(ctx, key, string(data), time.Duration(ttl)*time.Second).Result()
310316
return err
311317
}

store/redis/node_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ RdCPRPt513WozkJZZAjUSP2U
138138
s.rediaron.config.CertPath = "/tmp"
139139
node3, err := s.rediaron.doAddNode(ctx, nodename3, endpoint3, podname, ca, cert, certkey, cpu, share, memory, storage, labels, nil, nil, nil)
140140
s.NoError(err)
141-
engine3, err := s.rediaron.makeClient(ctx, node3, true)
141+
engine3, err := s.rediaron.makeClient(ctx, node3)
142142
s.NoError(err)
143143
_, err = engine3.Info(ctx)
144144
s.Error(err)
145145
// failed by get key
146146
node3.Name = "nokey"
147-
_, err = s.rediaron.makeClient(ctx, node3, true)
147+
_, err = s.rediaron.makeClient(ctx, node3)
148148
s.NoError(err)
149149
}
150150

0 commit comments

Comments
 (0)