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

quiche:: Implement QuicAlarm interface #7094

Merged
merged 9 commits into from Jun 10, 2019

Conversation

danzh2010
Copy link
Contributor

@danzh2010 danzh2010 commented May 28, 2019

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

Signed-off-by: Dan Zhang <danzh@google.com>
Signed-off-by: Dan Zhang <danzh@google.com>
@danzh2010
Copy link
Contributor Author

/assign @wu-bin


protected:
Event::SimulatedTimeSystemHelper time_system_;
EnvoyQuicClock clock_;
Copy link
Contributor

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.

Copy link
Contributor Author

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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.

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) {
Copy link
Contributor

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.

Copy link
Contributor Author

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)?

Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

inherit from NonCopyable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@jmarantz jmarantz self-assigned this May 29, 2019
include/envoy/event/timer.h Outdated Show resolved Hide resolved
timer_->enableTimerInUs(std::chrono::microseconds(getDurationBeforeDeadline().ToMicroseconds()));
}

void EnvoyQuicAlarm::UpdateImpl() { SetImpl(); }
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

source/extensions/quic_listeners/quiche/envoy_quic_alarm.h Outdated Show resolved Hide resolved
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));
Copy link
Contributor

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.

Copy link
Contributor Author

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));
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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 {
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor

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 Show resolved Hide resolved
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) {
Copy link
Contributor

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 {
Copy link
Contributor

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));
Copy link
Contributor

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);
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 ensure that fired() is false before base_scheduler_.run?

Copy link
Contributor Author

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) {
Copy link
Contributor

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?

Copy link
Contributor Author

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);
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 ensure that fired() is false before base_scheduler_.run?

Copy link
Contributor Author

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>
@danzh2010
Copy link
Contributor Author

Ping? @wu-bin @jmarantz

wu-bin
wu-bin previously approved these changes Jun 6, 2019
Copy link
Contributor

@wu-bin wu-bin left a 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{};
Copy link
Contributor

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?

Copy link
Contributor Author

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>
jmarantz
jmarantz previously approved these changes Jun 7, 2019
Copy link
Contributor

@jmarantz jmarantz left a 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(); });
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: use constructor initializer.

Copy link
Contributor Author

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.
Copy link
Contributor

@jmarantz jmarantz Jun 7, 2019

Choose a reason for hiding this comment

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

nit: TODO(#7170)

Copy link
Contributor

Choose a reason for hiding this comment

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

[fixed typo -> 7180 --> 7170]

Copy link
Contributor Author

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_; }
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fired() is not.

@jmarantz
Copy link
Contributor

jmarantz commented Jun 7, 2019

@envoyproxy/senior-maintainers over to you.

Copy link
Member

@mattklein123 mattklein123 left a 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

@mattklein123 mattklein123 self-assigned this Jun 7, 2019
Signed-off-by: Dan Zhang <danzh@google.com>
@danzh2010
Copy link
Contributor Author

/retest

@repokitteh-read-only
Copy link

🔨 rebuilding ci/circleci: tsan (failed build)

🐱

Caused by: a #7094 (comment) was created by @danzh2010.

see: more, trace.

@danzh2010
Copy link
Contributor Author

/retest

@repokitteh-read-only
Copy link

🔨 rebuilding ci/circleci: coverage (failed build)

🐱

Caused by: a #7094 (comment) was created by @danzh2010.

see: more, trace.

mattklein123
mattklein123 previously approved these changes Jun 7, 2019
Copy link
Member

@mattklein123 mattklein123 left a 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"
Copy link
Member

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.

Copy link
Contributor Author

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>
Copy link
Member

@mattklein123 mattklein123 left a comment

Choose a reason for hiding this comment

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

Thanks

@mattklein123 mattklein123 merged commit 68020a3 into envoyproxy:master Jun 10, 2019
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.

None yet

6 participants