Skip to content

Commit

Permalink
schedule: move the logic of patrol region from coordinator to checkers (
Browse files Browse the repository at this point in the history
#8428)

ref #7963

Signed-off-by: lhy1024 <[email protected]>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
lhy1024 and ti-chi-bot[bot] authored Aug 1, 2024
1 parent 45dac53 commit 3ce31ef
Show file tree
Hide file tree
Showing 14 changed files with 305 additions and 256 deletions.
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

0 comments on commit 3ce31ef

Please sign in to comment.