Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client/router: implement the query region gRPC client #8939

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,11 @@

// serviceModeKeeper is for service mode switching.
type serviceModeKeeper struct {
// RMutex here is for the future usage that there might be multiple goroutines
// triggering service mode switching concurrently.
sync.RWMutex
serviceMode pdpb.ServiceMode
tsoClient *tso.Cli
tsoSvcDiscovery sd.ServiceDiscovery
routerClient *router.Cli
}

func (k *serviceModeKeeper) close() {
Expand Down Expand Up @@ -570,21 +569,16 @@
return minTS.Physical, minTS.Logical, nil
}

func handleRegionResponse(res *pdpb.GetRegionResponse) *router.Region {
if res.Region == nil {
return nil
}
// EnableRouterClient enables the router client.
// This is only for test currently.
func (c *client) EnableRouterClient() {
c.inner.initRouterClient()
}

r := &router.Region{
Meta: res.Region,
Leader: res.Leader,
PendingPeers: res.PendingPeers,
Buckets: res.Buckets,
}
for _, s := range res.DownPeers {
r.DownPeers = append(r.DownPeers, s.Peer)
}
return r
func (c *client) getRouterClient() *router.Cli {
c.inner.RLock()
defer c.inner.RUnlock()
return c.inner.routerClient
}

// GetRegionFromMember implements the RPCClient interface.
Expand Down Expand Up @@ -623,7 +617,7 @@
errorMsg := fmt.Sprintf("[pd] can't get region info from member URLs: %+v", memberURLs)
return nil, errors.WithStack(errors.New(errorMsg))
}
return handleRegionResponse(resp), nil
return router.ConvertToRegion(resp), nil

Check warning on line 620 in client/client.go

View check run for this annotation

Codecov / codecov/patch

client/client.go#L620

Added line #L620 was not covered by tests
}

// GetRegion implements the RPCClient interface.
Expand All @@ -637,6 +631,10 @@
ctx, cancel := context.WithTimeout(ctx, c.inner.option.Timeout)
defer cancel()

if routerClient := c.getRouterClient(); routerClient != nil {
return routerClient.GetRegion(ctx, key, opts...)
}

options := &opt.GetRegionOp{}
for _, opt := range opts {
opt(options)
Expand All @@ -663,7 +661,7 @@
if err = c.respForErr(metrics.CmdFailedDurationGetRegion, start, err, resp.GetHeader()); err != nil {
return nil, err
}
return handleRegionResponse(resp), nil
return router.ConvertToRegion(resp), nil
}

// GetPrevRegion implements the RPCClient interface.
Expand All @@ -677,6 +675,10 @@
ctx, cancel := context.WithTimeout(ctx, c.inner.option.Timeout)
defer cancel()

if routerClient := c.getRouterClient(); routerClient != nil {
return routerClient.GetPrevRegion(ctx, key, opts...)
}

options := &opt.GetRegionOp{}
for _, opt := range opts {
opt(options)
Expand All @@ -703,7 +705,7 @@
if err = c.respForErr(metrics.CmdFailedDurationGetPrevRegion, start, err, resp.GetHeader()); err != nil {
return nil, err
}
return handleRegionResponse(resp), nil
return router.ConvertToRegion(resp), nil
}

// GetRegionByID implements the RPCClient interface.
Expand All @@ -717,6 +719,10 @@
ctx, cancel := context.WithTimeout(ctx, c.inner.option.Timeout)
defer cancel()

if routerClient := c.getRouterClient(); routerClient != nil {
return routerClient.GetRegionByID(ctx, regionID, opts...)
}

options := &opt.GetRegionOp{}
for _, opt := range opts {
opt(options)
Expand Down Expand Up @@ -744,7 +750,7 @@
if err = c.respForErr(metrics.CmdFailedDurationGetRegionByID, start, err, resp.GetHeader()); err != nil {
return nil, err
}
return handleRegionResponse(resp), nil
return router.ConvertToRegion(resp), nil
}

// ScanRegions implements the RPCClient interface.
Expand Down
Loading
Loading