Search in sources :

Example 1 with DBUtil

use of msec.org.DBUtil in project MSEC by Tencent.

the class RemoveServerProc method deleteServer.

private void deleteServer(String ip) {
    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 ip=?";
        List<Object> params = new ArrayList<Object>();
        params.add(cluster_info.getFirst_level_service_name());
        params.add(cluster_info.getSecond_level_service_name());
        params.add(ip);
        int updNum = util.updateByPreparedStatement(sql, params);
        if (updNum < 0) {
            logger.error(String.format("delete_server ERROR|%s", ip));
            return;
        }
    } catch (Exception e) {
        logger.error(e);
        return;
    } finally {
        util.releaseConn();
    }
}
Also used : DBUtil(msec.org.DBUtil) ArrayList(java.util.ArrayList) Logger(org.apache.log4j.Logger)

Example 2 with DBUtil

use of msec.org.DBUtil in project MSEC by Tencent.

the class QueryESClusterDetail method exec.

public QueryESClusterDetailResponse exec(QueryESClusterDetailRequest request) {
    Logger logger = Logger.getLogger(QueryESClusterDetail.class);
    QueryESClusterDetailResponse resp = new QueryESClusterDetailResponse();
    String result = checkIdentity();
    if (!result.equals("success")) {
        resp.setStatus(99);
        resp.setMessage(result);
        return resp;
    }
    boolean all_ok = true;
    int master_num = 0;
    util = new DBUtil();
    if (util.getConnection() == null) {
        resp.setStatus(100);
        resp.setMessage("db connect failed!");
        return resp;
    }
    cluster_ips = new ArrayList<>();
    HashMap<String, QueryESClusterDetailResponse.RTInfo> info_map = new HashMap<>();
    try {
        if (updatePlan(request.getFirst_level_service_name(), request.getSecond_level_service_name())) {
            resp.setMessage("refresh");
            resp.setStatus(101);
            return resp;
        }
        getStatus(request.getFirst_level_service_name(), request.getSecond_level_service_name());
        ESHelper helper = new ESHelper();
        helper.ClusterStatus(cluster_ips, request.getFirst_level_service_name() + "." + request.getSecond_level_service_name(), resp);
        if (resp.getHealth_status() != "INIT") {
            for (Map.Entry<String, QueryESClusterDetailResponse.RTInfo> entry : resp.getInfo_map().entrySet()) {
                if (!cluster_ips.contains(entry.getKey())) {
                    // add ip to service
                    addServer(request.getSecond_level_service_name(), request.getSecond_level_service_name(), entry.getKey(), resp.getServer_port());
                    resp.setMessage("refresh");
                    resp.setStatus(101);
                    return resp;
                }
            }
            for (String ip : cluster_ips) {
                QueryESClusterDetailResponse.RTInfo rt_info = resp.getInfo_map().get(ip);
                if (rt_info != null) {
                    if (!rt_info.isOK()) {
                        logger.error(String.format("ip failed|%s", ip));
                    }
                    updateStatus(request.getFirst_level_service_name(), request.getSecond_level_service_name(), ip, rt_info.isOK());
                } else {
                    resp.getInfo_map().put(ip, new QueryESClusterDetailResponse().new RTInfo());
                    updateStatus(request.getFirst_level_service_name(), request.getSecond_level_service_name(), ip, false);
                }
            }
        }
    } catch (Exception e) {
        logger.error(e);
        e.printStackTrace();
    } finally {
        util.releaseConn();
    }
    return resp;
}
Also used : HashMap(java.util.HashMap) Logger(org.apache.log4j.Logger) SQLException(java.sql.SQLException) DBUtil(msec.org.DBUtil) Map(java.util.Map) HashMap(java.util.HashMap) QueryESClusterDetailResponse(beans.response.QueryESClusterDetailResponse)

Example 3 with DBUtil

use of msec.org.DBUtil in project MSEC by Tencent.

the class ESHelper method updateStatus.

private void updateStatus(String ip, String status) {
    Logger logger = Logger.getLogger(InstallServerProc.class);
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        return;
    }
    try {
        String sql = "update t_install_plan set status=? where plan_id=? and ip=?";
        List<Object> params = new ArrayList<Object>();
        params.add(status);
        params.add(cluster_info.getPlan_id());
        params.add(ip);
        int updNum = util.updateByPreparedStatement(sql, params);
        if (updNum != 1) {
            return;
        }
    } catch (Exception e) {
        logger.error(e);
        logger.error(e);
        return;
    } finally {
        util.releaseConn();
    }
}
Also used : DBUtil(msec.org.DBUtil) Logger(org.apache.log4j.Logger) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 4 with DBUtil

use of msec.org.DBUtil in project MSEC by Tencent.

the class AddService method exec.

