Search in sources :

Example 11 with IPPortPair

use of beans.request.IPPortPair in project MSEC by Tencent.

the class QueryCapacityDetail method exec.

public QueryCapacityDetailResponse exec(CapacityRequest request) {
    Logger logger = Logger.getLogger("QueryCapacityDetail");
    QueryCapacityDetailResponse resp = new QueryCapacityDetailResponse();
    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;
    }
    ArrayList<CapacityDetailInfo> capacities = new ArrayList<CapacityDetailInfo>();
    AccessZooKeeper azk = new AccessZooKeeper();
    try {
        //该服务的所有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;
        }
        //获取每一个ip的负载信息
        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) {
                    CapacityDetailInfo c = new CapacityDetailInfo();
                    c.setIp(ip.getIp());
                    c.setLoad_level(load);
                    capacities.add(c);
                }
            } catch (Exception e) {
                e.printStackTrace();
                resp.setMessage(e.getMessage());
                resp.setStatus(100);
                return resp;
            }
        }
    } finally {
        util.releaseConn();
        azk.disconnect();
    }
    resp.setInfo(capacities);
    resp.setMessage("success");
    resp.setStatus(0);
    return resp;
}
Also used : DBUtil(ngse.org.DBUtil) IPPortPair(beans.request.IPPortPair) QueryCapacityDetailResponse(beans.response.QueryCapacityDetailResponse) ArrayList(java.util.ArrayList) CapacityDetailInfo(beans.dbaccess.CapacityDetailInfo) Logger(org.apache.log4j.Logger) AccessZooKeeper(ngse.org.AccessZooKeeper)

Example 12 with IPPortPair

use of beans.request.IPPortPair 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 13 with IPPortPair

use of beans.request.IPPortPair in project MSEC by Tencent.

the class QueryProcessInfo method exec.

public JsonRPCResponseBase exec(SecondLevelService request) {
    Logger logger = Logger.getLogger(QueryProcessInfo.class);
    JsonRPCResponseBase resp = new JsonRPCResponseBase();
    flsn = request.getFirst_level_service_name();
    slsn = request.getSecond_level_service_name();
    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;
    }
    FileInputStream in = null;
    try {
        String sql = "select   ip from t_second_level_service_ipinfo where first_level_service_name=? " + "and second_level_service_name=? and status='enabled' ";
        ArrayList<Object> params = new ArrayList<Object>();
        params.add(flsn);
        params.add(slsn);
        ArrayList<IPPortPair> iplist = util.findMoreRefResult(sql, params, IPPortPair.class);
        String cmdFileName = geneCmdFile();
        getHttpResponse().setContentType("text/html");
        getHttpResponse().setCharacterEncoding("UTF-8");
        ServletOutputStream out = getHttpResponse().getOutputStream();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>release result</title>");
        out.println("</head>");
        out.println("<body>");
        //这个标签用于原样输出
        out.println("<pre>");
        StringBuffer outFile = new StringBuffer();
        String localResultFileName = getLocalResultFileName();
        new File(localResultFileName).delete();
        byte[] buffer = new byte[1240];
        for (int i = 0; i < iplist.size(); i++) {
            String ip = iplist.get(i).getIp();
            out.println(String.format("------------------IP:%s---------------", ip));
            RemoteShell rs = new RemoteShell();
            outFile.delete(0, outFile.length());
            result = rs.SendCmdsToAgentAndRun(cmdFileName, ip, outFile);
            if (result == null || !result.equals("success")) {
                out.println(String.format("sending cmd file to %s failed:%s", ip, result));
                continue;
            }
            result = rs.GetFileFromAgent(localResultFileName, outFile.toString(), ip);
            if (result == null || !result.equals("success")) {
                out.println(String.format("get cmd result from %s failed:%s", ip, result));
                continue;
            }
            in = new FileInputStream(localResultFileName);
            while (true) {
                int len = in.read(buffer);
                if (len <= 0) {
                    break;
                }
                out.print(new String(buffer, 0, len));
            }
            in.close();
            new File(localResultFileName).delete();
        }
        out.println("</pre>");
        out.println("</body>");
        out.println("</html>");
        try {
            out.close();
        } catch (Exception e) {
        }
        return null;
    } catch (Exception e) {
        resp.setStatus(100);
        resp.setMessage(e.getMessage());
        e.printStackTrace();
        return resp;
    } finally {
        util.releaseConn();
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) {
            }
        }
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ArrayList(java.util.ArrayList) Logger(org.apache.log4j.Logger) FileInputStream(java.io.FileInputStream) IPPortPair(beans.request.IPPortPair) File(java.io.File)

Example 14 with IPPortPair

use of beans.request.IPPortPair in project MSEC by Tencent.

the class RollbackReleasePlan method exec.

