This repository was archived by the owner on Aug 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Memoize owner objects instead of querying the master all the time. #46
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -527,13 +527,42 @@ json::value KubernetesReader::GetOwner( | |
| const std::string api_version = owner_ref->Get<json::String>("apiVersion"); | ||
| const std::string kind = owner_ref->Get<json::String>("kind"); | ||
| const std::string name = owner_ref->Get<json::String>("name"); | ||
| const auto path_component = KindPath(api_version, kind); | ||
| const std::string uid = owner_ref->Get<json::String>("uid"); | ||
|
|
||
| // Even though we query by name, we should look up the owner by uid, | ||
| // to handle the case when an object is deleted and re-constructed. | ||
| const std::string encoded_ref = boost::algorithm::join( | ||
| std::vector<std::string>{api_version, kind, uid}, "/"); | ||
|
|
||
| std::lock_guard<std::recursive_mutex> lock(mutex_); | ||
| auto found = owners_.emplace(std::piecewise_construct, | ||
| std::forward_as_tuple(encoded_ref), | ||
| std::forward_as_tuple()); | ||
| json::value& owner = found.first->second; | ||
| if (found.second) { // Not found, inserted new. | ||
| const auto path_component = KindPath(api_version, kind); | ||
| #ifdef VERBOSE | ||
| LOG(DEBUG) << "KindPath returned {" << path_component.first << ", " | ||
| << path_component.second << "}"; | ||
| LOG(DEBUG) << "KindPath returned {" << path_component.first << ", " | ||
| << path_component.second << "}"; | ||
| #endif | ||
| return QueryMaster(path_component.first + "/namespaces/" + ns + "/" + | ||
| path_component.second + "/" + name); | ||
| owner = std::move( | ||
| QueryMaster(path_component.first + "/namespaces/" + ns + "/" + | ||
| path_component.second + "/" + name)); | ||
| // Sanity check: because we are looking up by name, the object we get | ||
| // back might have a different uid. | ||
| const json::Object* owner_obj = owner->As<json::Object>(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: the naming of the variables seems inconsistent.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Funny you should mention that -- I initially had it as
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SGTM! |
||
| const json::Object* metadata = owner_obj->Get<json::Object>("metadata"); | ||
| const std::string owner_uid = metadata->Get<json::String>("uid"); | ||
| if (owner_uid != uid) { | ||
| LOG(WARNING) << "Owner " << kind << "'" << name << "' (id " << uid | ||
| << ") disappeared before we could query it. Found id " | ||
| << owner_uid << " instead."; | ||
| owner.reset(); | ||
| throw QueryException("Owner " + kind + " " + name + " (id " + uid + | ||
| ") disappeared"); | ||
| } | ||
| } | ||
| return owner->Clone(); | ||
| } | ||
|
|
||
| json::value KubernetesReader::FindTopLevelOwner( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 it possible that this throws an exception, without killing the thread, which would end up leaving owner null, which would end up throwing another exception the next time due to owner->Clone() being on a nullptr? If so, can we add a sanity check of
if (owner == nullptr || found.second) {?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.
An exception will not allow the rest of this function to continue -- it'll be propagated up the stack, and caught, likely in
KubernetesReader::MetadataQuery. So such a check would be superfluous.