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

Use monotonic time points in write_controller.cc and rate_limiter.cc #1865

Closed

Conversation

xiaofeidu008
Copy link

@xiaofeidu008 xiaofeidu008 commented Feb 13, 2017

summary: NowMicros() provides non-monotonic time. When wall clock is
synchronized or changed, the non-monotonicity time points will affect write rate
controllers. This patch changes write_controller.cc and rate_limiter.cc to use
monotonic time points.

virtual uint64_t NowMicrosMonotonic() {
return std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you follow the convention to rely on specific implementation for this? Just leave this one as NowNanos() / 1000.

Copy link
Author

Choose a reason for hiding this comment

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

Okay. I will do that

@@ -107,7 +107,10 @@ void GenericRateLimiter::Request(int64_t bytes, const Env::IOPriority pri) {
(!queue_[Env::IO_LOW].empty() &&
&r == queue_[Env::IO_LOW].front()))) {
leader_ = &r;
timedout = r.cv.TimedWait(next_refill_us_);
int64_t delta = next_refill_us_ - env_->NowMicrosMonotonic();
delta = delta > 0 ? delta : 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you still need to wait if delta is 0?

Copy link
Author

Choose a reason for hiding this comment

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

Will update this

@facebook-github-bot
Copy link
Contributor

@xiaofeidu008 updated the pull request - view changes

@facebook-github-bot
Copy link
Contributor

@xiaofeidu008 updated the pull request - view changes

@xiaofeidu008 xiaofeidu008 changed the title Add NowMicrosMonotonic() and update write_controller.cc and rate_limiter.cc Use monotonic time points in write_controller.cc and rate_limiter.cc Feb 14, 2017
@facebook-github-bot
Copy link
Contributor

@siying has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@siying siying left a comment

Choose a reason for hiding this comment

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

It looks great!

Are you going to add comment in Env::NowNanos() saying monotonic clock is recommanded?

Let me know when are you ready to merge. I'll merge it as soon as you are ready.

@@ -61,6 +61,9 @@ class GenericRateLimiter : public RateLimiter {
private:
void Refill();
int64_t CalculateRefillBytesPerPeriod(int64_t rate_bytes_per_sec);
uint64_t NowMicrosMonotonic(Env* env) {
return env->NowNanos() / std::milli::den;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally the two NowMicrosMonotonic() functions should be consolidated as one helper function. We don't have a good place to place it. If you want, you can create util/env_util.h to host a file there. If it is too complicated to you, you can leave it as it is.

Copy link
Author

@xiaofeidu008 xiaofeidu008 Feb 14, 2017

Choose a reason for hiding this comment

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

I looked for that place and could not find one when creating the diff. This is a one-liner function (kinda like a macro). So it's fine to leave it as it is. In the future, if the number of these utility functions grow, we should add some utility files.

summary: NowMicros() provides non-monotonic time. When wall clock is
synchronized or changed, the non-monotonicity time points will affect write rate
controllers. This patch changes write_controller.cc and rate_limiter.cc to use
monotonic time points.
@facebook-github-bot
Copy link
Contributor

@xiaofeidu008 updated the pull request - view changes - changes since last import

@xiaofeidu008
Copy link
Author

@siying I have added comment for NowNanos(). You may merge anytime you want. I don't think I have permission to merge :-)

@facebook-github-bot
Copy link
Contributor

@siying has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

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.

None yet

3 participants