Search in sources :

Example 1 with SecondLevelService

use of beans.dbaccess.SecondLevelService in project MSEC by Tencent.

the class LoadBalance method writeIPConfigInfo.

//以IP为key的配置信息写入LB,whiteNameList为需要写的白名单IP列表,可以为null
public static String writeIPConfigInfo(AccessZooKeeper azk, ArrayList<String> whiteNameList) throws Exception {
    Logger logger = Logger.getLogger(LoadBalance.class);
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        return "db connect failed";
    }
    logger.info("write IP key info into LB ...");
    HashMap<String, HashSet<String>> ip2Svcs = new HashMap<String, HashSet<String>>();
    try {
        //获得所有服务
        ArrayList<SecondLevelService> services = getAllService(util);
        if (services == null) {
            logger.error("get all service failed");
            return "get service list from db failed";
        }
        //对每个服务获取下属的IP
        for (int i = 0; i < services.size(); i++) {
            SecondLevelService svc = services.get(i);
            ArrayList<IPPortPair> ips = getIPPortInfoByServiceName(svc.getFirst_level_service_name(), svc.getSecond_level_service_name(), util);
            if (ips == null) {
                logger.error("get ip list for " + svc.getSecond_level_service_name() + " failed.");
                return "get ip list from db failed";
            }
            logger.info("get ip list for " + svc.getSecond_level_service_name() + " successfully.ip number" + ips.size());
            //生成IP为key、servicename列表为value的反向映射关系
            geneIP2Svcs(svc.getFirst_level_service_name() + "." + svc.getSecond_level_service_name(), ips, ip2Svcs);
        }
        logger.info("gene IP->service map successfully, map size:" + ip2Svcs.size());
        if (whiteNameList == null) {
            //删除掉整个ip配置信息
            String path = "/nodeservices";
            azk.deleteRecursive(path);
            logger.info("delete whole tree:" + path);
        } else {
            //删除掉白名单中的ip配置信息
            for (int i = 0; i < whiteNameList.size(); i++) {
                String path = "/nodeservices/" + whiteNameList.get(i);
                azk.deleteRecursive(path);
                logger.info("delete node:" + path);
            }
        }
        //往LB写入ip为key的数据
        for (Map.Entry<String, HashSet<String>> entry : ip2Svcs.entrySet()) {
            if (//如果只更新列表中的指定的IP列表
            whiteNameList != null) {
                if (whiteNameList.indexOf(entry.getKey()) < 0) {
                    continue;
                }
            }
            String path = "/nodeservices/" + entry.getKey();
            String data = "{\"services\":[";
            Iterator<String> it = entry.getValue().iterator();
            int eleIndex = 0;
            while (it.hasNext()) {
                if (eleIndex > 0) {
                    //与前一个元素的分割
                    data += ",";
                }
                data += "\"" + it.next() + "\"";
                eleIndex++;
            }
            data += "]}";
            logger.info("write LB, path=" + path);
            logger.info("data=" + data);
            String result = azk.write(path, data.getBytes());
            if (result == null || !result.equals("success")) {
                logger.error("write LB failed:" + result);
                return result;
            }
        }
        return "success";
    } finally {
        util.releaseConn();
    }
}
Also used : OddSecondLevelService(beans.dbaccess.OddSecondLevelService) SecondLevelService(beans.dbaccess.SecondLevelService) Logger(org.apache.log4j.Logger) DBUtil(ngse.org.DBUtil) IPPortPair(beans.request.IPPortPair)

Example 2 with SecondLevelService

use of beans.dbaccess.SecondLevelService in project MSEC by Tencent.

the class LoadBalance method writeServiceConfigInfo.

//把所有服务的配置信息写入LB
public static String writeServiceConfigInfo(AccessZooKeeper azk) throws Exception {
    DBUtil util = new DBUtil();
    Logger logger = Logger.getLogger(LoadBalance.class);
    if (util.getConnection() == null) {
        return "db connect failed";
    }
    logger.info("write IP information of ALL services into LB...");
    try {
        //删除掉整个配置信息
        String path = "/nameservice";
        azk.deleteRecursive(path);
        logger.info("delete whole tree:" + path);
        //获得所有服务
        ArrayList<SecondLevelService> services = getAllService(util);
        if (services == null) {
            return "get standard service list from db failed";
        }
        logger.info("getAllService() OK, service number:" + services.size());
        //对每个服务获取下属的IP
        for (int i = 0; i < services.size(); i++) {
            SecondLevelService svc = services.get(i);
            ArrayList<IPPortPair> ips = getIPPortInfoByServiceName(svc.getFirst_level_service_name(), svc.getSecond_level_service_name(), util);
            if (ips == null) {
                return "get ip list from db failed";
            }
            logger.info("get ip for " + svc.getSecond_level_service_name() + " OK, ip number" + ips.size());
            //写到 LB系统里
            String result = writeOneServiceConfigInfo(azk, svc.getFirst_level_service_name() + "/" + svc.getSecond_level_service_name(), svc.getType().equals("standard"), ips);
            if (result == null || !result.equals("success")) {
                logger.error("write service IP information into LB failed!" + result);
                return result;
            }
            logger.info("write to LB ok");
        }
        return "success";
    } finally {
        util.releaseConn();
    }
}
Also used : OddSecondLevelService(beans.dbaccess.OddSecondLevelService) SecondLevelService(beans.dbaccess.SecondLevelService) DBUtil(ngse.org.DBUtil) IPPortPair(beans.request.IPPortPair) Logger(org.apache.log4j.Logger)

