-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Conversation
b0b2f80
to
f690729
Compare
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.
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_) { |
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 it be const auto& kv
?
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
source/common/runtime/runtime_impl.h
Outdated
@@ -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; |
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.
Could do just return std::unordered_map<std::string, const Snapshot::Entry*>();
- your call
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'll go with that. Much cleaner :)
include/envoy/runtime/runtime.h
Outdated
@@ -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. |
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 add something to this comment about the lifespan of the const Entry*
?
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: Chris Roche <[email protected]>
f690729
to
67328c7
Compare
Signed-off-by: Chris Roche <[email protected]>
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, thanks! Two small comments.
include/envoy/runtime/runtime.h
Outdated
/** | ||
* The raw runtime data. | ||
*/ | ||
std::string string_value; |
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.
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.
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
include/envoy/runtime/runtime.h
Outdated
* 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; |
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 the reason you are copying here the const Entry vs. not const Entry? If that's the case, I would probably do two things:
- I would use std::reference_wrapper to make it clear it can't be null.
- 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.
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.
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.
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.
Went with <string, const Entry> much nicer and the getAll can now return a const ref to the map instead. Much better!
…ntry> Signed-off-by: Chris Roche <[email protected]>
Signed-off-by: Chris Roche <[email protected]>
Signed-off-by: Chris Roche <[email protected]>
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, 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); |
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.
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.
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.
thought the same; I'd assume lightweight compared to the IO we're doing before it, though.
Comment added!
Signed-off-by: Chris Roche <[email protected]>
) * 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]>
This patch adds a
getAll
accessor to the underlying entries map in theRuntime::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)