public AddServiceResponse exec(AddServiceRequest request) throws UnsupportedEncodingException {
    AddServiceResponse response = new AddServiceResponse();
    response.setMessage("unkown error.");
    response.setStatus(100);
    String result = checkIdentity();
    if (!result.equals("success")) {
        response.setStatus(99);
        response.setMessage(result);
        return response;
    }
    if (request.getService_name() == null || request.getService_name().equals("") || request.getService_name().equals("RESERVED") || request.getService_level() == null || request.getService_level().equals("") || request.getService_level().equals("")) {
        response.setMessage("The name/level of service to be added should NOT be empty or RESERVED.");
        response.setStatus(100);
        return response;
    }
    if (request.getService_name().contains(".")) {
        response.setMessage("The name of service to be added should NOT contains \".\".");
        response.setStatus(100);
        return response;
    }
    if (request.getService_name().getBytes("UTF-8").length >= 64) {
        response.setMessage("The length of the name of service to be added should NOT be larger than 64 bytes.");
        response.setStatus(100);
        return response;
    }
    if (request.getService_level().equals("second_level") && (request.getService_parent() == null || request.getService_parent().equals(""))) {
        response.setMessage("The first level service name should NOT be empty.");
        response.setStatus(100);
        return response;
    }
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        response.setMessage("DB connect failed.");
        response.setStatus(100);
        return response;
    }
    String sql;
    List<Object> params = new ArrayList<Object>();
    if (request.getService_level().equals("first_level")) {
        sql = "insert into t_first_level_service( first_level_service_name, type) values(?, 'standard')";
        params.add(request.getService_name());
    } else {
        sql = "insert into t_second_level_service(second_level_service_name, first_level_service_name, status) values(?,?,'OK')";
        params.add(request.getService_name());
        params.add(request.getService_parent());
    }
    try {
        int addNum = util.updateByPreparedStatement(sql, params);
        if (addNum >= 0) {
            response.setAddNum(addNum);
            response.setMessage("success");
            response.setStatus(0);
            return response;
        }
    } catch (SQLException e) {
        response.setMessage("add record failed:" + e.toString());
        response.setStatus(100);
        e.printStackTrace();
        return response;
    } finally {
        util.releaseConn();
    }
    return response;
}
Also used : AddServiceResponse(beans.response.AddServiceResponse) DBUtil(msec.org.DBUtil) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList)

Example 5 with DBUtil

use of msec.org.DBUtil in project MSEC by Tencent.

the class GetPlanList method exec.

public GetPlanListResponse exec(GetPlanListRequest request) {
    GetPlanListResponse resp = new GetPlanListResponse();
    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<PlanInfo> planList;
    HashMap<String, String> plan_status_map = new HashMap<>();
    ArrayList<PlanInfo> plans = new ArrayList<>();
    try {
        String sql = "select plan_id, operation, status from t_install_plan where first_level_service_name=? and second_level_service_name=?" + " and create_time>=? and create_time<=? group by plan_id, operation, status order by plan_id desc";
        List<Object> params = new ArrayList<Object>();
        params.add(request.getFirst_level_service_name());
        params.add(request.getSecond_level_service_name());
        params.add(request.getBegin_date() + " 00:00:00");
        params.add(request.getEnd_date() + " 23:59:59");
        planList = util.findMoreRefResult(sql, params, PlanInfo.class);
        for (PlanInfo plan : planList) {
            String status = plan_status_map.get(plan.getPlan_id());
            if (status == null) {
                if (plan.getStatus().startsWith("Done")) {
                    plan_status_map.put(plan.getPlan_id(), status_ok);
                } else if (plan.getStatus().startsWith("[ERROR]")) {
                    plan_status_map.put(plan.getPlan_id(), status_error);
                } else {
                    plan_status_map.put(plan.getPlan_id(), status_ip);
                }
            } else {
                // ERROR > In Progress > Done.
                if (!plan.getStatus().startsWith("Done")) {
                    if (!status.equals(status_error)) {
                        if (plan.getStatus().startsWith("[ERROR]")) {
                            plan_status_map.put(plan.getPlan_id(), status_error);
                        } else {
                            // in progress
                            if (status.equals(status_ok)) {
                                plan_status_map.put(plan.getPlan_id(), status_ip);
                            }
                        }
                    }
                }
            }
        }
        for (PlanInfo plan : planList) {
            String status = plan_status_map.remove(plan.getPlan_id());
            if (status != null) {
                if (plan.getOperation().equals("add")) {
                    plan.setOperation(op_add);
                } else if (plan.getOperation().equals("del")) {
                    plan.setOperation(op_remove);
                } else {
                    plan.setOperation(op_recover);
                }
                plan.setStatus(status);
                plans.add(plan);
            }
        }
    } catch (Exception e) {
        resp.setStatus(100);
        resp.setMessage("db query exception!");
        e.printStackTrace();
        return resp;
    } finally {
        util.releaseConn();
    }
    resp.setPlans(plans);
    resp.setMessage("success");
    resp.setStatus(0);
    return resp;
}
Also used : PlanInfo(beans.dbaccess.PlanInfo) DBUtil(msec.org.DBUtil) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GetPlanListResponse(beans.response.GetPlanListResponse)

Aggregations

DBUtil (msec.org.DBUtil)24 Logger (org.apache.log4j.Logger)16 ArrayList (java.util.ArrayList)15 SQLException (java.sql.SQLException)12 ServerInfo (beans.dbaccess.ServerInfo)8 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)5 JedisDataException (redis.clients.jedis.exceptions.JedisDataException)5 ClusterInfo (beans.dbaccess.ClusterInfo)4 HashMap (java.util.HashMap)4 JedisClusterException (redis.clients.jedis.exceptions.JedisClusterException)4 Map (java.util.Map)3 StaffInfo (beans.dbaccess.StaffInfo)2 IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)2 NoNodeAvailableException (org.elasticsearch.client.transport.NoNodeAvailableException)2 MasterNotDiscoveredException (org.elasticsearch.discovery.MasterNotDiscoveredException)2 FirstLevelService (beans.dbaccess.FirstLevelService)1 PlanInfo (beans.dbaccess.PlanInfo)1 AddNewStaffResponse (beans.response.AddNewStaffResponse)1