Example 3 with SecondLevelService

use of beans.dbaccess.SecondLevelService in project MSEC by Tencent.

the class QueryCapacityList method exec.

public QueryCapacityListResponse exec(CapacityRequest request) {
    Logger logger = Logger.getLogger("QueryCapacityList");
    QueryCapacityListResponse resp = new QueryCapacityListResponse();
    String result = checkIdentity();
    if (!result.equals("success")) {
        resp.setStatus(99);
        resp.setMessage(result);
        return resp;
    }
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        resp.setStatus(100);
        resp.setMessage("db connect failed!");
        return resp;
    }
    List<CapacityBaseInfo> capacities = new ArrayList<CapacityBaseInfo>();
    AccessZooKeeper azk = new AccessZooKeeper();
    try {
        if (request.getFirst_level_service_name() == null || request.getFirst_level_service_name().length() < 1 || request.getSecond_level_service_name() == null || request.getSecond_level_service_name().length() < 1) {
            //所有服务
            ArrayList<SecondLevelService> svcList = LoadBalance.getAllService(util);
            if (svcList == null) {
                resp.setMessage("failed to get all service list");
                resp.setStatus(100);
                return resp;
            }
            for (int i = 0; i < svcList.size(); i++) {
                //每一个服务
                SecondLevelService svc = svcList.get(i);
                if (!svc.getType().equals("standard")) {
                    continue;
                }
                //用户可能指定了一级服务名进行过滤,即只查看该一级服务下所有二级服务的情况
                if (request.getFirst_level_service_name() != null && request.getFirst_level_service_name().length() > 0) {
                    if (!svc.getFirst_level_service_name().equals(request.getFirst_level_service_name())) {
                        continue;
                    }
                }
                CapacityBaseInfo c = new CapacityBaseInfo();
                //该服务的所有ip
                ArrayList<IPPortPair> iplist = LoadBalance.getIPPortInfoByServiceName(svc.getFirst_level_service_name(), svc.getSecond_level_service_name(), util);
                if (iplist == null) {
                    resp.setMessage("failed to get ip list for " + svc.getFirst_level_service_name() + "." + svc.getSecond_level_service_name());
                    resp.setStatus(100);
                    return resp;
                }
                c.setIp_count(iplist.size());
                c.setFirst_level_service_name(svc.getFirst_level_service_name());
                c.setSecond_level_service_name(svc.getSecond_level_service_name());
                //获取每一个ip的负载信息
                int sum = 0;
                int count = 0;
                int j;
                for (j = 0; j < iplist.size(); j++) {
                    IPPortPair ip = iplist.get(j);
                    String svcname = svc.getFirst_level_service_name() + "/" + svc.getSecond_level_service_name();
                    try {
                        int load = LoadBalance.readOneServiceLoadInfo(azk, svcname, ip.getIp());
                        if (load >= 0) {
                            sum += load;
                            count++;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        resp.setMessage(e.getMessage());
                        resp.setStatus(100);
                        return resp;
                    }
                }
                c.setIp_report_num(count);
                if (count > 0) {
                    c.setLoad_level(sum / count);
                } else {
                    c.setLoad_level(0);
                }
                capacities.add(c);
            }
        } else {
            CapacityBaseInfo c = new CapacityBaseInfo();
            //该服务的所有ip
            ArrayList<IPPortPair> iplist = LoadBalance.getIPPortInfoByServiceName(request.getFirst_level_service_name(), request.getSecond_level_service_name(), util);
            if (iplist == null) {
                resp.setMessage("failed to get ip list for " + request.getFirst_level_service_name() + "." + request.getSecond_level_service_name());
                resp.setStatus(100);
                return resp;
            }
            c.setIp_count(iplist.size());
            c.setFirst_level_service_name(request.getFirst_level_service_name());
            c.setSecond_level_service_name(request.getSecond_level_service_name());
            //获取每一个ip的负载信息
            int sum = 0;
            int count = 0;
            int j;
            for (j = 0; j < iplist.size(); j++) {
                IPPortPair ip = iplist.get(j);
                String svcname = request.getFirst_level_service_name() + "/" + request.getSecond_level_service_name();
                try {
                    int load = LoadBalance.readOneServiceLoadInfo(azk, svcname, ip.getIp());
                    if (load > 0) {
                        sum += load;
                        count++;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    resp.setMessage(e.getMessage());
                    resp.setStatus(100);
                    return resp;
                }
            }
            c.setIp_report_num(count);
            if (count > 0) {
                c.setLoad_level(sum / count);
            } else {
                c.setLoad_level(0);
            }
            capacities.add(c);
        }
    } finally {
        util.releaseConn();
        azk.disconnect();
    }
    resp.setCapacity_list((ArrayList<CapacityBaseInfo>) capacities);
    resp.setMessage("success");
    resp.setStatus(0);
    return resp;
}
Also used : SecondLevelService(beans.dbaccess.SecondLevelService) ArrayList(java.util.ArrayList) Logger(org.apache.log4j.Logger) CapacityBaseInfo(beans.dbaccess.CapacityBaseInfo) DBUtil(ngse.org.DBUtil) IPPortPair(beans.request.IPPortPair) QueryCapacityListResponse(beans.response.QueryCapacityListResponse) AccessZooKeeper(ngse.org.AccessZooKeeper)

Example 4 with SecondLevelService

use of beans.dbaccess.SecondLevelService in project MSEC by Tencent.

the class QuerySecondLevelServiceList method exec.

public QuerySecondLevelServiceListResponse exec(QuerySecondLevelServiceListRequest request) {
    QuerySecondLevelServiceListResponse resp = new QuerySecondLevelServiceListResponse();
    String result = checkIdentity();
    if (!result.equals("success")) {
        resp.setStatus(99);
        resp.setMessage(result);
        return resp;
    }
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        resp.setStatus(100);
        resp.setMessage("db connect failed!");
        return resp;
    }
    List<SecondLevelService> serviceList;
    String sql = "select first_level_service_name, second_level_service_name from t_second_level_service where type='standard' ";
    List<Object> params = new ArrayList<Object>();
    try {
        serviceList = util.findMoreRefResult(sql, params, SecondLevelService.class);
    } catch (Exception e) {
        resp.setStatus(100);
        resp.setMessage("db query exception!");
        e.printStackTrace();
        return resp;
    } finally {
        util.releaseConn();
    }
    resp.setService_list((ArrayList<SecondLevelService>) serviceList);
    resp.setMessage("success");
    resp.setStatus(0);
    return resp;
}
Also used : SecondLevelService(beans.dbaccess.SecondLevelService) QuerySecondLevelServiceListResponse(beans.response.QuerySecondLevelServiceListResponse) DBUtil(ngse.org.DBUtil) ArrayList(java.util.ArrayList)

