Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce0f101

Browse files
authored
add meta api (vesoft-inc#78)
1 parent e6c6fbd commit ce0f101

File tree

13 files changed

+359
-1
lines changed

13 files changed

+359
-1
lines changed

ccore/nebula/client_meta.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package nebula
22

3-
import "github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/types"
3+
import (
4+
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/types"
5+
)
46

57
type (
68
MetaClient interface {
79
Open() error
10+
AddHosts(endpoints []string) error
11+
DropHosts(endpoints []string) error
12+
ListSpaces() (types.Spaces, error)
13+
BalanceData(space string) (types.Balancer, error)
14+
BalanceLeader(space string) (types.Balancer, error)
15+
BalanceDataRemove(space string, endpoints []string) (types.Balancer, error)
816
Close() error
917
}
1018

@@ -31,6 +39,40 @@ func (c *defaultMetaClient) Close() error {
3139
return c.meta.close()
3240
}
3341

42+
func (c *defaultMetaClient) AddHosts(endpoints []string) error {
43+
return c.meta.AddHosts(endpoints)
44+
}
45+
46+
func (c *defaultMetaClient) DropHosts(endpoints []string) error {
47+
return c.meta.DropHosts(endpoints)
48+
}
49+
50+
func (c *defaultMetaClient) ListSpaces() (types.Spaces, error) {
51+
return c.meta.ListSpaces()
52+
}
53+
54+
func (c *defaultMetaClient) BalanceData(space string) (types.Balancer, error) {
55+
return c.meta.Balance(types.BalanceReq{
56+
Cmd: types.BalanceData,
57+
Space: space,
58+
})
59+
}
60+
61+
func (c *defaultMetaClient) BalanceLeader(space string) (types.Balancer, error) {
62+
return c.meta.Balance(types.BalanceReq{
63+
Cmd: types.BalanceLeader,
64+
Space: space,
65+
})
66+
}
67+
68+
func (c *defaultMetaClient) BalanceDataRemove(space string, endpoints []string) (types.Balancer, error) {
69+
return c.meta.Balance(types.BalanceReq{
70+
Cmd: types.BalanceDataRemove,
71+
Space: space,
72+
HostsToRemove: endpoints,
73+
})
74+
}
75+
3476
func (c *defaultMetaClient) defaultClient() *defaultClient {
3577
return (*defaultClient)(c)
3678
}

ccore/nebula/errors/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ var (
66
ErrUnsupportedVersion = errors.New("unsupported version")
77
ErrUnsupported = errors.New("unsupported")
88
ErrNoEndpoints = errors.New("no endpoints")
9+
ErrNoJobStats = errors.New("no job stats")
910
)

ccore/nebula/internal/driver/v2_5/driver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package v2_5
22

33
import (
44
"github.com/facebook/fbthrift/thrift/lib/go/thrift"
5+
56
nthrift "github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/internal/thrift/v2_5"
67
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/types"
78
)

ccore/nebula/internal/driver/v2_5/meta.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package v2_5
22

33
import (
44
"github.com/facebook/fbthrift/thrift/lib/go/thrift"
5+
6+
nerrors "github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/errors"
57
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/internal/thrift/v2_5/meta"
68
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/types"
79
)
@@ -39,3 +41,31 @@ func (c *defaultMetaClient) Close() error {
3941
}
4042
return nil
4143
}
44+
45+
func (c *defaultMetaClient) AddHosts(endpoints []string) error {
46+
return nerrors.ErrUnsupported
47+
}
48+
49+
func (c *defaultMetaClient) DropHosts(endpoints []string) error {
50+
return nerrors.ErrUnsupported
51+
}
52+
53+
func (c *defaultMetaClient) ListSpaces() (types.Spaces, error) {
54+
req := meta.NewListSpacesReq()
55+
56+
resp, err := c.meta.ListSpaces(req)
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
if err := codeErrorIfHappened(resp.Code, nil); err != nil {
62+
return nil, err
63+
}
64+
65+
return newSpacesWrapper(resp.Spaces), nil
66+
}
67+
68+
func (c *defaultMetaClient) Balance(req types.BalanceReq) (types.Balancer, error) {
69+
// TODO: add 2.5 Balance logic
70+
return nil, nerrors.ErrUnsupported
71+
}

ccore/nebula/internal/driver/v2_5/wrapper.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
nerrors "github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/errors"
55
nthrift "github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/internal/thrift/v2_5"
66
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/internal/thrift/v2_5/graph"
7+
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/internal/thrift/v2_5/meta"
78
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/types"
89
)
910

