Module: check_mk
Branch: master
Commit: 089d8c549f865b9cbb0a0aafb9b9dfced720ec53
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=089d8c549f865b…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Fri Nov 17 11:33:21 2017 +0100
5461 FIX Fixed WaitTimeout handling
The WaitTimeout given in a Livestatus query was handled incorrectly: It was
applied each time the condition was not true, and not for the whole query.
This has been fixed.
Change-Id: If64c4dd18310d40452dd8a76c9071404ca7ffd1e
---
.werks/5461 | 13 +++++++++++++
livestatus/src/Query.cc | 36 +++++++++++++++---------------------
livestatus/src/Query.h | 1 -
livestatus/src/waittriggers.cc | 2 ++
livestatus/src/waittriggers.h | 2 ++
5 files changed, 32 insertions(+), 22 deletions(-)
diff --git a/.werks/5461 b/.werks/5461
new file mode 100644
index 0000000..2a7ffb0
--- /dev/null
+++ b/.werks/5461
@@ -0,0 +1,13 @@
+Title: Fixed WaitTimeout handling
+Level: 1
+Component: livestatus
+Compatible: compat
+Edition: cre
+Version: 1.5.0i1
+Date: 1510914686
+Class: fix
+
+The WaitTimeout given in a Livestatus query was handled incorrectly: It was
+applied each time the condition was not true, and not for the whole query.
+This has been fixed.
+
diff --git a/livestatus/src/Query.cc b/livestatus/src/Query.cc
index 4759982..c59025a 100644
--- a/livestatus/src/Query.cc
+++ b/livestatus/src/Query.cc
@@ -30,6 +30,7 @@
#include <chrono>
#include <cmath>
#include <cstdlib>
+#include <mutex>
#include <ostream>
#include <ratio>
#include <stdexcept>
@@ -90,7 +91,7 @@ Query::Query(const std::list<std::string> &lines, Table
*table,
, _keepalive(false)
, _auth_user(nullptr)
, _wait_timeout(0)
- , _wait_trigger(nullptr)
+ , _wait_trigger(&trigger_all())
, _wait_object(nullptr)
, _separators("\n", ";", ",", "|")
, _show_column_headers(true)
@@ -694,27 +695,20 @@ const std::vector<std::unique_ptr<Aggregator>>
&Query::getAggregatorsFor(
}
void Query::doWait() {
- while (
- !_wait_condition->accepts(_wait_object, _auth_user, timezoneOffset())) {
- if (waitForTrigger() == std::cv_status::timeout) {
- return;
- }
- }
-}
-
-std::cv_status Query::waitForTrigger() const {
- auto trigger = _wait_trigger == nullptr ? &trigger_all() : _wait_trigger;
+ auto pred = [this] {
+ return _wait_condition->accepts(_wait_object, _auth_user,
+ timezoneOffset());
+ };
+ std::unique_lock<std::mutex> lock(trigger_mutex());
if (_wait_timeout == std::chrono::milliseconds(0)) {
Debug(_logger) << "waiting until condition becomes true";
- trigger_wait(*trigger);
- return std::cv_status::no_timeout;
- }
- Debug(_logger) << "waiting " << _wait_timeout.count()
- << "ms or until condition becomes true";
- auto status = trigger_wait_for(*trigger, _wait_timeout);
- if (status == std::cv_status::timeout) {
- Debug(_logger) << "wait timeout after " <<
_wait_timeout.count()
- << "ms";
+ _wait_trigger->wait(lock, pred);
+ } else {
+ Debug(_logger) << "waiting "
+ <<
std::chrono::duration_cast<std::chrono::milliseconds>(
+ _wait_timeout)
+ .count()
+ << "ms or until condition becomes true";
+ _wait_trigger->wait_for(lock, _wait_timeout, pred);
}
- return status;
}
diff --git a/livestatus/src/Query.h b/livestatus/src/Query.h
index f64f8e8..27732fe 100644
--- a/livestatus/src/Query.h
+++ b/livestatus/src/Query.h
@@ -111,7 +111,6 @@ private:
bool doStats() const;
void doWait();
- std::cv_status waitForTrigger() const;
void parseFilterLine(char *line, FilterStack &filters);
void parseStatsLine(char *line);
void parseStatsGroupLine(char *line);
diff --git a/livestatus/src/waittriggers.cc b/livestatus/src/waittriggers.cc
index 915626a..55475d9 100644
--- a/livestatus/src/waittriggers.cc
+++ b/livestatus/src/waittriggers.cc
@@ -40,6 +40,8 @@ std::condition_variable cond_command;
std::condition_variable cond_program;
} // namespace
+std::mutex &trigger_mutex() { return wait_mutex; }
+
std::condition_variable &trigger_all() { return cond_all; }
std::condition_variable &trigger_check() { return cond_check; }
diff --git a/livestatus/src/waittriggers.h b/livestatus/src/waittriggers.h
index 8e4a951..1f37ae2 100644
--- a/livestatus/src/waittriggers.h
+++ b/livestatus/src/waittriggers.h
@@ -28,8 +28,10 @@
#include "config.h" // IWYU pragma: keep
#include <chrono>
#include <condition_variable>
+#include <mutex>
#include <string>
+std::mutex &trigger_mutex();
std::condition_variable &trigger_all();
std::condition_variable &trigger_check();
std::condition_variable &trigger_state();