-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
resourcemanager: scheduler subtask in the pool's task #40670
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7a202eb
update
hawkingrei e2efe98
update
hawkingrei 21b013d
update
hawkingrei d71a10b
update
hawkingrei cbe2a3a
update config
hawkingrei ef4cc21
update config
hawkingrei 6f71d33
update config
hawkingrei f07c309
Update resourcemanager/pooltask/task_manager_iterator.go
hawkingrei 4af5ac1
clean
hawkingrei 542a811
fix misjudgment
hawkingrei f5ce60f
fix misjudgment
hawkingrei fbd18a3
fix misjudgment
hawkingrei c2ed193
Merge branch 'master' into improve_scheduler
hawkingrei 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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright 2023 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package pooltask | ||
|
||
import ( | ||
"container/list" | ||
"time" | ||
) | ||
|
||
func (t *TaskManager[T, U, C, CT, TF]) getBoostTask() (tid uint64, result *TaskBox[T, U, C, CT, TF]) { | ||
// boost task | ||
// 1、the count of running task is less than concurrency | ||
// 2、less run time, more possible to boost | ||
tid, element := t.iter(canBoost[T, U, C, CT, TF]) | ||
if element != nil { | ||
return tid, element.Value.(tContainer[T, U, C, CT, TF]).task | ||
} | ||
return 0, nil | ||
} | ||
|
||
func (t *TaskManager[T, U, C, CT, TF]) pauseTask() { | ||
// pause task, | ||
// 1、more run time, more possible to pause | ||
// 2、if task have been boosted, first to pause. | ||
tid, result := t.iter(canPause[T, U, C, CT, TF]) | ||
if result != nil { | ||
result.Value.(tContainer[T, U, C, CT, TF]).task.status.CompareAndSwap(RunningTask, StopTask) | ||
// delete it from list | ||
shardID := getShardID(tid) | ||
t.task[shardID].rw.Lock() | ||
defer t.task[shardID].rw.Unlock() | ||
t.task[shardID].stats[tid].stats.Remove(result) | ||
} | ||
} | ||
|
||
func (t *TaskManager[T, U, C, CT, TF]) iter(fn func(m *meta[T, U, C, CT, TF], max time.Time) (*list.Element, bool)) (tid uint64, result *list.Element) { | ||
var compareTS time.Time | ||
for i := 0; i < shard; i++ { | ||
breakFind := func(index int) (breakFind bool) { | ||
t.task[i].rw.RLock() | ||
defer t.task[i].rw.RUnlock() | ||
for id, stats := range t.task[i].stats { | ||
if result == nil { | ||
result = findTask[T, U, C, CT, TF](stats, RunningTask) | ||
tid = id | ||
compareTS = stats.createTS | ||
continue | ||
} | ||
newResult, pauseFind := fn(stats, compareTS) | ||
if pauseFind { | ||
result = newResult | ||
tid = id | ||
compareTS = stats.createTS | ||
return true | ||
} | ||
if newResult != nil { | ||
result = newResult | ||
tid = id | ||
compareTS = stats.createTS | ||
} | ||
} | ||
return false | ||
}(shard) | ||
if breakFind { | ||
break | ||
} | ||
} | ||
return tid, result | ||
} | ||
|
||
func canPause[T any, U any, C any, CT any, TF Context[CT]](m *meta[T, U, C, CT, TF], max time.Time) (result *list.Element, isBreak bool) { | ||
if m.initialConcurrency < m.running.Load() { | ||
box := findTask[T, U, C, CT, TF](m, RunningTask) | ||
if box != nil { | ||
return box, true | ||
} | ||
} | ||
if m.createTS.Before(max) { | ||
box := findTask[T, U, C, CT, TF](m, RunningTask) | ||
if box != nil { | ||
return box, false | ||
} | ||
} | ||
return nil, false | ||
} | ||
|
||
func canBoost[T any, U any, C any, CT any, TF Context[CT]](m *meta[T, U, C, CT, TF], min time.Time) (result *list.Element, isBreak bool) { | ||
if m.running.Load() >= m.initialConcurrency { | ||
return nil, true | ||
} | ||
if m.createTS.After(min) { | ||
box := getTask[T, U, C, CT, TF](m) | ||
if box != nil { | ||
return box, false | ||
} | ||
} | ||
return nil, false | ||
} | ||
|
||
func findTask[T any, U any, C any, CT any, TF Context[CT]](m *meta[T, U, C, CT, TF], status int32) *list.Element { | ||
for e := m.stats.Front(); e != nil; e = e.Next() { | ||
box := e.Value.(tContainer[T, U, C, CT, TF]) | ||
if box.task.status.Load() == status { | ||
return e | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func getTask[T any, U any, C any, CT any, TF Context[CT]](m *meta[T, U, C, CT, TF]) *list.Element { | ||
e := m.stats.Front() | ||
if e != nil { | ||
return e | ||
} | ||
return nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package pooltask | ||
|
||
// Overclock is to increase the concurrency of pool. | ||
func (t *TaskManager[T, U, C, CT, TF]) Overclock() (tid uint64, task *TaskBox[T, U, C, CT, TF]) { | ||
if t.concurrency > t.running.Load() { | ||
return t.getBoostTask() | ||
} | ||
return 0, nil | ||
} | ||
|
||
// Downclock is to decrease the concurrency of pool. | ||
func (t *TaskManager[T, U, C, CT, TF]) Downclock() { | ||
t.pauseTask() | ||
} |
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
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.
If here we call
canBoost
, and we returnnil, true
https://github.com/pingcap/tidb/pull/40670/files#diff-f1e67ff3fe9cec68e358a2e0ab7bc91ecca0dfcee2e9827282a0b6c060449a64R101. ThennewResult
is nil, is this expected and why?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.
Fixed, it is a misjudgment.