@@ -1136,3 +1137,19 @@ func newPlanNodeBranchInfoWrapper(planNodeBranchInfo *graph.PlanNodeBranchInfo)
11361137
func (w planNodeBranchInfoWrapper) Unwrap() interface{} {
11371138
return w.PlanNodeBranchInfo
11381139
}
1140+
1141+
type spaceWrapper struct {
1142+
Space *meta.IdName
1143+
}
1144+
1145+
func newSpacesWrapper(spaces []*meta.IdName) types.Spaces {
1146+
s := make([]types.Space, 0, len(spaces))
1147+
for _, space := range spaces {
1148+
s = append(s, spaceWrapper{Space: space})
1149+
}
1150+
return s
1151+
}
1152+
1153+
func (w spaceWrapper) GetName() string {
1154+
return string(w.Space.GetName())
1155+
}

ccore/nebula/internal/driver/v2_6/driver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package v2_6
22

33
import (
44
"github.com/facebook/fbthrift/thrift/lib/go/thrift"
5+
56
nthrift "github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/internal/thrift/v2_6"
67
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/types"
78
)

ccore/nebula/internal/driver/v2_6/meta.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package v2_6
22

33
import (
44
"github.com/facebook/fbthrift/thrift/lib/go/thrift"
5+
6+
nerrors "github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/errors"
57
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/internal/thrift/v2_6/meta"
68
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/types"
79
)
@@ -43,3 +45,31 @@ func (c *defaultMetaClient) Close() error {
4345
}
4446
return nil
4547
}
48+
49+
func (c *defaultMetaClient) AddHosts(endpoints []string) error {
50+
return nerrors.ErrUnsupported
51+
}
52+
53+
func (c *defaultMetaClient) DropHosts(endpoints []string) error {
54+
return nerrors.ErrUnsupported
55+
}
56+
57+
func (c *defaultMetaClient) ListSpaces() (types.Spaces, error) {
58+
req := meta.NewListSpacesReq()
59+
60+
resp, err := c.meta.ListSpaces(req)
61+
if err != nil {
62+
return nil, err
63+
}
64+
65+
if err := codeErrorIfHappened(resp.Code, nil); err != nil {
66+
return nil, err
67+
}
68+
69+
return newSpacesWrapper(resp.Spaces), nil
70+
}
71+
72+
func (c *defaultMetaClient) Balance(req types.BalanceReq) (types.Balancer, error) {
73+
// TODO: add 2.6 Balance logic
74+
return nil, nerrors.ErrUnsupported
75+
}

ccore/nebula/internal/driver/v2_6/wrapper.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
nerrors "github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/errors"
55
nthrift "github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/internal/thrift/v2_6"
66
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/internal/thrift/v2_6/graph"
7+
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/internal/thrift/v2_6/meta"
78
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/types"
89
)
910

@@ -1236,3 +1237,19 @@ func newPlanNodeBranchInfoWrapper(planNodeBranchInfo *graph.PlanNodeBranchInfo)
12361237
func (w planNodeBranchInfoWrapper) Unwrap() interface{} {
12371238
return w.PlanNodeBranchInfo
12381239
}
1240+
1241+
type spaceWrapper struct {
1242+
Space *meta.IdName
1243+
}
1244+
1245+
func newSpacesWrapper(spaces []*meta.IdName) types.Spaces {
1246+
s := make([]types.Space, 0, len(spaces))
1247+
for _, space := range spaces {
1248+
s = append(s, spaceWrapper{Space: space})
1249+
}
1250+
return s
1251+
}
1252+
1253+
func (w spaceWrapper) GetName() string {
1254+
return string(w.Space.GetName())
1255+
}

ccore/nebula/internal/driver/v3_0/driver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package v3_0
22

33
import (
44
"github.com/facebook/fbthrift/thrift/lib/go/thrift"
5+
56
nthrift "github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/internal/thrift/v3_0"
67
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/types"
78
)

