Module: check_mk
Branch: master
Commit: 1a1984ef27dd019e464c547b0c4257db844160eb
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=1a1984ef27dd01…
Author: Simon Betz <si(a)mathias-kettner.de>
Date: Thu Nov 29 12:44:07 2018 +0100
Make loading/filtering persisted sections race condition free
Change-Id: Iecce769aac82f1455f97c9b24fef533b871cc5c5
---
cmk_base/data_sources/abstract.py | 37 ++++++++++++++-----------------------
1 file changed, 14 insertions(+), 23 deletions(-)
diff --git a/cmk_base/data_sources/abstract.py b/cmk_base/data_sources/abstract.py
index ff61691..d181e69 100644
--- a/cmk_base/data_sources/abstract.py
+++ b/cmk_base/data_sources/abstract.py
@@ -430,7 +430,7 @@ class DataSource(object):
if not persisted_sections:
return host_sections
- for section_name, entry in persisted_sections.items():
+ for section_name, entry in persisted_sections.iteritems():
if len(entry) == 2:
continue # Skip entries of "old" format
@@ -450,44 +450,35 @@ class DataSource(object):
file_path = self._persisted_sections_file_path()
persisted_sections = store.load_data_from_file(file_path, {})
- persisted_sections =
self._filter_outdated_persisted_sections(persisted_sections)
+ filtered_persisted_sections =
self._filter_outdated_persisted_sections(persisted_sections)
- if not persisted_sections:
+ if not filtered_persisted_sections:
self._logger.debug("No persisted sections loaded")
- else:
- self._logger.debug(
- "Loaded persisted sections: %s" % (",
".join(persisted_sections.keys())))
+ try:
+ os.remove(self._persisted_sections_file_path())
+ except OSError:
+ pass
- return persisted_sections
+ return filtered_persisted_sections
# TODO: This is not race condition free when modifying the data. Either remove
# the possible write here and simply ignore the outdated sections or lock when
# reading and unlock after writing
def _filter_outdated_persisted_sections(self, persisted_sections):
+ filtered_persisted_sections = {}
now = time.time()
- modified = False
- for section_name, entry in persisted_sections.items():
+ for section_name, entry in persisted_sections.iteritems():
if len(entry) == 2:
persisted_until = entry[0]
else:
persisted_until = entry[1]
if not self._use_outdated_persisted_sections and now > persisted_until:
- self._logger.debug("Persisted section %s is outdated by %d seconds.
Deleting it." %
+ self._logger.debug("Persisted section %s is outdated by %d seconds.
Skipping it." %
(section_name, now - persisted_until))
- del persisted_sections[section_name]
- modified = True
-
- if not persisted_sections:
- try:
- os.remove(self._persisted_sections_file_path())
- except OSError:
- pass
-
- elif modified:
- self._store_persisted_sections(persisted_sections)
-
- return persisted_sections
+ continue
+ filtered_persisted_sections[section_name] = entry
+ return filtered_persisted_sections
@classmethod
def use_outdated_persisted_sections(cls):