MXS-2555 Beta Smart Router.

1. Remove persistence of performance data
2. Move global CanonicalPerformance into SmartRouter object
3. Implement another kill_all_others_v2. Left kill_all_others_v1
    in case it should be fixed and used instead.
This commit is contained in:
Niclas Antti
2019-06-13 13:43:30 +03:00
parent 1241300494
commit 6a7b6d4b89
6 changed files with 138 additions and 144 deletions

View File

@ -19,11 +19,9 @@
const std::string file_version = "Alpha"; // if a file has a different version string, discard it.
CanonicalPerformance::CanonicalPerformance(const std::string& persistent_file)
: m_persistent_file(persistent_file)
, m_nChanges(0)
CanonicalPerformance::CanonicalPerformance()
: m_nChanges(0)
{
read_persisted();
}
bool CanonicalPerformance::insert(const std::string& canonical, const PerformanceInfo& perf)
@ -50,71 +48,6 @@ PerformanceInfo CanonicalPerformance::find(const std::string& canonical)
void CanonicalPerformance::clear()
{
m_perfs.clear();
std::remove(m_persistent_file.c_str());
m_nChanges = 0;
}
// TODO, expensive. Saves the whole file whenever there are changes.
void CanonicalPerformance::persist() const
{
if (m_nChanges == 0)
{
return;
}
std::ofstream out(m_persistent_file);
if (!out)
{
MXS_ERROR("Could not open %s for writing", m_persistent_file.c_str());
}
out << file_version << '\n';
for (const auto& e : m_perfs)
{
out << e.first << '\n';
out << e.second.host() << '\n';
out << e.second.duration().count() << '\n';
}
m_nChanges = 0;
}
void CanonicalPerformance::read_persisted()
{
std::ifstream in(m_persistent_file);
if (!in)
{
return;
}
std::string version;
std::getline(in, version);
if (version != file_version)
{
MXS_INFO("%s version does not match the expected one. Discarding file.", m_persistent_file.c_str());
in.close();
std::remove(m_persistent_file.c_str());
return;
}
while (in)
{
std::string canonical;
std::string host_str;
std::string nano_str;
std::getline(in, canonical);
std::getline(in, host_str);
std::getline(in, nano_str);
if (!in)
{
break;
}
m_perfs.insert({canonical, {maxbase::Host(host_str), maxbase::Duration(std::stoull(nano_str))}});
}
m_nChanges = 0;
}
@ -132,7 +65,7 @@ std::string show_some(const std::string& str, int nchars)
}
// This needs TODO:
// These are TODOs for the GA version. The Beta version will not have persistence.
// 1. Read the file once at startup. There might also be a need to do cleanup
// of the file if the configuration has changed.
// 2. Updates to the data should "quickly" become globally available,
@ -153,44 +86,3 @@ std::string show_some(const std::string& str, int nchars)
// 3. If the measured time is very different from the stored one (+/20%),
// expire the entry (not implemented).
// More rules can be found out by testing.
namespace
{
std::mutex canon_mutex;
const std::string persistent_file = "/tmp/max_canonical_perf.dat"; // TODO config
CanonicalPerformance& canon_store()
{
// Note that the age of entries become "Now", the age was not written to file.
static CanonicalPerformance cp(persistent_file);
return cp;
}
}
PerformanceInfo perf_find(const std::string& canonical)
{
std::unique_lock<std::mutex> guard(canon_mutex);
auto perf = canon_store().find(canonical);
if (perf.is_valid() && perf.age() > std::chrono::minutes(1)) // TODO to config, but not yet
{
canon_store().remove(canonical);
return PerformanceInfo();
}
return perf;
}
bool perf_update(const std::string& canonical, const PerformanceInfo& perf)
{
std::unique_lock<std::mutex> guard(canon_mutex);
auto ret = canon_store().insert(canonical, perf);
canon_store().persist();
if (ret)
{
MXS_SDEBUG("Stored perf " << perf.duration() << ' ' << perf.host() << ' ' << show_some(canonical));
}
return ret;
}