From a1e0bb338f82a4593df68d0672dbc4f330a93ae1 Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Wed, 3 Apr 2024 11:08:02 +0800 Subject: [PATCH] *: fix panic log when call RegionRequestSender.String method Signed-off-by: crazycs520 --- internal/locate/region_request.go | 5 ++++- internal/locate/region_request_test.go | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/locate/region_request.go b/internal/locate/region_request.go index 221dbee77..b655031cc 100644 --- a/internal/locate/region_request.go +++ b/internal/locate/region_request.go @@ -117,7 +117,10 @@ type RegionRequestSender struct { } func (s *RegionRequestSender) String() string { - return fmt.Sprintf("{rpcError:%v,replicaSelector: %v}", s.rpcError, s.replicaSelector.String()) + if s.replicaSelector == nil { + return fmt.Sprintf("{rpcError:%v, replicaSelector: %v}", s.rpcError, s.replicaSelector) + } + return fmt.Sprintf("{rpcError:%v, replicaSelector: %v}", s.rpcError, s.replicaSelector.String()) } // RegionRequestRuntimeStats records the runtime stats of send region requests. diff --git a/internal/locate/region_request_test.go b/internal/locate/region_request_test.go index b12044071..c4042343e 100644 --- a/internal/locate/region_request_test.go +++ b/internal/locate/region_request_test.go @@ -818,3 +818,13 @@ func (s *testRegionRequestToSingleStoreSuite) TestClientExt() { s.NotNil(sender.client) s.Nil(sender.getClientExt()) } + +func (s *testRegionRequestToSingleStoreSuite) TestRegionRequestSenderString() { + sender := NewRegionRequestSender(s.cache, &fnClient{}) + loc, err := s.cache.LocateRegionByID(s.bo, s.region) + s.Nil(err) + // invalid region cache before sending request. + s.cache.InvalidateCachedRegion(loc.Region) + sender.SendReqCtx(s.bo, tikvrpc.NewRequest(tikvrpc.CmdGet, &kvrpcpb.GetRequest{}), loc.Region, time.Second, tikvrpc.TiKV) + s.Equal("{rpcError:cached region invalid, replicaSelector: }", sender.String()) +}