def handle_peers_ready_query(self, message_obj): peer_id = message_obj.sender if peer_id not in self._configs: return cmsg.fill_msg(types.CONFIG_ERROR), types.CONFIG_ERROR, None green_light = True for dep in message_obj.deps: if not dep in self._ready_peers: green_light = False return cmsg.fill_msg(types.READY_STATUS, receiver=peer_id, peers_ready=green_light), types.READY_STATUS, None
def register_config(self, connection): if self.peer is None: raise NoPeerError() msg = cmsg.fill_msg(types.REGISTER_PEER_CONFIG, sender=self.peer_id) params = self.core.local_params cmsg.dict2params(params, msg) ext_params = self.core.ext_param_defs # register also external param definitions: param_name <---> (peer_id_of_config_source, param_name) for par in ext_params: ext_def = ext_params[par] symname = ext_def[0] ext_params[par] = (self.core.config_sources[symname], ext_def[1]) cmsg.dict2params(ext_params, msg, field_name="ext_params") #connection.send_message(message=msg, type=types.REGISTER_PEER_CONFIG) reply = self.__query(connection, cmsg.pack_msg(msg), types.REGISTER_PEER_CONFIG) #print 'AAAAAAAAAAAAAAAAAA', reply, "(rq:", types.REGISTER_PEER_CONFIG,\ # "exp:", types.PEER_REGISTERED, ')' if reply is None: self.logger.error('config registration unsuccesful!!!! %s', str(reply)) elif not reply.type == types.PEER_REGISTERED: self.logger.error('config registration unsuccesful!!!! %s', str(reply))
def _handle_peer_ready_signal(self, p_msg): if not self.peer.ready_to_work and self.config_ready(): self.peer.ready_to_work = True self.send_peer_ready(self.peer.conn) return None, None else: return cmsg.fill_msg(types.CONFIG_ERROR), types.CONFIG_ERROR
def test(self): req = cmsg.fill_msg(types.GET_CONFIG_PARAMS, sender='aaa', param_names=['a'], receiver='') received = 0 for i in range(SEND): msg = self.__query(self.conn, req, types.DICT_GET_REQUEST_MESSAGE) if msg is None: print ":(((" continue if int(msg.message) == received + 1: received += 1 if received % 10000 == 0: print "made ", received, "queries" if received == SEND: print "[mx peer...] OK" else: print "[mx peer...] WUT?", received self.set_param('ach', '567')
def register_config(self, connection): if self.peer is None: raise NoPeerError() msg = cmsg.fill_msg(types.REGISTER_PEER_CONFIG, sender=self.peer_id) params = self.core.local_params cmsg.dict2params(params, msg) ext_params = self.core.ext_param_defs # register also external param definitions: param_name <---> (peer_id_of_config_source, param_name) for par in ext_params: ext_def = ext_params[par] symname = ext_def[0] ext_params[par] = (self.core.config_sources[symname], ext_def[1]) cmsg.dict2params(ext_params, msg, field_name="ext_params") # connection.send_message(message=msg, type=types.REGISTER_PEER_CONFIG) reply = self.__query(connection, cmsg.pack_msg(msg), types.REGISTER_PEER_CONFIG) # print 'AAAAAAAAAAAAAAAAAA', reply, "(rq:", types.REGISTER_PEER_CONFIG,\ # "exp:", types.PEER_REGISTERED, ')' if reply is None: self.logger.error('config registration unsuccesful!!!! %s', str(reply)) elif not reply.type == types.PEER_REGISTERED: self.logger.error('config registration unsuccesful!!!! %s', str(reply))
def handle_peer_ready(self, message_obj): peer_id = message_obj.peer_id if peer_id not in self._configs: return cmsg.fill_msg(types.CONFIG_ERROR), types.CONFIG_ERROR, None self._ready_peers.append(peer_id) launcher_msg = self.mtool.fill_msg('obci_peer_ready', peer_id=peer_id) return message_obj, types.PEER_READY, launcher_msg
def handle_register_peer_config(self, message_obj): params = cmsg.params2dict(message_obj) ext_params = cmsg.params2dict(message_obj, field_name="ext_params") peer_id = message_obj.sender launcher_msg = None if peer_id in self._configs: mtype = types.CONFIG_ERROR msg = cmsg.fill_msg(mtype) else: self._configs[peer_id] = params self._ext_configs[peer_id] = ext_params mtype = types.PEER_REGISTERED msg = cmsg.fill_msg(mtype, peer_id=peer_id) launcher_msg = self.mtool.fill_msg('obci_peer_registered', peer_id=peer_id, params=params) self._save_config() return msg, mtype, launcher_msg
def _request_ext_params(self, connection, retries=400): #TODO set timeout and retry count self.logger.info("requesting external parameters") if self.peer is None: raise NoPeerError def _unset_param_count(): return reduce(lambda x, y: x + y, [len(self.core.unset_params_for_source(src)) \ for src in self.core.used_config_sources()], 0) ready, details = self.core.config_ready() while not ready and retries: for src in self.core.used_config_sources(): params = self.core.unset_params_for_source(src).keys() msg = cmsg.fill_msg(types.GET_CONFIG_PARAMS, sender=self.peer_id, param_names=params, receiver=self.core.config_sources[src]) #print "requesting: {0}".format(msg) reply = self.__query(connection, cmsg.pack_msg(msg), types.GET_CONFIG_PARAMS) if reply == None: # raise something? continue if reply.type == types.CONFIG_ERROR: self.logger.warning("peer {0} has not yet started".format( msg.receiver)) elif reply.type == types.CONFIG_PARAMS: reply_msg = cmsg.unpack_msg(reply.type, reply.message) params = cmsg.params2dict(reply_msg) for par, val in params.iteritems(): self.core.set_param_from_source( reply_msg.sender, par, val) else: self.logger.error("WTF? {0}".format(reply.message)) # print '.',#"{0} external params still unset".format(_unset_param_count()) time.sleep(0.4) ready, details = self.core.config_ready() retries -= 1 if ready: self.logger.info("External parameters initialised %s", str(self.core.config_ready())) return ready, details
def _request_ext_params(self, connection, retries=400): # TODO set timeout and retry count self.logger.info("requesting external parameters") if self.peer is None: raise NoPeerError def _unset_param_count(): return reduce(lambda x, y: x + y, [len(self.core.unset_params_for_source(src)) for src in self.core.used_config_sources()], 0) ready, details = self.core.config_ready() while not ready and retries: for src in self.core.used_config_sources(): params = list(self.core.unset_params_for_source(src).keys()) msg = cmsg.fill_msg(types.GET_CONFIG_PARAMS, sender=self.peer_id, param_names=params, receiver=self.core.config_sources[src]) # print "requesting: {0}".format(msg) reply = self.__query(connection, cmsg.pack_msg(msg), types.GET_CONFIG_PARAMS) if reply is None: # raise something? continue if reply.type == types.CONFIG_ERROR: self.logger.warning("peer {0} has not yet started".format(msg.receiver)) elif reply.type == types.CONFIG_PARAMS: reply_msg = cmsg.unpack_msg(reply.type, reply.message) params = cmsg.params2dict(reply_msg) for par, val in params.items(): self.core.set_param_from_source(reply_msg.sender, par, val) else: self.logger.error("WTF? {0}".format(reply.message)) # print '.',#"{0} external params still unset".format(_unset_param_count()) time.sleep(0.4) ready, details = self.core.config_ready() retries -= 1 if ready: self.logger.info("External parameters initialised %s", str(self.core.config_ready())) return ready, details
def handle_update_params(self, message_obj): params = cmsg.params2dict(message_obj) param_owner = message_obj.sender if param_owner not in self._configs: launcher_msg = None return cmsg.fill_msg(types.CONFIG_ERROR, error_str="Peer unknown: {0}".format(param_owner)),\ types.CONFIG_ERROR,\ launcher_msg updated = {} for param in params: if param in self._configs[param_owner]: self._configs[param_owner][param] = params[param] updated[param] = params[param] if updated: mtype = types.PARAMS_CHANGED msg = cmsg.fill_msg(types.PARAMS_CHANGED, sender=param_owner) cmsg.dict2params(updated, msg) launcher_msg = self.mtool.fill_msg('obci_peer_params_changed', peer_id=param_owner, params=updated) self._save_config() return msg, mtype, launcher_msg return None, None, None
def run(self): conn = connect_client(type=peers.CONFIGURER, addresses=settings.MULTIPLEXER_ADDRESSES) time.sleep(4) for i in range(5): msg = cmsg.fill_msg(types.UPDATE_PARAMS, sender="p_a") params = dict(p=str(i)) cmsg.dict2params(params, msg) try: conn.send_message(message=msg, type=types.UPDATE_PARAMS) resp, sth = conn.receive_message() msg = cmsg.unpack_msg(resp.type, resp.message) print "got ", msg except Exception as e: print "error: ", e.args, " ", type(e) print i
def _get_params(self, param_owner, names, params=None): if params is None: params = {} self.logger.info("looking for %s, param names=%s" % (param_owner, str(names))) if param_owner not in self._configs: self.logger.info("%s not in %s" % (param_owner, str(self._configs))) return cmsg.fill_msg(types.CONFIG_ERROR), types.CONFIG_ERROR, None for name in names: if name in self._configs[param_owner]: params[name] = self._configs[param_owner][name] elif name in self._ext_configs[param_owner]: owner, name = self._ext_configs[param_owner][name] params = self._get_params(owner, [name], params) return params
def set_param(self, p_name, p_value): result = self.core.update_local_param(p_name, p_value) #TODO let know other peers... if self.query_conn: msg = cmsg.fill_msg(types.UPDATE_PARAMS, sender=self.peer_id) params = {p_name: p_value} cmsg.dict2params(params, msg) reply = self.__query(self.query_conn, msg, types.UPDATE_PARAMS) #self.connection.send_message(message=msg, type=types.UPDATE_PARAMS) val_short = str(p_value)[:300] + '[...]' self.logger.info(' param update:: %s %s', p_name, val_short) else: self.logger.warning('param updated locally %s, %s, %s', p_name, val_short, str(result)) return result
def set_param(self, p_name, p_value): result = self.core.update_local_param(p_name, p_value) # TODO let know other peers... if self.query_conn: msg = cmsg.fill_msg(types.UPDATE_PARAMS, sender=self.peer_id) params = {p_name: p_value} cmsg.dict2params(params, msg) self.__query(self.query_conn, msg, types.UPDATE_PARAMS) # self.connection.send_message(message=msg, type=types.UPDATE_PARAMS) val_short = str(p_value)[:300] + '[...]' self.logger.info(' param update:: %s %s', p_name, val_short) else: self.logger.warning('param updated locally %s, %s, %s', p_name, val_short, str(result)) return result
def handle_get_config_params(self, message_obj): param_owner = message_obj.receiver names = message_obj.param_names if param_owner == 'config_server': params = dict(experiment_uuid=self.exp_uuid) # elif param_owner not in self._configs: # return cmsg.fill_msg(types.CONFIG_ERROR), types.CONFIG_ERROR, None else: #TODO error when param_name does not exist? # params = {} # for name in names: # if name in self._configs[param_owner]: # params[name] = self._configs[param_owner][name] params = self._get_params(param_owner, names) if isinstance(params, tuple): return params mtype = types.CONFIG_PARAMS msg = cmsg.fill_msg(mtype, sender=param_owner) cmsg.dict2params(params, msg) return msg, mtype, None
def restart_scenario(conn, new_scenario, comment="Wait...", leave_on=[], overwrites=[]): """ new_scenario: scenario_path relative to obci_root overwrites: {'peer_id' : ['-p', 'param_name', 'param_value', '-p', 'etc', 'etc', '-f' 'config_ini_file_path_relative_to_obci_root']} leave_on: ['list', 'of', 'peer_ids', 'which', 'are', 'in', 'both', 'scenarios', 'and', 'we', 'do','not', 'want', 'them', 'to', 'restart'] """ if new_scenario.startswith('/') or new_scenario.startswith('~'): new_scenario = os.path.expanduser(new_scenario) else: new_scenario = os.path.join(obci_root(), new_scenario) conf_msg = cmsg.fill_msg(types.GET_CONFIG_PARAMS, sender='', param_names=['experiment_uuid'], receiver='config_server') try: reply = conn.query(message=conf_msg, type=types.GET_CONFIG_PARAMS) except OperationFailed: print "OperationFailed (in restart_scenario) Could not connect to config server" reply = None except OperationTimedOut: print "Operation timed out! (in restart_scenario) (could not connect to config server)" reply = None if reply == None: return if reply.type == types.CONFIG_ERROR: print "(in restart_scenario) could not acquire current experiment uuid" return elif reply.type == types.CONFIG_PARAMS: reply_msg = cmsg.unpack_msg(reply.type, reply.message) params = cmsg.params2dict(reply_msg) if not 'experiment_uuid' in params: print "no experiment_uuid!!!" else: uid = params['experiment_uuid'] if not uid: print 'experiment uuid unset!!!' else: ovr_list = ['--ovr'] for peer in overwrites: scan = overwrites[peer] params = overwrites[peer] cut = 0 while scan: if '-f' in scan: ind = scan.index('-f') params[cut+ind+1] = os.path.join(params[cut+ind+1]) cut = cut+ind+1 scan = params[cut:] else: break # for i in range(len(params)): # params[i] = params[i].replace(" ", "\ ") # print params[i] ovr_list += ['--peer', peer] ovr_list += params # leave_on_str = '' if not leave_on else ' --leave_on ' + ' '.join(leave_on) # overwr_str = '' if not overwrites else ' '.join(ovr_list) # command = "sleep 0.5 && obci morph " + str(uid) + " " + new_scenario + \ # " " + leave_on_str + overwr_str + " &" obci_call = ["obci"] try: subprocess.call(obci_call) except: # (OSError, WindowsError) can't call launcher like that obci_call = ["python", os.path.join(obci_root(), "control", "launcher", "obci_script.py")] subpr_call = obci_call + ['morph', str(uid), new_scenario, '--leave_on'] + leave_on + ovr_list print "---------------\n", subpr_call, "\n--------------" subprocess.call(subpr_call)
def restart_scenario(conn, new_scenario, comment="Wait...", leave_on=[], overwrites=[]): """ new_scenario: scenario_path relative to obci_root overwrites: {'peer_id' : ['-p', 'param_name', 'param_value', '-p', 'etc', 'etc', '-f' 'config_ini_file_path_relative_to_obci_root']} leave_on: ['list', 'of', 'peer_ids', 'which', 'are', 'in', 'both', 'scenarios', 'and', 'we', 'do','not', 'want', 'them', 'to', 'restart'] """ if new_scenario.startswith('/') or new_scenario.startswith('~'): new_scenario = os.path.expanduser(new_scenario) else: new_scenario = os.path.join(obci_root(), new_scenario) conf_msg = cmsg.fill_msg(types.GET_CONFIG_PARAMS, sender='', param_names=['experiment_uuid'], receiver='config_server') try: reply = conn.query(message=conf_msg, type=types.GET_CONFIG_PARAMS) except OperationFailed: print "OperationFailed (in restart_scenario) Could not connect to config server" reply = None except OperationTimedOut: print "Operation timed out! (in restart_scenario) (could not connect to config server)" reply = None if reply == None: return if reply.type == types.CONFIG_ERROR: print "(in restart_scenario) could not acquire current experiment uuid" return elif reply.type == types.CONFIG_PARAMS: reply_msg = cmsg.unpack_msg(reply.type, reply.message) params = cmsg.params2dict(reply_msg) if not 'experiment_uuid' in params: print "no experiment_uuid!!!" else: uid = params['experiment_uuid'] if not uid: print 'experiment uuid unset!!!' else: ovr_list = ['--ovr'] for peer in overwrites: scan = overwrites[peer] params = overwrites[peer] cut = 0 while scan: if '-f' in scan: ind = scan.index('-f') params[cut+ind+1] = os.path.join(obci_root(), params[cut+ind+1]) cut = cut+ind+1 scan = params[cut:] else: break # for i in range(len(params)): # params[i] = params[i].replace(" ", "\ ") # print params[i] ovr_list += ['--peer', peer] ovr_list += params # leave_on_str = '' if not leave_on else ' --leave_on ' + ' '.join(leave_on) # overwr_str = '' if not overwrites else ' '.join(ovr_list) # command = "sleep 0.5 && obci morph " + str(uid) + " " + new_scenario + \ # " " + leave_on_str + overwr_str + " &" subpr_call = ['obci', 'morph', str(uid), new_scenario, '--leave_on'] + leave_on + ovr_list print "---------------\n", subpr_call, "\n--------------" subprocess.call(subpr_call)