Search in sources :

Example 1 with InstallPlanResponse

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

the class InstallPlan method exec.

public InstallPlanResponse exec(InstallPlanRequest request) {
    Logger logger = Logger.getLogger(InstallPlan.class);
    InstallPlanResponse resp = new InstallPlanResponse();
    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 helper_server = null;
    try {
        if (request.getAdded_servers() == null || request.getAdded_servers().size() == 0) {
            resp.setStatus(101);
            resp.setMessage("Request field error.");
            return resp;
        }
        Collections.sort(request.getAdded_servers(), compByGroupId());
        //get cluster info;
        ClusterInfo cluster_info = null;
        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());
        try {
            cluster_info = util.findSimpleRefResult(sql, params, ClusterInfo.class);
            if (cluster_info == null || !cluster_info.getPlan_id().isEmpty()) {
                resp.setStatus(101);
                resp.setMessage("Please wait until the ongoing plan finishes.");
                return resp;
            }
        } catch (Exception e) {
            resp.setStatus(100);
            resp.setMessage("db query exception!");
            e.printStackTrace();
            return resp;
        }
        List<ServerInfo> serviceList;
        ArrayList<String> ip_port_list = new ArrayList<>();
        HashMap<String, IPInfo> ip_map = new HashMap<>();
        //get current redis cluster ips
        sql = "select ip, port, status from t_service_info where first_level_service_name=? and second_level_service_name=? order by group_id asc, master desc limit 1";
        params.clear();
        params.add(request.getFirst_level_service_name());
        params.add(request.getSecond_level_service_name());
        try {
            serviceList = util.findMoreRefResult(sql, params, ServerInfo.class);
        } catch (Exception e) {
            resp.setStatus(100);
            resp.setMessage("db query exception!");
            e.printStackTrace();
            return resp;
        }
        if (serviceList.size() > 0) {
            for (ServerInfo info : serviceList) {
                if (info.getStatus().equals("OK")) {
                    helper_server = info;
                    break;
                }
            }
            if (helper_server == null) {
                //没有可用机器
                resp.setStatus(101);
                resp.setMessage("Cluster state error!");
                return resp;
            }
        }
        String plan_id = AddSecondLevelServiceIPInfo.newPlanID();
        for (ServerInfo server : request.getAdded_servers()) {
            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) 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("Planning");
            params.add("add");
            try {
                int addNum = util.updateByPreparedStatement(sql, params);
                if (addNum < 0) {
                    resp.setMessage("Failed to insert plan.");
                    resp.setStatus(100);
                    return resp;
                }
            } catch (SQLException e) {
                resp.setMessage("add record failed:" + e.toString());
                resp.setStatus(100);
                e.printStackTrace();
                return resp;
            }
            ip_port_list.add(server.getIp() + ":" + Integer.toString(server.getPort()));
            if (ip_map.containsKey(server.getIp())) {
                ip_map.get(server.getIp()).getPort_status_map().put(server.getPort(), server.getStatus());
            } else {
                IPInfo ipinfo = new IPInfo(server.getIp(), server.getMemory());
                ipinfo.getPort_status_map().put(server.getPort(), server.getStatus());
                ip_map.put(server.getIp(), ipinfo);
            }
        }
        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());
        try {
            int addNum = util.updateByPreparedStatement(sql, params);
            if (addNum < 0) {
                resp.setMessage("Failed to insert plan.");
                resp.setStatus(100);
                return resp;
            }
        } catch (SQLException e) {
            resp.setStatus(100);
            resp.setMessage("db query exception!");
            e.printStackTrace();
            return resp;
        }
        cluster_info.setPlan_id(plan_id);
        cluster_info.setIp_port_list(ip_port_list);
        logger.info(ip_port_list);
        new Thread(new InstallServerProc(new ArrayList<IPInfo>(ip_map.values()), helper_server, cluster_info, "create", getServlet().getServletContext())).start();
        /*
                try {
                    JedisHelper helper = null;
                    if (serviceList.size() > 0) {
                        helper = new JedisHelper(request.getPlan_id(), serviceList.get(0).getIp(), serviceList.get(0).getPort(), copy_num);
                        new Thread(new ClusterProc(helper, ip_port_list, "add", getServlet().getServletContext())).start();
                    } else {//CreateSet
                        helper = new JedisHelper(request.getPlan_id(), serverList.get(0).getIp(), serverList.get(0).getPort(), copy_num);
                        new Thread(new ClusterProc(helper, ip_port_list, "create", getServlet().getServletContext())).start();
                    }
                } catch (Exception e) {
                    resp.setStatus(101);
                    resp.setMessage("Redis cluster exception!");
                    e.printStackTrace();
                    return resp;
                }
*/
        resp.setPlan_id(plan_id);
        resp.setMessage("success");
        resp.setStatus(0);
        return resp;
    } finally {
        util.releaseConn();
    }
}
Also used : SQLException(java.sql.SQLException) ServerInfo(beans.dbaccess.ServerInfo) Logger(org.apache.log4j.Logger) SQLException(java.sql.SQLException) ClusterInfo(beans.dbaccess.ClusterInfo) DBUtil(msec.org.DBUtil) InstallPlanResponse(beans.response.InstallPlanResponse)

Aggregations

ClusterInfo (beans.dbaccess.ClusterInfo)1 ServerInfo (beans.dbaccess.ServerInfo)1 InstallPlanResponse (beans.response.InstallPlanResponse)1 SQLException (java.sql.SQLException)1 DBUtil (msec.org.DBUtil)1 Logger (org.apache.log4j.Logger)1