Search in sources :

Example 16 with IPPortPair

use of beans.request.IPPortPair in project MSEC by Tencent.

the class AddOddSecondLevelServiceIPInfo method exec.

public AddOddSecondLevelServiceIPInfoResponse exec(OddSecondLevelServiceIPInfo request) {
    AddOddSecondLevelServiceIPInfoResponse response = new AddOddSecondLevelServiceIPInfoResponse();
    response.setMessage("unkown error.");
    response.setStatus(100);
    String result = checkIdentity();
    if (!result.equals("success")) {
        response.setStatus(99);
        response.setMessage(result);
        return response;
    }
    if (request.getIp() == null || request.getIp().equals("") || request.getSecond_level_service_name() == null || request.getSecond_level_service_name().equals("")) {
        response.setMessage("Some request field is  empty.");
        response.setStatus(100);
        return response;
    }
    ArrayList<String> ips = Tools.splitBySemicolon(request.getIp());
    if (ips == null || ips.size() == 0) {
        response.setMessage("IP 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;
    }
    ArrayList<IPPortPair> addedIP = new ArrayList<IPPortPair>();
    try {
        for (int i = 0; i < ips.size(); ++i) {
            String s = ips.get(i);
            int index = s.indexOf(":");
            if (index <= 0 || index == (s.length() - 1)) {
                continue;
            }
            String ip = s.substring(0, index);
            int port = Integer.valueOf(s.substring(index + 1));
            String sql;
            List<Object> params = new ArrayList<Object>();
            sql = "insert into t_second_level_service_ipinfo(ip,port,status, second_level_service_name,first_level_service_name, comm_proto) values(?,?,?,?,?,?)";
            params.add(ip);
            params.add(port);
            params.add("disabled");
            params.add(request.getSecond_level_service_name());
            params.add(request.getFirst_level_service_name());
            params.add(request.getComm_proto());
            try {
                int addNum = util.updateByPreparedStatement(sql, params);
                if (addNum >= 0) {
                    IPPortPair pair = new IPPortPair();
                    pair.setIp(ip);
                    pair.setPort(port);
                    pair.setStatus("disabled");
                    addedIP.add(pair);
                } else {
                    response.setMessage("failed to insert table");
                    response.setStatus(100);
                    return response;
                }
            } catch (SQLException e) {
                response.setMessage("add record failed:" + e.toString());
                response.setStatus(100);
                e.printStackTrace();
                return response;
            }
        }
        response.setMessage("success");
        response.setStatus(0);
        response.setAddedIPs(addedIP);
        return response;
    } finally {
        util.releaseConn();
    }
}
Also used : DBUtil(ngse.org.DBUtil) IPPortPair(beans.request.IPPortPair) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) AddOddSecondLevelServiceIPInfoResponse(beans.response.AddOddSecondLevelServiceIPInfoResponse)

Example 17 with IPPortPair

use of beans.request.IPPortPair in project MSEC by Tencent.

the class ChangeCapacity method exec.

public JsonRPCResponseBase exec(ChangeCapacityRequest request) {
    JsonRPCResponseBase response = new JsonRPCResponseBase();
    String result = checkIdentity();
    if (!result.equals("success")) {
        response.setStatus(99);
        response.setMessage(result);
        return response;
    }
    Logger logger = Logger.getLogger(ChangeCapacity.class);
    //把ip列表刷新到LB的服务器里并下发给各个客户端
    AccessZooKeeper azk = new AccessZooKeeper();
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        response.setStatus(100);
        response.setMessage("db connect failed");
        return response;
    }
    try {
        //step1:更新扩缩容的IP的状态
        if (//扩容
        request.getAction_type().equals("expand")) {
            updateIPStatus(request, true, util);
        } else // 缩容
        {
            updateIPStatus(request, false, util);
        }
        logger.info("update matchine's status OK, matchine Numbers:" + request.getIp_list().size());
        //step2: 获得该服务下所有enabled的IP,写入LB系统
        ArrayList<IPPortPair> ips = LoadBalance.getIPPortInfoByServiceName(request.getFirst_level_service_name(), request.getSecond_level_service_name(), util);
        if (ips == null) {
            response.setStatus(100);
            response.setMessage("get ip info from db failed");
            return response;
        }
        logger.info("get enabled IP, they will be write to LB server, number:" + ips.size());
        result = LoadBalance.writeOneServiceConfigInfo(azk, request.getFirst_level_service_name() + "/" + request.getSecond_level_service_name(), true, ips);
        if (result == null || !result.equals("success")) {
            response.setStatus(100);
            response.setMessage(result);
            return response;
        }
        logger.info("write into LB server successfully.");
        //step3: 更新涉及到的IP为key的配置信息
        ArrayList<String> whiteNameList = new ArrayList<>();
        for (int i = 0; i < request.getIp_list().size(); i++) {
            whiteNameList.add(request.getIp_list().get(i).getIp());
        }
        logger.info("update the IP-key information in LB server");
        result = LoadBalance.writeIPConfigInfo(azk, whiteNameList);
        if (result == null || !result.equals("success")) {
            response.setStatus(100);
            response.setMessage(result);
            return response;
        }
        response.setStatus(0);
        response.setMessage("success");
        return response;
    } catch (Exception e) {
        e.printStackTrace();
        response.setStatus(100);
        response.setMessage(e.getMessage());
        return response;
    } finally {
        azk.disconnect();
        util.releaseConn();
    }
}
Also used : DBUtil(ngse.org.DBUtil) JsonRPCResponseBase(ngse.org.JsonRPCResponseBase) IPPortPair(beans.request.IPPortPair) ArrayList(java.util.ArrayList) Logger(org.apache.log4j.Logger) AccessZooKeeper(ngse.org.AccessZooKeeper)

