From 9b7518e7ac00b5793e86c22b008e4966f0f286d1 Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Tue, 31 Oct 2023 10:33:54 +0530 Subject: [PATCH] Fix missing files --- include/housekeeper.h | 35 +++++++++++++++++++++++++++++ src/housekeeper.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 include/housekeeper.h create mode 100644 src/housekeeper.cpp diff --git a/include/housekeeper.h b/include/housekeeper.h new file mode 100644 index 00000000..41243cbd --- /dev/null +++ b/include/housekeeper.h @@ -0,0 +1,35 @@ +#pragma once +#include +#include +#include + +class HouseKeeper { +private: + mutable std::mutex mutex; + std::condition_variable cv; + + std::atomic quit = false; + std::atomic interval_seconds = 1800; + + HouseKeeper() {} + + ~HouseKeeper() {} + + +public: + + static HouseKeeper &get_instance() { + static HouseKeeper instance; + return instance; + } + + HouseKeeper(HouseKeeper const &) = delete; + + void operator=(HouseKeeper const &) = delete; + + void init(uint32_t interval_seconds); + + void run(); + + void stop(); +}; diff --git a/src/housekeeper.cpp b/src/housekeeper.cpp new file mode 100644 index 00000000..07150e11 --- /dev/null +++ b/src/housekeeper.cpp @@ -0,0 +1,51 @@ +#include +#include "housekeeper.h" + +void HouseKeeper::run() { + uint64_t prev_persistence_s = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()).count(); + + while(!quit) { + std::unique_lock lk(mutex); + cv.wait_for(lk, std::chrono::seconds(60), [&] { return quit.load(); }); + + if(quit) { + lk.unlock(); + break; + } + + auto now_ts_seconds = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()).count(); + + if(now_ts_seconds - prev_persistence_s < interval_seconds) { + continue; + } + + // iterate through all collections and repair all hnsw graphs + auto coll_names = CollectionManager::get_instance().get_collection_names(); + + for(auto& coll_name: coll_names) { + auto coll = CollectionManager::get_instance().get_collection(coll_name); + if(coll == nullptr) { + continue; + } + + coll->do_housekeeping(); + LOG(INFO) << "Ran housekeeping."; + } + + prev_persistence_s = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()).count(); + + lk.unlock(); + } +} + +void HouseKeeper::stop() { + quit = true; + cv.notify_all(); +} + +void HouseKeeper::init(uint32_t interval_seconds) { + this->interval_seconds = interval_seconds; +}