Skip to content

server: add an API for registering for notifications for server instance life…#6254

Merged
mattklein123 merged 6 commits intoenvoyproxy:masterfrom
eziskind:lifecycle
Mar 14, 2019
Merged

server: add an API for registering for notifications for server instance life…#6254
mattklein123 merged 6 commits intoenvoyproxy:masterfrom
eziskind:lifecycle

Conversation

@eziskind
Copy link
Copy Markdown
Contributor

…cycle events

Description: add an API to allow components and extensions to register for server instance lifecycle events (such as startup and shutdown).
Risk Level: low
Testing: unit tests
Docs Changes:
Release Notes:
Fixes #2802

Signed-off-by: Elisha Ziskind eziskind@google.com

…cycle events

Signed-off-by: Elisha Ziskind <eziskind@google.com>
Signed-off-by: Elisha Ziskind <eziskind@google.com>
Signed-off-by: Elisha Ziskind <eziskind@google.com>
@dnoe dnoe self-assigned this Mar 12, 2019
* The server instance is being shutdown and the dispatcher is about the exit.
*/
ShutdownExit
};
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 API restricts us from distinguishing between normal and abnormal shutdowns in the future. Do you think it's useful to build that capability in? It's unclear from the PR description what the motivation is for this change so I can't answer that without more context.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We can easily add additional values to the Stages enum to represent certain failure cases. Also it should be easy to extend the API to pass a parameter in the StageCallback function so that we can pass things like an error code or other state to the listeners. I'd rather wait till we have a more concrete use case for that though and keep things simple for now.

The bug for this (#2802) has some more context about the motivation for this change. In my particular use case I have a filter that needs to be given a chance to flush cached state when envoy is shutting down.

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.

Sounds good to me, thanks for expanding the comment.

Http::ContextImpl http_context_;
std::unique_ptr<Memory::HeapShrinker> heap_shrinker_;
std::thread::id main_thread_id_;
std::unordered_map<Stage, std::vector<StageCallback>> stage_callbacks_;
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.

absl::flat_hash_map? Or perhaps more pragmatically, just use a vector since the possible stages are all known at compile time, and there are only two.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

From a quick search it seems like std::unordered_map is much more common than absl::flat_hash_map in the envoy code base. The Stage enum values are strongly typed so to use them as indexes into a vector I think I'd need to do a static_cast to int which seems somewhat ugly so using a map seems better.

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.

Most of the code in Envoy by lines was written before absl::flat_hash_map was open sourced, so I suspect that's the main reason why it's more common. I think it's still a good practice to use flat_hash_map where possible in new code, so I'd use it here.

I think it's good as a map rather than a vector, your reasoning makes total sense. This isn't performance critical so if it's more readable this way that's the right solution.

Signed-off-by: Elisha Ziskind <eziskind@google.com>
@eziskind
Copy link
Copy Markdown
Contributor Author

/retest

@repokitteh-read-only
Copy link
Copy Markdown

🔨 rebuilding ci/circleci: api (failed build)

🐱

Caused by: a #6254 (comment) was created by @eziskind.

see: more, trace.

Signed-off-by: Elisha Ziskind <eziskind@google.com>
dnoe
dnoe previously approved these changes Mar 13, 2019
Copy link
Copy Markdown
Contributor

@dnoe dnoe left a comment

Choose a reason for hiding this comment

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

LGTM - I'd consider using flat_hash_map, up to you.

Http::ContextImpl http_context_;
std::unique_ptr<Memory::HeapShrinker> heap_shrinker_;
std::thread::id main_thread_id_;
std::unordered_map<Stage, std::vector<StageCallback>> stage_callbacks_;
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.

Most of the code in Envoy by lines was written before absl::flat_hash_map was open sourced, so I suspect that's the main reason why it's more common. I think it's still a good practice to use flat_hash_map where possible in new code, so I'd use it here.

I think it's good as a map rather than a vector, your reasoning makes total sense. This isn't performance critical so if it's more readable this way that's the right solution.

* The server instance is being shutdown and the dispatcher is about the exit.
*/
ShutdownExit
};
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.

Sounds good to me, thanks for expanding the comment.

@dnoe
Copy link
Copy Markdown
Contributor

dnoe commented Mar 13, 2019

@mattklein123 for senior review

Copy link
Copy Markdown
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 LGTM with 1 question.

shutdown_ = true;
restarter_.terminateParent();
dispatcher_->exit();
dispatcher_->post([this] {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm pretty sure this method must have always run on the main thread due to the terminateParent() call, right? Why is the post now necessary?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yup, you're right. shutdown is called from the admin quitquitquit handler and I wasn't sure if that was always run on the main thread but integration tests which exercise this code path still pass without this change so I'll revert it.

Signed-off-by: Elisha Ziskind <eziskind@google.com>
Copy link
Copy Markdown
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!

@mattklein123 mattklein123 merged commit b4ac13c into envoyproxy:master Mar 14, 2019
@eziskind eziskind deleted the lifecycle branch March 14, 2019 16:19
spenceral added a commit to spenceral/envoy that referenced this pull request Mar 20, 2019
* master: (59 commits)
  http fault: add response rate limit injection (envoyproxy#6267)
  xds: introduce initial_fetch_timeout option to limit initialization time (envoyproxy#6048)
  test: fix cpuset-threads tests (envoyproxy#6278)
  server: add an API for registering for notifications for server instance life… (envoyproxy#6254)
  remove remains of TestBase (envoyproxy#6286)
  dubbo_proxy: Implement the routing of Dubbo requests (envoyproxy#5973)
  Revert "stats: add new BoolIndicator stat type (envoyproxy#5813)" (envoyproxy#6280)
  runtime: codifying runtime guarded features (envoyproxy#6134)
  mysql_filter: fix integration test flakes (envoyproxy#6272)
  tls: update BoringSSL to debed9a4 (3683). (envoyproxy#6273)
  rewrite buffer implementation to eliminate evbuffer dependency (envoyproxy#5441)
  Remove the dependency from TimeSystem to libevent by using the Event::Scheduler abstraction as a delegate. (envoyproxy#6240)
  fuzz: fix use of literal in default initialization. (envoyproxy#6268)
  http: add HCM functionality required for rate limiting (envoyproxy#6242)
  Disable mysql_integration_test until it is deflaked. (envoyproxy#6250)
  test: use ipv6_only IPv6 addresses in custom cluster integration tests. (envoyproxy#6260)
  tracing: If parent span is propagated with empty string, it causes th… (envoyproxy#6263)
  upstream: fix oss-fuzz issue envoyproxy#11095. (envoyproxy#6220)
  Wire up panic mode subset to receive updates (envoyproxy#6221)
  docs: clarify xds docs with warming information (envoyproxy#6236)
  ...
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.

3 participants