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

cluster: fix the bug that region statistics are not updated after flow-round-by-digit change #4304

Merged
merged 3 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion server/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,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 {
HunDunDM marked this conversation as resolved.
Show resolved Hide resolved
saveCache, needSync = true, true
}

Expand Down
3 changes: 2 additions & 1 deletion server/core/region_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func WithDownPeers(downPeers []*pdpb.PeerStats) RegionCreateOption {

// WithFlowRoundByDigit set the digit, which use to round to the nearest number
func WithFlowRoundByDigit(digit int) RegionCreateOption {
flowRoundDivisor := uint64(math.Pow10(digit))
rleungx marked this conversation as resolved.
Show resolved Hide resolved
return func(region *RegionInfo) {
region.flowRoundDivisor = uint64(math.Pow10(digit))
region.flowRoundDivisor = flowRoundDivisor
}
}

Expand Down
80 changes: 80 additions & 0 deletions server/core/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,86 @@ 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(250), WithFlowRoundByDigit(2)},
optionsB: []RegionCreateOption{SetWrittenBytes(349), WithFlowRoundByDigit(2)},
needSync: false,
},
{
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
18 changes: 9 additions & 9 deletions server/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,14 +666,14 @@ func (s *heartbeatServer) Recv() (*pdpb.RegionHeartbeatRequest, error) {

// RegionHeartbeat implements gRPC PDServer.
func (s *Server) RegionHeartbeat(stream pdpb.PD_RegionHeartbeatServer) error {
server := &heartbeatServer{stream: stream}
FlowRoundByDigit := s.persistOptions.GetPDServerConfig().FlowRoundByDigit
var (
forwardStream pdpb.PD_RegionHeartbeatClient
cancel context.CancelFunc
lastForwardedHost string
lastBind time.Time
errCh chan error
server = &heartbeatServer{stream: stream}
regionFlowRoundByDigitOption = core.WithFlowRoundByDigit(s.persistOptions.GetPDServerConfig().FlowRoundByDigit)
forwardStream pdpb.PD_RegionHeartbeatClient
cancel context.CancelFunc
lastForwardedHost string
lastBind time.Time
errCh chan error
)
defer func() {
// cancel the forward stream
Expand Down Expand Up @@ -750,11 +750,11 @@ func (s *Server) RegionHeartbeat(stream pdpb.PD_RegionHeartbeatServer) error {
regionHeartbeatCounter.WithLabelValues(storeAddress, storeLabel, "report", "bind").Inc()
s.hbStreams.BindStream(storeID, server)
// refresh FlowRoundByDigit
FlowRoundByDigit = s.persistOptions.GetPDServerConfig().FlowRoundByDigit
regionFlowRoundByDigitOption = core.WithFlowRoundByDigit(s.persistOptions.GetPDServerConfig().FlowRoundByDigit)
HunDunDM marked this conversation as resolved.
Show resolved Hide resolved
lastBind = time.Now()
}

region := core.RegionFromHeartbeat(request, core.WithFlowRoundByDigit(FlowRoundByDigit))
region := core.RegionFromHeartbeat(request, regionFlowRoundByDigitOption)
if region.GetLeader() == nil {
log.Error("invalid request, the leader is nil", zap.Reflect("request", request), errs.ZapError(errs.ErrLeaderNil))
regionHeartbeatCounter.WithLabelValues(storeAddress, storeLabel, "report", "invalid-leader").Inc()
Expand Down