From 5f95924f36a65724cb532ea494ec6380094dc2cd Mon Sep 17 00:00:00 2001 From: HunDunDM Date: Mon, 8 Nov 2021 14:25:23 +0800 Subject: [PATCH] cluster: fix the bug that region statistics are not updated after flow-round-by-digit change close #4295 Signed-off-by: HunDunDM --- server/core/region.go | 3 +- server/core/region_test.go | 75 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/server/core/region.go b/server/core/region.go index 9dc93c53a1c..ca43dc63b84 100644 --- a/server/core/region.go +++ b/server/core/region.go @@ -550,7 +550,8 @@ func GenerateRegionGuideFunc(enableLog bool) RegionGuideFunc { // Once flow has changed, will update the cache. // Because keys and bytes are strongly related, only bytes are judged. if region.GetRoundBytesWritten() != origin.GetRoundBytesWritten() || - region.GetRoundBytesRead() != origin.GetRoundBytesRead() { + region.GetRoundBytesRead() != origin.GetRoundBytesRead() || + region.flowRoundDivisor != origin.flowRoundDivisor { saveCache, needSync = true, true } diff --git a/server/core/region_test.go b/server/core/region_test.go index 9b8948596dc..c577f6e2982 100644 --- a/server/core/region_test.go +++ b/server/core/region_test.go @@ -204,6 +204,81 @@ func (s *testRegionInfoSuite) TestRegionWriteRate(c *C) { } } +var _ = Suite(&testRegionGuideSuite{}) + +type testRegionGuideSuite struct { + RegionGuide RegionGuideFunc +} + +func (s *testRegionGuideSuite) SetUpSuite(c *C) { + s.RegionGuide = GenerateRegionGuideFunc(false) +} + +func (s *testRegionGuideSuite) TestNeedSync(c *C) { + meta := &metapb.Region{ + Id: 1000, + StartKey: []byte("a"), + EndKey: []byte("z"), + RegionEpoch: &metapb.RegionEpoch{ConfVer: 100, Version: 100}, + Peers: []*metapb.Peer{ + {Id: 11, StoreId: 1, Role: metapb.PeerRole_Voter}, + {Id: 12, StoreId: 1, Role: metapb.PeerRole_Voter}, + {Id: 13, StoreId: 1, Role: metapb.PeerRole_Voter}, + }, + } + region := NewRegionInfo(meta, meta.Peers[0]) + + testcases := []struct { + optionsA []RegionCreateOption + optionsB []RegionCreateOption + needSync bool + }{ + { + optionsA: []RegionCreateOption{WithLeader(nil)}, + needSync: true, + }, + { + optionsA: []RegionCreateOption{WithLeader(meta.Peers[1])}, + needSync: true, + }, + { + optionsA: []RegionCreateOption{WithPendingPeers(meta.Peers[1:2])}, + needSync: true, + }, + { + optionsA: []RegionCreateOption{WithDownPeers([]*pdpb.PeerStats{{Peer: meta.Peers[1], DownSeconds: 600}})}, + needSync: true, + }, + { + optionsA: []RegionCreateOption{SetWrittenBytes(200), WithFlowRoundByDigit(2)}, + optionsB: []RegionCreateOption{SetWrittenBytes(300), WithFlowRoundByDigit(2)}, + needSync: true, + }, + { + optionsA: []RegionCreateOption{SetWrittenBytes(200), WithFlowRoundByDigit(4)}, + optionsB: []RegionCreateOption{SetWrittenBytes(300), WithFlowRoundByDigit(4)}, + needSync: false, + }, + { + optionsA: []RegionCreateOption{SetWrittenBytes(100000), WithFlowRoundByDigit(4)}, + optionsB: []RegionCreateOption{SetWrittenBytes(200), WithFlowRoundByDigit(2)}, + needSync: true, + }, + { + optionsA: []RegionCreateOption{SetWrittenBytes(100000), WithFlowRoundByDigit(127)}, + optionsB: []RegionCreateOption{SetWrittenBytes(0), WithFlowRoundByDigit(2)}, + needSync: true, + }, + } + + for _, t := range testcases { + regionA := region.Clone(t.optionsA...) + regionB := region.Clone(t.optionsB...) + _, _, _, needSync := s.RegionGuide(regionA, regionB) + c.Assert(needSync, Equals, t.needSync) + } +} + var _ = Suite(&testRegionMapSuite{}) type testRegionMapSuite struct{}