Search in sources :

Example 6 with ServerInfo

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

the class JedisHelper method insertInfo.

private void insertInfo(String ip, int port, String master_host) {
    changed = true;
    Logger logger = Logger.getLogger(InstallServerProc.class);
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        return;
    }
    try {
        if (master_host.isEmpty()) {
            String sql = "select name from t_set_id_name where name not in (select distinct set_id from t_service_info where first_level_service_name=? and second_level_service_name=?) order by name asc limit 1";
            List<Object> params = new ArrayList<Object>();
            params.add(first_level_service_name);
            params.add(second_level_service_name);
            Map<String, Object> result = util.findSimpleResult(sql, params);
            String set_id = result.get("name").toString();
            sql = "select COALESCE(max(group_id),0) as group_id from t_service_info where first_level_service_name=? and second_level_service_name=?";
            result = util.findSimpleResult(sql, params);
            if (result.size() != 1) {
                logger.error("get_group_id ERROR");
                return;
            }
            int group_id = ((Long) result.get("group_id")).intValue() + 1;
            ServerInfo server_info = new ServerInfo();
            server_info.setIp(ip);
            server_info.setPort(port);
            server_info.setSet_id(set_id);
            server_info.setGroup_id(group_id);
            server_info.setMemory(cluster_info.getMemory_per_instance());
            server_info.setMaster(true);
            sql = "insert into t_service_info(first_level_service_name, second_level_service_name, ip, port, set_id, group_id, memory, master) values(?,?,?,?,?,?,?,?)";
            params.add(ip);
            params.add(port);
            params.add(set_id);
            params.add(group_id);
            params.add(cluster_info.getMemory_per_instance());
            params.add(true);
            int updNum = util.updateByPreparedStatement(sql, params);
            if (updNum < 0) {
                logger.error(String.format("insert_master ERROR|%s|%d", ip, port));
                return;
            }
            host_map.put(ip + ":" + port, server_info);
        } else {
            ServerInfo server_info = host_map.get(master_host);
            if (server_info == null) {
                logger.error(String.format("insert_master ERROR|Master %s not found", master_host));
            } else {
                ServerInfo slave_info = new ServerInfo(server_info);
                slave_info.setIp(ip);
                slave_info.setPort(port);
                slave_info.setMaster(false);
                String sql = "insert into t_service_info(first_level_service_name, second_level_service_name, ip, port, set_id, group_id, memory, master) values(?,?,?,?,?,?,?,?)";
                List<Object> params = new ArrayList<Object>();
                params.add(first_level_service_name);
                params.add(second_level_service_name);
                params.add(ip);
                params.add(port);
                params.add(slave_info.getSet_id());
                params.add(slave_info.getGroup_id());
                params.add(cluster_info.getMemory_per_instance());
                params.add(false);
                int updNum = util.updateByPreparedStatement(sql, params);
                if (updNum < 0) {
                    logger.error(String.format("insert_slave ERROR|%s|%d|%s", ip, port, master_host));
                    return;
                }
                host_map.put(ip + ":" + port, slave_info);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return;
    } finally {
        util.releaseConn();
    }
}
Also used : DBUtil(msec.org.DBUtil) ServerInfo(beans.dbaccess.ServerInfo) Logger(org.apache.log4j.Logger) JedisClusterException(redis.clients.jedis.exceptions.JedisClusterException) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException)

Example 7 with ServerInfo

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

the class JedisHelper method deleteInfo.

private void deleteInfo(String set_id) {
    changed = true;
    Logger logger = Logger.getLogger(InstallServerProc.class);
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        return;
    }
    try {
        String sql = "delete from t_service_info where first_level_service_name=? and second_level_service_name=? and set_id=?";
        List<Object> params = new ArrayList<Object>();
        params.add(first_level_service_name);
        params.add(second_level_service_name);
        params.add(set_id);
        int updNum = util.updateByPreparedStatement(sql, params);
        if (updNum < 0) {
            logger.error(String.format("delete_set ERROR|%s", set_id));
            return;
        }
        for (Iterator<Map.Entry<String, ServerInfo>> it = host_map.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<String, ServerInfo> entry = it.next();
            if (entry.getValue().getSet_id().equals(set_id)) {
                it.remove();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return;
    } finally {
        util.releaseConn();
    }
}
Also used : ServerInfo(beans.dbaccess.ServerInfo) Logger(org.apache.log4j.Logger) JedisClusterException(redis.clients.jedis.exceptions.JedisClusterException) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) DBUtil(msec.org.DBUtil)

