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: Implement linger for AppendRecordStream #48

Closed
wants to merge 6 commits into from

Conversation

vrongmeal
Copy link
Member

Resolves: #37

@vrongmeal vrongmeal requested a review from a team as a code owner November 8, 2024 09:09
@vrongmeal
Copy link
Member Author

Do we want to make tokio optional here?

@shikhar
Copy link
Member

shikhar commented Nov 8, 2024

Do we want to make tokio optional here?

Tonic has a hard dependency on tokio right? So it's ok...

@vrongmeal
Copy link
Member Author

Tonic has a hard dependency on tokio right? So it's ok...

Yeah, that's what I thought!

src/streams.rs Outdated Show resolved Hide resolved
src/streams.rs Outdated Show resolved Hide resolved
Copy link
Member

@shikhar shikhar left a comment

Choose a reason for hiding this comment

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

Can you add a unit test exercising the combination of triggers - count, size, and linger. Using a paused tokio runtime makes it easy to advance time by doing a sleep (https://docs.rs/tokio/latest/tokio/time/fn.pause.html#auto-advance).

Also, can we try to use a Sleep directly instead of Interval? I think it can be easier to reason about.

Signed-off-by: Vaibhav Rabber <[email protected]>
Signed-off-by: Vaibhav Rabber <[email protected]>
Signed-off-by: Vaibhav Rabber <[email protected]>
@vrongmeal vrongmeal force-pushed the vrongmeal/linger-time branch from 5dc2a29 to b12765a Compare November 12, 2024 13:20
Signed-off-by: Vaibhav Rabber <[email protected]>
@vrongmeal vrongmeal changed the title feat: Implement linger_time for AppendRecordStream feat: Implement linger for AppendRecordStream Nov 12, 2024
@@ -30,6 +34,7 @@ impl Default for AppendRecordStreamOpts {
max_batch_size: ByteSize::mib(1),
match_seq_num: None,
fencing_token: None,
linger: None,
Copy link
Member

Choose a reason for hiding this comment

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

Let's default to 5ms (batching is generally helpful), and let it be disabled by making with_linger take an Option<Duration>.

@shikhar
Copy link
Member

shikhar commented Nov 12, 2024

Re: AppendRecordStreamError::BatchSizeTooLarge - I think this can be an assertion on with_max_batch_size, and we can remove that error.

BTW same goes for max_batch_records, which is required to be under 1000 by the service, but is not currently validated.

types,
};

#[rstest]
Copy link
Member

Choose a reason for hiding this comment

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

nice! can you run these in the CI?

Comment on lines +216 to +217
// Reset the linger sleep future since the old one is polled ready.
self.linger_sleep = self.opts.linger_sleep_fut();
Copy link
Member

Choose a reason for hiding this comment

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

Comment on lines +163 to +169
if self
.linger_sleep
.as_mut()
.is_some_and(|fut| fut.poll_unpin(cx).is_pending())
{
return Poll::Pending;
}
Copy link
Member

@shikhar shikhar Nov 13, 2024

Choose a reason for hiding this comment

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

The idea is that we want to trigger an append if any of these conditions can be satisfied:

  • max_batch_records accumulated from underlying stream
  • max_batch_size accumulated from underlying stream
  • linger sleep expired - and there is at least 1 accumulated record

So waiting upfront for linger time to expire when the underlying record stream could potentially satisfy the first 2 conditions, does not seem right to me...

BTW, I have a feeling this adaptor from AppendRecord to batched AppendInput given this combination of triggers may be clearest to implement using async_stream::stream!, and let that generate the state machine. Hand-written futures and streams tend to be hard to write correctly (and review)...

Copy link
Member Author

Choose a reason for hiding this comment

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

This bugged me at first thinking linger is used to rate limit in many cases but makes sense if it matches the other conditions.

Will use async_stream::stream in that case, would make it much clearer.

@vrongmeal
Copy link
Member Author

Also, the comments regarding opts suggests that I should make the fields in AppendRecordStreamOpts private so they are only accessed using the methods.

@vrongmeal
Copy link
Member Author

Going to close this PR in favour of #55 soon.

@vrongmeal vrongmeal closed this Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

linger time support for AppendRecordStream
3 participants