From 22452b57d16196602840c84b39d66ee924f998d5 Mon Sep 17 00:00:00 2001 From: Kenshin <35095889+kqzh@users.noreply.github.com> Date: Fri, 1 Apr 2022 13:54:56 +0800 Subject: [PATCH] add list zone (#97) --- ccore/nebula/client_meta.go | 13 ++++++ ccore/nebula/internal/driver/v2_5/meta.go | 4 ++ ccore/nebula/internal/driver/v2_6/meta.go | 4 ++ ccore/nebula/internal/driver/v3_0/meta.go | 11 +++++ ccore/nebula/internal/driver/v3_0/wrapper.go | 49 ++++++++++++++++++++ ccore/nebula/types/driver.go | 13 +++++- 6 files changed, 93 insertions(+), 1 deletion(-) diff --git a/ccore/nebula/client_meta.go b/ccore/nebula/client_meta.go index f1d0c9b..b5f3785 100644 --- a/ccore/nebula/client_meta.go +++ b/ccore/nebula/client_meta.go @@ -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 } @@ -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() } diff --git a/ccore/nebula/internal/driver/v2_5/meta.go b/ccore/nebula/internal/driver/v2_5/meta.go index 97cfde1..ab10f4f 100644 --- a/ccore/nebula/internal/driver/v2_5/meta.go +++ b/ccore/nebula/internal/driver/v2_5/meta.go @@ -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 +} diff --git a/ccore/nebula/internal/driver/v2_6/meta.go b/ccore/nebula/internal/driver/v2_6/meta.go index e492788..bd140bc 100644 --- a/ccore/nebula/internal/driver/v2_6/meta.go +++ b/ccore/nebula/internal/driver/v2_6/meta.go @@ -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 +} diff --git a/ccore/nebula/internal/driver/v3_0/meta.go b/ccore/nebula/internal/driver/v3_0/meta.go index cfcc99e..f245df8 100644 --- a/ccore/nebula/internal/driver/v3_0/meta.go +++ b/ccore/nebula/internal/driver/v3_0/meta.go @@ -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 +} diff --git a/ccore/nebula/internal/driver/v3_0/wrapper.go b/ccore/nebula/internal/driver/v3_0/wrapper.go index 356b494..c37bd96 100644 --- a/ccore/nebula/internal/driver/v3_0/wrapper.go +++ b/ccore/nebula/internal/driver/v3_0/wrapper.go @@ -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 diff --git a/ccore/nebula/types/driver.go b/ccore/nebula/types/driver.go index 74ec474..2ba0810 100644 --- a/ccore/nebula/types/driver.go +++ b/ccore/nebula/types/driver.go @@ -41,6 +41,7 @@ type ( ListSpaces() (Spaces, error) Balance(req BalanceReq) (Balancer, error) ListHosts() (Hosts, error) + ListZones() (Zones, error) Close() error } @@ -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 }