From a9c6161def024fc89e3a59a2bd01386f9ad2c209 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Tue, 1 Mar 2022 15:28:03 +0100 Subject: [PATCH] IcingaDB#Stop(): don't block shutdown, timeout instead --- lib/icingadb/icingadb.cpp | 10 ++++++++-- lib/icingadb/icingadb.hpp | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/icingadb/icingadb.cpp b/lib/icingadb/icingadb.cpp index bf3f06594..1adcaee84 100644 --- a/lib/icingadb/icingadb.cpp +++ b/lib/icingadb/icingadb.cpp @@ -172,7 +172,9 @@ void IcingaDB::Start(bool runtimeCreated) m_Rcon->SuppressQueryKind(Prio::CheckResult); m_Rcon->SuppressQueryKind(Prio::RuntimeStateSync); - m_HistoryThread = std::thread([this]() { ForwardHistoryEntries(); }); + Ptr keepAlive (this); + + m_HistoryThread = std::async(std::launch::async, [this, keepAlive]() { ForwardHistoryEntries(); }); } void IcingaDB::ExceptionHandler(boost::exception_ptr exp) @@ -234,7 +236,11 @@ void IcingaDB::Stop(bool runtimeRemoved) Log(LogInformation, "IcingaDB") << "Flushing history data buffer to Redis."; - m_HistoryThread.join(); + if (m_HistoryThread.wait_for(std::chrono::minutes(1)) == std::future_status::timeout) { + Log(LogCritical, "IcingaDB") + << "Flushing takes more than one minute (while we're about to shut down). Giving up and discarding " + << m_HistoryBulker.Size() << " queued history queries."; + } Log(LogInformation, "IcingaDB") << "'" << GetName() << "' stopped."; diff --git a/lib/icingadb/icingadb.hpp b/lib/icingadb/icingadb.hpp index 665b29694..3d3dc04cd 100644 --- a/lib/icingadb/icingadb.hpp +++ b/lib/icingadb/icingadb.hpp @@ -15,10 +15,10 @@ #include "remote/messageorigin.hpp" #include #include +#include #include #include #include -#include #include #include @@ -181,7 +181,7 @@ private: Timer::Ptr m_StatsTimer; WorkQueue m_WorkQueue{0, 1, LogNotice}; - std::thread m_HistoryThread; + std::future m_HistoryThread; Bulker m_HistoryBulker {4096, std::chrono::milliseconds(250)}; String m_PrefixConfigObject;