Search in sources :

Example 1 with QuerySecondLevelServiceDetailResponse

use of beans.response.QuerySecondLevelServiceDetailResponse in project MSEC by Tencent.

the class QuerySecondLevelServiceDetail method exec.

public QuerySecondLevelServiceDetailResponse exec(QuerySecondLevelServiceDetailRequest request) {
    Logger logger = Logger.getLogger(QuerySecondLevelServiceDetail.class);
    QuerySecondLevelServiceDetailResponse resp = new QuerySecondLevelServiceDetailResponse();
    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<ServerInfo> serverList;
    ClusterInfo cluster_info;
    try {
        String sql = "select copy_num, memory_per_instance, plan_id from t_second_level_service where first_level_service_name=? and second_level_service_name=?";
        List<Object> params = new ArrayList<Object>();
        params.add(request.getFirst_level_service_name());
        params.add(request.getSecond_level_service_name());
        cluster_info = util.findSimpleRefResult(sql, params, ClusterInfo.class);
        if (!cluster_info.getPlan_id().isEmpty()) {
            ArrayList<ServerInfo> planList;
            sql = "select ip, port, set_id, group_id, memory, master, status, operation, recover_host from t_install_plan where plan_id = ? order by group_id asc, master desc";
            params.clear();
            params.add(cluster_info.getPlan_id());
            planList = util.findMoreRefResult(sql, params, ServerInfo.class);
            String operation = "";
            //0:"ok", 1: "in progress" or 2: "error"
            int server_status = 0;
            for (ServerInfo server : planList) {
                operation = server.getOperation();
                int status = 0;
                if (!server.getStatus().startsWith("Done")) {
                    if (server.getStatus().startsWith("[ERROR]")) {
                        status = 2;
                    } else
                        status = 1;
                }
                server_status = Math.max(status, server_status);
            }
            if (server_status != 1) {
                //not in progress
                if (server_status == 0) {
                    //ok, remove service_info first...TODO, needs to judge automatically
                    if (operation.equals("add")) {
                        for (ServerInfo server : planList) {
                            sql = "insert into t_service_info(first_level_service_name, second_level_service_name, ip, port, set_id, group_id, memory, master) values(?,?,?,?,?,?,?,?)";
                            params.clear();
                            params.add(request.getFirst_level_service_name());
                            params.add(request.getSecond_level_service_name());
                            params.add(server.getIp());
                            params.add(server.getPort());
                            params.add(server.getSet_id());
                            params.add(server.getGroup_id());
                            params.add(server.getMemory());
                            params.add(server.isMaster());
                            try {
                                int addNum = util.updateByPreparedStatement(sql, params);
                                if (addNum < 0) {
                                    resp.setMessage("failed to insert table");
                                    resp.setStatus(100);
                                    return resp;
                                }
                            } catch (SQLException e) {
                                resp.setMessage("add record failed:" + e.toString());
                                resp.setStatus(100);
                                e.printStackTrace();
                                return resp;
                            }
                        }
                    } else if (operation.equals("del")) {
                        sql = "delete from t_service_info where first_level_service_name=? and second_level_service_name=? and set_id=?";
                        params.clear();
                        params.add(request.getFirst_level_service_name());
                        params.add(request.getSecond_level_service_name());
                        params.add(planList.get(0).getSet_id());
                        int delNum = util.updateByPreparedStatement(sql, params);
                        if (delNum < 0) {
                            resp.setMessage("failed to delete table");
                            resp.setStatus(100);
                            return resp;
                        }
                    } else if (operation.equals("rec")) {
                        String[] ip_pair = planList.get(0).getRecover_host().split(":");
                        sql = "update t_service_info set ip=?, port=? where first_level_service_name=? and second_level_service_name=? and ip=? and port=?";
                        params.clear();
                        params.add(planList.get(0).getIp());
                        params.add(planList.get(0).getPort());
                        params.add(request.getFirst_level_service_name());
                        params.add(request.getSecond_level_service_name());
                        params.add(ip_pair[0]);
                        params.add(Integer.parseInt(ip_pair[1]));
                        int addNum = util.updateByPreparedStatement(sql, params);
                        if (addNum < 0) {
                            resp.setMessage("failed to update table");
                            resp.setStatus(100);
                            return resp;
                        }
                    }
                }
                sql = "update t_second_level_service set plan_id=? where first_level_service_name=? and second_level_service_name=?";
                params.clear();
                params.add("");
                params.add(request.getFirst_level_service_name());
                params.add(request.getSecond_level_service_name());
                int addNum = util.updateByPreparedStatement(sql, params);
                if (addNum < 0) {
                    resp.setMessage("failed to update table");
                    resp.setStatus(100);
                    return resp;
                }
                cluster_info.setPlan_id("");
            }
        }
        sql = "select ip, port, set_id, group_id, memory, master from t_service_info where first_level_service_name=? and second_level_service_name=? order by group_id asc, master desc";
        params.clear();
        params.add(request.getFirst_level_service_name());
        params.add(request.getSecond_level_service_name());
        serverList = util.findMoreRefResult(sql, params, ServerInfo.class);
        if (serverList.size() > 0 && cluster_info.getPlan_id().isEmpty()) {
            //working plan will cause config inconsistent...
            HashMap<String, ServerInfo> server_map = new HashMap<>();
            for (ServerInfo server : serverList) {
                server_map.put(server.getIp() + ":" + server.getPort(), server);
            }
            JedisHelper helper = new JedisHelper(server_map, request.getFirst_level_service_name(), request.getSecond_level_service_name(), cluster_info);
            helper.CheckStatusDetail();
            if (!helper.isOK()) {
                resp.setStatus(101);
                resp.setMessage(helper.getError_message());
                return resp;
            }
            if (helper.isChanged()) {
                serverList = util.findMoreRefResult(sql, params, ServerInfo.class);
            }
        }
    } catch (Exception e) {
        resp.setStatus(100);
        resp.setMessage("db query exception!");
        e.printStackTrace();
        return resp;
    } finally {
        util.releaseConn();
    }
    resp.setServers((ArrayList<ServerInfo>) serverList);
    resp.setCluster_info(cluster_info);
    resp.setMessage("success");
    resp.setStatus(0);
    return resp;
}
Also used : SQLException(java.sql.SQLException) HashMap(java.util.HashMap) ServerInfo(beans.dbaccess.ServerInfo) ArrayList(java.util.ArrayList) Logger(org.apache.log4j.Logger) SQLException(java.sql.SQLException) QuerySecondLevelServiceDetailResponse(beans.response.QuerySecondLevelServiceDetailResponse) ClusterInfo(beans.dbaccess.ClusterInfo) DBUtil(msec.org.DBUtil)

Aggregations

ClusterInfo (beans.dbaccess.ClusterInfo)1 ServerInfo (beans.dbaccess.ServerInfo)1 QuerySecondLevelServiceDetailResponse (beans.response.QuerySecondLevelServiceDetailResponse)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 DBUtil (msec.org.DBUtil)1 Logger (org.apache.log4j.Logger)1