-
Notifications
You must be signed in to change notification settings - Fork 26
logpuller: add region task priority queue for subscription client #1660
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
Open
asddongmen
wants to merge
59
commits into
pingcap:master
Choose a base branch
from
asddongmen:impl-puller-prior-queue
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,865
−278
Open
Changes from 23 commits
Commits
Show all changes
59 commits
Select commit
Hold shift + click to select a range
b4dcf82
logpuller: add region task priority queue for subscription client
asddongmen 8b79ff9
adjust priority
asddongmen f3d9c1c
update
hongyunyan 1fd5241
Merge remote-tracking branch 'hyy/0818' into impl-puller-prior-queue
asddongmen e25972c
update
hongyunyan b7f18e5
Merge remote-tracking branch 'hyy/0818' into impl-puller-prior-queue
asddongmen d9fa3c9
puller: add region req cache
asddongmen 5b3a257
add config
asddongmen ffe838a
add metrics
asddongmen 533683a
puller: add ticker
asddongmen d15e8c0
puller: add denug log
asddongmen eb91e00
puller: force add high prior task
asddongmen 86d0694
adjust some paremeter
asddongmen 726fb15
Merge remote-tracking branch 'upstream/master' into impl-puller-prior…
asddongmen a2918a6
add more debug log
asddongmen b987418
add metrics
asddongmen 95926bf
adjust
asddongmen bcf95a8
add debug log
asddongmen e091010
fix bug
asddongmen 7aa97fa
fix bug 2
asddongmen 4f9c797
puller: add unit test
asddongmen d885810
puller: fix a bug
asddongmen 4faae24
puller: fix a bug 2
asddongmen 8df69ac
Merge remote-tracking branch 'upstream/master' into impl-puller-prior…
asddongmen 6b0bc1d
puller: fix a bug 3
asddongmen 86062b2
puller: add debug log
asddongmen 59274e7
puller: add debug log
asddongmen 45efe07
puller: add debug log 2
asddongmen ff4a5fc
puller: fix bug 2
asddongmen bd55041
puller: fix bug 3
asddongmen 1b75be6
puller: fix bug 4
asddongmen a357b7f
puller: fix bug 5
asddongmen 18a0f15
puller: adjust
asddongmen f02a947
puller: adjust 2
asddongmen 8a6d2e3
puller: adjust 3
asddongmen 8788d5c
puller: adjust 4
asddongmen 732ea58
adjust log
asddongmen ca806dc
adjust 3
asddongmen 799e770
adjust 4
asddongmen 4d286a6
adjust 5
asddongmen 7afd299
fix data race
asddongmen 46aed04
make fmt
asddongmen 9cebbc3
update grafana
asddongmen c3efdd7
fix ut
asddongmen d37ce1d
add ut
asddongmen 9ce395d
add ut
asddongmen 4213039
merge upstream
asddongmen ddb278f
fix metrics
asddongmen f8d6a0b
merge upstream master
asddongmen 7793669
adjust metrics
asddongmen 5057695
add subscribe region count metrics
asddongmen ce6b2e6
refine metrics
asddongmen a3d6112
fix some typo
asddongmen fe5548d
fix metrics
asddongmen 4f8f383
update
hongyunyan ca50036
update
hongyunyan e9b2e2d
puller: filter out negative metrics
asddongmen 346f8ab
update
hongyunyan 42532a3
Merge remote-tracking branch 'hyy/0905-2' into impl-puller-prior-queue
asddongmen 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 hidden or 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,122 @@ | ||
// Copyright 2024 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package logpuller | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/pingcap/ticdc/utils/heap" | ||
) | ||
|
||
// PriorityQueue is a thread-safe priority queue for region tasks | ||
// It integrates a signal channel to support blocking operations | ||
type PriorityQueue struct { | ||
mu sync.Mutex | ||
heap *heap.Heap[PriorityTask] | ||
|
||
// signal channel for blocking operations | ||
signal chan struct{} | ||
} | ||
|
||
// NewPriorityQueue creates a new priority queue | ||
func NewPriorityQueue() *PriorityQueue { | ||
return &PriorityQueue{ | ||
heap: heap.NewHeap[PriorityTask](), | ||
signal: make(chan struct{}, 1024), | ||
} | ||
} | ||
|
||
// Push adds a task to the priority queue and sends a signal | ||
// This is a non-blocking operation | ||
func (pq *PriorityQueue) Push(task PriorityTask) { | ||
pq.mu.Lock() | ||
pq.heap.AddOrUpdate(task) | ||
pq.mu.Unlock() | ||
|
||
// Send signal to notify waiting consumers | ||
select { | ||
case pq.signal <- struct{}{}: | ||
default: | ||
// Signal channel is full, ignore | ||
} | ||
} | ||
|
||
// Pop removes and returns the highest priority task | ||
// This is a blocking operation that waits for a signal | ||
// Returns nil if the context is cancelled | ||
func (pq *PriorityQueue) Pop(ctx context.Context) PriorityTask { | ||
for { | ||
// First try to pop without waiting | ||
pq.mu.Lock() | ||
task, ok := pq.heap.PopTop() | ||
pq.mu.Unlock() | ||
|
||
if ok { | ||
return task | ||
} | ||
|
||
// Queue is empty, wait for signal | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-pq.signal: | ||
// Got signal, try to pop again | ||
continue | ||
} | ||
} | ||
} | ||
|
||
// TryPop attempts to pop a task without blocking | ||
// Returns nil if the queue is empty | ||
func (pq *PriorityQueue) TryPop() PriorityTask { | ||
pq.mu.Lock() | ||
defer pq.mu.Unlock() | ||
|
||
task, ok := pq.heap.PopTop() | ||
if !ok { | ||
return nil | ||
} | ||
return task | ||
} | ||
|
||
// Peek returns the highest priority task without removing it | ||
// Returns nil if the queue is empty | ||
func (pq *PriorityQueue) Peek() PriorityTask { | ||
pq.mu.Lock() | ||
defer pq.mu.Unlock() | ||
|
||
task, ok := pq.heap.PeekTop() | ||
if !ok { | ||
return nil | ||
} | ||
return task | ||
} | ||
|
||
// Len returns the number of tasks in the queue | ||
func (pq *PriorityQueue) Len() int { | ||
pq.mu.Lock() | ||
defer pq.mu.Unlock() | ||
|
||
return pq.heap.Len() | ||
} | ||
|
||
// Close closes the signal channel | ||
func (pq *PriorityQueue) Close() { | ||
// pop all tasks | ||
for pq.Len() > 0 { | ||
pq.TryPop() | ||
} | ||
close(pq.signal) | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.