ArchivedTheos are truncated on a server start

This commit is contained in:
Taha Afzal committed 2023-07-04 03:13:46 +00:00
1 parent 09e2a1764f
commit ad1a45cf49
4 files changed
+575 -34

No files matched your search

@@ -8,6 +8,7 @@
#include "lib/persistence/SqliteStore.h"
#include "lib/utils/PBUtils.h"
#include "lib/utils/Puid.h"
#include "lib/utils/TimeStamp.h"
#include "lib/utils/bts_assert.h"
#include "server/sparkd/algo/AlgoDefinitionServiceServer.h"
#include <boost/algorithm/string.hpp>
@@ -90,6 +91,11 @@ struct AnalyticsServiceServer::PImpl {
const std::string vs_usage_ns = "volsurface_usage";
const std::string reports_ns = "reports";
const std::string archived_theos_ns = "archived_theos";
// this is the total number of vs data that will be stored in the db,
// beyond that, old values will be recycled
const size_t vs_history_sz = 5000;
// anologous to above for archived theos
const size_t archived_theos_history_sz = 1000;
std::unique_ptr<persistence::IStore> vs_store;
std::unique_ptr<persistence::IStore> exp_params_store;
std::unique_ptr<persistence::IStore> vs_usage_store;
@@ -133,6 +139,7 @@ AnalyticsServiceServer::AnalyticsServiceServer(
// publishing all vol surfaces
LOG(Gen, Info, "Starting AnalyticsServiceServer");
_clean_up_vss();
_clean_up_archived_theos();
auto vs_table = me->vs_store->retrieve_all();
for (auto const &ns_vals : vs_table) {
const std::string &vs_name = ns_vals.first;
@@ -429,47 +436,69 @@ void AnalyticsServiceServer::get_archived_theos(
AnalyticsServiceServer::~AnalyticsServiceServer() {}
// for a map where the key is the epoch timestamp, this returns the keys to
// delete such that final size is final_sz
std::set<std::string> get_keys_to_delete(
const std::map<std::string, std::string> &time_keyed_map, size_t final_sz) {
if (time_keyed_map.size() <= final_sz) {
return {};
}
std::vector<uint64_t> keys_uint;
for (const auto &kv : time_keyed_map) {
try {
keys_uint.push_back(std::stoull(kv.first));
} catch (int) {
LOG(Gen, Error,
"AnalyticsServiceServer::get_keys_to_delete could not convert "
"key: {} to uint",
kv.first);
}
}
// this is sorted lowest to highest
std::sort(keys_uint.begin(), keys_uint.end());
size_t n_del_rows = keys_uint.size() - final_sz;
std::vector<uint64_t> keys_uint_to_del =
std::vector<uint64_t>(keys_uint.begin(), keys_uint.begin() + n_del_rows);
std::set<std::string> keys_to_del;
for (auto k : keys_uint_to_del) {
keys_to_del.insert(std::to_string(k));
}
return keys_to_del;
}
void AnalyticsServiceServer::_clean_up_vss() {
const size_t final_sz = 500;
const size_t final_sz = me->vs_history_sz;
auto all_rows = me->vs_store->retrieve_all();
for (const auto &ns_k : all_rows) {
auto t0 = utils::TimeStamp::now().to_uint64();
const auto &vs = ns_k.first;
const auto &vs_row_map = ns_k.second;
if (vs_row_map.size() <= final_sz) {
LOG(Gen, Info,
"AnalyticsServiceServer::_clean_up_vss ignoring vs: {} because it "
"has only {} rows",
vs, vs_row_map.size());
continue;
}
std::vector<uint64_t> keys_uint;
for (const auto &r : vs_row_map) {
const auto &k = r.first;
try {
keys_uint.push_back(std::stoull(k));
} catch (int) {
LOG(Gen, Error,
"AnalyticsServiceServer::_clean_up_vss could not convert "
"key: "
"{} to uint",
k);
}
}
// sort and remove
std::sort(keys_uint.begin(), keys_uint.end());
size_t n_del_rows = keys_uint.size() - final_sz;
std::vector<uint64_t> keys_uint_to_del = std::vector<uint64_t>(
keys_uint.begin(), keys_uint.begin() + n_del_rows);
std::set<std::string> keys_to_del;
for (auto k : keys_uint_to_del) {
keys_to_del.insert(std::to_string(k));
}
LOG(Gen, Info,
"AnalyticsServiceServer::_clean_up_vss vs: {} n_rows: {} removing {} "
"rows",
vs, keys_uint.size(), keys_to_del.size());
auto keys_to_del = get_keys_to_delete(vs_row_map, final_sz);
me->vs_store->remove(vs, keys_to_del);
auto t1 = utils::TimeStamp::now().to_uint64();
LOG(Gen, Info,
"AnalyticsServiceServer::_clean_up_vss vs: {} n_rows: {} n_dels {} "
"time: {} us",
vs, vs_row_map.size(), keys_to_del.size(), t1 - t0);
}
}
void AnalyticsServiceServer::_clean_up_archived_theos() {
auto t0 = utils::TimeStamp::now().to_uint64();
std::map<std::string, std::map<std::string, std::string>> all_data =
me->theos_store->retrieve_all();
auto it = all_data.find(me->archived_theos_ns);
if (it == all_data.end())
return;
const std::map<std::string, std::string> &theo_data = it->second;
std::set<std::string> keys_to_del =
get_keys_to_delete(theo_data, me->archived_theos_history_sz);
me->theos_store->remove(me->archived_theos_ns, keys_to_del);
auto t1 = utils::TimeStamp::now().to_uint64();
LOG(Gen, Info,
"AnalyticsServiceServer::_clean_up_archived_theos n_dels: {} time: {} "
"us",
keys_to_del.size(), t1 - t0);
}
} // namespace bts::analytics