Search in sources :

Example 1 with CapacityBaseInfo

use of beans.dbaccess.CapacityBaseInfo 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)

Aggregations

CapacityBaseInfo (beans.dbaccess.CapacityBaseInfo)1 SecondLevelService (beans.dbaccess.SecondLevelService)1 IPPortPair (beans.request.IPPortPair)1 QueryCapacityListResponse (beans.response.QueryCapacityListResponse)1 ArrayList (java.util.ArrayList)1 AccessZooKeeper (ngse.org.AccessZooKeeper)1 DBUtil (ngse.org.DBUtil)1 Logger (org.apache.log4j.Logger)1