Search in sources :

Example 1 with DBUtil

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

the class LibraryFileUpload method insertTable.

private String insertTable(String firstName, String secondName, String baseFileName, String memo) {
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        return "DB connect failed.";
    }
    String sql;
    List<Object> params = new ArrayList<Object>();
    sql = "insert into t_library_file(first_level_service_name,  second_level_service_name,file_name, memo) values(?,?,?,?)";
    params.add(firstName);
    params.add(secondName);
    params.add(baseFileName);
    params.add(memo);
    try {
        int addNum = util.updateByPreparedStatement(sql, params);
        if (addNum >= 0) {
            return "success";
        }
        return "insert failed";
    } catch (SQLException e) {
        return "insert failed";
    } finally {
        util.releaseConn();
    }
}
Also used : DBUtil(ngse.org.DBUtil) SQLException(java.sql.SQLException)

Example 2 with DBUtil

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

the class LoadBalance method writeIPConfigInfo.

//以IP为key的配置信息写入LB,whiteNameList为需要写的白名单IP列表,可以为null
public static String writeIPConfigInfo(AccessZooKeeper azk, ArrayList<String> whiteNameList) throws Exception {
    Logger logger = Logger.getLogger(LoadBalance.class);
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        return "db connect failed";
    }
    logger.info("write IP key info into LB ...");
    HashMap<String, HashSet<String>> ip2Svcs = new HashMap<String, HashSet<String>>();
    try {
        //获得所有服务
        ArrayList<SecondLevelService> services = getAllService(util);
        if (services == null) {
            logger.error("get all service failed");
            return "get service list from db failed";
        }
        //对每个服务获取下属的IP
        for (int i = 0; i < services.size(); i++) {
            SecondLevelService svc = services.get(i);
            ArrayList<IPPortPair> ips = getIPPortInfoByServiceName(svc.getFirst_level_service_name(), svc.getSecond_level_service_name(), util);
            if (ips == null) {
                logger.error("get ip list for " + svc.getSecond_level_service_name() + " failed.");
                return "get ip list from db failed";
            }
            logger.info("get ip list for " + svc.getSecond_level_service_name() + " successfully.ip number" + ips.size());
            //生成IP为key、servicename列表为value的反向映射关系
            geneIP2Svcs(svc.getFirst_level_service_name() + "." + svc.getSecond_level_service_name(), ips, ip2Svcs);
        }
        logger.info("gene IP->service map successfully, map size:" + ip2Svcs.size());
        if (whiteNameList == null) {
            //删除掉整个ip配置信息
            String path = "/nodeservices";
            azk.deleteRecursive(path);
            logger.info("delete whole tree:" + path);
        } else {
            //删除掉白名单中的ip配置信息
            for (int i = 0; i < whiteNameList.size(); i++) {
                String path = "/nodeservices/" + whiteNameList.get(i);
                azk.deleteRecursive(path);
                logger.info("delete node:" + path);
            }
        }
        //往LB写入ip为key的数据
        for (Map.Entry<String, HashSet<String>> entry : ip2Svcs.entrySet()) {
            if (//如果只更新列表中的指定的IP列表
            whiteNameList != null) {
                if (whiteNameList.indexOf(entry.getKey()) < 0) {
                    continue;
                }
            }
            String path = "/nodeservices/" + entry.getKey();
            String data = "{\"services\":[";
            Iterator<String> it = entry.getValue().iterator();
            int eleIndex = 0;
            while (it.hasNext()) {
                if (eleIndex > 0) {
                    //与前一个元素的分割
                    data += ",";
                }
                data += "\"" + it.next() + "\"";
                eleIndex++;
            }
            data += "]}";
            logger.info("write LB, path=" + path);
            logger.info("data=" + data);
            String result = azk.write(path, data.getBytes());
            if (result == null || !result.equals("success")) {
                logger.error("write LB failed:" + result);
                return result;
            }
        }
        return "success";
    } finally {
        util.releaseConn();
    }
}
Also used : OddSecondLevelService(beans.dbaccess.OddSecondLevelService) SecondLevelService(beans.dbaccess.SecondLevelService) Logger(org.apache.log4j.Logger) DBUtil(ngse.org.DBUtil) IPPortPair(beans.request.IPPortPair)

Example 3 with DBUtil

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

the class LoadBalance method writeServiceConfigInfo.

//把所有服务的配置信息写入LB
public static String writeServiceConfigInfo(AccessZooKeeper azk) throws Exception {
    DBUtil util = new DBUtil();
    Logger logger = Logger.getLogger(LoadBalance.class);
    if (util.getConnection() == null) {
        return "db connect failed";
    }
    logger.info("write IP information of ALL services into LB...");
    try {
        //删除掉整个配置信息
        String path = "/nameservice";
        azk.deleteRecursive(path);
        logger.info("delete whole tree:" + path);
        //获得所有服务
        ArrayList<SecondLevelService> services = getAllService(util);
        if (services == null) {
            return "get standard service list from db failed";
        }
        logger.info("getAllService() OK, service number:" + services.size());
        //对每个服务获取下属的IP
        for (int i = 0; i < services.size(); i++) {
            SecondLevelService svc = services.get(i);
            ArrayList<IPPortPair> ips = getIPPortInfoByServiceName(svc.getFirst_level_service_name(), svc.getSecond_level_service_name(), util);
            if (ips == null) {
                return "get ip list from db failed";
            }
            logger.info("get ip for " + svc.getSecond_level_service_name() + " OK, ip number" + ips.size());
            //写到 LB系统里
            String result = writeOneServiceConfigInfo(azk, svc.getFirst_level_service_name() + "/" + svc.getSecond_level_service_name(), svc.getType().equals("standard"), ips);
            if (result == null || !result.equals("success")) {
                logger.error("write service IP information into LB failed!" + result);
                return result;
            }
            logger.info("write to LB ok");
        }
        return "success";
    } finally {
        util.releaseConn();
    }
}
Also used : OddSecondLevelService(beans.dbaccess.OddSecondLevelService) SecondLevelService(beans.dbaccess.SecondLevelService) DBUtil(ngse.org.DBUtil) IPPortPair(beans.request.IPPortPair) Logger(org.apache.log4j.Logger)

