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
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
add resource error response in /health end-point (#1505)
Krunal Gandhi77ade5ff6563
diff --git a/include/cached_resource_stat.h b/include/cached_resource_stat.hindex 2c409dd0..a271c818 100644--- a/include/cached_resource_stat.h+++ b/include/cached_resource_stat.h@@ -5,6 +5,13 @@#include <sys/statvfs.h>class cached_resource_stat_t {+public:+ enum resource_check_t {+ OK,+ OUT_OF_DISK,+ OUT_OF_MEMORY+ };+private:const static size_t REFRESH_INTERVAL_SECS = 5;uint64_t disk_total_bytes = 0;@@ -18,25 +25,22 @@ private:uint64_t last_checked_ts = 0;+ resource_check_t resource_error;+cached_resource_stat_t() = default;~cached_resource_stat_t() = default;public:-static cached_resource_stat_t& get_instance() {static cached_resource_stat_t instance;return instance;}- enum resource_check_t {- OK,- OUT_OF_DISK,- OUT_OF_MEMORY- };-// On Mac, we will only check for disk usageresource_check_t has_enough_resources(const std::string& data_dir_path,const int disk_used_max_percentage,const int memory_used_max_percentage);++ const resource_check_t get_out_of_resource_error() const;};diff --git a/src/cached_resource_stat.cpp b/src/cached_resource_stat.cppindex c21c77ff..fb3e2f88 100644--- a/src/cached_resource_stat.cpp+++ b/src/cached_resource_stat.cpp@@ -65,6 +65,8 @@ cached_resource_stat_t::has_enough_resources(const std::string& data_dir_path,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;++ resource_error = cached_resource_stat_t::OUT_OF_DISK;return cached_resource_stat_t::OUT_OF_DISK;}@@ -77,6 +79,7 @@ cached_resource_stat_t::has_enough_resources(const std::string& data_dir_path,uint64_t all_memory_used = (memory_total_bytes - memory_available_bytes) + (swap_total_bytes - swap_free_bytes);if(all_memory_used >= memory_total_bytes) {+ resource_error = cached_resource_stat_t::OUT_OF_MEMORY;return cached_resource_stat_t::OUT_OF_MEMORY;}@@ -89,8 +92,14 @@ cached_resource_stat_t::has_enough_resources(const std::string& data_dir_path,LOG(INFO) << "memory_total: " << memory_total_bytes << ", memory_available: " << memory_available_bytes<< ", all_memory_used: " << all_memory_used << ", free_mem: " << free_mem<< ", memory_free_min: " << memory_free_min_bytes;+ resource_error = cached_resource_stat_t::OUT_OF_MEMORY;return cached_resource_stat_t::OUT_OF_MEMORY;}+ resource_error = cached_resource_stat_t::OK;return cached_resource_stat_t::OK;}++const cached_resource_stat_t::resource_check_t cached_resource_stat_t::get_out_of_resource_error() const {+ return resource_error;+}\ No newline at end of filediff --git a/src/core_api.cpp b/src/core_api.cppindex 9930e354..b2563e7e 100644--- a/src/core_api.cpp+++ b/src/core_api.cpp@@ -290,6 +290,11 @@ bool get_health(const std::shared_ptr<http_req>& req, const std::shared_ptr<httpbool alive = server->is_alive();result["ok"] = alive;+ auto resource_error = cached_resource_stat_t::get_instance().get_out_of_resource_error();+ if (resource_error != cached_resource_stat_t::resource_check_t::OK) {+ result["resource_error"] = std::string(magic_enum::enum_name(resource_error));+ }+if(alive) {res->set_body(200, result.dump());} else {