-
Notifications
You must be signed in to change notification settings - Fork 11
Add file health check support #85
Changes from all commits
698d249
87c6b58
72ef856
4d1d6a6
3043bde
58f96cb
fd136c9
b77b963
a9f5dd5
0c929f3
5139186
a548f96
eb7cf22
6fa3edc
371b6f4
4175bf2
f78db61
bbb4b98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * Copyright 2018 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
|
|
||
| #include "health_checker.h" | ||
|
|
||
| #include <boost/filesystem.hpp> | ||
| #include <boost/algorithm/string/join.hpp> | ||
| #include <vector> | ||
| #include <fstream> | ||
|
|
||
| namespace google { | ||
|
|
||
| HealthChecker::HealthChecker(const MetadataAgentConfiguration& config) | ||
| : config_(config) { | ||
| boost::filesystem::create_directories( | ||
| boost::filesystem::path(config_.HealthCheckFile()).parent_path()); | ||
| std::remove(config_.HealthCheckFile().c_str()); | ||
|
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. I assume this is just defensive programming in case we are running in space with a non-ephemeral filesystem?
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. Yes. We don't want a brand new agent process to start out unhealthy.
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. Correct. We want to make sure the "unhealthy" flag is not set at startup.
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 |
||
| } | ||
|
|
||
| void HealthChecker::SetUnhealthy(const std::string& component) { | ||
| std::lock_guard<std::mutex> lock(mutex_); | ||
| unhealthy_components_.insert(component); | ||
| std::ofstream health_file(config_.HealthCheckFile()); | ||
| health_file << std::endl; | ||
| } | ||
|
|
||
| bool HealthChecker::IsHealthy() const { | ||
| std::lock_guard<std::mutex> lock(mutex_); | ||
| return unhealthy_components_.empty(); | ||
| } | ||
|
|
||
| void HealthChecker::CleanupForTest() { | ||
| boost::filesystem::remove_all(boost::filesystem::path( | ||
| config_.HealthCheckFile()).parent_path()); | ||
| } | ||
|
|
||
| } // namespace google | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Copyright 2018 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
| #ifndef HEALTH_CHECKER_H_ | ||
| #define HEALTH_CHECKER_H_ | ||
|
|
||
| #include <string> | ||
| #include <mutex> | ||
| #include <set> | ||
|
|
||
| #include "configuration.h" | ||
|
|
||
| namespace google { | ||
|
|
||
| // Collects and reports health information about the metadata agent. | ||
| class HealthChecker { | ||
| public: | ||
| HealthChecker(const MetadataAgentConfiguration& config); | ||
| void SetUnhealthy(const std::string& component); | ||
|
|
||
| private: | ||
| friend class HealthCheckerUnittest; | ||
|
|
||
| bool IsHealthy() const; | ||
| void CleanupForTest(); | ||
|
|
||
| const MetadataAgentConfiguration& config_; | ||
| std::set<std::string> unhealthy_components_; | ||
| mutable std::mutex mutex_; | ||
| }; | ||
|
|
||
| } // namespace google | ||
|
|
||
| #endif // HEALTH_CHECKER_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.
Why does this test need to be a friend?
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.
In the health check unit tests I set the health check file location. This is set via the configuration object, so I need the test to be a friend so that I can pass it a stream rather than giving an actual file.
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 about this some more... This isn't the only place we will want to parse configuration in a test. We have a good approach in
configuration_unittest, namely a static function that parses a config from a string. Would it make sense to factor that out into aconfig_test_helperand use that whenever we need that functionality? If we go with that approach, we'd want a separate unit test for that helper.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 agree with your suggestion, but I would create a separate PR for this, and track that as an independent bug, and potentially rebase this onto master once the prerequisite PR has been merged. I'll need to do the same with my Kubernetes tests.
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.
While I agree, I think its best left for a separate commit.
Uh oh!
There was an error while loading. Please reload this page.
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.
Ok, great. I'm glad we're all on the same page. Let's finish implementing this test, and then refactor to create a helper (in a separate PR) before we invest too much more time into replicating this code in other tests?
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.
Yep