def lrAddall(self): self.lrAddself() src_lrc = lrCluster(self.src_level) src_lr_id = src_lrc.randomLr() src_price = getLrProperty(src_lr_id, "price") dst_level_list = getAlllevel() sql_data = [] for dst_level in dst_level_list: if dst_level == self.src_level: continue check_cloud_res, relationship_list = checkCloud( dst_level, [self.src_level]) dlr_level_list = {} for k, v in enumerate(relationship_list): dlr_level_list[v] = k if dlr_level_list.has_key(self.src_level): layer_distance = dlr_level_list[self.src_level] else: layer_distance = 0 if len(check_cloud_res) <= 1: weight = path = 0 else: check_operator_res = checkOperator(dst_level, [self.src_level]) if len(check_operator_res["conn_list"]) != 0: weight = 1 elif len(check_operator_res["half_conn"]) != 0: weight = 0.5 if src_price == 0: if layer_distance: path = layer_distance / weight else: impassable_num = len(dlr_level_list) path = impassable_num / weight else: if layer_distance: path = math.sqrt( src_price / 100) * layer_distance / weight else: impassable_num = len(dlr_level_list) path = math.sqrt( src_price / 100) * impassable_num / weight sql_data.append( (dst_level, self.src_level, weight, layer_distance, path)) # print "*"*40, dst_level, self.src_level, weight, layer_distance, path redis_client.hmset("qos:%s:%s" % (dst_level, self.src_level), { "weight": weight, "layer_distance": layer_distance, "path": path }) if not sql_data: return 0 else: sqli = """insert into net_qos (`level_src`, `level_dst`, `weight`, `layer_distance`, `path`) values (%s, %s, %s, %s, %s)""" return db.execOnly(sqli, sql_data)
def addLrnode(self, sql_data, level_id, plevel_id): sqli = """insert into lr_node (`lr_id`, `name`, `plevel_id`, `ip`, `port`, `cluster`, `cloud`, `price`, `lr_type`, `sysload`, `level_id`, `last_subtime`, `active`) values ( "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", %s, "%s", %s, '1')""" % sql_data try: res = db.execOnly(sqli) except: res = 0 if res > 0: level_cluster, level_type = write_level(level_id) redis_client.hmset( "level:%s" % level_id, { "plevel_id": plevel_id, "level_cluster": level_cluster, "level_type": level_type }) # 生成 ancestors/level 的redis记录 l_list = redis_client.keys("level:%s" % level_id) if not l_list: redis_client.rpush("ancestors:%s" % level_id, level_id) for l in getAncestor(level_id, []): redis_client.rpush("ancestors:%s" % level_id, l) self.logger.info("Add lr_node with lrid=%s done." % self.lr_id) return { "code": 200, "status": "success", "msg": "Add lr_node with lrid=%s done." % self.lr_id } else: self.logger.error( "Add lr_node with lrid=%s failed, insert statement failed to execute." % self.lr_id) return { "code": 400, "status": "exist", "msg": "Add lr_node with lrid=%s failed." % self.lr_id }
logger.error("API for %s failure" % db_table) api_data = res["data"]["data"] if not api_data: continue query_data = [] if db_table == "adjacency": for user_lr in api_data: if user_lr["user_id"] == '' or user_lr[ "node_level"] == '' or user_lr["vsp_id"] == '': continue ep_user = "******" % (user_lr["user_id"], user_lr["account"]) query_data.append( (user_lr["account"], ep_user, str(user_lr["node_level"]), user_lr["vsp_id"])) redis_client.hmset( "user:%s" % ep_user, { "level_id": str(user_lr["node_level"]), "vsp_id": user_lr["vsp_id"] }) sqli = """insert into `adjacency` (`account`, `ep_id`, `level_id`, `vsp_id`) values (%s, %s, %s, %s)""" elif db_table == "lr_node": lr_all_list = [] for lr_info in api_data: lr_all_list.append(lr_info["id"]) query_data.append( (lr_info["id"], lr_info["name"], lr_info["p_node_level"], lr_info["ip"], lr_info["port"], lr_info["op_name"], lr_info["net_type"], lr_info["price"], lr_info["lr_type"], str(lr_info["lrc_ids"]), lr_info["level_id"])) # ------------------- redis_client.hmset( "lrid:%s:%s" % (lr_info["level_id"], lr_info["id"]), {
def modLrnode(self, sql_data, level_id, plevel_id, old_level, lrid): sqlu = """update lr_node set `name`="%s", `plevel_id`="%s", `ip`="%s", `port`="%s", `cluster`="%s", `cloud`="%s", `price`="%s", `lr_type`="%s", `sysload`=%s, `level_id`='%s', `last_subtime`=%s where `lr_id`='%s' """ % sql_data sqlu_1 = """update level_node set `plevel_id`='%s' where `level_id`='%s'""" % ( plevel_id, level_id) sql_select = """select `level_id` from `level_node`""" level_list = [x[0] for x in db.execFetch(sql_select)] # sql_parent = """select `plevel_id` from level_node where `level_id`='%s'"""%old_level # old_parent_level = db.execFetch(sql_parent)[0][0] if level_id not in level_list: if old_level: sql_check = """select lr_id from lr_node where level_id='%s'""" % old_level result = db.execFetch(sql_check) if len(result) <= 1: sql_s_old = """select `level_id`, `plevel_id` from level_node where `level_id`='%s'""" % old_level sql_u_old = """update level_node set `plevel_id`='%s' where `plevel_id`='%s'""" need_r = db.execFetch(sql_s_old) if need_r: db.execFetch(sql_u_old % (need_r[0][1], old_level)) for l in need_r: redis_client.hset("level:%s" % l[0], "plevel_id", l[1]) sql_delete = """delete from `level_node` where `level_id`='%s'""" % old_level db.execOnly(sql_delete) redis_client.delete("level:%s" % old_level) db.execOnly(sqlu) res = write_level(level_id) redis_client.hmset( "level:%s" % level_id, { "plevel_id": plevel_id, "level_cluster": res[0], "level_type": res[1] }) reset_ancestors() self.logger.info("Add new level and qos for lrid=%s done." % self.lr_id) # return {"code": 200, "status": "success", "msg": "Add new level and qos for lrid=%s done."%self.lr_id} else: if level_id != old_level: sql_check = """select lr_id from lr_node where level_id='%s'""" % old_level result = db.execFetch(sql_check) if len(result) <= 1: sql_delete = """delete from `level_node` where `level_id`='%s'""" % old_level db.execOnly(sql_delete) redis_client.delete("level:%s" % old_level) reset_ancestors() # print sqlu try: db.execOnly(sqlu_1) result = db.execOnly(sqlu) write_level(old_level) res = write_level(level_id) redis_client.hmset( "level:%s" % level_id, { "plevel_id": plevel_id, "level_cluster": res[0], "level_type": res[1] }) redis_client.delete("ancestors:%s" % level_id) redis_client.rpush("ancestors:%s" % level_id, level_id) for l in getAncestor(level_id, []): redis_client.rpush("ancestors:%s" % level_id, l) except Exception, e: # print "**************",e result = 0
def lrAddself(self): dst_level_list = getAlllevel() # dst_level_list.remove(self.src_level) level_conn_list, relationship_list = checkCloud( self.src_level, dst_level_list) operator_res = checkOperator(self.src_level, dst_level_list) operator_half_conn_list = set(operator_res["half_conn"]) operator_not_conn_list = set(operator_res["not_conn"]) res_half_list = list(set(level_conn_list) & operator_half_conn_list) res_conn_list = list( set(level_conn_list) - set(res_half_list) - operator_not_conn_list) self.logger.info("level_id: %s - connlist: %s - halflist: %s " % (self.src_level, res_conn_list, res_half_list)) dlr_level_list = {} for k, v in enumerate(relationship_list): dlr_level_list[v] = k sql_data_1 = [] for dst_level in dst_level_list: if dst_level == self.src_level: weight = 1 layer_distance = 0 path = weight sql_data_1.append( (self.src_level, dst_level, weight, layer_distance, path)) redis_client.hmset( "qos:%s:%s" % (self.src_level, dst_level), { "weight": weight, "layer_distance": layer_distance, "path": path }) continue dst_lrc = lrCluster(dst_level) dst_lr_id = dst_lrc.randomLr() dst_price = getLrProperty(dst_lr_id, "price") if dlr_level_list.has_key(dst_level): layer_distance = dlr_level_list[dst_level] else: layer_distance = 0 if (dst_level not in res_conn_list) and (dst_level not in res_half_list): weight = 0 path = 0 else: if dst_level in res_conn_list: weight = 1 elif dst_level in res_half_list: weight = 0.5 if dst_price == 0: if layer_distance: path = layer_distance / weight else: impassable_num = len(dlr_level_list) path = impassable_num / weight else: if layer_distance: path = math.sqrt( dst_price / 100) * layer_distance / weight else: impassable_num = len(dlr_level_list) path = math.sqrt( dst_price / 100) * impassable_num / weight sql_data_1.append( (self.src_level, dst_level, weight, layer_distance, path)) redis_client.hmset("qos:%s:%s" % (self.src_level, dst_level), { "weight": weight, "layer_distance": layer_distance, "path": path }) if not sql_data_1: return 0 else: sqli = """insert into net_qos (`level_src`, `level_dst`, `weight`, `layer_distance`, `path`) values (%s, %s, %s, %s, %s)""" return db.execOnly(sqli, sql_data_1)