use of beans.request.IPPortPair in project MSEC by Tencent.
the class QueryCapacityDetail method exec.
public QueryCapacityDetailResponse exec(CapacityRequest request) {
Logger logger = Logger.getLogger("QueryCapacityDetail");
QueryCapacityDetailResponse resp = new QueryCapacityDetailResponse();
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;
}
ArrayList<CapacityDetailInfo> capacities = new ArrayList<CapacityDetailInfo>();
AccessZooKeeper azk = new AccessZooKeeper();
try {
//该服务的所有ip
ArrayList<IPPortPair> iplist = LoadBalance.getIPPortInfoByServiceName(request.getFirst_level_service_name(), request.getSecond_level_service_name(), util);
if (iplist == null) {
resp.setMessage("failed to get ip list for " + request.getFirst_level_service_name() + "." + request.getSecond_level_service_name());
resp.setStatus(100);
return resp;
}
//获取每一个ip的负载信息
int j;
for (j = 0; j < iplist.size(); j++) {
IPPortPair ip = iplist.get(j);
String svcname = request.getFirst_level_service_name() + "/" + request.getSecond_level_service_name();
try {
int load = LoadBalance.readOneServiceLoadInfo(azk, svcname, ip.getIp());
if (load >= 0) {
CapacityDetailInfo c = new CapacityDetailInfo();
c.setIp(ip.getIp());
c.setLoad_level(load);
capacities.add(c);
}
} catch (Exception e) {
e.printStackTrace();
resp.setMessage(e.getMessage());
resp.setStatus(100);
return resp;
}
}
} finally {
util.releaseConn();
azk.disconnect();
}
resp.setInfo(capacities);
resp.setMessage("success");
resp.setStatus(0);
return resp;
}
use of beans.request.IPPortPair in project MSEC by Tencent.
the class QueryCapacityList method exec.
public QueryCapacityListResponse exec(CapacityRequest request) {
Logger logger = Logger.getLogger("QueryCapacityList");
QueryCapacityListResponse resp = new QueryCapacityListResponse();
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<CapacityBaseInfo> capacities = new ArrayList<CapacityBaseInfo>();
AccessZooKeeper azk = new AccessZooKeeper();
try {
if (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) {
//所有服务
ArrayList<SecondLevelService> svcList = LoadBalance.getAllService(util);
if (svcList == null) {
resp.setMessage("failed to get all service list");
resp.setStatus(100);
return resp;
}
for (int i = 0; i < svcList.size(); i++) {
//每一个服务
SecondLevelService svc = svcList.get(i);
if (!svc.getType().equals("standard")) {
continue;
}
//用户可能指定了一级服务名进行过滤,即只查看该一级服务下所有二级服务的情况
if (request.getFirst_level_service_name() != null && request.getFirst_level_service_name().length() > 0) {
if (!svc.getFirst_level_service_name().equals(request.getFirst_level_service_name())) {
continue;
}
}
CapacityBaseInfo c = new CapacityBaseInfo();
//该服务的所有ip
ArrayList<IPPortPair> iplist = LoadBalance.getIPPortInfoByServiceName(svc.getFirst_level_service_name(), svc.getSecond_level_service_name(), util);
if (iplist == null) {
resp.setMessage("failed to get ip list for " + svc.getFirst_level_service_name() + "." + svc.getSecond_level_service_name());
resp.setStatus(100);
return resp;
}
c.setIp_count(iplist.size());
c.setFirst_level_service_name(svc.getFirst_level_service_name());
c.setSecond_level_service_name(svc.getSecond_level_service_name());
//获取每一个ip的负载信息
int sum = 0;
int count = 0;
int j;
for (j = 0; j < iplist.size(); j++) {
IPPortPair ip = iplist.get(j);
String svcname = svc.getFirst_level_service_name() + "/" + svc.getSecond_level_service_name();
try {
int load = LoadBalance.readOneServiceLoadInfo(azk, svcname, ip.getIp());
if (load >= 0) {
sum += load;
count++;
}
} catch (Exception e) {
e.printStackTrace();
resp.setMessage(e.getMessage());
resp.setStatus(100);
return resp;
}
}
c.setIp_report_num(count);
if (count > 0) {
c.setLoad_level(sum / count);
} else {
c.setLoad_level(0);
}
capacities.add(c);
}
} else {
CapacityBaseInfo c = new CapacityBaseInfo();
//该服务的所有ip
ArrayList<IPPortPair> iplist = LoadBalance.getIPPortInfoByServiceName(request.getFirst_level_service_name(), request.getSecond_level_service_name(), util);
if (iplist == null) {
resp.setMessage("failed to get ip list for " + request.getFirst_level_service_name() + "." + request.getSecond_level_service_name());
resp.setStatus(100);
return resp;
}
c.setIp_count(iplist.size());
c.setFirst_level_service_name(request.getFirst_level_service_name());
c.setSecond_level_service_name(request.getSecond_level_service_name());
//获取每一个ip的负载信息
int sum = 0;
int count = 0;
int j;
for (j = 0; j < iplist.size(); j++) {
IPPortPair ip = iplist.get(j);
String svcname = request.getFirst_level_service_name() + "/" + request.getSecond_level_service_name();
try {
int load = LoadBalance.readOneServiceLoadInfo(azk, svcname, ip.getIp());
if (load > 0) {
sum += load;
count++;
}
} catch (Exception e) {
e.printStackTrace();
resp.setMessage(e.getMessage());
resp.setStatus(100);
return resp;
}
}
c.setIp_report_num(count);
if (count > 0) {
c.setLoad_level(sum / count);
} else {
c.setLoad_level(0);
}
capacities.add(c);
}
} finally {
util.releaseConn();
azk.disconnect();
}
resp.setCapacity_list((ArrayList<CapacityBaseInfo>) capacities);
resp.setMessage("success");
resp.setStatus(0);
return resp;
}
use of beans.request.IPPortPair in project MSEC by Tencent.
the class QueryProcessInfo method exec.
public JsonRPCResponseBase exec(SecondLevelService request) {
Logger logger = Logger.getLogger(QueryProcessInfo.class);
JsonRPCResponseBase resp = new JsonRPCResponseBase();
flsn = request.getFirst_level_service_name();
slsn = request.getSecond_level_service_name();
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;
}
FileInputStream in = null;
try {
String sql = "select ip from t_second_level_service_ipinfo where first_level_service_name=? " + "and second_level_service_name=? and status='enabled' ";
ArrayList<Object> params = new ArrayList<Object>();
params.add(flsn);
params.add(slsn);
ArrayList<IPPortPair> iplist = util.findMoreRefResult(sql, params, IPPortPair.class);
String cmdFileName = geneCmdFile();
getHttpResponse().setContentType("text/html");
getHttpResponse().setCharacterEncoding("UTF-8");
ServletOutputStream out = getHttpResponse().getOutputStream();
out.println("<html>");
out.println("<head>");
out.println("<title>release result</title>");
out.println("</head>");
out.println("<body>");
//这个标签用于原样输出
out.println("<pre>");
StringBuffer outFile = new StringBuffer();
String localResultFileName = getLocalResultFileName();
new File(localResultFileName).delete();
byte[] buffer = new byte[1240];
for (int i = 0; i < iplist.size(); i++) {
String ip = iplist.get(i).getIp();
out.println(String.format("------------------IP:%s---------------", ip));
RemoteShell rs = new RemoteShell();
outFile.delete(0, outFile.length());
result = rs.SendCmdsToAgentAndRun(cmdFileName, ip, outFile);
if (result == null || !result.equals("success")) {
out.println(String.format("sending cmd file to %s failed:%s", ip, result));
continue;
}
result = rs.GetFileFromAgent(localResultFileName, outFile.toString(), ip);
if (result == null || !result.equals("success")) {
out.println(String.format("get cmd result from %s failed:%s", ip, result));
continue;
}
in = new FileInputStream(localResultFileName);
while (true) {
int len = in.read(buffer);
if (len <= 0) {
break;
}
out.print(new String(buffer, 0, len));
}
in.close();
new File(localResultFileName).delete();
}
out.println("</pre>");
out.println("</body>");
out.println("</html>");
try {
out.close();
} catch (Exception e) {
}
return null;
} catch (Exception e) {
resp.setStatus(100);
resp.setMessage(e.getMessage());
e.printStackTrace();
return resp;
} finally {
util.releaseConn();
if (in != null) {
try {
in.close();
} catch (Exception e) {
}
}
}
}
use of beans.request.IPPortPair in project MSEC by Tencent.
the class RollbackReleasePlan method exec.
public JsonRPCResponseBase exec(ReleasePlan request) {
Logger logger = Logger.getLogger(RollbackReleasePlan.class);
JsonRPCResponseBase resp = new JsonRPCResponseBase();
plan_id = request.getPlan_id();
flsn = request.getFirst_level_service_name();
slsn = request.getSecond_level_service_name();
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;
}
try {
String sql = "select dest_ip_list from t_release_plan where plan_id=?";
ArrayList<Object> 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();
IPPortPair[] ips = objectMapper.readValue(dest_ip_list, IPPortPair[].class);
logger.info("destination IP number;" + ips.length);
//对每个IP执行回滚
getHttpResponse().setContentType("text/html");
getHttpResponse().setCharacterEncoding("UTF-8");
ServletOutputStream out = getHttpResponse().getOutputStream();
safeWrite("<html>", out);
safeWrite("<head>", out);
safeWrite("<title>release result</title>", out);
safeWrite("</head>", out);
safeWrite("<body>", out);
//这个标签用于原样输出
safeWrite("<pre>", out);
updatePlanStatus(util, "rolling back");
doRollback(ips, out, util);
safeWrite("</pre>", out);
safeWrite("</body>", out);
safeWrite("</html>", out);
//update plan status
updatePlanStatus(util, "roll back successfully");
try {
out.close();
} catch (Exception e) {
}
return null;
} catch (Exception e) {
resp.setStatus(100);
resp.setMessage(e.getMessage());
e.printStackTrace();
try {
updatePlanStatus(util, "failed to roll back");
} catch (Exception e1) {
}
return resp;
} finally {
util.releaseConn();
}
}
use of beans.request.IPPortPair in project MSEC by Tencent.
the class RefreshCapacity 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(RefreshCapacity.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(), 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 < ips.size(); i++) {
whiteNameList.add(ips.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();
}
}
Aggregations