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

schedule: move the logic of patrol region from coordinator to checkers #8428

Merged
merged 7 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 5 additions & 5 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,23 +371,23 @@ func TestPriorityQueue(t *testing.T) {
pq.Remove(uint64(1))
re.Nil(pq.Get(1))
re.Equal(2, pq.Len())
entry := pq.Peek()
entry := pq.peek()
re.Equal(2, entry.Priority)
re.Equal(testData[2], entry.Value)

// case3 update 3's priority to highest
pq.Put(-1, testData[3])
entry = pq.Peek()
entry = pq.peek()
re.Equal(-1, entry.Priority)
re.Equal(testData[3], entry.Value)
pq.Remove(entry.Value.ID())
re.Equal(testData[2], pq.Peek().Value)
re.Equal(testData[2], pq.peek().Value)
re.Equal(1, pq.Len())

// case4 remove all element
pq.Remove(uint64(2))
re.Equal(0, pq.Len())
re.Empty(pq.items)
re.Nil(pq.Peek())
re.Nil(pq.Tail())
re.Nil(pq.peek())
re.Nil(pq.tail())
}
32 changes: 17 additions & 15 deletions pkg/cache/priority_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ import (
"github.com/tikv/pd/pkg/btree"
)

// defaultDegree default btree degree, the depth is h<log(degree)(capacity+1)/2
// defaultDegree is default btree degree, the depth is h<log(degree)(capacity+1)/2.
const defaultDegree = 4

// PriorityQueue queue has priority and preempt
// PriorityQueue queue has priority and preempt.
type PriorityQueue struct {
items map[uint64]*Entry
btree *btree.BTreeG[*Entry]
capacity int
}

// NewPriorityQueue construct of priority queue
// NewPriorityQueue constructs of priority queue.
func NewPriorityQueue(capacity int) *PriorityQueue {
return &PriorityQueue{
items: make(map[uint64]*Entry),
Expand All @@ -37,12 +37,12 @@ func NewPriorityQueue(capacity int) *PriorityQueue {
}
}

// PriorityQueueItem avoid convert cost
// PriorityQueueItem avoids convert cost.
type PriorityQueueItem interface {
ID() uint64
}

// Put put value with priority into queue
// Put puts value with priority into queue.
func (pq *PriorityQueue) Put(priority int, value PriorityQueueItem) bool {
id := value.ID()
entry, ok := pq.items[id]
Expand All @@ -66,28 +66,30 @@ func (pq *PriorityQueue) Put(priority int, value PriorityQueueItem) bool {
return true
}

// Get find entry by id from queue
// Get finds entry by id from queue.
func (pq *PriorityQueue) Get(id uint64) *Entry {
return pq.items[id]
}

// Peek return the highest priority entry
func (pq *PriorityQueue) Peek() *Entry {
// peek returns the highest priority entry.
// It only is used for test.
func (pq *PriorityQueue) peek() *Entry {
if max, ok := pq.btree.Max(); ok {
return max
}
return nil
}

// Tail return the lowest priority entry
func (pq *PriorityQueue) Tail() *Entry {
// tail returns the lowest priority entry.
// It only is used for test.
func (pq *PriorityQueue) tail() *Entry {
if min, ok := pq.btree.Min(); ok {
return min
}
return nil
}

// Elems return all elements in queue
// Elems returns all elements in queue.
func (pq *PriorityQueue) Elems() []*Entry {
rs := make([]*Entry, pq.Len())
count := 0
Expand All @@ -99,26 +101,26 @@ func (pq *PriorityQueue) Elems() []*Entry {
return rs
}

// Remove remove value from queue
// Remove removes value from queue.
func (pq *PriorityQueue) Remove(id uint64) {
if v, ok := pq.items[id]; ok {
pq.btree.Delete(v)
delete(pq.items, id)
}
}

// Len return queue size
// Len returns queue size.
func (pq *PriorityQueue) Len() int {
return pq.btree.Len()
}

// Entry a pair of region and it's priority
// Entry is a pair of region and its priority.
type Entry struct {
Priority int
Value PriorityQueueItem
}

// Less return true if the entry has smaller priority
// Less returns true if the entry has smaller priority.
func (r *Entry) Less(other *Entry) bool {
left := r.Priority
right := other.Priority
Expand Down
Loading