Example 8 with ServerInfo

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

the class JedisHelper method CheckStatusDetail.

public HashMap<String, ClusterStatus> CheckStatusDetail() {
    Logger logger = Logger.getLogger(JedisHelper.class);
    //HashMap<String, ClusterStatus> nodeid_status_map = new HashMap<>();
    setMigrating(false);
    setCreated(false);
    setOK(true);
    master_nodes.clear();
    allocated_slots.clear();
    cluster_status_map.clear();
    master_acks.clear();
    slot_sigs.clear();
    master_slave_infos.clear();
    for (Map.Entry<String, Jedis> entry : cluster.entrySet()) {
        //get cluster nodes...
        //<id> <ip:port> <flags> <master> <ping-sent> <pong-recv> <config-epoch> <link-state> <slot> <slot> ... <slot>
        //we only check myself node to get detailed info...
        //If jedis is null, we omit and discover this node from other live masters....
        CheckOneServer(entry.getKey(), entry.getValue());
    }
    //check config consistence, alarm!
    if (slot_sigs.size() > 0) {
        HashSet<String> hs = new HashSet<>();
        hs.addAll(slot_sigs.values());
        if (hs.size() != 1) {
            setOK(false);
            setError_message("[ERROR] Nodes don't agree about config! Please consult DBA for help.");
            logger.error(getError_message());
            logger.error(slot_sigs);
            return cluster_status_map;
        }
    }
    //check new ips...
    for (Map.Entry<String, ArrayList<String>> entry : master_acks.entrySet()) {
        if (!host_map.containsKey(entry.getKey())) {
            //check new master
            logger.info(String.format("AddMaster|%s", entry.getKey()));
            try {
                String[] ip_pair = entry.getKey().split(":");
                Jedis jedis = new Jedis(ip_pair[0], Integer.parseInt(ip_pair[1]));
                jedis.connect();
                cluster.put(entry.getKey(), jedis);
                CheckOneServer(entry.getKey(), jedis);
                insertInfo(ip_pair[0], Integer.parseInt(ip_pair[1]), "");
            } catch (JedisConnectionException ex) {
                cluster.put(entry.getKey(), null);
            }
        } else {
            if (!host_map.get(entry.getKey()).isMaster()) {
            //TODO,update to master
            }
        }
        ArrayList<String> slaves = master_slave_infos.get(entry.getKey());
        if (slaves != null) {
            for (String slave : slaves) {
                //check new slaves
                if (!host_map.containsKey(slave)) {
                    logger.info(String.format("AddSlave|%s|%s", slave, entry.getKey()));
                    String[] ip_pair = slave.split(":");
                    Jedis jedis = new Jedis(ip_pair[0], Integer.parseInt(ip_pair[1]));
                    jedis.connect();
                    cluster.put(slave, jedis);
                    CheckOneServer(slave, jedis);
                    insertInfo(ip_pair[0], Integer.parseInt(ip_pair[1]), entry.getKey());
                } else {
                    if (host_map.get(slave).getSet_id() != host_map.get(entry.getKey()).getSet_id()) {
                    //TODO, update to other slave
                    }
                }
            }
        }
    }
    logger.info(master_acks);
    logger.info(master_slave_infos);
    logger.info(host_map);
    //check slot coverage, alarm!
    if (allocated_slots.size() != CLUSTER_HASH_SLOTS) {
        setOK(false);
        setError_message("[ERROR] Nodes don't agree about config! Please consult DBA for help.");
        logger.error(getError_message());
        logger.error(allocated_slots.size());
        return cluster_status_map;
    } else
        setCreated(true);
    //check open slot, fixable -> migrating
    //remove invalid set...
    ArrayList<String> invalid_set_ids = new ArrayList<>();
    for (Map.Entry<String, ServerInfo> entry : host_map.entrySet()) {
        if (entry.getValue().isMaster() && master_acks.get(entry.getKey()) == null) {
            invalid_set_ids.add(entry.getValue().getSet_id());
        }
    }
    if (invalid_set_ids.size() > 0) {
        logger.info(String.format("deleteInfo|%s", invalid_set_ids));
    }
    for (String set_id : invalid_set_ids) deleteInfo(set_id);
    /*
        for(Map.Entry<String, ClusterStatus> entry : cluster_status_map.entrySet()) {
            ClusterStatus status = entry.getValue();
            String s;
            if(status.isOK()) {
                if (status.isMaster())
                    s = "M ";
                else
                    s = "S ";
                s += status.getIp_port();
                if (!status.isMaster()) {
                    ClusterStatus master_status = nodeid_status_map.get(status.getMaster_nodeid());
                    if(master_status != null)
                    {
                        s += " " + master_status.getIp_port();
                    }
                    else
                    {
                        s += " master error";
                    }
                }
                else {   //slave don't have slots...
                    s += " " + Integer.toString(status.getRunning_slots().size());
                }
            }
            else
            {
                s = "FAIL " + entry.getKey();
            }
            logger.info(s);
        }
        */
    return cluster_status_map;
}
Also used : ServerInfo(beans.dbaccess.ServerInfo) Logger(org.apache.log4j.Logger) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException)

