-
Notifications
You must be signed in to change notification settings - Fork 728
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
tools: support changing the percentage of heartbeat #7698
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -186,7 +186,8 @@ func newEndKey(id uint64, keyLen int) []byte { | |||||||||||||
|
||||||||||||||
// Regions simulates all regions to heartbeat. | ||||||||||||||
type Regions struct { | ||||||||||||||
regions []*pdpb.RegionHeartbeatRequest | ||||||||||||||
regions []*pdpb.RegionHeartbeatRequest | ||||||||||||||
awakenRegions atomic.Value | ||||||||||||||
|
||||||||||||||
updateRound int | ||||||||||||||
|
||||||||||||||
|
@@ -258,22 +259,27 @@ func (rs *Regions) update(cfg *config.Config, options *config.Options, indexes [ | |||||||||||||
rs.updateEpoch = pick(indexes, cfg, options.GetEpochUpdateRatio()) | ||||||||||||||
rs.updateSpace = pick(indexes, cfg, options.GetSpaceUpdateRatio()) | ||||||||||||||
rs.updateFlow = pick(indexes, cfg, options.GetFlowUpdateRatio()) | ||||||||||||||
updatedRegionsMap := make(map[int]*pdpb.RegionHeartbeatRequest) | ||||||||||||||
var awakenRegions []*pdpb.RegionHeartbeatRequest | ||||||||||||||
|
||||||||||||||
// update leader | ||||||||||||||
for _, i := range rs.updateLeader { | ||||||||||||||
region := rs.regions[i] | ||||||||||||||
region.Leader = region.Region.Peers[rs.updateRound%cfg.Replica] | ||||||||||||||
updatedRegionsMap[i] = region | ||||||||||||||
} | ||||||||||||||
// update epoch | ||||||||||||||
for _, i := range rs.updateEpoch { | ||||||||||||||
region := rs.regions[i] | ||||||||||||||
region.Region.RegionEpoch.Version += 1 | ||||||||||||||
updatedRegionsMap[i] = region | ||||||||||||||
} | ||||||||||||||
// update space | ||||||||||||||
for _, i := range rs.updateSpace { | ||||||||||||||
region := rs.regions[i] | ||||||||||||||
region.ApproximateSize = uint64(bytesUnit * rand.Float64()) | ||||||||||||||
region.ApproximateKeys = uint64(keysUint * rand.Float64()) | ||||||||||||||
updatedRegionsMap[i] = region | ||||||||||||||
} | ||||||||||||||
// update flow | ||||||||||||||
for _, i := range rs.updateFlow { | ||||||||||||||
|
@@ -286,12 +292,21 @@ func (rs *Regions) update(cfg *config.Config, options *config.Options, indexes [ | |||||||||||||
Get: uint64(queryUnit * rand.Float64()), | ||||||||||||||
Put: uint64(queryUnit * rand.Float64()), | ||||||||||||||
} | ||||||||||||||
updatedRegionsMap[i] = region | ||||||||||||||
} | ||||||||||||||
// update interval | ||||||||||||||
for _, region := range rs.regions { | ||||||||||||||
region.Interval.StartTimestamp = region.Interval.EndTimestamp | ||||||||||||||
region.Interval.EndTimestamp = region.Interval.StartTimestamp + regionReportInterval | ||||||||||||||
} | ||||||||||||||
for _, region := range updatedRegionsMap { | ||||||||||||||
awakenRegions = append(awakenRegions, region) | ||||||||||||||
} | ||||||||||||||
noUpdatedRegions := pickNoUpdatedRegions(indexes, cfg, options.GetNoUpdateRatio(), updatedRegionsMap) | ||||||||||||||
for _, i := range noUpdatedRegions { | ||||||||||||||
awakenRegions = append(awakenRegions, rs.regions[i]) | ||||||||||||||
} | ||||||||||||||
rs.awakenRegions.Store(awakenRegions) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func createHeartbeatStream(ctx context.Context, cfg *config.Config) pdpb.PD_RegionHeartbeatClient { | ||||||||||||||
|
@@ -312,8 +327,14 @@ func createHeartbeatStream(ctx context.Context, cfg *config.Config) pdpb.PD_Regi | |||||||||||||
|
||||||||||||||
func (rs *Regions) handleRegionHeartbeat(wg *sync.WaitGroup, stream pdpb.PD_RegionHeartbeatClient, storeID uint64, rep report.Report) { | ||||||||||||||
defer wg.Done() | ||||||||||||||
var regions []*pdpb.RegionHeartbeatRequest | ||||||||||||||
for _, region := range rs.regions { | ||||||||||||||
var regions, toUpdate []*pdpb.RegionHeartbeatRequest | ||||||||||||||
updatedRegions := rs.awakenRegions.Load() | ||||||||||||||
if updatedRegions == nil { | ||||||||||||||
toUpdate = rs.regions | ||||||||||||||
} else { | ||||||||||||||
toUpdate = updatedRegions.([]*pdpb.RegionHeartbeatRequest) | ||||||||||||||
} | ||||||||||||||
for _, region := range toUpdate { | ||||||||||||||
if region.Leader.StoreId != storeID { | ||||||||||||||
continue | ||||||||||||||
} | ||||||||||||||
|
@@ -411,6 +432,28 @@ func pick(slice []int, cfg *config.Config, ratio float64) []int { | |||||||||||||
return append(slice[:0:0], slice[0:int(float64(cfg.RegionCount)*ratio)]...) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func pickNoUpdatedRegions(slice []int, cfg *config.Config, ratio float64, updatedMap map[int]*pdpb.RegionHeartbeatRequest) []int { | ||||||||||||||
if ratio == 0 { | ||||||||||||||
return nil | ||||||||||||||
} | ||||||||||||||
rand.Shuffle(cfg.RegionCount, func(i, j int) { | ||||||||||||||
slice[i], slice[j] = slice[j], slice[i] | ||||||||||||||
}) | ||||||||||||||
NoUpdatedRegionsNum := int(float64(cfg.RegionCount) * ratio) | ||||||||||||||
res := make([]int, 0, NoUpdatedRegionsNum) | ||||||||||||||
i := 0 | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
for { | ||||||||||||||
if len(res) == NoUpdatedRegionsNum { | ||||||||||||||
break | ||||||||||||||
} | ||||||||||||||
if _, ok := updatedMap[slice[i]]; !ok { | ||||||||||||||
res = append(res, slice[i]) | ||||||||||||||
} | ||||||||||||||
i++ | ||||||||||||||
} | ||||||||||||||
return res | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func main() { | ||||||||||||||
rand.New(rand.NewSource(0)) // Ensure consistent behavior multiple times | ||||||||||||||
cfg := config.NewConfig() | ||||||||||||||
|
@@ -562,6 +605,11 @@ func runHTTPServer(cfg *config.Config, options *config.Options) { | |||||||||||||
pprof.Register(engine) | ||||||||||||||
engine.PUT("config", func(c *gin.Context) { | ||||||||||||||
newCfg := cfg.Clone() | ||||||||||||||
newCfg.FlowUpdateRatio = options.GetFlowUpdateRatio() | ||||||||||||||
newCfg.LeaderUpdateRatio = options.GetLeaderUpdateRatio() | ||||||||||||||
newCfg.EpochUpdateRatio = options.GetEpochUpdateRatio() | ||||||||||||||
newCfg.SpaceUpdateRatio = options.GetSpaceUpdateRatio() | ||||||||||||||
newCfg.NoUpdateRatio = options.GetNoUpdateRatio() | ||||||||||||||
if err := c.BindJSON(&newCfg); err != nil { | ||||||||||||||
c.String(http.StatusBadRequest, err.Error()) | ||||||||||||||
return | ||||||||||||||
|
@@ -576,6 +624,7 @@ func runHTTPServer(cfg *config.Config, options *config.Options) { | |||||||||||||
output.LeaderUpdateRatio = options.GetLeaderUpdateRatio() | ||||||||||||||
output.EpochUpdateRatio = options.GetEpochUpdateRatio() | ||||||||||||||
output.SpaceUpdateRatio = options.GetSpaceUpdateRatio() | ||||||||||||||
output.NoUpdateRatio = options.GetNoUpdateRatio() | ||||||||||||||
|
||||||||||||||
c.IndentedJSON(http.StatusOK, output) | ||||||||||||||
}) | ||||||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And do we need check the
NoUpdatedRegionsNum + len(updatedMap)
should less thanlen(slice)