Skip to content

Commit 0e792e2

Browse files
committed
feat(go-client): add ListNodes API for meta admin
1 parent b0e4c59 commit 0e792e2

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

go-client/admin/client.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ type Client interface {
5454
// Empty `args` means "list all available tables"; Otherwise, the only parameter would
5555
// specify the status of the returned tables.
5656
ListTables(args ...interface{}) ([]*replication.AppInfo, error)
57+
58+
// Empty `args` means "list all alive nodes"; Otherwise, the only parameter would
59+
// specify the status of the returned nodes.
60+
ListNodes(args ...interface{}) ([]*admin.NodeInfo, error)
5761
}
5862

5963
type Config struct {
@@ -234,3 +238,29 @@ func (c *rpcBasedClient) ListTables(args ...interface{}) ([]*replication.AppInfo
234238
}
235239
return c.listTables(args[0].(replication.AppStatus))
236240
}
241+
242+
func (c *rpcBasedClient) listNodes(status admin.NodeStatus) ([]*admin.NodeInfo, error) {
243+
req := &admin.ConfigurationListNodesRequest{
244+
Status: status,
245+
}
246+
247+
var nodes []*admin.NodeInfo
248+
var respErr error
249+
err := c.callMeta("ListNodes", req, func(iresp interface{}) {
250+
resp := iresp.(*admin.ConfigurationListNodesResponse)
251+
nodes = resp.Infos
252+
respErr = base.GetResponseError(resp)
253+
})
254+
if err != nil {
255+
return nodes, err
256+
}
257+
258+
return nodes, respErr
259+
}
260+
261+
func (c *rpcBasedClient) ListNodes(args ...interface{}) ([]*admin.NodeInfo, error) {
262+
if len(args) == 0 {
263+
return c.listNodes(admin.NodeStatus_NS_ALIVE)
264+
}
265+
return c.listNodes(args[0].(admin.NodeStatus))
266+
}

go-client/admin/client_test.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"testing"
2626
"time"
2727

28+
"github.com/apache/incubator-pegasus/go-client/admin"
2829
"github.com/apache/incubator-pegasus/go-client/idl/replication"
2930
"github.com/apache/incubator-pegasus/go-client/pegasus"
3031
"github.com/stretchr/testify/assert"
@@ -43,6 +44,22 @@ func defaultConfig() Config {
4344
}
4445
}
4546

47+
func defaultReplicaServers() []string {
48+
return []string{"0.0.0.0:34801", "0.0.0.0:34802", "0.0.0.0:34803"}
49+
}
50+
51+
func timeoutConfig() Config {
52+
return Config{
53+
MetaServers: []string{"0.0.0.0:123456"},
54+
Timeout: 500 * time.Millisecond,
55+
}
56+
}
57+
58+
func testAdmin_Timeout(t *testing.T, exec func(c *admin.Client) error) {
59+
c := NewClient(timeoutConfig())
60+
assert.Equal(t, context.DeadlineExceeded, exec(c))
61+
}
62+
4663
func TestAdmin_Table(t *testing.T) {
4764
c := NewClient(defaultConfig())
4865

@@ -75,13 +92,9 @@ func TestAdmin_Table(t *testing.T) {
7592
}
7693

7794
func TestAdmin_ListTablesTimeout(t *testing.T) {
78-
c := NewClient(Config{
79-
MetaServers: []string{"0.0.0.0:123456"},
80-
Timeout: 500 * time.Millisecond,
95+
testAdmin_Timeout(t, func(c *admin.Client) (err, error) {
96+
_, err := c.ListTables()
8197
})
82-
83-
_, err := c.ListTables()
84-
assert.Equal(t, err, context.DeadlineExceeded)
8598
}
8699

87100
// Ensures after the call `CreateTable` ends, the table must be right available to access.
@@ -145,3 +158,31 @@ func TestAdmin_GetAppEnvs(t *testing.T) {
145158
assert.Empty(t, tb.Envs)
146159
}
147160
}
161+
162+
func TestAdmin_ListNodes(t *testing.T) {
163+
c := NewClient(defaultConfig())
164+
165+
nodes, err := c.ListNodes()
166+
assert.Nil(t, err)
167+
168+
expectedReplicaServers := defaultReplicaServers()
169+
170+
// Compare slice length.
171+
assert.Equal(t, len(expectedReplicaServers), len(nodes))
172+
173+
actualReplicaServers := make([]int, len(nodes))
174+
for _, node := range nodes {
175+
// Each node should be alive.
176+
assert.Equal(t, admin.NodeStatus_NS_ALIVE, node.Status)
177+
actualReplicaServers[i] = node.Address.GetAddress()
178+
}
179+
180+
// Match elements without extra ordering.
181+
assert.ElementsMatch(t, expectedReplicaServers, actualReplicaServers)
182+
}
183+
184+
func TestAdmin_ListNodesTimeout(t *testing.T) {
185+
testAdmin_Timeout(t, func(c *admin.Client) (err, error) {
186+
_, err := c.ListNodes()
187+
})
188+
}

0 commit comments

Comments
 (0)