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

runtime: add getAll accessor to Snapshot to access underlying Entry map #2311

Merged
merged 6 commits into from
Jan 4, 2018

Conversation

rodaine
Copy link
Member

@rodaine rodaine commented Jan 4, 2018

This patch adds a getAll accessor to the underlying entries map in the Runtime::Snapshot. This will be used in the forthcoming /runtime admin endpoint (#514).

Risk Level: Low (new unused feature)
Testing: Unit
Docs Changes: N/A (will be added when the admin endpoint is created)
Release Notes: N/A (likewise)

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

A few nitpicky comments. The format check failed, I recommend running fix_format using the docker container to make sure you're always using the same version of clang-format as CI.

std::unordered_map<std::string, const Snapshot::Entry*> entries;
entries.reserve(values_.size());

for (auto& kv : values_) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can it be const auto& kv?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@@ -190,6 +185,11 @@ class NullLoaderImpl : public Loader {
return default_value;
}

std::unordered_map<std::string, const Snapshot::Entry*> getAll() const override {
std::unordered_map<std::string, const Snapshot::Entry*> entries;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could do just return std::unordered_map<std::string, const Snapshot::Entry*>(); - your call

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll go with that. Much cleaner :)

@@ -96,6 +113,12 @@ class Snapshot {
* @return uint64_t the runtime value or the default value.
*/
virtual uint64_t getInteger(const std::string& key, uint64_t default_value) const PURE;

/**
* Fetch the raw runtime entries map.
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 add something to this comment about the lifespan of the const Entry*?

Copy link
Member 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: Chris Roche <[email protected]>
dnoe
dnoe previously approved these changes Jan 4, 2018
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, thanks! Two small comments.

/**
* The raw runtime data.
*/
std::string string_value;
Copy link
Member

Choose a reason for hiding this comment

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

We are not fully consistent here, but I think most structs still use _ at the end of members, so I would probably change it back.

Copy link
Member Author

Choose a reason for hiding this comment

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

👌 done

* Fetch the raw runtime entries map. The map data is safe only for the lifetime of the Snapshot.
* @return std::unordered_map<std::string, const Entry*> the raw map of loaded runtime values.
*/
virtual std::unordered_map<std::string, const Entry*> getAll() const PURE;
Copy link
Member

Choose a reason for hiding this comment

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

Is the reason you are copying here the const Entry vs. not const Entry? If that's the case, I would probably do two things:

  1. I would use std::reference_wrapper to make it clear it can't be null.
  2. I would probably return a vector of pairs, vs. a map. I don't think the caller will really ever need the map functionality, is they could otherwise call the snapshot directly if they need a single value.

Copy link
Member

Choose a reason for hiding this comment

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

P.S., I'm not sure but you might also be able to just make the internal map store const Entry. I'm not sure if this will work or not due to STL copy semantics, but it might. I would try that first.

Copy link
Member Author

Choose a reason for hiding this comment

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

Went with <string, const Entry> much nicer and the getAll can now return a const ref to the map instead. Much better!

Chris Roche added 2 commits January 4, 2018 13:41
Signed-off-by: Chris Roche <[email protected]>
Signed-off-by: Chris Roche <[email protected]>
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.

nice, much better. small comment then LGTM

@@ -244,7 +249,8 @@ void SnapshotImpl::walkDirectory(const std::string& path, const std::string& pre
entry.uint_value_.value(converted);
}

values_[full_prefix] = entry;
values_.erase(full_prefix);
Copy link
Member

Choose a reason for hiding this comment

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

It's unfortunate that this now requires two hash lookups, but I think it's worth it as the interface is much better IMO. I think in C++17 http://en.cppreference.com/w/cpp/container/unordered_map/insert_or_assign could be used here, but I'm not totally sure. I would probably drop a small comment on why we are doing erase/insert here.

Copy link
Member Author

Choose a reason for hiding this comment

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

thought the same; I'd assume lightweight compared to the IO we're doing before it, though.

Comment added!

@mattklein123 mattklein123 merged commit 536fd63 into envoyproxy:master Jan 4, 2018
@rodaine rodaine deleted the runtime-get-all branch January 4, 2018 23:27
Shikugawa pushed a commit to Shikugawa/envoy that referenced this pull request Mar 28, 2020
)

* add basic metadata exchange

Signed-off-by: Kuat Yessenov <[email protected]>

* simplify

Signed-off-by: Kuat Yessenov <[email protected]>

* review feedback

Signed-off-by: Kuat Yessenov <[email protected]>
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