Example 18 with IPPortPair

use of beans.request.IPPortPair in project MSEC by Tencent.

the class QueryBusiLogField method doGetBusiLogField.

private String doGetBusiLogField(BusiLogField request, ArrayList<BusiLogField> fields) {
    Logger logger = Logger.getLogger(QueryBusinessLog.class);
    String sJson = String.format("{\"getFieldsReq\":{\"appName\":null}}");
    logger.info("to log srv:" + sJson);
    String lens = String.format("%10d", sJson.length());
    ArrayList<IPPortPair> ipPortPairs = QueryBusinessLog.getBusiLogSrvIPPort();
    if (ipPortPairs == null || ipPortPairs.size() < 1) {
        return "get log server ip failed";
    }
    log_srv_ip = ipPortPairs.get(0).getIp();
    log_srv_port = ipPortPairs.get(0).getPort();
    Socket socket = new Socket();
    try {
        socket.setSoTimeout(5000);
        socket.connect(new InetSocketAddress(log_srv_ip, log_srv_port), 3000);
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(lens.getBytes());
        outputStream.write(sJson.getBytes());
        socket.shutdownOutput();
        //收应答包
        // length field 10bytes
        byte[] buf = new byte[10240];
        int total_len = 0;
        while (total_len < 10) {
            int len = socket.getInputStream().read(buf, total_len, 10 - total_len);
            if (len <= 0) {
                return "receive json len failed.";
            }
            total_len += len;
        }
        int jsonLen = new Integer(new String(buf, 0, total_len).trim()).intValue();
        logger.info(String.format("response json string len:%d", jsonLen));
        if (jsonLen > buf.length) {
            logger.error("response json is too long" + jsonLen);
            return "response json is too long";
        }
        total_len = 0;
        while (total_len < jsonLen) {
            int len = socket.getInputStream().read(buf, total_len, jsonLen - total_len);
            if (len <= 0) {
                return "receive json string failed.";
            }
            total_len += len;
        }
        logger.info(String.format("read logserver response bytes number:%d", total_len));
        String jsonStr = new String(buf, 0, total_len);
        logger.info("from log srv:" + jsonStr);
        JSONObject jsonObject = new JSONObject(jsonStr);
        jsonObject = jsonObject.getJSONObject("getFieldsRsp");
        int status = jsonObject.getInt("ret");
        if (status != 0) {
            String message = jsonObject.getString("errmsg");
            logger.error("log server returns errcode, msg:" + message);
            return message;
        }
        JSONArray infoArray = jsonObject.getJSONArray("fieldsInfo");
        for (int i = 0; i < infoArray.length(); i++) {
            JSONObject info = infoArray.getJSONObject(i);
            BusiLogField field = new BusiLogField();
            field.setField_type(info.getString("field_type"));
            field.setField_name(info.getString("field_name"));
            logger.info(String.format("field%d:%s,%s", i, field.getField_name(), field.getField_type()));
            fields.add(field);
        }
        return "success";
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return e.getMessage();
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (Exception e) {
            }
            ;
        }
    }
}
Also used : BusiLogField(beans.request.BusiLogField) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) JSONArray(org.json.JSONArray) Logger(org.apache.log4j.Logger) IPPortPair(beans.request.IPPortPair) JSONObject(org.json.JSONObject) Socket(java.net.Socket)

Example 19 with IPPortPair

use of beans.request.IPPortPair in project MSEC by Tencent.

the class QueryReleasePlanDetail method exec.

