-
Notifications
You must be signed in to change notification settings - Fork 11
Factor MetadataStore, MetadataReporter, and MetadataAgent into their own files. #87
Conversation
rbuskens
left a comment
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.
Thanks for breaking up your changes into bite-sized chunks. A few minor comments.
src/api_server.h
Outdated
| // Runs the metadata tasks. | ||
| class MetadataAgent { | ||
| public: | ||
| using Metadata = MetadataStore::Metadata; |
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.
This doesn't seem to be used anywhere in the MetadataAgent class. Can we remove?
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.
NM. I see you remove this in a later commit.
src/api_server.h
Outdated
| #include <memory> | ||
|
|
||
| #include "configuration.h" | ||
| #include "reporter.h" |
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 don't think we need to include this header file 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.
No longer relevant.
src/reporter.h
Outdated
|
|
||
| namespace google { | ||
|
|
||
| // Storage for the metadata mapping. |
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.
This comment isn't correct. Please fix.
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.
Right. This does bring up an interesting question of whether we should reference the config/storage objects directly instead of lugging the agent pointer around. Let me try that instead.
src/agent.h
Outdated
| #include <memory> | ||
|
|
||
| #include "configuration.h" | ||
| #include "reporter.h" |
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.
Would "class MetadataReporter" be enough?
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.
Yes, it would. Done.
Replace includes by forward declarations in headers whenever possible.
igorpeshansky
left a comment
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.
Thanks for the review. PTAL.
src/api_server.h
Outdated
| #include <memory> | ||
|
|
||
| #include "configuration.h" | ||
| #include "reporter.h" |
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.
No longer relevant.
src/agent.h
Outdated
| #include <memory> | ||
|
|
||
| #include "configuration.h" | ||
| #include "reporter.h" |
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.
Yes, it would. Done.
src/reporter.h
Outdated
|
|
||
| namespace google { | ||
|
|
||
| // Storage for the metadata mapping. |
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.
Right. This does bring up an interesting question of whether we should reference the config/storage objects directly instead of lugging the agent pointer around. Let me try that instead.
| #ifndef AGENT_H_ | ||
| #define AGENT_H_ | ||
|
|
||
| //#include "config.h" |
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.
Let's remove unnecessary imports, same throughout.
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.
This specific one was a "reminder" to set up autotools to make this more portable. It appears all over the place. Probably no sense in keeping it, but I'd rather do it as a separate cleanup to keep this PR focused.
| std::lock_guard<std::mutex> lock(agent_.resource_mu_); | ||
| const auto result = agent_.resource_map_.find(id); | ||
| if (result == agent_.resource_map_.end()) { | ||
| std::lock_guard<std::mutex> lock(store_.resource_mu_); |
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 don't love the fact that the resource_mu_ is being handled outside of the store, can we setup getters to protect direct access of resource_mu_?
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 don't love this either, and was thinking along the same lines, except in a different PR. Let's keep this one focused on the refactoring.
| << " headers: " << request.headers | ||
| << " body: " << request.body; | ||
| } | ||
| if (request.method == "GET" && request.destination.find(kPrefix) == 0) { |
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'd be awesome to call something like MonitoredResourceHandler here, so that we can easily start adding other handlers based on method / path such as HealthcheckHandler, then, each handler would be easily testable as well.
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.
Good idea, bad timing. This is supposed to be a refactoring with a specific purpose — I'll do this in a follow-up PR if you don't mind.
| #include <boost/network/protocol/http/client.hpp> | ||
| #include <chrono> | ||
|
|
||
| #include "configuration.h" |
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 this line necessary? I only ask because it didn't exist before, and it doesn't seem like any lines added call out this as a requirement any more than the lines before this PR. Same question throughout.
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 got moved from the header file.
| } | ||
| std::this_thread::sleep_for(period_); | ||
| } | ||
| //LOG(INFO) << "Metadata reporter exiting"; |
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'd vote for uncommenting this, so I can create a log metric, but please remove if you disagree.
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'd like to keep this PR focused on the refactoring. Happy to do it in a follow-up PR — there are a few other places where extra logging would help.
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.
FWIW, I think this specific bit follows an infinite loop, so will never be printed. It's commented out pending the TODO above the loop.
| }); | ||
| // TODO: This is probably all kinds of inefficient... | ||
| const int size = metadata_entry->ToString().size(); | ||
| if (empty_size + size > limit_bytes) { |
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.
Just curious, do we expect this to happen often? Do you know what likely scenarios would exist where this would be exceeded frequently? I'll setup log metrics for this line, and try to reproduce the event, so that we can get a handle on frequency once I hear from you.
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.
This is just defensive programming to catch individual metadata entries that would take the whole request over the size limit. We haven't seen any so far, but given that we include the raw output of an unrelated API, this isn't too outrageous of a scenario in my mind.
igorpeshansky
left a comment
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.
Thanks for the review. PTAL.
| #ifndef AGENT_H_ | ||
| #define AGENT_H_ | ||
|
|
||
| //#include "config.h" |
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.
This specific one was a "reminder" to set up autotools to make this more portable. It appears all over the place. Probably no sense in keeping it, but I'd rather do it as a separate cleanup to keep this PR focused.
| << " headers: " << request.headers | ||
| << " body: " << request.body; | ||
| } | ||
| if (request.method == "GET" && request.destination.find(kPrefix) == 0) { |
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.
Good idea, bad timing. This is supposed to be a refactoring with a specific purpose — I'll do this in a follow-up PR if you don't mind.
| std::lock_guard<std::mutex> lock(agent_.resource_mu_); | ||
| const auto result = agent_.resource_map_.find(id); | ||
| if (result == agent_.resource_map_.end()) { | ||
| std::lock_guard<std::mutex> lock(store_.resource_mu_); |
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 don't love this either, and was thinking along the same lines, except in a different PR. Let's keep this one focused on the refactoring.
| #include <boost/network/protocol/http/client.hpp> | ||
| #include <chrono> | ||
|
|
||
| #include "configuration.h" |
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 got moved from the header file.
| } | ||
| std::this_thread::sleep_for(period_); | ||
| } | ||
| //LOG(INFO) << "Metadata reporter exiting"; |
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'd like to keep this PR focused on the refactoring. Happy to do it in a follow-up PR — there are a few other places where extra logging would help.
| }); | ||
| // TODO: This is probably all kinds of inefficient... | ||
| const int size = metadata_entry->ToString().size(); | ||
| if (empty_size + size > limit_bytes) { |
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.
This is just defensive programming to catch individual metadata entries that would take the whole request over the size limit. We haven't seen any so far, but given that we include the raw output of an unrelated API, this isn't too outrageous of a scenario in my mind.
|
LGTM. |
src/api_server.cc
Outdated
| auth_(environment_), | ||
| period_(period_s), | ||
| reporter_thread_(&MetadataReporter::ReportMetadata, this) {} | ||
| reporter_thread_([=]() { ReportMetadata(); }) {} |
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.
Nit: Can't this be [this] rather than [=]? That would seem to be tighter/more explicit to me. I know that this change basically belongs to another commit and thus my questioning it here is maybe not very useful.
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 can, and probably should in the future. However, #74 used [=] consistently throughout, so I'm just following that style. If we decide to tighten these captures, we could do it in a separate cleanup (and at this point I'm not sure it's worth it).
|
|
||
| #include "configuration.h" | ||
| #include "reporter.h" | ||
| #include "store.h" |
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 believe that this could also be replaced with a forward declaration of MetadataStore 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.
Not really -- we need the full definition of MetadataStore to declare a field of this type, otherwise we'll get a "field has incomplete type" compiler error.
We could, however, forward-declare MetadataAgentConfiguration, which I did.
bmoyles0117
left a comment
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
supriyagarg
left a comment
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
igorpeshansky
left a comment
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.
Responded to comments, PTAL.
|
|
||
| #include "configuration.h" | ||
| #include "reporter.h" | ||
| #include "store.h" |
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.
Not really -- we need the full definition of MetadataStore to declare a field of this type, otherwise we'll get a "field has incomplete type" compiler error.
We could, however, forward-declare MetadataAgentConfiguration, which I did.
src/api_server.cc
Outdated
| auth_(environment_), | ||
| period_(period_s), | ||
| reporter_thread_(&MetadataReporter::ReportMetadata, this) {} | ||
| reporter_thread_([=]() { ReportMetadata(); }) {} |
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 can, and probably should in the future. However, #74 used [=] consistently throughout, so I'm just following that style. If we decide to tighten these captures, we could do it in a separate cleanup (and at this point I'm not sure it's worth it).
|
LGTM |
Note: this is a large refactoring, but I tried to keep each commit incremental. Please review one commit at a time.