public JsonRPCResponseBase exec(ReleasePlan request) {
    Logger logger = Logger.getLogger(RollbackReleasePlan.class);
    JsonRPCResponseBase resp = new JsonRPCResponseBase();
    plan_id = request.getPlan_id();
    flsn = request.getFirst_level_service_name();
    slsn = request.getSecond_level_service_name();
    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;
    }
    try {
        String sql = "select dest_ip_list from t_release_plan where plan_id=?";
        ArrayList<Object> params = new ArrayList<Object>();
        params.add(request.getPlan_id());
        Map<String, Object> res = util.findSimpleResult(sql, params);
        if (res.get("dest_ip_list") == null) {
            resp.setStatus(100);
            resp.setMessage("query dest ip list failed.");
            return resp;
        }
        String dest_ip_list = (String) (res.get("dest_ip_list"));
        ObjectMapper objectMapper = new ObjectMapper();
        IPPortPair[] ips = objectMapper.readValue(dest_ip_list, IPPortPair[].class);
        logger.info("destination IP number;" + ips.length);
        //对每个IP执行回滚
        getHttpResponse().setContentType("text/html");
        getHttpResponse().setCharacterEncoding("UTF-8");
        ServletOutputStream out = getHttpResponse().getOutputStream();
        safeWrite("<html>", out);
        safeWrite("<head>", out);
        safeWrite("<title>release result</title>", out);
        safeWrite("</head>", out);
        safeWrite("<body>", out);
        //这个标签用于原样输出
        safeWrite("<pre>", out);
        updatePlanStatus(util, "rolling back");
        doRollback(ips, out, util);
        safeWrite("</pre>", out);
        safeWrite("</body>", out);
        safeWrite("</html>", out);
        //update plan status
        updatePlanStatus(util, "roll back successfully");
        try {
            out.close();
        } catch (Exception e) {
        }
        return null;
    } catch (Exception e) {
        resp.setStatus(100);
        resp.setMessage(e.getMessage());
        e.printStackTrace();
        try {
            updatePlanStatus(util, "failed to roll back");
        } catch (Exception e1) {
        }
        return resp;
    } finally {
        util.releaseConn();
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ArrayList(java.util.ArrayList) Logger(org.apache.log4j.Logger) IPPortPair(beans.request.IPPortPair) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 15 with IPPortPair

use of beans.request.IPPortPair in project MSEC by Tencent.

the class RefreshCapacity method exec.

public JsonRPCResponseBase exec(CapacityRequest request) {
    JsonRPCResponseBase response = new JsonRPCResponseBase();
    String result = checkIdentity();
    if (!result.equals("success")) {
        response.setStatus(99);
        response.setMessage(result);
        return response;
    }
    Logger logger = Logger.getLogger(RefreshCapacity.class);
    //把ip列表刷新到LB的服务器里并下发给各个客户端
    AccessZooKeeper azk = new AccessZooKeeper();
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        response.setStatus(100);
        response.setMessage("db connect failed");
        return response;
    }
    try {
        //step2: 获得该服务下所有enabled的IP,写入LB系统
        ArrayList<IPPortPair> ips = LoadBalance.getIPPortInfoByServiceName(request.getFirst_level_service_name(), request.getSecond_level_service_name(), util);
        if (ips == null) {
            response.setStatus(100);
            response.setMessage("get ip info from db failed");
            return response;
        }
        logger.info("get enabled IP, they will be write to LB server, number:" + ips.size());
        result = LoadBalance.writeOneServiceConfigInfo(azk, request.getFirst_level_service_name() + "/" + request.getSecond_level_service_name(), true, ips);
        if (result == null || !result.equals("success")) {
            response.setStatus(100);
            response.setMessage(result);
            return response;
        }
        logger.info("write into LB server successfully.");
        //step3: 更新涉及到的IP为key的配置信息
        ArrayList<String> whiteNameList = new ArrayList<>();
        for (int i = 0; i < ips.size(); i++) {
            whiteNameList.add(ips.get(i).getIp());
        }
        logger.info("update the IP-key information in LB server");
        result = LoadBalance.writeIPConfigInfo(azk, whiteNameList);
        if (result == null || !result.equals("success")) {
            response.setStatus(100);
            response.setMessage(result);
            return response;
        }
        response.setStatus(0);
        response.setMessage("success");
        return response;
    } catch (Exception e) {
        e.printStackTrace();
        response.setStatus(100);
        response.setMessage(e.getMessage());
        return response;
    } finally {
        azk.disconnect();
        util.releaseConn();
    }
}
Also used : DBUtil(ngse.org.DBUtil) JsonRPCResponseBase(ngse.org.JsonRPCResponseBase) IPPortPair(beans.request.IPPortPair) ArrayList(java.util.ArrayList) Logger(org.apache.log4j.Logger) AccessZooKeeper(ngse.org.AccessZooKeeper)

Aggregations

IPPortPair (beans.request.IPPortPair)22 Logger (org.apache.log4j.Logger)18 ArrayList (java.util.ArrayList)12 DBUtil (ngse.org.DBUtil)12 AccessZooKeeper (ngse.org.AccessZooKeeper)7 JSONObject (org.json.JSONObject)7 OutputStream (java.io.OutputStream)5 InetSocketAddress (java.net.InetSocketAddress)5 Socket (java.net.Socket)5 ServletOutputStream (javax.servlet.ServletOutputStream)5 JsonRPCResponseBase (ngse.org.JsonRPCResponseBase)4 SecondLevelService (beans.dbaccess.SecondLevelService)3 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)3 JSONArray (org.json.JSONArray)3 BusinessLogResult (beans.dbaccess.BusinessLogResult)2 OddSecondLevelService (beans.dbaccess.OddSecondLevelService)2 SQLException (java.sql.SQLException)2 CapacityBaseInfo (beans.dbaccess.CapacityBaseInfo)1 CapacityDetailInfo (beans.dbaccess.CapacityDetailInfo)1 BusiLogField (beans.request.BusiLogField)1