Skip to content

Commit

Permalink
add get node status method (#351)
Browse files Browse the repository at this point in the history
* add get node status method

* add unittests
  • Loading branch information
tonicmuroq authored Mar 2, 2021
1 parent 02fd27e commit f44a0e0
Show file tree
Hide file tree
Showing 12 changed files with 1,538 additions and 1,294 deletions.
6 changes: 6 additions & 0 deletions cluster/calcium/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func (c *Calcium) SetNodeStatus(ctx context.Context, nodename string, ttl int64)
return logger.Err(errors.WithStack(c.store.SetNodeStatus(ctx, node, ttl)))
}

// GetNodeStatus set status of a node
// it's used to report whether a node is still alive
func (c *Calcium) GetNodeStatus(ctx context.Context, nodename string) (*types.NodeStatus, error) {
return c.store.GetNodeStatus(ctx, nodename)
}

// NodeStatusStream returns a stream of node status for subscribing
func (c *Calcium) NodeStatusStream(ctx context.Context) chan *types.NodeStatus {
return c.store.NodeStatusStream(ctx)
Expand Down
23 changes: 23 additions & 0 deletions cluster/calcium/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,29 @@ func TestSetNodeStatus(t *testing.T) {
assert.NoError(c.SetNodeStatus(ctx, node.Name, 10))
}

func TestGetNodeStatus(t *testing.T) {
assert := assert.New(t)
c := NewTestCluster()
ctx := context.Background()
store := c.store.(*storemocks.Store)

ns := &types.NodeStatus{
Nodename: "test",
Podname: "test",
Alive: true,
}
// failed
store.On("GetNodeStatus", mock.Anything, mock.Anything).Return(nil, types.ErrBadCount).Once()
_, err := c.GetNodeStatus(ctx, "test")
assert.Error(err)

store.On("GetNodeStatus", mock.Anything, mock.Anything).Return(ns, nil)
s, err := c.GetNodeStatus(ctx, "test")
assert.NoError(err)
assert.Equal(s.Nodename, "test")
assert.True(s.Alive)
}

func TestNodeStatusStream(t *testing.T) {
assert := assert.New(t)
c := NewTestCluster()
Expand Down
1 change: 1 addition & 0 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Cluster interface {
GetNode(ctx context.Context, nodename string) (*types.Node, error)
SetNode(ctx context.Context, opts *types.SetNodeOptions) (*types.Node, error)
SetNodeStatus(ctx context.Context, nodename string, ttl int64) error
GetNodeStatus(ctx context.Context, nodename string) (*types.NodeStatus, error)
NodeStatusStream(ctx context.Context) chan *types.NodeStatus
// node resource
NodeResource(ctx context.Context, nodename string, fix bool) (*types.NodeResource, error)
Expand Down
23 changes: 23 additions & 0 deletions cluster/mocks/Cluster.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rpc/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const (
SetNode codes.Code = 1035
// SetNodeStatus .
SetNodeStatus codes.Code = 1036
// GetNodeStatus .
GetNodeStatus codes.Code = 1038
// GetNodeResource .
GetNodeResource codes.Code = 1037

Expand Down
Loading

0 comments on commit f44a0e0

Please sign in to comment.