Example 5 with SecondLevelService

use of beans.dbaccess.SecondLevelService in project MSEC by Tencent.

the class AddSecondLevelServiceConfigTag method getStandardServicePort.

private int getStandardServicePort(DBUtil util, String flsn, String slsn) throws Exception {
    Logger logger = Logger.getLogger(AddSecondLevelServiceConfigTag.class);
    String sql = "select port from t_second_level_service where first_level_service_name=? " + "and second_level_service_name=? and type='standard'";
    List<Object> params = new ArrayList<>();
    params.add(flsn);
    params.add(slsn);
    List<SecondLevelService> svcList = util.findMoreRefResult(sql, params, SecondLevelService.class);
    if (svcList == null || svcList.size() != 1) {
        throw new Exception("invalid record in t_second_level_service:" + flsn + "." + slsn);
    }
    return svcList.get(0).getPort();
}
Also used : SecondLevelService(beans.dbaccess.SecondLevelService) ArrayList(java.util.ArrayList) Logger(org.apache.log4j.Logger) IOException(java.io.IOException) SQLException(java.sql.SQLException)

Aggregations

SecondLevelService (beans.dbaccess.SecondLevelService)5 DBUtil (ngse.org.DBUtil)4 Logger (org.apache.log4j.Logger)4 IPPortPair (beans.request.IPPortPair)3 ArrayList (java.util.ArrayList)3 OddSecondLevelService (beans.dbaccess.OddSecondLevelService)2 CapacityBaseInfo (beans.dbaccess.CapacityBaseInfo)1 QueryCapacityListResponse (beans.response.QueryCapacityListResponse)1 QuerySecondLevelServiceListResponse (beans.response.QuerySecondLevelServiceListResponse)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 AccessZooKeeper (ngse.org.AccessZooKeeper)1