Search in sources :

Example 1 with RecoverPlanResponse

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

the class RecoverPlan method exec.

public RecoverPlanResponse exec(RecoverPlanRequest request) {
    Logger logger = Logger.getLogger(RecoverPlan.class);
    RecoverPlanResponse resp = new RecoverPlanResponse();
    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;
    }
    ServerInfo server = null;
    String plan_id = "";
    try {
        String[] ip_pair = request.getOld_host().split(":");
        if (ip_pair.length != 2) {
            resp.setStatus(101);
            resp.setMessage("Request host error!");
            return resp;
        }
        ServerInfo old_server;
        //get plan_id;
        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());
        ClusterInfo cluster_info = util.findSimpleRefResult(sql, params, ClusterInfo.class);
        if (!cluster_info.getPlan_id().isEmpty()) {
            resp.setStatus(101);
            resp.setMessage("Please wait until the ongoing plan finishes.");
            return resp;
        }
        sql = "select ip, port, set_id, group_id, memory, master from t_service_info where first_level_service_name=? and second_level_service_name=? and ip=? and port=?";
        params.add(ip_pair[0]);
        params.add(Integer.parseInt(ip_pair[1]));
        old_server = util.findSimpleRefResult(sql, params, ServerInfo.class);
        if (old_server == null) {
            resp.setStatus(101);
            resp.setMessage("Recover plan error!");
            return resp;
        }
        sql = "select distinct set_id from t_service_info where first_level_service_name=? and second_level_service_name=? and ip=?";
        params.clear();
        params.add(request.getFirst_level_service_name());
        params.add(request.getSecond_level_service_name());
        params.add(request.getNew_ip());
        ArrayList<Map<String, Object>> set_infos = util.findModeResult(sql, params);
        if (set_infos.size() > 1 || (set_infos.size() == 1 && !set_infos.get(0).get("set_id").toString().equals(old_server.getSet_id()))) {
            resp.setStatus(101);
            resp.setMessage("Request IP already exists in other set!");
            return resp;
        }
        plan_id = AddSecondLevelServiceIPInfo.newPlanID();
        //use the same settings except ip
        server = old_server;
        server.setFirst_level_service_name(request.getFirst_level_service_name());
        server.setSecond_level_service_name(request.getSecond_level_service_name());
        server.setIp(request.getNew_ip());
        server.setStatus("Planning");
        sql = "insert into t_install_plan(plan_id, first_level_service_name, second_level_service_name, ip, port, set_id, group_id, memory, master, status, operation, recover_host) values(?,?,?,?,?,?,?,?,?,?,?,?)";
        params.clear();
        params.add(plan_id);
        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());
        params.add(server.getStatus());
        params.add("rec");
        params.add(request.getOld_host());
        int addNum = util.updateByPreparedStatement(sql, params);
        if (addNum < 0) {
            resp.setMessage("Failed to insert plan.");
            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(plan_id);
        params.add(request.getFirst_level_service_name());
        params.add(request.getSecond_level_service_name());
        addNum = util.updateByPreparedStatement(sql, params);
        if (addNum < 0) {
            resp.setMessage("Failed to insert plan.");
            resp.setStatus(100);
            return resp;
        }
        InstallPlan.IPInfo info = new InstallPlan().new IPInfo(server.getIp(), server.getMemory());
        info.getPort_status_map().put(server.getPort(), server.getStatus());
        ArrayList<InstallPlan.IPInfo> infos = new ArrayList<>();
        infos.add(info);
        cluster_info.setPlan_id(plan_id);
        new Thread(new InstallServerProc(infos, server, cluster_info, "recover", getServlet().getServletContext())).start();
    } catch (Exception e) {
        resp.setStatus(100);
        resp.setMessage("db query exception!");
        e.printStackTrace();
        return resp;
    } finally {
        util.releaseConn();
    }
    resp.setPlan_id(plan_id);
    resp.setMessage("success");
    resp.setStatus(0);
    return resp;
}
Also used : ServerInfo(beans.dbaccess.ServerInfo) ArrayList(java.util.ArrayList) Logger(org.apache.log4j.Logger) SQLException(java.sql.SQLException) ClusterInfo(beans.dbaccess.ClusterInfo) DBUtil(msec.org.DBUtil) RecoverPlanResponse(beans.response.RecoverPlanResponse) Map(java.util.Map)

Aggregations

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