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
quiche:: Implement QuicAlarm interface #7094
Conversation
Signed-off-by: Dan Zhang <danzh@google.com>
Signed-off-by: Dan Zhang <danzh@google.com>
/assign @wu-bin |
|
||
protected: | ||
Event::SimulatedTimeSystemHelper time_system_; | ||
EnvoyQuicClock clock_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where does this come from? I am not seeing it in the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in #7028
namespace Envoy { | ||
namespace Quic { | ||
|
||
class EnvoyQuicAlarm : public quic::QuicAlarm { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably worth some class comments here and in other classes. I can tell you are trying to mate two different time-systems and it may be worth a whiteboard discussion on the strategy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added comment. The current plan is to use TimeSystem to implement QuicTime and Timer to implement QuicAlarm. These two are quite straight forward integration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. Consider using TimeSource rather than TimeSystem if possible. The only user of TimeSystem outside of tests currently is DIspatcher and we'd prefer to keep it that way if possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
QuicClock has a ApproximateNow() which needs to interact with Dispatcher to get a timestamp in each event loop. This is not implemented yet, see TODO in EnvoyQuicClock.
TimeSystem has createScheduler() which allows us to plumb through to pass around that timestamp. So I use TimeSystem for QuicTime.
And EnvoyQuicClock will be instantiated with the TimeSystem in Dispatcher.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the semantics of ApproximateNow()? How would it interact with the dispatcher? Might be worth a quick write-up to explain your plans. Maybe discuss in the context of a github issue, and reference that issue here in a code comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
opened #7177. Since alarm is not directly related to QuicClock, I didn't reference the issue here. I think the original TODO in QuicClock is sufficient.
include/envoy/event/timer.h
Outdated
virtual void enableTimer(const std::chrono::milliseconds& d) PURE; | ||
virtual void enableTimerInUs(const std::chrono::microseconds& d) PURE; | ||
|
||
virtual void enableTimer(const std::chrono::milliseconds& d) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you are trying to do but I have a slightly different suggestion. See test/test_common/test_time_system.h and the definitions of sleep() for the pattern. It references TimeSystem::Duration which is defined in include/envoy/event/timer.h, and does not tie the caller to any particular unit.
That way it can be up to the caller whether to specify milliseconds or microseconds or whatever.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you suggesting adding another member function like void enableTimerAfterDuration(Duration d)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but I was suggesting naming it enableTimer(Duration d) as an inline template method, to avoid having to change any existing callsites.
Look at the pattern in test_time_system.h.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can template method be virtual?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I misunderstood your suggestion. Please ignore my previous question.
You mean change Timer interface to be something as below right?
virtual void enableTimer(const MonotonicTime::duration& d) PURE;
template void enableTimer(const D& duration) {
enableTimer(std::chrono::duration_castMonotonicTime::duration(duration));
}
But doing so still requires changes to all EXPECT_CALL(timer, enableTime(some_duration)). I don't have strong preference to this template approach v.s. adding enableTimerInUs(). If the refactor takes some large scope change, I would prefer the former one actually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The difference is that we still keep the old enableTimer(const std::chrono::milliseconds&) there. Is that ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think that'd be OK if it works. I've had difficulties in the past overriding one of two overloaded virtual methods, which I think is what we would happen with mocks. It's worth trying.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway I wind to wind up in a state where there's one enableTimer entry that takes a Duration rather than having variants for different time units. It might be simplest to start a separate PR that makes that change, including some boring changes to mocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to single enableTimer taking a Duration. It seems to have higher resolution than milliseconds and low resolution duration types(e.g. std::chrono::seconds) can implicitly convert to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree to do it in separate PR. Filed #6588 for tracking.
I talked to Bin and Ian offline, having milliseconds granularity is fine for now. In QUICHE code, QuicAlarm has always been scheduled with (Now + some milliseconds) as RTT is usually in ms granularity. The only exception comes when it is scheduled with Now in which case casting now to milliseconds doesn't change the expected behavior. So I will use existing enbableTimer() for now.
namespace Envoy { | ||
namespace Quic { | ||
|
||
class EnvoyQuicAlarmFactory : public quic::QuicAlarmFactory { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inherit from NonCopyable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
timer_->enableTimerInUs(std::chrono::microseconds(getDurationBeforeDeadline().ToMicroseconds())); | ||
} | ||
|
||
void EnvoyQuicAlarm::UpdateImpl() { SetImpl(); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If an alarm is Set() at time_1 and Update() at time_2, will it fire twice in both times?
In any case, it's not obvious that simply calling SetImpl() will cancel the previously set alarm, a comment will be helpful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
alarm1->Set(clock_.Now() + QuicTime::Delta::FromMicroseconds(10)); | ||
EXPECT_TRUE(alarm1->IsSet()); | ||
auto unowned_delegate2 = new TestDelegate(); | ||
quic::QuicArenaScopedPtr<quic::QuicAlarm> alarm2(alarm_factory_.CreateAlarm(unowned_delegate2)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: alarm2 not needed for this test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alarm2 is needed to verify that a not-cancelled alarm should have fired.
quic::QuicArenaScopedPtr<quic::QuicAlarm> alarm1(alarm_factory_.CreateAlarm(unowned_delegate1)); | ||
alarm1->Set(clock_.Now() + QuicTime::Delta::FromMicroseconds(10)); | ||
auto unowned_delegate2 = new TestDelegate(); | ||
quic::QuicArenaScopedPtr<quic::QuicAlarm> alarm2(alarm_factory_.CreateAlarm(unowned_delegate2)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: alarm2 not needed for this test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alarm2 is needed to verify that a not-reset alarm shouldn't have fired.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the behavior of alarm2 in this test is the same as in the previous test, if you separate it out then it can be shared. But again your call.
bool fired_; | ||
}; | ||
|
||
class EnvoyQuicAlarmTest : public ::testing::Test { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some alarm library "eagerly fires" when a alarm deadline is <= now. It seems like this implementation doesn't do that, can you add a test to ensure that behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does "eagerly fire" mean? Current implementation do schedule the alarm in same event loop. Added test FireAlarmWithPastDeadline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the test:)
By "eagerly fire" I mean if you schedule an alarm to fire "in the past", the schedule api will call the Fire() callback before it returns, for such alarms we need to make sure the callback is safe to run in both the calling thread and the event loop.
Signed-off-by: Dan Zhang <danzh@google.com>
include/envoy/event/timer.h
Outdated
virtual void enableTimer(const std::chrono::milliseconds& d) PURE; | ||
virtual void enableTimerInUs(const std::chrono::microseconds& d) PURE; | ||
|
||
virtual void enableTimer(const std::chrono::milliseconds& d) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think changing all EXPECT_CALLs is fine, but I'm not a big fan of using the same name for the virtual and template functions, it just feel very wired.
Long term, I'd suggest we use a duration type that does not imply the unit of time, like absl::Duration, for the enableTimer interface, but it's out of the scope of this PR.
bool fired_; | ||
}; | ||
|
||
class EnvoyQuicAlarmTest : public ::testing::Test { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the test:)
By "eagerly fire" I mean if you schedule an alarm to fire "in the past", the schedule api will call the Fire() callback before it returns, for such alarms we need to make sure the callback is safe to run in both the calling thread and the event loop.
quic::QuicArenaScopedPtr<quic::QuicAlarm> alarm1(alarm_factory_.CreateAlarm(unowned_delegate1)); | ||
alarm1->Set(clock_.Now() + QuicTime::Delta::FromMicroseconds(10)); | ||
auto unowned_delegate2 = new TestDelegate(); | ||
quic::QuicArenaScopedPtr<quic::QuicAlarm> alarm2(alarm_factory_.CreateAlarm(unowned_delegate2)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the behavior of alarm2 in this test is the same as in the previous test, if you separate it out then it can be shared. But again your call.
auto unowned_delegate = new TestDelegate(); | ||
quic::QuicArenaScopedPtr<quic::QuicAlarm> alarm(alarm_factory_.CreateAlarm(unowned_delegate)); | ||
alarm->Set(clock_.Now() - QuicTime::Delta::FromMicroseconds(10)); | ||
base_scheduler_.run(Dispatcher::RunType::NonBlock); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you ensure that fired() is false before base_scheduler_.run?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
EXPECT_TRUE(unowned_delegate->fired()); | ||
} | ||
|
||
TEST_F(EnvoyQuicAlarmTest, FireAlarmWithPastDeadline) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is identical to SetAlarmToPastTime. Maybe change it to UpdateAlarmToPastTime and test that Update() to a past time will fire the alarm immediately?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
quic::QuicArenaScopedPtr<quic::QuicAlarm> alarm(alarm_factory_.CreateAlarm(unowned_delegate)); | ||
// alarm becomes active upon Set(). | ||
alarm->Set(clock_.Now() - QuicTime::Delta::FromMicroseconds(10)); | ||
base_scheduler_.run(Dispatcher::RunType::NonBlock); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you ensure that fired() is false before base_scheduler_.run?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Signed-off-by: Dan Zhang <danzh@google.com>
Signed-off-by: Dan Zhang <danzh@google.com>
Signed-off-by: Dan Zhang <danzh@google.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for separating out the timer resolution issue in #7170. This PR is much easier to review now:)
EnvoyQuicAlarm(Event::Scheduler& scheduler, quic::QuicClock& clock, | ||
quic::QuicArenaScopedPtr<quic::QuicAlarm::Delegate> delegate); | ||
|
||
~EnvoyQuicAlarm() override{}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Can we check that IsSet() is false in destructor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Signed-off-by: Dan Zhang <danzh@google.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice. I'm not sure I fully understand the semantics of the Quic library you are mating too, so I'm good with @wu-bin's review there. Envoy side LGTM.
EnvoyQuicAlarm::EnvoyQuicAlarm(Event::Scheduler& scheduler, quic::QuicClock& clock, | ||
quic::QuicArenaScopedPtr<quic::QuicAlarm::Delegate> delegate) | ||
: QuicAlarm(std::move(delegate)), scheduler_(scheduler), clock_(clock) { | ||
timer_ = scheduler_.createTimer([this]() { Fire(); }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: use constructor initializer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
void EnvoyQuicAlarm::CancelImpl() { timer_->disableTimer(); } | ||
|
||
void EnvoyQuicAlarm::SetImpl() { | ||
// TODO switch to use microseconds after issue #7170 is addressed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: TODO(#7170)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[fixed typo -> 7180 --> 7170]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
TestDelegate() : fired_(false) {} | ||
|
||
void OnAlarm() override { fired_ = true; } | ||
bool fired() const { return fired_; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this also an override?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fired() is not.
@envoyproxy/senior-maintainers over to you. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM other than remaining @jmarantz nits.
/wait
Signed-off-by: Dan Zhang <danzh@google.com>
/retest |
🔨 rebuilding |
/retest |
🔨 rebuilding |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
#pragma once | ||
|
||
// QUICHE allows unused parameters. | ||
#pragma GCC diagnostic ignored "-Wunused-parameter" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you do GCC diagnostic push
/ pop
respectively before and after quiche imports? So the check still applies to envoy code base.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Signed-off-by: Dan Zhang <danzh@google.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
Implement EnvoyQuicAlarm and EnvoyQuicAlarmFactory. They are backed by Event::Scheduler.
Added a new interface enableTimerInUs() in Timer to allow smaller granularity.
Risk Level: middle, Timer::enableTimer() behavior is changed.
Testing: Added tests under test/extensions/quic_listeners/quiche:envoy_quic_alarm_test; enableTimerInUs() covered by existing envoy tests.
Part of #2557