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

feat(stream): add approx percentile cache for global approx percentile #18026

Merged
merged 13 commits into from
Aug 16, 2024

Conversation

kwannoel
Copy link
Contributor

I hereby agree to the terms of the RisingWave Labs, Inc. Contributor License Agreement.

What's changed and what's your intention?

Just load the entire state into memory on bootstrap. When processing chunks, we will immediately persist the updates, rather than waiting for barrier to write to state tables, to avoid contributing to barrier latency. On barrier, we will just scan the cache and output the percentile agg.

Other improvements

  1. We delete buckets once their counts reach 0 to save space.
  2. We keep old output in memory. If there's no change in the output of the approx percentile, we can just clone and dispatch the old output rather than do a table scan and rediscover the approx percentile.

Checklist

  • I have written necessary rustdoc comments
  • I have added necessary unit tests and integration tests
  • I have added test labels as necessary. See details.
  • I have added fuzzing tests or opened an issue to track them. (Optional, recommended for new SQL features Sqlsmith: Sql feature generation #7934).
  • My PR contains breaking changes. (If it deprecates some features, please create a tracking issue to remove them in the future).
  • All checks passed in ./risedev check (or alias, ./risedev c)
  • My PR changes performance-critical code. (Please run macro/micro-benchmarks and show the results.)
  • My PR contains critical fixes that are necessary to be merged into the latest release. (Please check out the details)

Documentation

  • My PR needs documentation updates. (Please use the Release note section below to summarize the impact on users)

Release note

If this PR includes changes that directly affect users or other significant modifications relevant to the community, kindly draft a release note to provide a concise summary of these changes. Please prioritize highlighting the impact these changes will have on users.

@kwannoel
Copy link
Contributor Author

Seems like performance got worse.......

@kwannoel kwannoel marked this pull request as draft August 13, 2024 09:42
@kwannoel kwannoel marked this pull request as ready for review August 13, 2024 09:48
@kwannoel
Copy link
Contributor Author

Let me test it with fs storage rather than in-memory.

@graphite-app graphite-app bot requested a review from a team August 13, 2024 10:17
@kwannoel kwannoel force-pushed the kwannoel/approx-percentile-cache-v2 branch from 38f075a to 9e8bb23 Compare August 14, 2024 04:34
@kwannoel kwannoel force-pushed the kwannoel/approx-percentile-cache-v2 branch from 9e8bb23 to 67cda7c Compare August 14, 2024 07:09
@kwannoel kwannoel marked this pull request as draft August 14, 2024 07:59
@kwannoel kwannoel marked this pull request as ready for review August 15, 2024 03:24
@graphite-app graphite-app bot requested a review from a team August 15, 2024 03:50
@kwannoel
Copy link
Contributor Author

Screenshot 2024-08-16 at 12 14 20 PM

@kwannoel
Copy link
Contributor Author

kwannoel commented Aug 16, 2024

Performance improved 2x. I will polish up this PR and get it ready to be merged.

@kwannoel
Copy link
Contributor Author

Ready for review.

#[for_await]
for keyed_row in self
.bucket_state_table
.rev_iter_with_prefix(&[Datum::None; 0], &neg_bounds, PrefetchOptions::default())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask why we use rev iter here? IMO the self.cache.neg_buckets.insert will work regardless of iter order?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

// Update
impl<S: StateStore> GlobalApproxPercentileState<S> {
pub fn apply_chunk(&mut self, chunk: StreamChunk) -> StreamExecutorResult<()> {
for (_op, row) in chunk.rows() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ignoring op here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because from the upstream, the input is always an INSERT. We only care about the delta column of the row, which will update our counts accordingly. (e.g. -100, +10, +5, etc...)

I will add a comment on it.

It's a special way of processing stream chunks. The behaviour does not change from previous approx_percentile implementation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IC. May add a debug_assert here to ensure the correctness.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@kwannoel kwannoel requested a review from stdrc August 16, 2024 06:58
Copy link
Member

@stdrc stdrc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kwannoel kwannoel added this pull request to the merge queue Aug 16, 2024
Merged via the queue into main with commit 7b807cf Aug 16, 2024
31 of 32 checks passed
@kwannoel kwannoel deleted the kwannoel/approx-percentile-cache-v2 branch August 16, 2024 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants