Skip to content

rate-limiting: Add ScheduledStartingRateLimiter#281

Merged
mum4k merged 5 commits intoenvoyproxy:masterfrom
oschaaf:scheduled-starting-rate-limiter
Jan 15, 2020
Merged

rate-limiting: Add ScheduledStartingRateLimiter#281
mum4k merged 5 commits intoenvoyproxy:masterfrom
oschaaf:scheduled-starting-rate-limiter

Conversation

@oschaaf
Copy link
Copy Markdown
Member

@oschaaf oschaaf commented Jan 15, 2020

As per discussion [1], this is intended to be used as a replacement
for the way we offset worker request release timings today.

[1] #219 (comment)

Signed-off-by: Otto van der Schaaf oschaaf@we-amp.com

As per discussion [1], this is intended to be used as a replacement
for the way we offset worker request release timings today.

[1] envoyproxy#219 (comment)

Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
@oschaaf oschaaf added P2 waiting-for-review A PR waiting for a review. labels Jan 15, 2020
@oschaaf oschaaf mentioned this pull request Jan 15, 2020
1 task
Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
@mum4k mum4k requested a review from dubious90 January 15, 2020 15:05
@mum4k
Copy link
Copy Markdown
Collaborator

mum4k commented Jan 15, 2020

@dubious90 please review and assign to me once done.

Copy link
Copy Markdown
Contributor

@dubious90 dubious90 left a comment

Choose a reason for hiding this comment

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

This way of implementing this feature makes a lot of sense to me. Thanks for putting this change together. A few small comments/questions.

.Times(AtLeast(1))
.WillRepeatedly(Return(true));

if (starting_late) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure if starting_late is adding any extra coverage for this test case. This test should already be testing what happens when enough time has passed and when not enough time has passed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not sure I follow; the intent here is to skip the scheduled delay entirely, so the first call to acquireOne() will hit the line that logs the warning that the "first acquisition attempt was late":

https://github.com/envoyproxy/nighthawk/pull/281/files/de66b5e48997d31728125385f33265bfb62094df#diff-698caf1e153397f66dc8ca94b64a391fR72

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, okay. I didn't process the comments in line 74-76 properly when I read this code the first time. I follow now.

Is there a way to assert that the log line was actually hit? Or is this just for line code coverage?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is just for coverage. Unfortunately we can't really mock the logging today and I couldn't find an efficient way to assert on hitting that line

}

// We should expect zero releases until it is time to start.
while (time_system.monotonicTime() < scheduled_starting_time) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If this test case doesn't succeed, it will run infinitely rather than fail. Can you add in a tripping mechanism?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The simulated time system here is guaranteed to move forward in this loop with 1ms steps we add via the call to time_system.sleep(1ms) below.. afaict this is guaranteed to terminate?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nevermind here. I was overcomplicated the situation in my head. Sorry about that!

// about this happening in the logs.
if (!aquisition_attempted_) {
aquisition_attempted_ = true;
ENVOY_LOG(warn, "First acquisition attempt was late");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we add in an identifier here, so this message makes more sense for a debugger who might not have context into this specific rate limiter?
Something like
ScheduledStartingRateLimiter first acquisition attempt was late

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

}

void ScheduledStartingRateLimiter::releaseOne() {
if (timeSource().monotonicTime() < scheduled_starting_time_) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm probably missing something, but couldn't a cancel operation (not yet implemented) result in releasing before the time arrives? What are we trying to prevent by failing here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Right now, it is only valid to release what has been acquired. So when we hit this, it ought to be impossible to have a preceding successful acquisition because we're not due to start yet. The intent is to catch programming mistakes early when consuming this, but possibly this is overly paranoid.. shall I remove this?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This explanation makes sense to me. I don't think it's necessary to remove this.

@dubious90 dubious90 added waiting-for-changes A PR waiting for comments to be resolved and changes to be applied. and removed waiting-for-review A PR waiting for a review. labels Jan 15, 2020
Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
@oschaaf oschaaf added waiting-for-review A PR waiting for a review. and removed waiting-for-changes A PR waiting for comments to be resolved and changes to be applied. labels Jan 15, 2020
Copy link
Copy Markdown
Contributor

@dubious90 dubious90 left a comment

Choose a reason for hiding this comment

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

Just one small followup left. In parallel assigning to mum4k for approval

}

void ScheduledStartingRateLimiter::releaseOne() {
if (timeSource().monotonicTime() < scheduled_starting_time_) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This explanation makes sense to me. I don't think it's necessary to remove this.

.Times(AtLeast(1))
.WillRepeatedly(Return(true));

if (starting_late) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, okay. I didn't process the comments in line 74-76 properly when I read this code the first time. I follow now.

Is there a way to assert that the log line was actually hit? Or is this just for line code coverage?

}

// We should expect zero releases until it is time to start.
while (time_system.monotonicTime() < scheduled_starting_time) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nevermind here. I was overcomplicated the situation in my head. Sorry about that!

…g-rate-limiter

Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
@mum4k mum4k merged commit 403be21 into envoyproxy:master Jan 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 waiting-for-review A PR waiting for a review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants