-
Notifications
You must be signed in to change notification settings - Fork 288
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
kv/client: add incremental scan region count limit #1926
Merged
ti-chi-bot
merged 8 commits into
pingcap:release-4.0
from
amyangfei:chery-pick-4.0-region-scan-limit
Jun 9, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2ce2580
kv/client: add incremental scan region count limit
amyangfei 2819a96
fix region token not release
amyangfei e802309
Merge branch 'release-4.0' into chery-pick-4.0-region-scan-limit
amyangfei 2b6fae3
fix unit test
amyangfei 5c2d80b
Update cdc/kv/client.go
overvenus 8ef495a
Merge branch 'release-4.0' into chery-pick-4.0-region-scan-limit
ti-chi-bot f881572
fix unit test
amyangfei 189a367
Merge branch 'release-4.0' into chery-pick-4.0-region-scan-limit
ti-chi-bot 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,163 @@ | ||
// Copyright 2021 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 kv | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/pingcap/errors" | ||
"github.com/pingcap/ticdc/pkg/util" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
const ( | ||
// buffer size for ranged region consumer | ||
regionRouterChanSize = 16 | ||
// sizedRegionRouter checks region buffer every 100ms | ||
sizedRegionCheckInterval = 100 * time.Millisecond | ||
) | ||
|
||
// LimitRegionRouter defines an interface that can buffer singleRegionInfo | ||
// and provide token based consumption | ||
type LimitRegionRouter interface { | ||
// Chan returns a singleRegionInfo channel that can be consumed from | ||
Chan() <-chan singleRegionInfo | ||
// AddRegion adds an singleRegionInfo to buffer, this function is thread-safe | ||
AddRegion(task singleRegionInfo) | ||
// Acquire acquires one token | ||
Acquire(id string) | ||
// Release gives back one token, this function is thread-safe | ||
Release(id string) | ||
// Run runs in background and does some logic work | ||
Run(ctx context.Context) error | ||
} | ||
|
||
type srrMetrics struct { | ||
changefeed string | ||
table string | ||
tokens map[string]prometheus.Gauge | ||
} | ||
|
||
func newSrrMetrics(ctx context.Context) *srrMetrics { | ||
changefeed := util.ChangefeedIDFromCtx(ctx) | ||
_, table := util.TableIDFromCtx(ctx) | ||
return &srrMetrics{ | ||
changefeed: changefeed, | ||
table: table, | ||
tokens: make(map[string]prometheus.Gauge), | ||
} | ||
} | ||
|
||
type sizedRegionRouter struct { | ||
buffer map[string][]singleRegionInfo | ||
output chan singleRegionInfo | ||
lock sync.Mutex | ||
metrics *srrMetrics | ||
tokens map[string]int | ||
sizeLimit int | ||
} | ||
|
||
// NewSizedRegionRouter creates a new sizedRegionRouter | ||
func NewSizedRegionRouter(ctx context.Context, sizeLimit int) *sizedRegionRouter { | ||
return &sizedRegionRouter{ | ||
buffer: make(map[string][]singleRegionInfo), | ||
output: make(chan singleRegionInfo, regionRouterChanSize), | ||
sizeLimit: sizeLimit, | ||
tokens: make(map[string]int), | ||
metrics: newSrrMetrics(ctx), | ||
} | ||
} | ||
|
||
func (r *sizedRegionRouter) Chan() <-chan singleRegionInfo { | ||
return r.output | ||
} | ||
|
||
func (r *sizedRegionRouter) AddRegion(sri singleRegionInfo) { | ||
r.lock.Lock() | ||
var id string | ||
// if rpcCtx is not provided, use the default "" bucket | ||
if sri.rpcCtx != nil { | ||
id = sri.rpcCtx.Addr | ||
} | ||
if r.sizeLimit > r.tokens[id] && len(r.output) < regionRouterChanSize { | ||
r.output <- sri | ||
} else { | ||
r.buffer[id] = append(r.buffer[id], sri) | ||
} | ||
r.lock.Unlock() | ||
} | ||
|
||
func (r *sizedRegionRouter) Acquire(id string) { | ||
r.lock.Lock() | ||
defer r.lock.Unlock() | ||
r.tokens[id]++ | ||
if _, ok := r.metrics.tokens[id]; !ok { | ||
r.metrics.tokens[id] = clientRegionTokenSize.WithLabelValues(id, r.metrics.table, r.metrics.changefeed) | ||
} | ||
r.metrics.tokens[id].Inc() | ||
} | ||
|
||
func (r *sizedRegionRouter) Release(id string) { | ||
r.lock.Lock() | ||
defer r.lock.Unlock() | ||
r.tokens[id]-- | ||
if _, ok := r.metrics.tokens[id]; !ok { | ||
r.metrics.tokens[id] = clientRegionTokenSize.WithLabelValues(id, r.metrics.table, r.metrics.changefeed) | ||
} | ||
r.metrics.tokens[id].Dec() | ||
} | ||
|
||
func (r *sizedRegionRouter) Run(ctx context.Context) error { | ||
ticker := time.NewTicker(sizedRegionCheckInterval) | ||
defer ticker.Stop() | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return errors.Trace(ctx.Err()) | ||
case <-ticker.C: | ||
r.lock.Lock() | ||
for id, buf := range r.buffer { | ||
available := r.sizeLimit - r.tokens[id] | ||
// the tokens used could be more then size limit, since we have | ||
// a sized channel as level1 cache | ||
if available <= 0 { | ||
continue | ||
} | ||
if available > len(buf) { | ||
available = len(buf) | ||
} | ||
// to avoid deadlock because when consuming from the output channel. | ||
// onRegionFail could decrease tokens, which requires lock protection. | ||
if available > regionRouterChanSize-len(r.output) { | ||
available = regionRouterChanSize - len(r.output) | ||
} | ||
if available == 0 { | ||
continue | ||
} | ||
for i := 0; i < available; i++ { | ||
select { | ||
case <-ctx.Done(): | ||
r.lock.Unlock() | ||
return errors.Trace(ctx.Err()) | ||
case r.output <- buf[i]: | ||
} | ||
} | ||
r.buffer[id] = r.buffer[id][available:] | ||
} | ||
r.lock.Unlock() | ||
} | ||
} | ||
} |
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.
Note the fix for this bug should be picked to master @amyangfei