Search in sources :

Example 1 with ClusterInfo

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

Example 2 with ClusterInfo

use of beans.dbaccess.ClusterInfo in project MSEC by Tencent.

the class RemovePlan method exec.

public RemovePlanResponse exec(RemovePlanRequest request) {
    RemovePlanResponse resp = new RemovePlanResponse();
    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;
    }
    JedisHelper helper = null;
    List<ServerInfo> serverList;
    ServerInfo ok_server;
    try {
        String sql;
        List<Object> params = new ArrayList<Object>();
        //get plan_id;
        sql = "select copy_num, memory_per_instance, plan_id from t_second_level_service where first_level_service_name=? and second_level_service_name=?";
        params.add(request.getFirst_level_service_name());
        params.add(request.getSecond_level_service_name());
        try {
            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;
            }
        } catch (Exception e) {
            resp.setStatus(100);
            resp.setMessage("db query exception!");
            e.printStackTrace();
            return resp;
        }
        sql = "select ip, port, set_id, group_id, memory, master, status from t_service_info where first_level_service_name=? and second_level_service_name=? and master=1 and status='OK' limit 1";
        try {
            ok_server = util.findSimpleRefResult(sql, params, ServerInfo.class);
        } catch (Exception e) {
            resp.setStatus(100);
            resp.setMessage("OK server not found!");
            e.printStackTrace();
            return resp;
        }
        sql = "select ip, port, set_id, group_id, memory, master, 'Planning' as status from t_service_info where first_level_service_name=? and second_level_service_name=? and set_id=? order by group_id asc, master desc";
        params.add(request.getSet_id());
        try {
            serverList = util.findMoreRefResult(sql, params, ServerInfo.class);
        } catch (Exception e) {
            resp.setStatus(100);
            resp.setMessage("db query exception!");
            e.printStackTrace();
            return resp;
        }
        if (serverList == null || serverList.size() == 0) {
            resp.setStatus(100);
            resp.setMessage("set id error!");
            return resp;
        }
        String plan_id = AddSecondLevelServiceIPInfo.newPlanID();
        for (ServerInfo server : serverList) {
            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(server.getStatus());
            params.add("del");
            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;
            }
        }
        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;
        }
        int copy_num = 0;
        ArrayList<String> ip_port_list = new ArrayList<>();
        for (ServerInfo server : serverList) {
            if (server.getSet_id().equals(serverList.get(0).getSet_id()) && server.getGroup_id() == serverList.get(0).getGroup_id())
                copy_num++;
            if (server.isMaster())
                ip_port_list.add(server.getIp() + ":" + Integer.toString(server.getPort()));
        }
        try {
            helper = new JedisHelper(plan_id, ok_server.getIp(), ok_server.getPort(), copy_num);
            new Thread(new ClusterProc(helper, ip_port_list, "remove", getServlet().getServletContext())).start();
        } catch (Exception e) {
            resp.setStatus(101);
            resp.setMessage("Redis cluster exception!");
            e.printStackTrace();
            return resp;
        }
        resp.setMessage("success");
        resp.setPlan_id(plan_id);
        resp.setStatus(0);
        return resp;
    } finally {
        util.releaseConn();
        if (helper != null)
            helper.close();
    }
}
Also used : SQLException(java.sql.SQLException) ServerInfo(beans.dbaccess.ServerInfo) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) ClusterInfo(beans.dbaccess.ClusterInfo) DBUtil(msec.org.DBUtil) RemovePlanResponse(beans.response.RemovePlanResponse)

Example 3 with ClusterInfo

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

Example 4 with ClusterInfo

use of beans.dbaccess.ClusterInfo 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)4 ServerInfo (beans.dbaccess.ServerInfo)4 SQLException (java.sql.SQLException)4 DBUtil (msec.org.DBUtil)4 ArrayList (java.util.ArrayList)3 Logger (org.apache.log4j.Logger)3 InstallPlanResponse (beans.response.InstallPlanResponse)1 QuerySecondLevelServiceDetailResponse (beans.response.QuerySecondLevelServiceDetailResponse)1 RecoverPlanResponse (beans.response.RecoverPlanResponse)1 RemovePlanResponse (beans.response.RemovePlanResponse)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1