Skip to content

Commit

Permalink
add list zone (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
kqzh authored Apr 1, 2022
1 parent f599adf commit 22452b5
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 1 deletion.
13 changes: 13 additions & 0 deletions ccore/nebula/client_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type (
BalanceLeader(space string) (types.Balancer, error)
BalanceDataRemove(space string, endpoints []string) (types.Balancer, error)
ListHosts() (types.Hosts, error)
ListZones() (types.Zones, error)
Close() error
}

Expand Down Expand Up @@ -51,6 +52,18 @@ func (c *defaultMetaClient) ListHosts() (resp types.Hosts, err error) {
return
}

func (c *defaultMetaClient) ListZones() (resp types.Zones, err error) {
retryErr := c.retryDo(func() (types.MetaBaser, error) {
resp, err = c.meta.ListZones()
return resp, err
})
if retryErr != nil {
return nil, retryErr
}

return
}

func (c *defaultMetaClient) Close() error {
return c.meta.close()
}
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 @@ -80,3 +80,7 @@ func (c *defaultMetaClient) ListHosts() (types.Hosts, error) {

return newHostsWrapper(resp), nil
}

func (c *defaultMetaClient) ListZones() (types.Zones, error) {
return nil, nerrors.ErrUnsupported
}
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 @@ -84,3 +84,7 @@ func (c *defaultMetaClient) ListHosts() (types.Hosts, error) {

return newHostsWrapper(resp), nil
}

func (c *defaultMetaClient) ListZones() (types.Zones, error) {
return nil, nerrors.ErrUnsupported
}
11 changes: 11 additions & 0 deletions ccore/nebula/internal/driver/v3_0/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,14 @@ func (c *defaultMetaClient) ListHosts() (types.Hosts, error) {

return newHostsWrapper(resp), nil
}

func (c *defaultMetaClient) ListZones() (types.Zones, error) {
req := meta.NewListZonesReq()

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

return newZonesWrapper(resp), nil
}
49 changes: 49 additions & 0 deletions ccore/nebula/internal/driver/v3_0/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,55 @@ func newHostsWrapper(resp *meta.ListHostsResp) types.Hosts {
}
}

type zoneWrapper struct {
zoneName string
hosts []*types.HostAddr
}

func (z zoneWrapper) GetName() string {
return z.zoneName
}

func (z zoneWrapper) GetHosts() []*types.HostAddr {
return z.hosts
}

type zonesWrapper struct {
metaBaserWrap
zones []types.Zone
}

func (z zonesWrapper) GetZones() []types.Zone {
return z.zones
}

func newZonesWrapper(resp *meta.ListZonesResp) types.Zones {
zones := make([]types.Zone, 0, len(resp.Zones))
for _, zone := range resp.Zones {
hosts := make([]*types.HostAddr, 0, len(zone.GetNodes()))
for _, host := range zone.GetNodes() {
hosts = append(hosts, &types.HostAddr{
Host: host.GetHost(),
Port: host.GetPort(),
})
}
zones = append(zones, zoneWrapper{
zoneName: string(zone.GetZoneName()),
hosts: hosts,
})
}
return zonesWrapper{
metaBaserWrap: metaBaserWrap{
code: nerrors.ErrorCode(resp.GetCode()),
leader: types.HostAddr{
Host: resp.GetLeader().GetHost(),
Port: resp.GetLeader().GetPort(),
},
},
zones: zones,
}
}

type metaBaserWrap struct {
code nerrors.ErrorCode
leader types.HostAddr
Expand Down
13 changes: 12 additions & 1 deletion ccore/nebula/types/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type (
ListSpaces() (Spaces, error)
Balance(req BalanceReq) (Balancer, error)
ListHosts() (Hosts, error)
ListZones() (Zones, error)
Close() error
}

Expand Down Expand Up @@ -98,11 +99,21 @@ type (
GetHostItem() HostItem
}

Hosts interface {
Hosts interface {
MetaBaser
GetHosts() []Host
}

Zone interface {
GetName() string
GetHosts() []*HostAddr
}

Zones interface {
MetaBaser
GetZones() []Zone
}

Coder interface {
GetCode() nerrors.ErrorCode
}
Expand Down

0 comments on commit 22452b5

Please sign in to comment.