use of beans.dbaccess.ServerInfo in project MSEC by Tencent.
the class GetPlanDetail method exec.
public GetPlanDetailResponse exec(GetPlanDetailRequest request) {
GetPlanDetailResponse resp = new GetPlanDetailResponse();
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;
}
JedisHelper helper = null;
List<ServerInfo> serverList;
int copy_num = 0;
ArrayList<String> ip_port_list = new ArrayList<>();
try {
String sql;
List<Object> params = new ArrayList<Object>();
String plan_id = request.getPlan_id();
if (request.getPlan_id() == null || request.getPlan_id().isEmpty()) {
resp.setStatus(101);
resp.setMessage("parameter error!");
return resp;
} else {
sql = "select ip, port, set_id, group_id, memory, master, status from t_install_plan where plan_id=? and first_level_service_name=? and second_level_service_name=? order by set_id asc, group_id asc, master desc";
params.clear();
params.add(request.getPlan_id());
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
try {
serverList = util.findMoreRefResult(sql, params, ServerInfo.class);
} catch (Exception e) {
resp.setStatus(100);
resp.setMessage("db query exception!");
e.printStackTrace();
return resp;
}
}
if (serverList.size() == 0) {
resp.setStatus(100);
resp.setMessage("plan id error!");
return resp;
}
boolean finished = true;
for (ServerInfo server : serverList) {
if (!server.getStatus().startsWith("Done")) {
if (server.getStatus().startsWith("[ERROR]")) {
finished = true;
break;
} else {
//in progress...
finished = false;
}
}
}
resp.setFinished(finished);
resp.setServers((ArrayList<ServerInfo>) serverList);
resp.setMessage("success");
resp.setStatus(0);
return resp;
} finally {
util.releaseConn();
}
}
use of beans.dbaccess.ServerInfo in project MSEC by Tencent.
the class AddSecondLevelServiceIPInfo method exec.
public AddSecondLevelServiceIPInfoResponse exec(AddSecondLevelServiceIPInfoRequest request) {
Logger logger = Logger.getLogger("AddSecondLevelServiceIPInfo");
AddSecondLevelServiceIPInfoResponse response = new AddSecondLevelServiceIPInfoResponse();
int instance_memory = request.getInstance_type() * INSTANCE_MEMORY_STEP;
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("")) {
response.setMessage("Some request field is empty.");
response.setStatus(100);
return response;
}
ArrayList<String> ips = Tools.splitBySemicolon(request.getIp());
Set<String> set_ips = new HashSet<>(ips);
if (ips.size() < 2 || set_ips.size() != ips.size()) {
response.setMessage("IP input error!");
response.setStatus(100);
return response;
}
if (ips.size() % request.getCopy() != 0) {
response.setMessage("the number of IPs is not divisible by the number of copies!");
response.setStatus(100);
return response;
}
DBUtil util = new DBUtil();
if (util.getConnection() == null) {
response.setMessage("DB connect failed.");
response.setStatus(100);
return response;
}
try {
//check if the IP has been installed in set...
String sql = "select ip, port, group_id, memory, master, status from t_service_info where first_level_service_name=? and second_level_service_name=? and ip in (";
List<Object> params = new ArrayList<>();
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
for (int i = 0; i < ips.size(); i++) {
if (i == 0) {
params.add(ips.get(i));
sql += "?";
} else {
params.add(ips.get(i));
sql += ",?";
}
}
sql += ")";
try {
List<ServerInfo> serverList = util.findMoreRefResult(sql, params, ServerInfo.class);
if (serverList.size() > 0) {
response.setStatus(101);
response.setMessage("Input IP(s) is already installed.");
return response;
}
} catch (Exception e) {
response.setStatus(100);
response.setMessage("db query exception!");
e.printStackTrace();
return response;
}
int group_id_index = 0;
sql = "select COALESCE(max(group_id),0) as group_id from t_service_info where first_level_service_name=? and second_level_service_name=?";
params.clear();
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
try {
Map<String, Object> result_map = util.findSimpleResult(sql, params);
if (result_map.size() != 1) {
response.setStatus(101);
response.setMessage("db set error!");
return response;
}
group_id_index = ((Long) result_map.get("group_id")).intValue();
} catch (Exception e) {
response.setStatus(100);
response.setMessage("db query exception!");
e.printStackTrace();
return response;
}
ArrayList<AddSecondLevelServiceIPInfoResponse.IPInfo> addedIPs = new ArrayList<>();
for (String ip : ips) {
AddSecondLevelServiceIPInfoResponse.IPInfo info = response.new IPInfo();
info.setIp(ip);
RemoteShell remoteShell = new RemoteShell();
StringBuffer output = new StringBuffer();
result = remoteShell.SendCmdsToRunAndGetResultBack(ipinfo_cmd, ip, output);
if (result == null || !result.equals("success")) {
logger.info(String.format("remote error:%s|%s", ip, result));
info.setStatus_message("shell");
} else {
logger.info(output);
String[] ipinfo_results = output.toString().split(" ");
if (ipinfo_results.length != 4) {
info.setStatus_message("shell");
} else {
//core mhz mem cpu
info.setCpu_cores(Integer.parseInt(ipinfo_results[0]));
info.setCpu_mhz(Integer.parseInt(ipinfo_results[1]));
info.setMemory_avail(Integer.parseInt(ipinfo_results[2]) / 1024);
info.setCpu_load(Integer.parseInt(ipinfo_results[3]));
}
}
addedIPs.add(info);
}
response.setAdded_ips(addedIPs, instance_memory);
ArrayList<String> set_ids = new ArrayList<>();
//get set ids
int set_num = ips.size() / request.getCopy();
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 ?";
params.clear();
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
params.add(set_num);
try {
ArrayList<Map<String, Object>> result_list = util.findModeResult(sql, params);
if (result_list.size() != set_num) {
response.setStatus(101);
response.setMessage("db set error!");
return response;
}
for (int i = 0; i < result_list.size(); i++) set_ids.add(result_list.get(i).get("name").toString());
} catch (SQLException e) {
response.setStatus(100);
response.setMessage("db query exception!");
e.printStackTrace();
return response;
}
CalcAddedIPForServers(response, request.getCopy(), set_ids, group_id_index, instance_memory);
if (response.getMessage().equals("success") && group_id_index == 0) {
//update t_second_level_service
logger.info(String.format("Create|Update copy_num & mem_per_instance|%d|%d", group_id_index, instance_memory));
sql = "update t_second_level_service set copy_num=?, memory_per_instance=? where first_level_service_name=? and second_level_service_name=?";
params.clear();
params.add(request.getCopy());
params.add(instance_memory);
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
try {
int addNum = util.updateByPreparedStatement(sql, params);
if (addNum < 0) {
response.setMessage("failed to update table");
response.setStatus(100);
return response;
}
} catch (SQLException e) {
response.setMessage("update status failed:" + e.toString());
response.setStatus(100);
e.printStackTrace();
return response;
}
} else
logger.info(group_id_index);
return response;
} finally {
util.releaseConn();
}
}
use of beans.dbaccess.ServerInfo in project MSEC by Tencent.
the class QuerySecondLevelServiceDetail method exec.
public QuerySecondLevelServiceDetailResponse exec(QuerySecondLevelServiceDetailRequest request) {
Logger logger = Logger.getLogger(QuerySecondLevelServiceDetail.class);
QuerySecondLevelServiceDetailResponse resp = new QuerySecondLevelServiceDetailResponse();
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<ServerInfo> serverList;
ClusterInfo cluster_info;
try {
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());
cluster_info = util.findSimpleRefResult(sql, params, ClusterInfo.class);
if (!cluster_info.getPlan_id().isEmpty()) {
ArrayList<ServerInfo> planList;
sql = "select ip, port, set_id, group_id, memory, master, status, operation, recover_host from t_install_plan where plan_id = ? order by group_id asc, master desc";
params.clear();
params.add(cluster_info.getPlan_id());
planList = util.findMoreRefResult(sql, params, ServerInfo.class);
String operation = "";
//0:"ok", 1: "in progress" or 2: "error"
int server_status = 0;
for (ServerInfo server : planList) {
operation = server.getOperation();
int status = 0;
if (!server.getStatus().startsWith("Done")) {
if (server.getStatus().startsWith("[ERROR]")) {
status = 2;
} else
status = 1;
}
server_status = Math.max(status, server_status);
}
if (server_status != 1) {
//not in progress
if (server_status == 0) {
//ok, remove service_info first...TODO, needs to judge automatically
if (operation.equals("add")) {
for (ServerInfo server : planList) {
sql = "insert into t_service_info(first_level_service_name, second_level_service_name, ip, port, set_id, group_id, memory, master) values(?,?,?,?,?,?,?,?)";
params.clear();
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());
try {
int addNum = util.updateByPreparedStatement(sql, params);
if (addNum < 0) {
resp.setMessage("failed to insert table");
resp.setStatus(100);
return resp;
}
} catch (SQLException e) {
resp.setMessage("add record failed:" + e.toString());
resp.setStatus(100);
e.printStackTrace();
return resp;
}
}
} else if (operation.equals("del")) {
sql = "delete from t_service_info where first_level_service_name=? and second_level_service_name=? and set_id=?";
params.clear();
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
params.add(planList.get(0).getSet_id());
int delNum = util.updateByPreparedStatement(sql, params);
if (delNum < 0) {
resp.setMessage("failed to delete table");
resp.setStatus(100);
return resp;
}
} else if (operation.equals("rec")) {
String[] ip_pair = planList.get(0).getRecover_host().split(":");
sql = "update t_service_info set ip=?, port=? where first_level_service_name=? and second_level_service_name=? and ip=? and port=?";
params.clear();
params.add(planList.get(0).getIp());
params.add(planList.get(0).getPort());
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
params.add(ip_pair[0]);
params.add(Integer.parseInt(ip_pair[1]));
int addNum = util.updateByPreparedStatement(sql, params);
if (addNum < 0) {
resp.setMessage("failed to update table");
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("");
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
int addNum = util.updateByPreparedStatement(sql, params);
if (addNum < 0) {
resp.setMessage("failed to update table");
resp.setStatus(100);
return resp;
}
cluster_info.setPlan_id("");
}
}
sql = "select ip, port, set_id, group_id, memory, master from t_service_info where first_level_service_name=? and second_level_service_name=? order by group_id asc, master desc";
params.clear();
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
serverList = util.findMoreRefResult(sql, params, ServerInfo.class);
if (serverList.size() > 0 && cluster_info.getPlan_id().isEmpty()) {
//working plan will cause config inconsistent...
HashMap<String, ServerInfo> server_map = new HashMap<>();
for (ServerInfo server : serverList) {
server_map.put(server.getIp() + ":" + server.getPort(), server);
}
JedisHelper helper = new JedisHelper(server_map, request.getFirst_level_service_name(), request.getSecond_level_service_name(), cluster_info);
helper.CheckStatusDetail();
if (!helper.isOK()) {
resp.setStatus(101);
resp.setMessage(helper.getError_message());
return resp;
}
if (helper.isChanged()) {
serverList = util.findMoreRefResult(sql, params, ServerInfo.class);
}
}
} catch (Exception e) {
resp.setStatus(100);
resp.setMessage("db query exception!");
e.printStackTrace();
return resp;
} finally {
util.releaseConn();
}
resp.setServers((ArrayList<ServerInfo>) serverList);
resp.setCluster_info(cluster_info);
resp.setMessage("success");
resp.setStatus(0);
return resp;
}
use of beans.dbaccess.ServerInfo in project MSEC by Tencent.
the class RemovePlan method exec.
public RemovePlanResponse exec(RemovePlanRequest request) {
RemovePlanResponse resp = new RemovePlanResponse();
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;
}
JedisHelper helper = null;
List<ServerInfo> serverList;
ServerInfo ok_server;
try {
String sql;
List<Object> params = new ArrayList<Object>();
//get plan_id;
sql = "select copy_num, memory_per_instance, plan_id from t_second_level_service where first_level_service_name=? and second_level_service_name=?";
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
try {
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;
}
} catch (Exception e) {
resp.setStatus(100);
resp.setMessage("db query exception!");
e.printStackTrace();
return resp;
}
sql = "select ip, port, set_id, group_id, memory, master, status from t_service_info where first_level_service_name=? and second_level_service_name=? and master=1 and status='OK' limit 1";
try {
ok_server = util.findSimpleRefResult(sql, params, ServerInfo.class);
} catch (Exception e) {
resp.setStatus(100);
resp.setMessage("OK server not found!");
e.printStackTrace();
return resp;
}
sql = "select ip, port, set_id, group_id, memory, master, 'Planning' as status from t_service_info where first_level_service_name=? and second_level_service_name=? and set_id=? order by group_id asc, master desc";
params.add(request.getSet_id());
try {
serverList = util.findMoreRefResult(sql, params, ServerInfo.class);
} catch (Exception e) {
resp.setStatus(100);
resp.setMessage("db query exception!");
e.printStackTrace();
return resp;
}
if (serverList == null || serverList.size() == 0) {
resp.setStatus(100);
resp.setMessage("set id error!");
return resp;
}
String plan_id = AddSecondLevelServiceIPInfo.newPlanID();
for (ServerInfo server : serverList) {
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) 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("del");
try {
int addNum = util.updateByPreparedStatement(sql, params);
if (addNum < 0) {
resp.setMessage("failed to insert table");
resp.setStatus(100);
return resp;
}
} catch (SQLException e) {
resp.setMessage("add record failed:" + e.toString());
resp.setStatus(100);
e.printStackTrace();
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());
try {
int addNum = util.updateByPreparedStatement(sql, params);
if (addNum < 0) {
resp.setMessage("Failed to insert plan.");
resp.setStatus(100);
return resp;
}
} catch (SQLException e) {
resp.setStatus(100);
resp.setMessage("db query exception!");
e.printStackTrace();
return resp;
}
int copy_num = 0;
ArrayList<String> ip_port_list = new ArrayList<>();
for (ServerInfo server : serverList) {
if (server.getSet_id().equals(serverList.get(0).getSet_id()) && server.getGroup_id() == serverList.get(0).getGroup_id())
copy_num++;
if (server.isMaster())
ip_port_list.add(server.getIp() + ":" + Integer.toString(server.getPort()));
}
try {
helper = new JedisHelper(plan_id, ok_server.getIp(), ok_server.getPort(), copy_num);
new Thread(new ClusterProc(helper, ip_port_list, "remove", getServlet().getServletContext())).start();
} catch (Exception e) {
resp.setStatus(101);
resp.setMessage("Redis cluster exception!");
e.printStackTrace();
return resp;
}
resp.setMessage("success");
resp.setPlan_id(plan_id);
resp.setStatus(0);
return resp;
} finally {
util.releaseConn();
if (helper != null)
helper.close();
}
}
use of beans.dbaccess.ServerInfo in project MSEC by Tencent.
the class InstallPlan method exec.
public InstallPlanResponse exec(InstallPlanRequest request) {
Logger logger = Logger.getLogger(InstallPlan.class);
InstallPlanResponse resp = new InstallPlanResponse();
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 helper_server = null;
try {
if (request.getAdded_servers() == null || request.getAdded_servers().size() == 0) {
resp.setStatus(101);
resp.setMessage("Request field error.");
return resp;
}
Collections.sort(request.getAdded_servers(), compByGroupId());
//get cluster info;
ClusterInfo cluster_info = null;
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());
try {
cluster_info = util.findSimpleRefResult(sql, params, ClusterInfo.class);
if (cluster_info == null || !cluster_info.getPlan_id().isEmpty()) {
resp.setStatus(101);
resp.setMessage("Please wait until the ongoing plan finishes.");
return resp;
}
} catch (Exception e) {
resp.setStatus(100);
resp.setMessage("db query exception!");
e.printStackTrace();
return resp;
}
List<ServerInfo> serviceList;
ArrayList<String> ip_port_list = new ArrayList<>();
HashMap<String, IPInfo> ip_map = new HashMap<>();
//get current redis cluster ips
sql = "select ip, port, status from t_service_info where first_level_service_name=? and second_level_service_name=? order by group_id asc, master desc limit 1";
params.clear();
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
try {
serviceList = util.findMoreRefResult(sql, params, ServerInfo.class);
} catch (Exception e) {
resp.setStatus(100);
resp.setMessage("db query exception!");
e.printStackTrace();
return resp;
}
if (serviceList.size() > 0) {
for (ServerInfo info : serviceList) {
if (info.getStatus().equals("OK")) {
helper_server = info;
break;
}
}
if (helper_server == null) {
//没有可用机器
resp.setStatus(101);
resp.setMessage("Cluster state error!");
return resp;
}
}
String plan_id = AddSecondLevelServiceIPInfo.newPlanID();
for (ServerInfo server : request.getAdded_servers()) {
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) 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("Planning");
params.add("add");
try {
int addNum = util.updateByPreparedStatement(sql, params);
if (addNum < 0) {
resp.setMessage("Failed to insert plan.");
resp.setStatus(100);
return resp;
}
} catch (SQLException e) {
resp.setMessage("add record failed:" + e.toString());
resp.setStatus(100);
e.printStackTrace();
return resp;
}
ip_port_list.add(server.getIp() + ":" + Integer.toString(server.getPort()));
if (ip_map.containsKey(server.getIp())) {
ip_map.get(server.getIp()).getPort_status_map().put(server.getPort(), server.getStatus());
} else {
IPInfo ipinfo = new IPInfo(server.getIp(), server.getMemory());
ipinfo.getPort_status_map().put(server.getPort(), server.getStatus());
ip_map.put(server.getIp(), ipinfo);
}
}
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());
try {
int addNum = util.updateByPreparedStatement(sql, params);
if (addNum < 0) {
resp.setMessage("Failed to insert plan.");
resp.setStatus(100);
return resp;
}
} catch (SQLException e) {
resp.setStatus(100);
resp.setMessage("db query exception!");
e.printStackTrace();
return resp;
}
cluster_info.setPlan_id(plan_id);
cluster_info.setIp_port_list(ip_port_list);
logger.info(ip_port_list);
new Thread(new InstallServerProc(new ArrayList<IPInfo>(ip_map.values()), helper_server, cluster_info, "create", getServlet().getServletContext())).start();
/*
try {
JedisHelper helper = null;
if (serviceList.size() > 0) {
helper = new JedisHelper(request.getPlan_id(), serviceList.get(0).getIp(), serviceList.get(0).getPort(), copy_num);
new Thread(new ClusterProc(helper, ip_port_list, "add", getServlet().getServletContext())).start();
} else {//CreateSet
helper = new JedisHelper(request.getPlan_id(), serverList.get(0).getIp(), serverList.get(0).getPort(), copy_num);
new Thread(new ClusterProc(helper, ip_port_list, "create", getServlet().getServletContext())).start();
}
} catch (Exception e) {
resp.setStatus(101);
resp.setMessage("Redis cluster exception!");
e.printStackTrace();
return resp;
}
*/
resp.setPlan_id(plan_id);
resp.setMessage("success");
resp.setStatus(0);
return resp;
} finally {
util.releaseConn();
}
}
Aggregations