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

refactor(source): remove chunk splitting logic in apply_rate_limit #19826

Open
wants to merge 1 commit into
base: rc/respect-chunk-size-limit
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
35 changes: 18 additions & 17 deletions src/stream/src/executor/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,24 +148,25 @@ pub async fn apply_rate_limit(stream: BoxChunkSourceStream, rate_limit_rps: Opti
}

let limiter = limiter.as_ref().unwrap();
let limit = rate_limit_rps.unwrap() as usize;

let required_permits = compute_rate_limit_chunk_permits(&chunk, limit);
if required_permits <= limit {
let n = NonZeroU32::new(required_permits as u32).unwrap();
// `InsufficientCapacity` should never happen because we have check the cardinality
limiter.until_n_ready(n).await.unwrap();
yield chunk;
} else {
// Cut the chunk into smaller chunks
for chunk in chunk.split(limit) {
let n = NonZeroU32::new(compute_rate_limit_chunk_permits(&chunk, limit) as u32)
.unwrap();
// chunks split should have effective chunk size <= limit
limiter.until_n_ready(n).await.unwrap();
yield chunk;
}
let burst = rate_limit_rps.unwrap() as usize;

let mut required_permits = compute_rate_limit_chunk_permits(&chunk, burst);
if required_permits > burst {
// This should not happen after https://github.com/risingwavelabs/risingwave/pull/19698.
// But if it does happen, let's don't panic and just log an error.
tracing::error!(
chunk_size,
required_permits,
burst,
"unexpected large chunk size"
);
required_permits = burst;
}

let n = NonZeroU32::new(required_permits as u32).unwrap();
// `InsufficientCapacity` should never happen because we have check the cardinality
limiter.until_n_ready(n).await.unwrap();
yield chunk;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/stream/src/executor/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Execute for DummyExecutor {
}
}

pub fn compute_rate_limit_chunk_permits(chunk: &StreamChunk, limit: usize) -> usize {
pub fn compute_rate_limit_chunk_permits(chunk: &StreamChunk, burst: usize) -> usize {
let chunk_size = chunk.capacity();
let ends_with_update = if chunk_size >= 2 {
// Note we have to check if the 2nd last is `U-` to be consistenct with `StreamChunkBuilder`.
Expand All @@ -32,7 +32,7 @@ pub fn compute_rate_limit_chunk_permits(chunk: &StreamChunk, limit: usize) -> us
} else {
false
};
if chunk_size == limit + 1 && ends_with_update {
if chunk_size == burst + 1 && ends_with_update {
// If the chunk size exceed limit because of the last `Update` operation,
// we should minus 1 to make sure the permits consumed is within the limit (max burst).
chunk_size - 1
Expand Down
Loading