Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Fix health resource status logic.

Kishore Nallana7f9354d5cc6
diff --git a/include/cached_resource_stat.h b/include/cached_resource_stat.h
index ae34e708..833cdfe4 100644
--- a/include/cached_resource_stat.h
+++ b/include/cached_resource_stat.h
@@ -19,10 +19,16 @@ private:
std::atomic<uint64_t> last_checked_ts = 0;
std::mutex m;
+ resource_check_t resource_status = OK;
+
cached_resource_stat_t() = default;
~cached_resource_stat_t() = default;
+ static cached_resource_stat_t::resource_check_t get_resource_status(const std::string& data_dir_path,
+ const int disk_used_max_percentage,
+ const int memory_used_max_percentage);
+
public:
static cached_resource_stat_t& get_instance() {
static cached_resource_stat_t instance;
diff --git a/src/cached_resource_stat.cpp b/src/cached_resource_stat.cpp
index 55927190..e8530716 100644
--- a/src/cached_resource_stat.cpp
+++ b/src/cached_resource_stat.cpp
@@ -13,6 +13,21 @@ cached_resource_stat_t::has_enough_resources(const std::string& data_dir_path,
std::unique_lock lk(m);
+ uint64_t now = std::chrono::duration_cast<std::chrono::seconds>(
+ std::chrono::system_clock::now().time_since_epoch()).count();
+
+ if((now - last_checked_ts) < REFRESH_INTERVAL_SECS) {
+ return resource_status;
+ }
+
+ resource_status = get_resource_status(data_dir_path, disk_used_max_percentage, memory_used_max_percentage);
+ last_checked_ts = now;
+ return resource_status;
+}
+
+cached_resource_stat_t::resource_check_t
+cached_resource_stat_t::get_resource_status(const std::string& data_dir_path, const int disk_used_max_percentage,
+ const int memory_used_max_percentage) {
uint64_t disk_total_bytes = 0;
uint64_t disk_used_bytes = 0;
@@ -22,68 +37,60 @@ cached_resource_stat_t::has_enough_resources(const std::string& data_dir_path,
uint64_t swap_total_bytes = 0;
uint64_t swap_free_bytes = 0;
- uint64_t now = std::chrono::duration_cast<std::chrono::seconds>(
- std::chrono::system_clock::now().time_since_epoch()).count();
-
- if((now - last_checked_ts) > REFRESH_INTERVAL_SECS) {
- // get disk usage
- struct statvfs st{};
- statvfs(data_dir_path.c_str(), &st);
- disk_total_bytes = st.f_blocks * st.f_frsize;
- disk_used_bytes = (st.f_blocks - st.f_bavail) * st.f_frsize;
-
- // get memory and swap usage
- std::string token;
- std::ifstream file("/proc/meminfo");
-
- while(file >> token) {
- if(token == "MemTotal:") {
- uint64_t value_kb;
- if(file >> value_kb) {
- memory_total_bytes = value_kb * 1024;
- }
+ // get disk usage
+ struct statvfs st{};
+ statvfs(data_dir_path.c_str(), &st);
+ disk_total_bytes = st.f_blocks * st.f_frsize;
+ disk_used_bytes = (st.f_blocks - st.f_bavail) * st.f_frsize;
+
+ // get memory and swap usage
+ std::string token;
+ std::ifstream file("/proc/meminfo");
+
+ while(file >> token) {
+ if(token == "MemTotal:") {
+ uint64_t value_kb;
+ if(file >> value_kb) {
+ memory_total_bytes = value_kb * 1024;
}
+ }
- else if(token == "MemAvailable:") {
- uint64_t value_kb;
- if(file >> value_kb) {
- memory_available_bytes = value_kb * 1024;
- }
+ else if(token == "MemAvailable:") {
+ uint64_t value_kb;
+ if(file >> value_kb) {
+ memory_available_bytes = value_kb * 1024;
}
+ }
- else if(token == "SwapTotal:") {
- uint64_t value_kb;
- if(file >> value_kb) {
- swap_total_bytes = value_kb * 1024;
- }
+ else if(token == "SwapTotal:") {
+ uint64_t value_kb;
+ if(file >> value_kb) {
+ swap_total_bytes = value_kb * 1024;
}
+ }
- else if(token == "SwapFree:") {
- uint64_t value_kb;
- if(file >> value_kb) {
- swap_free_bytes = value_kb * 1024;
- }
-
- // since "SwapFree" appears last in the file
- break;
+ else if(token == "SwapFree:") {
+ uint64_t value_kb;
+ if(file >> value_kb) {
+ swap_free_bytes = value_kb * 1024;
}
- }
- last_checked_ts = now;
+ // since "SwapFree" appears last in the file
+ break;
+ }
}
- lk.unlock();
+ if(memory_total_bytes == 0) {
+ // if there is an error in fetching the stat, we will return `OK`
+ return cached_resource_stat_t::OK;
+ }
double disk_used_percentage = (double(disk_used_bytes)/double(disk_total_bytes)) * 100;
if(disk_used_percentage > disk_used_max_percentage) {
LOG(INFO) << "disk_total_bytes: " << disk_total_bytes << ", disk_used_bytes: " << disk_used_bytes
<< ", disk_used_percentage: " << disk_used_percentage;
- return cached_resource_stat_t::OUT_OF_DISK;
- }
- if(memory_total_bytes == 0) {
- // if there is an error in fetching the stat, we will return `OK`
- return cached_resource_stat_t::OK;
+ return cached_resource_stat_t::OUT_OF_DISK;
}
// Calculate sum of RAM + SWAP used as all_memory_used