diff options
author | Ilya Mashchenko <ilyamaschenko@gmail.com> | 2018-11-26 12:43:44 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-26 12:43:44 +0300 |
commit | 44eef8d1039dfea033a1caeb3e7fcfcc5d34a828 (patch) | |
tree | 13b6b6ae77fe89eafde8fcbac953d2fbc33074a7 /collectors/python.d.plugin/python_modules | |
parent | 4964cfc9d9b00b983e7a700d18bc363c32d3f1de (diff) |
python.d: use real time for calc sinceLast (#4720)
* use real time clock to calc since last update
* python.d: remove unused attr from RuntimeCounters
Diffstat (limited to 'collectors/python.d.plugin/python_modules')
3 files changed, 54 insertions, 59 deletions
diff --git a/collectors/python.d.plugin/python_modules/bases/FrameworkServices/SimpleService.py b/collectors/python.d.plugin/python_modules/bases/FrameworkServices/SimpleService.py index dd53fbc14a..9708ef7181 100644 --- a/collectors/python.d.plugin/python_modules/bases/FrameworkServices/SimpleService.py +++ b/collectors/python.d.plugin/python_modules/bases/FrameworkServices/SimpleService.py @@ -5,7 +5,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later from threading import Thread -from time import sleep +from time import sleep, time from third_party.monotonic import monotonic @@ -17,25 +17,42 @@ RUNTIME_CHART_UPDATE = 'BEGIN netdata.runtime_{job_name} {since_last}\n' \ 'SET run_time = {elapsed}\n' \ 'END\n' +PENALTY_EVERY = 5 + class RuntimeCounters: def __init__(self, configuration): """ :param configuration: <dict> """ - self.FREQ = int(configuration.pop('update_every')) - self.START_RUN = 0 - self.NEXT_RUN = 0 - self.PREV_UPDATE = 0 - self.SINCE_UPDATE = 0 - self.ELAPSED = 0 - self.RETRIES = 0 - self.RETRIES_MAX = configuration.pop('retries') - self.PENALTY = 0 - self.RUNS = 1 - - def is_sleep_time(self): - return self.START_RUN < self.NEXT_RUN + self.update_every = int(configuration.pop('update_every')) + self.max_retries = int(configuration.pop('retries')) + + self.start_mono = 0 + self.start_real = 0 + self.retries = 0 + self.penalty = 0 + self.elapsed = 0 + self.prev_update = 0 + self.runs = 1 + + def calc_next(self): + self.start_mono = monotonic() + return self.start_mono - (self.start_mono % self.update_every) + self.update_every + self.penalty + + def sleep_until_next(self): + next_time = self.calc_next() + while self.start_mono < next_time: + sleep(next_time - self.start_mono) + self.start_mono = monotonic() + self.start_real = time() + + def handle_retries(self): + self.retries += 1 + if self.retries % PENALTY_EVERY: + return True + self.penalty = self.retries * self.update_every / 2 + return self.retries < self.max_retries class SimpleService(Thread, PythonDLimitedLogger, OldVersionCompatibility, object): @@ -83,11 +100,11 @@ class SimpleService(Thread, PythonDLimitedLogger, OldVersionCompatibility, objec @property def runs_counter(self): - return self._runtime_counters.RUNS + return self._runtime_counters.runs @property def update_every(self): - return self._runtime_counters.FREQ + return self._runtime_counters.update_every @update_every.setter def update_every(self, value): @@ -95,7 +112,7 @@ class SimpleService(Thread, PythonDLimitedLogger, OldVersionCompatibility, objec :param value: <int> :return: """ - self._runtime_counters.FREQ = value + self._runtime_counters.update_every = value def get_update_every(self): return self.update_every @@ -163,41 +180,40 @@ class SimpleService(Thread, PythonDLimitedLogger, OldVersionCompatibility, objec :return: None """ job = self._runtime_counters - self.debug('started, update frequency: {freq}, ' - 'retries: {retries}'.format(freq=job.FREQ, retries=job.RETRIES_MAX - job.RETRIES)) + self.debug('started, update frequency: {freq}, retries: {retries}'.format( + freq=job.update_every, + retries=job.max_retries - job.retries), + ) while True: - job.START_RUN = monotonic() - - job.NEXT_RUN = job.START_RUN - (job.START_RUN % job.FREQ) + job.FREQ + job.PENALTY - - self.sleep_until_next_run() + job.sleep_until_next() - if job.PREV_UPDATE: - job.SINCE_UPDATE = int((job.START_RUN - job.PREV_UPDATE) * 1e6) + since = 0 + if job.prev_update: + since = int((job.start_real - job.prev_update) * 1e6) try: - updated = self.update(interval=job.SINCE_UPDATE) + updated = self.update(interval=since) except Exception as error: self.error('update() unhandled exception: {error}'.format(error=error)) updated = False - job.RUNS += 1 + job.runs += 1 if not updated: - if not self.manage_retries(): + if not job.handle_retries(): return else: - job.ELAPSED = int((monotonic() - job.START_RUN) * 1e3) - job.PREV_UPDATE = job.START_RUN - job.RETRIES, job.PENALTY = 0, 0 + job.elapsed = int((monotonic() - job.start_mono) * 1e3) + job.prev_update = job.start_real + job.retries, job.penalty = 0, 0 safe_print(RUNTIME_CHART_UPDATE.format(job_name=self.name, - since_last=job.SINCE_UPDATE, - elapsed=job.ELAPSED)) + since_last=since, + elapsed=job.elapsed)) self.debug('update => [{status}] (elapsed time: {elapsed}, ' 'retries left: {retries})'.format(status='OK' if updated else 'FAILED', - elapsed=job.ELAPSED if updated else '-', - retries=job.RETRIES_MAX - job.RETRIES)) + elapsed=job.elapsed if updated else '-', + retries=job.max_retries - job.retries)) def update(self, interval): """ @@ -233,27 +249,6 @@ class SimpleService(Thread, PythonDLimitedLogger, OldVersionCompatibility, objec return updated - def manage_retries(self): - rc = self._runtime_counters - rc.RETRIES += 1 - if rc.RETRIES % 5 == 0: - rc.PENALTY = int(rc.RETRIES * self.update_every / 2) - if rc.RETRIES >= rc.RETRIES_MAX: - self.error('stopped after {0} data collection failures in a row'.format(rc.RETRIES_MAX)) - return False - return True - - def sleep_until_next_run(self): - job = self._runtime_counters - - # sleep() is interruptable - while job.is_sleep_time(): - sleep_time = job.NEXT_RUN - job.START_RUN - self.debug('sleeping for {sleep_time} to reach frequency of {freq} sec'.format(sleep_time=sleep_time, - freq=job.FREQ + job.PENALTY)) - sleep(sleep_time) - job.START_RUN = monotonic() - def get_data(self): return self._get_data() diff --git a/collectors/python.d.plugin/python_modules/bases/charts.py b/collectors/python.d.plugin/python_modules/bases/charts.py index 2963739ec3..0a0719056f 100644 --- a/collectors/python.d.plugin/python_modules/bases/charts.py +++ b/collectors/python.d.plugin/python_modules/bases/charts.py @@ -45,7 +45,7 @@ def create_runtime_chart(func): ok = func(*args, **kwargs) if ok: safe_print(RUNTIME_CHART_CREATE.format(job_name=self.name, - update_every=self._runtime_counters.FREQ)) + update_every=self._runtime_counters.update_every)) return ok return wrapper diff --git a/collectors/python.d.plugin/python_modules/bases/loggers.py b/collectors/python.d.plugin/python_modules/bases/loggers.py index 39be77a794..098294d3e1 100644 --- a/collectors/python.d.plugin/python_modules/bases/loggers.py +++ b/collectors/python.d.plugin/python_modules/bases/loggers.py @@ -34,7 +34,7 @@ def limiter(log_max_count=30, allowed_in_seconds=60): def on_decorator(func): def on_call(*args): - current_time = args[0]._runtime_counters.START_RUN + current_time = args[0]._runtime_counters.start_mono lc = args[0]._logger_counters if lc.logged and lc.logged % log_max_count == 0: |