def __init__(self, max_wait_for_replay, max_queue_size, throttling_delay, agent_config): super(TransactionManager, self).__init__(agent_config) self._MAX_WAIT_FOR_REPLAY = max_wait_for_replay self._MAX_QUEUE_SIZE = max_queue_size self._THROTTLING_DELAY = throttling_delay self._flush_without_ioloop = False # useful for tests self._transactions = [] # List of all non commited transactions self._total_count = 0 # Maintain size/count not to recompute it everytime self._total_size = 0 self._flush_count = 0 self._transactions_received = 0 self._transactions_flushed = 0 # Global counter to assign a number to each transaction: we may have an issue # if this overlaps self._counter = 0 self._trs_to_flush = None # Current transactions being flushed self._last_flush = datetime.now() # Last flush (for throttling) # Track an initial status message. check_status.ForwarderStatus().persist()
def flush(self): if self._trs_to_flush is not None: log.debug("A flush is already in progress, not doing anything") return to_flush = [] # Do we have something to do ? now = datetime.now() for tr in self._transactions: if tr.time_to_flush(now): to_flush.append(tr) count = len(to_flush) should_log = self._flush_count + 1 <= FLUSH_LOGGING_INITIAL or \ (self._flush_count + 1) % FLUSH_LOGGING_PERIOD == 0 if count > 0: if should_log: log.info( "Flushing %s transaction%s during flush #%s" % (count, util.plural(count), str(self._flush_count + 1))) else: log.debug( "Flushing %s transaction%s during flush #%s" % (count, util.plural(count), str(self._flush_count + 1))) timer = util.Timer() self._trs_to_flush = to_flush self.flush_next() # The emit time is reported on the next run. dimensions = self._set_dimensions({ 'component': 'monasca-agent', 'service': 'monitoring' }) emit_measurement = metrics.Measurement('monasca.emit_time_sec', time.time(), timer.step(), dimensions) MetricTransaction([emit_measurement], headers={'Content-Type': 'application/json'}) else: if should_log: log.info("No transaction to flush during flush #%s" % str(self._flush_count + 1)) else: log.debug("No transaction to flush during flush #%s" % str(self._flush_count + 1)) if self._flush_count + 1 == FLUSH_LOGGING_INITIAL: log.info( "First flushes done, next flushes will be logged every %s flushes." % FLUSH_LOGGING_PERIOD) self._flush_count += 1 check_status.ForwarderStatus( queue_length=self._total_count, queue_size=self._total_size, flush_count=self._flush_count, transactions_received=self._transactions_received, transactions_flushed=self._transactions_flushed).persist()