Example 4 with DBUtil

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

the class MonitorBySvcOrIP method initMonitorIPAndPort.

public static void initMonitorIPAndPort() throws Exception {
    DBUtil util = new DBUtil();
    Logger logger = Logger.getLogger(MonitorBySvcOrIP.class);
    try {
        if (util.getConnection() == null) {
            Exception e = new Exception("connect db failed.");
            throw e;
        }
        String sql = sql = "select ip,port from t_second_level_service_ipinfo where second_level_service_name='monitor' and " + "first_level_service_name='RESERVED'  and status='enabled'";
        ArrayList<OddSecondLevelServiceIPInfo> list = util.findMoreRefResult(sql, null, OddSecondLevelServiceIPInfo.class);
        if (list == null || list.size() < 1) {
            throw new Exception("no db record exists.");
        }
        monitor_server_ip = list.get(0).getIp();
        //为什么加1,是个很长的故事,源于monitor的开发者将读写分离成两个端口
        monitor_server_port = list.get(0).getPort().intValue() + 1;
        logger.info(String.format("monitor server:%s:%d", monitor_server_ip, monitor_server_port));
    } finally {
        util.releaseConn();
    }
}
Also used : DBUtil(ngse.org.DBUtil) ByteString(com.google.protobuf.ByteString) Logger(org.apache.log4j.Logger) ExecutionException(java.util.concurrent.ExecutionException) OddSecondLevelServiceIPInfo(beans.dbaccess.OddSecondLevelServiceIPInfo)

Example 5 with DBUtil

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

the class AddIDLTag method exec.

public AddIDLTagResponse exec(IDL request) {
    AddIDLTagResponse response = new AddIDLTagResponse();
    response.setMessage("unkown error.");
    response.setStatus(100);
    //检查身份
    String result = checkIdentity();
    if (!result.equals("success")) {
        response.setStatus(99);
        response.setMessage(result);
        return response;
    }
    if (request.getTag_name() == null || request.getTag_name().equals("") || request.getFirst_level_service_name() == null || request.getFirst_level_service_name().length() < 1 || request.getSecond_level_service_name() == null || request.getSecond_level_service_name().length() < 1 || request.getContent() == null || request.getContent().length() < 1) {
        response.setMessage("Some request field is  empty.");
        response.setStatus(100);
        return response;
    }
    //连接数据库
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        response.setMessage("DB connect failed.");
        response.setStatus(100);
        return response;
    }
    try {
        //先在服务器保存文件
        String filename = IDL.getIDLFileName(request.getFirst_level_service_name(), request.getSecond_level_service_name(), request.getTag_name());
        if (!SaveIDLContent(filename, request.getContent())) {
            response.setMessage("save file failed.");
            response.setStatus(100);
            return response;
        }
        //在数据库里记录一条
        String sql;
        List<Object> params = new ArrayList<Object>();
        sql = "insert into t_idl_tag(tag_name, memo, second_level_service_name,first_level_service_name) values(?,?,?,?)";
        params.add(request.getTag_name());
        if (request.getMemo() == null) {
            params.add("");
        } else {
            params.add(request.getMemo());
        }
        params.add(request.getSecond_level_service_name());
        params.add(request.getFirst_level_service_name());
        int addNum = util.updateByPreparedStatement(sql, params);
        if (addNum > 0) {
            response.setAddNumber(addNum);
            response.setMessage("success");
            response.setStatus(0);
            return response;
        } else {
            response.setAddNumber(addNum);
            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;
    } finally {
        util.releaseConn();
    }
}
Also used : DBUtil(ngse.org.DBUtil) SQLException(java.sql.SQLException) AddIDLTagResponse(beans.response.AddIDLTagResponse) ArrayList(java.util.ArrayList)

Aggregations

DBUtil (ngse.org.DBUtil)51 ArrayList (java.util.ArrayList)36 SQLException (java.sql.SQLException)23 Logger (org.apache.log4j.Logger)20 IPPortPair (beans.request.IPPortPair)12 OddSecondLevelServiceIPInfo (beans.dbaccess.OddSecondLevelServiceIPInfo)7 AccessZooKeeper (ngse.org.AccessZooKeeper)6 JsonRPCResponseBase (ngse.org.JsonRPCResponseBase)6 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)5 SecondLevelService (beans.dbaccess.SecondLevelService)4 OddSecondLevelService (beans.dbaccess.OddSecondLevelService)2 ReleasePlan (beans.request.ReleasePlan)2 IOException (java.io.IOException)2 HttpSession (javax.servlet.http.HttpSession)2 CapacityBaseInfo (beans.dbaccess.CapacityBaseInfo)1 CapacityDetailInfo (beans.dbaccess.CapacityDetailInfo)1 IDL (beans.dbaccess.IDL)1 LibraryFile (beans.dbaccess.LibraryFile)1 MachineInfo (beans.dbaccess.MachineInfo)1 OddFirstLevelService (beans.dbaccess.OddFirstLevelService)1