region_cache: refine GetTiFlashComputeRPCContextByConsistentHash

Signed-off-by: guo-shaoge shaoge1994@163.com
This commit is contained in:
Liqi Geng 2022-12-19 15:59:31 +08:00 committed by GitHub
commit a4f5c00b46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 11 deletions

View File

@ -758,17 +758,12 @@ func (c *RegionCache) GetTiFlashRPCContext(bo *retry.Backoffer, id RegionVerID,
// 3. Compute which tiflash_compute node should handle this region by consistent hash.
// 4. Replace infos(addr/Store) that indicate where the region is stored to infos that indicate where the region will be computed.
// NOTE: This function make sure the returned slice of RPCContext and the input ids correspond to each other.
func (c *RegionCache) GetTiFlashComputeRPCContextByConsistentHash(bo *retry.Backoffer, ids []RegionVerID) (res []*RPCContext, err error) {
stores, err := c.GetTiFlashComputeStores(bo)
if err != nil {
return nil, err
}
if len(stores) == 0 {
return nil, errors.New("number of tiflash_compute node is zero")
}
func (c *RegionCache) GetTiFlashComputeRPCContextByConsistentHash(bo *retry.Backoffer, ids []RegionVerID, stores []*Store) (res []*RPCContext, err error) {
hasher := consistent.New()
for _, store := range stores {
if !isStoreContainLabel(store.labels, tikvrpc.EngineLabelKey, tikvrpc.EngineLabelTiFlashCompute) {
return nil, errors.New("expect store should be tiflash_compute")
}
hasher.Add(store.GetAddr())
}
@ -777,11 +772,12 @@ func (c *RegionCache) GetTiFlashComputeRPCContextByConsistentHash(bo *retry.Back
if err != nil {
return nil, err
}
rpcCtx, err := c.GetTiFlashRPCContext(bo, id, true)
rpcCtx, err := c.GetTiFlashRPCContext(bo, id, false)
if err != nil {
return nil, err
}
if rpcCtx == nil {
logutil.Logger(context.Background()).Info("rpcCtx is nil", zap.Any("region", id.String()))
return nil, nil
}

View File

@ -879,7 +879,11 @@ func (s *RegionRequestSender) getRPCContext(
case tikvrpc.TiDB:
return &RPCContext{Addr: s.storeAddr}, nil
case tikvrpc.TiFlashCompute:
rpcCtxs, err := s.regionCache.GetTiFlashComputeRPCContextByConsistentHash(bo, []RegionVerID{regionID})
stores, err := s.regionCache.GetTiFlashComputeStores(bo)
if err != nil {
return nil, err
}
rpcCtxs, err := s.regionCache.GetTiFlashComputeRPCContextByConsistentHash(bo, []RegionVerID{regionID}, stores)
if err != nil {
return nil, err
}