def randomLr(self, name="level"): """ 返回当前lrcluster内sysload最低的lr """ # if name == "level": # sqls = """select `lr_id` from `lr_node` where `level_id`='%s' order by `sysload` limit 1""" % self.lrcid # res = db.execFetch(sqls) # if len(res) == 0: return None # lrid = res[0][0] # # for redis if name == "level": lr_list = redis_client.keys("lrid:%s:*" % self.lrcid) if not lr_list: return None best_lr = lr_list[0] for lr in lr_list: sysload = redis_client.hget(lr, "sysload") active = redis_client.hget(lr, "active") if sysload < redis_client.hget(best_lr, "sysload") and active == '1': best_lr = lr lrid = best_lr.split(":")[2] else: lr_list = [] sqls = """ select `lr_id`,`lrc_ids`, `sysload` from `lr_node` where `lrc_id`!='[]'""" # order by `sysload` limit 1""" % self.lrcid res = db.execFetch(sqls) if len(res) == 0: return None for lr in res: lrc_ids = eval(lr[1]) if self.lrcid in lrc_ids: lr_list.append({"lrid": lr[0], "sysload": lr[2]}) lr_list.sort(key=lambda x: (x["sysload"])) lrid = lr_list[0]["lrid"] return lrid
def choose_result(level_id): level_cluster = redis_client.hget("level:%s" % level_id, "level_cluster") level_cluster_list = level_cluster.split(":") lr_list = redis_client.keys("lr:%s:*" % level_id) if len(level_cluster_list) == 1: lr = best_lr(level_id, lr_list) if lr: res = {"status": True, "type": "lrc", "data": lr} else: res = {"status": False, "type": "lrc", "data": None} return res else: lrc_dict = {} lr_res = [] for cluster_type in level_cluster_list: lrc_dict[cluster_type] = [] for lr in lr_list: lr_cluster = redis_client.hget(lr, "cluster") lrc_dict[lr_cluster].append(lr) for k in lrc_dict.keys(): lr = best_lr(level_id, lrc_dict[k]) if not lr: res = {"status": False, "type": "lrg", "data": None} return res lr_res.append(lr) res = {"status": True, "type": "lrg", "data": lr_res} return res
def getAdjacency(epid, item="level_id"): """ 获取指定userid距离最近的层级LR集群 """ ## for redis if item == "level_id": ret_id = redis_client.hget("user:%s" % epid, "level_id") else: ret_id = redis_client.hget("user:%s" % epid, "vsp_id") return ret_id
def _elect_need_cluster(self, level_clusters_list, user_level, user): level_clusters = set(level_clusters_list) exist_clusters = set() if self.existing_level.has_key(user_level): for lr in self.existing_level[user_level]: lr_cluster = redis_client.hget("lr:%s:%s" % (user_level, lr), "cluster") exist_clusters.add(lr_cluster) need_clusters = list(level_clusters - exist_clusters) self.added_level[user_level] = self.existing_level[user_level] for need_cluster in need_clusters: lr = level_cluster_lr(user_level, need_cluster) self.added_level[user_level].append(lr) for lr_obj in self.added_level[user_level]: lrip, lrport, lrtype = redis_client.hmget( "lr:%s:%s" % (user_level, lr_obj), "ip", "port", "lr_type") self.added_data.append({ "lrid": lr_obj, "ip": lrip, "port": lrport, "lr_type": lrtype, "epids": [user], "star": False }) return 1 else: res = loop_choose([user_level], self.logger) if res: data = set_data(self.caller, res) for i in data: i["epids"] = [user] self.added_data.append(i) return 2 else: return 0
def getDlrRoot(level_id): p_level_list = getAncestor(level_id, []) p_level_list.insert(0, level_id) p_level_list.reverse() print p_level_list while True: now_level = p_level_list[-1] lr_level = redis_client.keys("lrid:%s:*" % now_level)[0] lr_type = redis_client.hget(lr_level, "lr_type") if lr_type == "lr": return lr_level.split(":")[1] else: plevel = getPlevel(lr_level.split(":")[1]) p_level_lr = redis_client.keys("lrid:%s:*" % plevel)[0] p_lr_type = redis_client.hget(p_level_lr, "lr_type") if p_lr_type == "lr": return lr_level.split(":")[1] p_level_list.pop()
def split_level(level_id): ancestors_list = redis_client.lrange("ancestors:%s" % level_id, 0, -1) dlr_ancestors_list = [] lr_ancestors_list = [] for level in ancestors_list: level_type = redis_client.hget("level:%s" % level, "level_type") if level_type == "dlr": dlr_ancestors_list.append(level) dlr_len = len(dlr_ancestors_list) lr_ancestors_list = ancestors_list[dlr_len:] return [dlr_ancestors_list, lr_ancestors_list]
def _add_star(self, epid=''): f = 0 for lr in self.added_data: if lr["star"] == True: if epid != '': lr["epids"].append(epid) f = 1 if f == 0: res = set_star(self.caller, self.logger) if epid != '': res["epids"].append(epid) star_level = redis_client.hget("lr:*:%s" % res["lrid"], "level_id") self.added_level[star_level] = [res["lrid"]] self.added_data.append(res)
def getValidlevel(self): qos_list = redis_client.keys("qos:%s:*" % self.src_level) level_list = [] for qos in qos_list: weight = redis_client.hget(qos, "weight") if float(weight) != 0.0: level_list.append(qos.split(":")[2]) # sqls = """select `level_dst` from `net_qos` where `level_src`='%s' and weight!=0"""%self.src_level # res = db.execFetch(sqls) # level_list = [] # for level in res: # level_list.append(level[0]) return level_list
def get_user_info(user): user_level = redis_client.get("user:%s" % user) if user_level: user_level_cluster_value = redis_client.hget("level:%s" % user_level, "level_cluster") if len(user_level_cluster_value) == 1: user_level_cluster = "lrc" else: user_level_cluster = "lrg" dlr_parents, lr_parents = split_level(user_level) if dlr_parents: dlr_root = dlr_parents[-1] else: dlr_root = "" res = { "status": True, "user_level": user_level, "user_level_cluster": user_level_cluster, "dlr_root": dlr_root, "dlr_parents": dlr_parents } else: res = {"status": False} return res
def addcallee(self, user): user_level = redis_client.get("user:%s" % user) if user_level: user_level_cluster_value = redis_client.hget( "level:%s" % user_level, "level_cluster") user_level_cluster_value = user_level_cluster_value.split(":") if len(user_level_cluster_value) == 1: user_level_cluster = "lrc" else: user_level_cluster = "lrg" dlr_parents, lr_parents = split_level(user_level) if dlr_parents: dlr_root = dlr_parents[-1] else: dlr_root = "" else: self.logger.warning("Not found the level for EP user [%s]" % user) return False # 要添加的用户所在层是否已经存在,若存在,只将用户id加到epids键中 res = self._fill_added(user_level, user) if res: return self.added_data # LRG会议,国际台 if self.is_lrg: if not dlr_root and user_level_cluster == "lrg": self._elect_need_cluster(user_level_cluster_value, user_level, user) self.logger.info("LRG meeting, return to normal.") return self.added_data else: star_level = redis_client.hget("lr:*:%s" % self.star["lrid"], "level_id") star_lrid = self.star["lrid"] self._fill_added(star_level, user, star_lrid) # res = self._fill_added(star_level, user, star_lrid) # if not res: # self.added_level[star_level] = [self.star["lrid"]] # self.star["epids"] = [user] # self.added_data.append(self.star) self.logger.info( "LRG meeting, added user[%s] exception, use star LR." % user) return self.added_data if not dlr_root: if self.star: star_level = redis_client.hget("lr:*:%s" % self.star["lrid"], "level_id") star_lrid = self.star["lrid"] self._add_star(user) # self._fill_added(star_level, user, star_lrid) else: self._add_star(user) self.logger.info("Normal lr user[%s], use star LR." % user) else: if user_level_cluster == "lrg": if self.star: num = self._elect_need_cluster(user_level_cluster_value, user_level, user) if num == 0: self._add_star(user) else: num = self._elect_need_cluster(user_level_cluster_value, user_level, user) if num == 0: self._add_star(user) else: self._add_star() self.logger.info("Normal dlrg user[%s]." % user) else: n = None for dlr_level in dlr_parents: if dlr_level in self.existing_level: n = dlr_level break if n: lrid = self.existing_level[n][0] lr = self.existing_lr[lrid] lr["epids"] = [user] self.added_data.append(lr) else: res = loop_choose([dlr_root], self.logger) if not res: self.logger.error( "User %s on level %s has not valid dlr.") if self.star: star_level = redis_client.hget( "lr:*:%s" % self.star["lrid"], "level_id") self.added_level[star_level] = [self.star["lrid"]] self.star["epids"] = [user] self.added_data.append(self.star) else: self._add_star(user) else: lr_id = res["data"].split(":")[2] self._fill_added(dlr_root, user, lr_id) if not self.star: self._add_star() self.logger.info("Normal dlrc user[%s]." % user)
def elect_func(callerid, adjacency_list, all_person_level, logger): all_dlr_level_parents = {} dlr_root_level = None lr_type_users = [] for level_id in adjacency_list: level_type = redis_client.hget("level:%s" % level_id, "level_type") # parents_list = redis_client.lrange("ancestors:%s"%level_id, 0, -1) if level_type == "dlr": dlr_parents_list, _ = split_level(level_id) dlr_root_level = dlr_parents_list[-1] all_dlr_level_parents[level_id] = dlr_parents_list else: lr_type_users += all_person_level[level_id] if not dlr_root_level: caller_level = redis_client.get("user:%s" % callerid) caller_lr_parents_list = redis_client.lrange( "ancestors:%s" % caller_level, 0, -1) result = loop_choose(caller_lr_parents_list, logger) # 国际会议允许不在同一层下的用户开会,主叫必须是国际台所在层 if result: epids = [] for epid in all_person_level.values(): epids += epid data = set_data(callerid, result) for x in data: x["epids"] = epids if len(data) > 1: # star = nc_config.DEFAULT_STAR_LR star = nc_config.DEFAULT_LR star["star"] = True data.append(star) logger.warning( "Personlist does not have dlr type, returning the caller's adjacency [%s/%s]" % (callerid, caller_level)) return data else: return nc_config.DEFAULT_LR group_dict = group_uniq(all_dlr_level_parents) group_dict_copy = copy.copy(group_dict) response_data = [] for target, adjacency_list_x in group_dict.items(): dlr_parents_list, _ = split_level(target) res = loop_choose(dlr_parents_list, logger) if not res: logger.warning( "LANs where level %s don't find a valid DLR, will use caller's LR." % target) group_dict_copy.pop(target) else: data = set_data( callerid, res ) # {"lrid": lrid, "ip":lrip, "port":lrport, "lr_type":lrtype, "epids": [], "star": False} epids = [] for adjacency in adjacency_list_x: epids += all_person_level[adjacency] for x in data: x["epids"] = epids response_data += data diff_target = set(group_dict) - set(group_dict_copy) if len(response_data) == 1 and len(diff_target) == 0: if lr_type_users: star = set_star(callerid, logger) star["epids"] = lr_type_users response_data.append(star) return response_data star = set_star(callerid, logger) use_caller_lr_list = [] epids = [] for target in diff_target: use_caller_lr_list += group_dict[target] for adjacency in use_caller_lr_list: epids += all_person_level[adjacency] epids += lr_type_users star["epids"] = epids response_data.append(star) return response_data
def post(self): logger = logging.getLogger("nc") req = self.request reqbody = eval(self.request.body) caller = reqbody["callerId"] logger.info("%s %s %s %s -MSM request data: %s-" % (req.remote_ip, req.method, req.uri, req.headers["User-Agent"], reqbody)) caller_account = caller.split("_")[2] force_res = detectForce(caller_account) if force_res["statu"]: lr_node = lrNode(force_res["lrid"]) lr_res = lr_node.getLrnode() logger.info("The account [%s] has a specified LR [%s]" % (caller_account, force_res["lrid"])) self.write({"code": 200, "status": "success", "data": [lr_res]}) return ''' 判断多点会议 ''' if reqbody.has_key("mt_type"): if reqbody["mt_type"] == "multi": logger.info("Multipoint meeting start..") caller_vsp = getAdjacency(caller, item="vsp_id") caller_vsp_obj = vspObj(caller_vsp) caller_lrg = caller_vsp_obj.getGrid() logger.info("Found user vsp id: %s, lr grid id: %s" % (caller_vsp, caller_lrg)) if not caller_lrg: res = { "code": 200, "status": "success", "data": nc_config.DEFAULT_LR } logger.warn( "Multipoint meeting. Not found caller's adjacency LR, or this VSP has no LRG, return default lr" ) self.write(res) return else: lr_list = [] caller_lrg_obj = lrGrid(caller_lrg) caller_lrc_list = caller_lrg_obj.getCluster() logger.info("Found lr cluster ids: %s:" % caller_lrc_list) for member in caller_lrc_list: lr_cluster = lrCluster(member) lr_list.append(lr_cluster.randomLr(name="cluster")) response_list = [] for lr_id in lr_list: lr_node = lrNode(lr_id) response_list.append(lr_node.getLrnode()) logger.info("Found lr_list in lrc_list: %s" % response_list) self.write({ "code": 200, "status": "success", "data": response_list }) return ''' 判断公开课 ''' if reqbody.has_key("scope"): public_mt_type = reqbody["scope"] if public_mt_type == "inner": level_id = getAdjacency(caller) if not level_id: res = { "code": 404, "status": "warning", "data": nc_config.DEFAULT_LR } logger.info( "Stranger meeting. Not found caller's adjacency LR, return default lr" ) self.write(res) return lr_level = redis_client.keys("lrid:%s:*" % level_id)[0] lr_type = redis_client.hget(lr_level, "lr_type") if lr_type == "lr": level_res = lr_level.split(":")[1] else: level_res = getDlrRoot(lr_level.split(":")[1]) lr_cluster = lrCluster(level_res) lr_id = lr_cluster.randomLr() lr_node = lrNode(lr_id) lr_res = lr_node.getLrnode() logger.info( "Stranger meeting, type [%s], caller [%s], dlr [%s]" % (public_mt_type, caller, lr_res)) self.write({ "code": 200, "status": "success", "data": [lr_res] }) return else: level_list = getRootlevel() if not level_list: res = { "code": 404, "status": "warning", "data": nc_config.DEFAULT_LR } logger.info( "Stranger meeting. Not found root level, return default lr" ) self.write(res) return lr_cluster = lrCluster(level_list[0]) lr_id = lr_cluster.randomLr() lr_node = lrNode(lr_id) lr_res = lr_node.getLrnode() logger.info( "Stranger meeting, type [%s], caller [%s], SO lr [%s]" % (public_mt_type, caller, lr_res)) self.write({ "code": 200, "status": "success", "data": [lr_res] }) return # else: # level_id = getAdjacency(caller) # level_list = redis_client.keys("qos:%s:*"%level_id) # best_level = level_list[0] # for level in level_list: # layer_count = redis_client.hget(level, "layer_distance") # if layer_count > redis_client.hget(best_level, "layer_distance"): best_level = level # lr_cluster = lrCluster(best_level.split(":")[2]) # lr_id = lr_cluster.randomLr() # lr_node = lrNode(lr_id) # lr_res = lr_node.getLrnode() # logger.info("Stranger meeting, type [%s], caller [%s], lr [%s]"%(public_mt_type, caller, lr_res)) # self.write({"code": 200, "status": "success", "data": [lr_res]}) # return # 非公开会议 personlist = reqbody["calleeList"] personlist.append(caller) logger.info("Standard meeting starting, members -> [%s]" % personlist) near_level_set = set() for user in personlist: res_level = getAdjacency(user) if not res_level: logger.info( "No matching level target found, please check if the user_id [%s] exists." % user) break if res_level not in near_level_set: near_level_set.add(res_level) if len(near_level_set) < 1: res = { "code": 404, "msg": "Find the near_level_set failure for the personlist [%s], use default LR." % personlist, "data": nc_config.DEFAULT_LR } self.write(res) elif len(near_level_set) == 1: res_lrc = lrCluster(near_level_set.pop()) meeting_lr = res_lrc.randomLr() if meeting_lr: lr_node = lrNode(meeting_lr) lr_res = lr_node.getLrnode() logger.info("The meeting has the same LR=>[%s]" % lr_res) self.write({ "code": 200, "status": "success", "data": [lr_res] }) else: logger.info( "The members of the meeting are at the same level, but the LR fails at this level." ) self.write({ "code": 404, "status": "success", "data": nc_config.DEFAULT_LR }) else: # for level_id in near_level_set: # netqos = netQos(level_id) # valid_level_list = netqos.getValidlevel() # valid_level_list.append(level_id) # all_valid_level.append(valid_level_list) # logger.info("near_lr: %s, find the level_list: %s"%(level_id, valid_level_list)) # result = set(all_valid_level[0]) # for i in range(len(all_valid_level)): # result = result & set(all_valid_level[i]) # level_list = list(result) all_valid_level = [] near_level_list = list(near_level_set) for i in range(len(near_level_list)): netqos = netQos(near_level_list[i]) valid_level_list = netqos.getValidlevel() # valid_level_list.append(near_level_set[i]) if i == 0: all_valid_level = valid_level_list all_valid_level = [ val for val in all_valid_level if val in valid_level_list ] if len(all_valid_level) == 0: data = nc_config.DEFAULT_LR logger.warning( "Failure to calculate the result, return default lr") res = { "code": 404, "status": "Find the level_list error, use default lr", "data": data } self.write(res) return # sqls_2 = """select `path` from `net_qos` where `level_src`='%s' and `level_dst`='%s'""" result_list = [] for x in all_valid_level: flag = False sum = 0 for y in near_level_list: if y != x: # value = db.execFetch(sqls_2%(y, x))[0][0] value = float( redis_client.hget("qos:%s:%s" % (y, x), "path")) if value == 0: flag = True else: value = 0 sum = sum + value if flag: continue result_list.append({"level": x, "path": sum}) if not result_list: res = { "code": 404, "msg": "Check the result_list failure for the personlist [%s], use default LR." % personlist, "data": nc_config.DEFAULT_LR } self.write(res) return result_list.sort(key=lambda x: (x["path"])) lr_cluster = lrCluster(result_list[0]["level"]) lr_id = lr_cluster.randomLr() lr_node = lrNode(lr_id) res = lr_node.getLrnode() logger.info("Selected list %s, final choice %s" % (result_list, res)) self.write({"code": 200, "status": "success", "data": [res]})