Skip to content

Commit

Permalink
cluster: fix the bug that region statistics are not updated after flo…
Browse files Browse the repository at this point in the history
…w-round-by-digit change

close tikv#4295

Signed-off-by: HunDunDM <hundundm@gmail.com>
  • Loading branch information
HunDunDM authored and ti-chi-bot committed Nov 8, 2021
1 parent 02139dc commit 5f95924
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
3 changes: 2 additions & 1 deletion server/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
75 changes: 75 additions & 0 deletions server/core/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down

0 comments on commit 5f95924

Please sign in to comment.