Skip to content

Commit

Permalink
feat: add api AddHostsIntoZone (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
kqzh authored Oct 19, 2023
1 parent d2e5f5f commit b8de5de
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ccore/nebula/client_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type (
MetaClient interface {
Open() error
AddHosts(endpoints []string) (types.MetaBaser, error)
AddHostsIntoZone(zone string, endpoints []string, isNew bool) (types.MetaBaser, error)
DropHosts(endpoints []string) (types.MetaBaser, error)
ListSpaces() (types.Spaces, error)
BalanceData(space string) (types.Balancer, error)
Expand Down Expand Up @@ -80,6 +81,18 @@ func (c *defaultMetaClient) AddHosts(endpoints []string) (resp types.MetaBaser,
return
}

func (c *defaultMetaClient) AddHostsIntoZone(zone string, endpoints []string, isNew bool) (resp types.MetaBaser, err error) {
retryErr := c.retryDo(func() (types.MetaBaser, error) {
resp, err = c.meta.AddHostsIntoZone(zone, endpoints, isNew)
return resp, err
})
if retryErr != nil {
return nil, retryErr
}

return
}

func (c *defaultMetaClient) DropHosts(endpoints []string) (resp types.MetaBaser, err error) {
retryErr := c.retryDo(func() (types.MetaBaser, error) {
resp, err = c.meta.DropHosts(endpoints)
Expand Down
4 changes: 4 additions & 0 deletions ccore/nebula/internal/driver/v2_5/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (c *defaultMetaClient) AddHosts(endpoints []string) (types.MetaBaser, error
return nil, nerrors.ErrUnsupported
}

func (c *defaultMetaClient) AddHostsIntoZone(zone string, endpoints []string, isNew bool) (types.MetaBaser, error) {
return nil, nerrors.ErrUnsupported
}

func (c *defaultMetaClient) DropHosts(endpoints []string) (types.MetaBaser, error) {
return nil, nerrors.ErrUnsupported
}
Expand Down
4 changes: 4 additions & 0 deletions ccore/nebula/internal/driver/v2_6/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (c *defaultMetaClient) AddHosts(endpoints []string) (types.MetaBaser, error
return nil, nerrors.ErrUnsupported
}

func (c *defaultMetaClient) AddHostsIntoZone(zone string, endpoints []string, isNew bool) (types.MetaBaser, error) {
return nil, nerrors.ErrUnsupported
}

func (c *defaultMetaClient) DropHosts(endpoints []string) (types.MetaBaser, error) {
return nil, nerrors.ErrUnsupported
}
Expand Down
42 changes: 42 additions & 0 deletions ccore/nebula/internal/driver/v3_0/meta.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v3_0

import (
"fmt"
"net"
"strconv"

Expand Down Expand Up @@ -86,6 +87,47 @@ func (c *defaultMetaClient) AddHosts(endpoints []string) (types.MetaBaser, error
}, nil
}

func (c *defaultMetaClient) AddHostsIntoZone(zone string, endpoints []string, isNew bool) (types.MetaBaser, error) {
hostsToAdd := make([]*nthrift.HostAddr, 0, len(endpoints))
for _, ep := range endpoints {
host, portStr, err := net.SplitHostPort(ep)
if err != nil {
return nil, err
}

port, err := strconv.Atoi(portStr)
if err != nil {
return nil, err
}

hostsToAdd = append(hostsToAdd, &nthrift.HostAddr{
Host: host,
Port: nthrift.Port(port),
})
}

req := &meta.AddHostsIntoZoneReq{
Hosts: hostsToAdd,
ZoneName: []byte(zone),
IsNew: isNew,
}

fmt.Println(req)

resp, err := c.meta.AddHostsIntoZone(req)
if err != nil {
return nil, err
}

return metaBaserWrap{
code: nerrors.ErrorCode(resp.GetCode()),
leader: types.HostAddr{
Host: resp.GetLeader().GetHost(),
Port: resp.GetLeader().GetPort(),
},
}, nil
}

func (c *defaultMetaClient) DropHosts(endpoints []string) (types.MetaBaser, error) {
hostsToDrop := make([]*nthrift.HostAddr, 0, len(endpoints))
for _, ep := range endpoints {
Expand Down
1 change: 1 addition & 0 deletions ccore/nebula/types/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type (
Open() error
VerifyClientVersion() error
AddHosts(endpoints []string) (MetaBaser, error)
AddHostsIntoZone(zone string, endpoints []string, isNew bool) (MetaBaser, error)
DropHosts(endpoints []string) (MetaBaser, error)
ListSpaces() (Spaces, error)
Balance(req BalanceReq) (Balancer, error)
Expand Down

0 comments on commit b8de5de

Please sign in to comment.