Example 9 with ServerInfo

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

the class AddSecondLevelServiceIPInfo method CalcAddedIPForServers.

public void CalcAddedIPForServers(AddSecondLevelServiceIPInfoResponse response, int copy, ArrayList<String> set_ids, int group_id_index, int instance_memory) {
    boolean check_ok = true;
    int total_memory = 0;
    Map<String, Integer> set_instance_map = new HashMap<>();
    int i = 0;
    for (AddSecondLevelServiceIPInfoResponse.IPInfo ip : response.getAdded_ips()) {
        if (!ip.getStatus_message().equals("shell")) {
            if (ip.getCpu_load() >= 10) {
                ip.setStatus_message("cpu");
                check_ok = false;
            } else {
                if (ip.getInstance_num() < 1) {
                    ip.setStatus_message("memory");
                    check_ok = false;
                } else {
                    ip.setStatus_message("ok");
                    String set_id = set_ids.get(i / copy);
                    if (set_instance_map.containsKey(set_id))
                        set_instance_map.put(set_id, Math.min(set_instance_map.get(set_id), ip.getInstance_num()));
                    else
                        set_instance_map.put(set_id, ip.getInstance_num());
                    i++;
                }
            }
        } else
            check_ok = false;
    }
    if (!check_ok) {
        response.setMessage("machine");
    } else {
        response.setMessage("success");
        i = 0;
        int allocated_group_id = group_id_index + 1;
        int allocated_instance_num = 0;
        for (AddSecondLevelServiceIPInfoResponse.IPInfo ip : response.getAdded_ips()) {
            ArrayList<ServerInfo> servers = new ArrayList<>();
            String set_id = set_ids.get(i / copy);
            int set_innerid = i % copy;
            ip.setSet_id(set_id);
            int instance_num = set_instance_map.get(set_id);
            for (int j = 0; j < instance_num; j++) {
                ServerInfo server = new ServerInfo();
                server.setGroup_id(j + allocated_group_id);
                if (j >= allocated_instance_num && j < allocated_instance_num + (instance_num - allocated_instance_num) / (copy - set_innerid)) {
                    server.setMaster(true);
                } else
                    server.setMaster(false);
                server.setSet_id(set_id);
                server.setIp(ip.getIp());
                server.setPort(10000 + j + allocated_group_id);
                server.setMemory(instance_memory);
                servers.add(server);
            }
            ip.setServers(servers);
            allocated_instance_num += (instance_num - allocated_instance_num) / (copy - set_innerid);
            i++;
            if (set_innerid == copy - 1) {
                allocated_group_id += instance_num;
                allocated_instance_num = 0;
            }
        }
    }
    response.setStatus(0);
}
Also used : ServerInfo(beans.dbaccess.ServerInfo) AddSecondLevelServiceIPInfoResponse(beans.response.AddSecondLevelServiceIPInfoResponse)

Example 10 with ServerInfo

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

ServerInfo (beans.dbaccess.ServerInfo)10 DBUtil (msec.org.DBUtil)8 Logger (org.apache.log4j.Logger)7 SQLException (java.sql.SQLException)6 ClusterInfo (beans.dbaccess.ClusterInfo)4 ArrayList (java.util.ArrayList)4 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)3 AddSecondLevelServiceIPInfoResponse (beans.response.AddSecondLevelServiceIPInfoResponse)2 JedisClusterException (redis.clients.jedis.exceptions.JedisClusterException)2 JedisDataException (redis.clients.jedis.exceptions.JedisDataException)2 GetPlanDetailResponse (beans.response.GetPlanDetailResponse)1 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 RemoteShell (msec.org.RemoteShell)1