public QueryReleasePlanDetailResponse exec(ReleasePlan request) {
    QueryReleasePlanDetailResponse resp = new QueryReleasePlanDetailResponse();
    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<ReleasePlan> planList;
    try {
        String sql = "select plan_id, first_level_service_name,second_level_service_name," + "config_tag,idl_tag, sharedobject_tag, status,memo,backend_task_status " + "from t_release_plan where plan_id=?";
        List<Object> params = new ArrayList<Object>();
        params.add(request.getPlan_id());
        planList = util.findMoreRefResult(sql, params, ReleasePlan.class);
        if (planList.size() != 1) {
            resp.setStatus(100);
            resp.setMessage("Plan does NOT exist.");
            return resp;
        }
        resp.setDetail(planList.get(0));
        ReleasePlan detail = resp.getDetail();
        sql = "select dest_ip_list from t_release_plan where plan_id=?";
        params = new ArrayList<Object>();
        params.add(request.getPlan_id());
        Map<String, Object> res = util.findSimpleResult(sql, params);
        if (res.get("dest_ip_list") == null) {
            resp.setStatus(100);
            resp.setMessage("query dest ip list failed.");
            return resp;
        }
        String dest_ip_list = (String) (res.get("dest_ip_list"));
        ObjectMapper objectMapper = new ObjectMapper();
        Object o = objectMapper.readValue(dest_ip_list, new ArrayList<IPPortPair>().getClass());
        detail.setDest_ip_list((ArrayList<IPPortPair>) o);
    } catch (Exception e) {
        resp.setStatus(100);
        resp.setMessage("db query exception!");
        e.printStackTrace();
        return resp;
    } finally {
        util.releaseConn();
    }
    resp.setMessage("success");
    resp.setStatus(0);
    return resp;
}
Also used : ArrayList(java.util.ArrayList) QueryReleasePlanDetailResponse(beans.response.QueryReleasePlanDetailResponse) ReleasePlan(beans.request.ReleasePlan) DBUtil(ngse.org.DBUtil) IPPortPair(beans.request.IPPortPair) JSONObject(org.json.JSONObject) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 20 with IPPortPair

use of beans.request.IPPortPair in project MSEC by Tencent.

the class RefreshOddCapacity method exec.

public JsonRPCResponseBase exec(CapacityRequest request) {
    JsonRPCResponseBase response = new JsonRPCResponseBase();
    String result = checkIdentity();
    if (!result.equals("success")) {
        response.setStatus(99);
        response.setMessage(result);
        return response;
    }
    Logger logger = Logger.getLogger(RefreshOddCapacity.class);
    //把ip列表刷新到LB的服务器里并下发给各个客户端
    AccessZooKeeper azk = new AccessZooKeeper();
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        response.setStatus(100);
        response.setMessage("db connect failed");
        return response;
    }
    try {
        //step2: 获得该服务下所有enabled的IP,写入LB系统
        ArrayList<IPPortPair> ips = LoadBalance.getIPPortInfoByServiceName(request.getFirst_level_service_name(), request.getSecond_level_service_name(), util);
        if (ips == null) {
            response.setStatus(100);
            response.setMessage("get ip info from db failed");
            return response;
        }
        logger.info("get enabled IP, they will be write to LB server, number:" + ips.size());
        result = LoadBalance.writeOneServiceConfigInfo(azk, request.getFirst_level_service_name() + "/" + request.getSecond_level_service_name(), false, ips);
        if (result == null || !result.equals("success")) {
            response.setStatus(100);
            response.setMessage(result);
            return response;
        }
        logger.info("write into LB server successfully.");
        response.setStatus(0);
        response.setMessage("success");
        return response;
    } catch (Exception e) {
        e.printStackTrace();
        response.setStatus(100);
        response.setMessage(e.getMessage());
        return response;
    } finally {
        azk.disconnect();
        util.releaseConn();
    }
}
Also used : DBUtil(ngse.org.DBUtil) JsonRPCResponseBase(ngse.org.JsonRPCResponseBase) IPPortPair(beans.request.IPPortPair) Logger(org.apache.log4j.Logger) AccessZooKeeper(ngse.org.AccessZooKeeper)

Aggregations

IPPortPair (beans.request.IPPortPair)22 Logger (org.apache.log4j.Logger)18 ArrayList (java.util.ArrayList)12 DBUtil (ngse.org.DBUtil)12 AccessZooKeeper (ngse.org.AccessZooKeeper)7 JSONObject (org.json.JSONObject)7 OutputStream (java.io.OutputStream)5 InetSocketAddress (java.net.InetSocketAddress)5 Socket (java.net.Socket)5 ServletOutputStream (javax.servlet.ServletOutputStream)5 JsonRPCResponseBase (ngse.org.JsonRPCResponseBase)4 SecondLevelService (beans.dbaccess.SecondLevelService)3 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)3 JSONArray (org.json.JSONArray)3 BusinessLogResult (beans.dbaccess.BusinessLogResult)2 OddSecondLevelService (beans.dbaccess.OddSecondLevelService)2 SQLException (java.sql.SQLException)2 CapacityBaseInfo (beans.dbaccess.CapacityBaseInfo)1 CapacityDetailInfo (beans.dbaccess.CapacityDetailInfo)1 BusiLogField (beans.request.BusiLogField)1