Skip to content

Commit e4558f4

Browse files
committed
add list pd command: .pd
1 parent 7253b9e commit e4558f4

File tree

5 files changed

+52
-28
lines changed

5 files changed

+52
-28
lines changed

cli/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var RegisteredCmds = []tcli.Cmd{
5454
kvcmds.PrintSysVarsCmd{},
5555
kvcmds.SysVarCmd{},
5656
opcmds.ListStoresCmd{},
57+
opcmds.ListPDCmd{},
5758
//opcmds.ConnectCmd{},
5859
//opcmds.ConfigEditorCmd{},
5960
}

client/client.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ func (kvs KVS) Print() {
3838
kvmaps := make([]map[string]interface{}, len(kvs))
3939
for i := 0; i < len(kvs); i++ {
4040
kvmaps[i] = make(map[string]interface{})
41-
//kvmaps[i]["K"], kvmaps[i]["V"] = string(kvs[i].K), string(kvs[i].V)
4241
kvmaps[i][string(kvs[i].K)] = string(kvs[i].V)
4342
}
4443
out, _ := json.MarshalIndent(kvmaps, "", " ")
@@ -105,6 +104,7 @@ type Client interface {
105104
GetClientMode() TiKV_MODE
106105
GetClusterID() string
107106
GetStores() ([]StoreInfo, error)
107+
GetPDs() ([]PDInfo, error)
108108
GetPDClient() pd.Client
109109

110110
Put(ctx context.Context, kv KV) error
@@ -145,6 +145,7 @@ type StoreInfo struct {
145145
Addr string
146146
State string
147147
StatusAddress string
148+
Labels string
148149
}
149150

150151
type PDInfo struct {
@@ -153,22 +154,23 @@ type PDInfo struct {
153154
}
154155

155156
func (StoreInfo) TableTitle() []string {
156-
return []string{"Store ID", "Version", "Address", "State", "Status Address"}
157+
return []string{"Store ID", "Version", "Address", "State", "Status Address", "Labels"}
157158
}
158159

159-
func (s *StoreInfo) Flatten() []string {
160-
return []string{s.ID, s.Version, s.Addr, s.State, s.StatusAddress}
160+
func (s StoreInfo) Flatten() []string {
161+
return []string{s.ID, s.Version, s.Addr, s.State, s.StatusAddress, s.Labels}
161162
}
162163

163164
func (s StoreInfo) String() string {
164-
return fmt.Sprintf("store_id:\"%s\" version:\"%s\" addr:\"%s\" state:\"%s\" status_addr:\"%s\"", s.ID, s.Version, s.Addr, s.State, s.StatusAddress)
165+
return fmt.Sprintf("store_id:\"%s\" version:\"%s\" addr:\"%s\" state:\"%s\" status_addr:\"%s\" labels:\"%s\"",
166+
s.ID, s.Version, s.Addr, s.State, s.StatusAddress, s.Labels)
165167
}
166168

167169
func (p PDInfo) TableTitle() []string {
168170
return []string{"Name", "Client URLs"}
169171
}
170172

171-
func (p *PDInfo) Flatten() []string {
173+
func (p PDInfo) Flatten() []string {
172174
return []string{p.Name, strings.Join(p.ClientURLs, ",")}
173175
}
174176

client/rawkv_client.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,15 @@ func (c *rawkvClient) GetClusterID() string {
4747
}
4848

4949
func (c *rawkvClient) GetStores() ([]StoreInfo, error) {
50-
return nil, errors.New("rawkvClient does not support GetStores()")
50+
panic("rawkvClient does not support GetStores()")
51+
}
52+
53+
func (c *rawkvClient) GetPDs() ([]PDInfo, error) {
54+
panic("rawkvClient does not support GetPDs()")
5155
}
5256

5357
func (c *rawkvClient) GetPDClient() pd.Client {
54-
log.Fatal("rawkvClient does not support GetPDClient()")
55-
return nil
58+
panic("rawkvClient does not support GetPDClient()")
5659
}
5760

5861
func (c *rawkvClient) Put(ctx context.Context, kv KV) error {
@@ -89,6 +92,7 @@ func (c *rawkvClient) Scan(ctx context.Context, prefix []byte) (KVS, int, error)
8992
// count only mode will ignore this
9093
limit := scanOpts.GetInt(tcli.ScanOptLimit, 100)
9194
if countOnly {
95+
// BUG, TODO
9296
limit = MaxRawKVScanLimit
9397
}
9498

client/txnkv_client.go

+19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"strings"
78

89
"github.com/c4pt0r/tcli/utils"
910

@@ -51,12 +52,18 @@ func (c *txnkvClient) GetStores() ([]StoreInfo, error) {
5152
return nil, err
5253
}
5354
for _, store := range stores {
55+
labels := store.GetLabels()
56+
var strLabels []string
57+
for _, label := range labels {
58+
strLabels = append(strLabels, fmt.Sprintf("%s=%s", label.Key, label.Value))
59+
}
5460
ret = append(ret, StoreInfo{
5561
ID: fmt.Sprintf("%d", store.GetId()),
5662
Version: store.GetVersion(),
5763
Addr: store.GetAddress(),
5864
State: store.GetState().String(),
5965
StatusAddress: store.GetStatusAddress(),
66+
Labels: strings.Join(strLabels, ","),
6067
})
6168
}
6269
return ret, nil
@@ -227,3 +234,15 @@ func (c *txnkvClient) BatchDelete(ctx context.Context, kvs []KV) error {
227234
}
228235
return tx.Commit(context.Background())
229236
}
237+
238+
func (c *txnkvClient) GetPDs() ([]PDInfo, error) {
239+
pds, err := c.txnClient.GetPDClient().GetAllMembers(context.TODO())
240+
if err != nil {
241+
return nil, err
242+
}
243+
var ret []PDInfo
244+
for _, pd := range pds {
245+
ret = append(ret, PDInfo{Name: pd.Name, ClientURLs: pd.GetClientUrls()})
246+
}
247+
return ret, nil
248+
}

opcmds/cmd_listpd.go

+17-19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55

66
"github.com/c4pt0r/tcli"
7+
"github.com/c4pt0r/tcli/client"
8+
"github.com/c4pt0r/tcli/utils"
79
)
810

911
type ListPDCmd struct{}
@@ -22,24 +24,20 @@ func (c ListPDCmd) LongHelp() string {
2224

2325
func (c ListPDCmd) Handler() func(ctx context.Context) {
2426
return func(ctx context.Context) {
25-
/*
26-
utils.OutputWithElapse(func() error {
27-
pds, err := client.GetTiKVClient().GetPDClient().GetAllMembers(context.TODO())
28-
if err != nil {
29-
return err
30-
}
31-
32-
var output [][]string = [][]string{
33-
(client.PDInfo).TableTitle(client.PDInfo{}),
34-
}
35-
for _, pd := range pds {
36-
//TODO
37-
panic("not implemented")
38-
//output = append(output, pd.Flatten())
39-
}
40-
utils.PrintTable(output)
41-
return nil
42-
})
43-
*/
27+
utils.OutputWithElapse(func() error {
28+
pds, err := client.GetTiKVClient().GetPDs()
29+
if err != nil {
30+
return err
31+
}
32+
33+
var output [][]string = [][]string{
34+
(client.PDInfo).TableTitle(client.PDInfo{}),
35+
}
36+
for _, pd := range pds {
37+
output = append(output, pd.Flatten())
38+
}
39+
utils.PrintTable(output)
40+
return nil
41+
})
4442
}
4543
}

0 commit comments

Comments
 (0)