def push_states(self): # lock the state and generate its various forms global STATE_LOCK with STATE_LOCK: # do not push states until every member's health status # has been determined if not STATE.health_converged: return # generate ppdns distribution form of the state dist_form = STATE.to_dist_dict() # generate generic form of the state generic_form = util.instance_to_dict(STATE, ignore_private=True) # all memcache pushes must succeed in order to # update the state pushed flag(self.state_ts) pushes_ok = 0 # push PPDNS distribution form of the state val = self.sm.set(config.BASE['SHARED_MEM_PPDNS_STATE_KEY'], dist_form) if val is True: pushes_ok += 1 else: log_msg = ('failed to write ppdns state to the shared memory') LOG.error(log_msg) # push generic form of the state # add timestampt to the object generic_form['timestamp'] = STATE_TIMESTAMP val = self.sm.set(config.BASE['SHARED_MEM_GENERIC_STATE_KEY'], generic_form) if val is True: pushes_ok += 1 else: log_msg = ('failed to write generic state to the shared memory') LOG.error(log_msg) # push state timestamp last val = self.sm.set(config.BASE['SHARED_MEM_STATE_TIMESTAMP_KEY'], STATE_TIMESTAMP) if val is True: pushes_ok += 1 else: log_msg = ('failed to write state timestamp to the shared memory') LOG.error(log_msg) # if all memcache pushes are successful # set self.state_ts to STATE_TIMESTAMP so we don't attempt to # push again until STATE_TIMESTAMP changes if pushes_ok == 3: LOG.debug('synced state to the shared memory') self.state_ts = STATE_TIMESTAMP
def run(self): """Main execution loop""" # init last_scan_state_time so we know when to run the first scan last_scan_state_time = time.time() # Track whether status of any of the backend servers changed. # If the state has changed we will push it to share _mem, but no more # often than SCAN_STATE_INTERVAL self.state_changed = False while True: # read probe response and process it try: # block with a small timeout, # non-blocking will load cpu needlessly probe = self.probe_response_queue.get( True, PROBE_RESPONSE_QUEUE_WAIT) except queue.Empty: # nothing on the queue pass else: self._process_probe(probe) # periodically iterate the state and issue new probe requests, # if there was a state change in the last SCAN_STATE_INTERVAL, # push it to shared mem if time.time() - last_scan_state_time > SCAN_STATE_INTERVAL: # update last scan state time last_scan_state_time = time.time() # if the state changed, push it to the shared memory if self.state_changed: # push PPDSN distribution form of the state self._sm.set(config.BASE['SHARED_MEM_PPDNS_STATE_KEY'], self.state.to_dist_dict()) # push generic form of the state obj = util.instance_to_dict(self.state) obj['timestamp'] = time.time() self._sm.set(config.BASE['SHARED_MEM_GENERIC_STATE_KEY'], obj) # reset state changed flag self.state_changed = False LOG.debug('synced state, version to the shared memory') # iterate the state, issue new probe requests self._scan_state()
def push_states(self): # lock the state and generate its various forms global STATE_LOCK with STATE_LOCK: # generate ppdns distribution form of the state dist_form = STATE.to_dist_dict() # generate generic form of the state generic_form = util.instance_to_dict(STATE) # all memcache pushes must succeed in order to # reset state changed flag pushes_ok = 0 # push PPDNS distribution form of the state val = self.sm.set(config.BASE['SHARED_MEM_PPDNS_STATE_KEY'], dist_form) if val is True: pushes_ok += 1 else: log_msg = 'failed to write ppdns state to the shared memory' LOG.warning(log_msg) # push generic form of the state # add timestampt to the object generic_form['timestamp'] = STATE_TIMESTAMP val = self.sm.set(config.BASE['SHARED_MEM_GENERIC_STATE_KEY'], generic_form) if val is True: pushes_ok += 1 else: log_msg = 'failed to write generic state to the shared memory' LOG.warning(log_msg) # push state timestamp last val = self.sm.set(config.BASE['SHARED_MEM_STATE_TIMESTAMP_KEY'], STATE_TIMESTAMP) if val is True: pushes_ok += 1 else: log_msg = 'failed to write state timestamp to the shared memory' LOG.warning(log_msg) # if all memcache pushes are successful # set self.state_ts to STATE_TIMESTAMP so we don't attempt to # push again until STATE_TIMESTAMP changes if pushes_ok == 3: LOG.debug('synced state to the shared memory') self.state_ts = STATE_TIMESTAMP
def print_state(self): print('state:') print(to_pretty_json(util.instance_to_dict(self.state)))
def run(self): """Main execution loop""" # init last_scan_state_time so we know when to run the first scan last_scan_state_time = time.time() # Track whether status of any of the backend servers changed. # If the state has changed we will push it to shared mem, but no more # often than SCAN_STATE_INTERVAL self.state_changed = False while True: # read probe response and process it try: # block with a small timeout, # non-blocking will load cpu needlessly probe = self.prober_responses.get( block=True, timeout=PROBE_RESPONSES_QUEUE_WAIT) except queue.Empty: # nothing on the queue pass else: self._process_probe(probe) # periodically iterate the state and issue new probe requests, # if there was a state change in the last SCAN_STATE_INTERVAL, # push it to shared mem if time.time() - last_scan_state_time > SCAN_STATE_INTERVAL: # update last scan state time last_scan_state_time = time.time() # if the state changed, push it to the shared memory if self.state_changed: # all memcache pushes must succeed in order to # reset state changed flag pushes_ok = 0 # push PPDNS distribution form of the state val = self._sm.set( config.BASE['SHARED_MEM_PPDNS_STATE_KEY'], self.state.to_dist_dict()) if val is True: pushes_ok += 1 else: log_msg = ('failed to write ppdns ' 'state to the shared memory') LOG.warning(log_msg) # push generic form of the state obj = util.instance_to_dict(self.state) # add epoch time timestampt to the object obj['timestamp'] = time.time() val = self._sm.set( config.BASE['SHARED_MEM_GENERIC_STATE_KEY'], obj) if val is True: pushes_ok += 1 else: log_msg = ('failed to write generic ' 'state to the shared memory') LOG.warning(log_msg) # if all memcache pushes are successful reset # state changed flag, otherwise keep it as True # so a push is attempted on the next iteration if pushes_ok == 2: LOG.debug('synced state to the shared memory') # reset state changed flag self.state_changed = False # iterate the state, issue new probe requests self._scan_state()