ccore/nebula/internal/driver/v3_0/meta.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package v3_0
22

33
import (
4+
"net"
5+
"strconv"
6+
47
"github.com/facebook/fbthrift/thrift/lib/go/thrift"
8+
9+
nerrors "github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/errors"
10+
nthrift "github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/internal/thrift/v3_0"
511
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/internal/thrift/v3_0/meta"
612
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/types"
713
)
@@ -43,3 +49,114 @@ func (c *defaultMetaClient) Close() error {
4349
}
4450
return nil
4551
}
52+
53+
func (c *defaultMetaClient) AddHosts(endpoints []string) error {
54+
hostsToAdd := make([]*nthrift.HostAddr, 0, len(endpoints))
55+
for _, ep := range endpoints {
56+
host, portStr, err := net.SplitHostPort(ep)
57+
if err != nil {
58+
return err
59+
}
60+
61+
port, err := strconv.Atoi(portStr)
62+
if err != nil {
63+
return err
64+
}
65+
66+
hostsToAdd = append(hostsToAdd, &nthrift.HostAddr{
67+
Host: host,
68+
Port: nthrift.Port(port),
69+
})
70+
}
71+
72+
req := &meta.AddHostsReq{
73+
Hosts: hostsToAdd,
74+
}
75+
resp, err := c.meta.AddHosts(req)
76+
if err != nil {
77+
return err
78+
}
79+
return codeErrorIfHappened(resp.Code, nil)
80+
}
81+
82+
func (c *defaultMetaClient) DropHosts(endpoints []string) error {
83+
hostsToDrop := make([]*nthrift.HostAddr, 0, len(endpoints))
84+
for _, ep := range endpoints {
85+
host, portStr, err := net.SplitHostPort(ep)
86+
if err != nil {
87+
return err
88+
}
89+
90+
port, err := strconv.Atoi(portStr)
91+
if err != nil {
92+
return err
93+
}
94+
95+
hostsToDrop = append(hostsToDrop, &nthrift.HostAddr{
96+
Host: host,
97+
Port: nthrift.Port(port),
98+
})
99+
}
100+
101+
req := &meta.DropHostsReq{
102+
Hosts: hostsToDrop,
103+
}
104+
resp, err := c.meta.DropHosts(req)
105+
if err != nil {
106+
return err
107+
}
108+
return codeErrorIfHappened(resp.Code, nil)
109+
}
110+
111+
func (c *defaultMetaClient) ListSpaces() (types.Spaces, error) {
112+
req := meta.NewListSpacesReq()
113+
114+
resp, err := c.meta.ListSpaces(req)
115+
if err != nil {
116+
return nil, err
117+
}
118+
119+
if err := codeErrorIfHappened(resp.Code, nil); err != nil {
120+
return nil, err
121+
}
122+
123+
return newSpacesWrapper(resp.Spaces), nil
124+
}
125+
126+
func (c *defaultMetaClient) Balance(req types.BalanceReq) (types.Balancer, error) {
127+
paras := make([][]byte, 0)
128+
129+
var cmd meta.AdminCmd
130+
switch req.Cmd {
131+
case types.BalanceLeader:
132+
cmd = meta.AdminCmd_LEADER_BALANCE
133+
case types.BalanceData:
134+
cmd = meta.AdminCmd_DATA_BALANCE
135+
case types.BalanceDataRemove:
136+
cmd = meta.AdminCmd_DATA_BALANCE
137+
for _, ep := range req.HostsToRemove {
138+
paras = append(paras, []byte(ep))
139+
}
140+
default:
141+
return nil, nerrors.ErrUnsupported
142+
}
143+
144+
paras = append(paras, []byte(req.Space))
145+
146+
metaReq := &meta.AdminJobReq{
147+
Op: meta.AdminJobOp_ADD,
148+
Cmd: cmd,
149+
Paras: paras,
150+
}
151+
152+
resp, err := c.meta.RunAdminJob(metaReq)
153+
if err != nil {
154+
return nil, err
155+
}
156+
157+
if err := codeErrorIfHappened(resp.Code, nil); err != nil {
158+
return nil, err
159+
}
160+
161+
return newBalancerWrap(c.meta, req.Space, resp.Result_.JobID), nil
162+
}

0 commit comments

Comments
 (0)