use of msec.org.DBUtil in project MSEC by Tencent.
the class JedisHelper method deleteInfo.
private void deleteInfo(String set_id) {
changed = true;
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 set_id=?";
List<Object> params = new ArrayList<Object>();
params.add(first_level_service_name);
params.add(second_level_service_name);
params.add(set_id);
int updNum = util.updateByPreparedStatement(sql, params);
if (updNum < 0) {
logger.error(String.format("delete_set ERROR|%s", set_id));
return;
}
for (Iterator<Map.Entry<String, ServerInfo>> it = host_map.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, ServerInfo> entry = it.next();
if (entry.getValue().getSet_id().equals(set_id)) {
it.remove();
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
return;
} finally {
util.releaseConn();
}
}
use of msec.org.DBUtil in project MSEC by Tencent.
the class RecoverPlan method exec.
public RecoverPlanResponse exec(RecoverPlanRequest request) {
Logger logger = Logger.getLogger(RecoverPlan.class);
RecoverPlanResponse resp = new RecoverPlanResponse();
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 server = null;
String plan_id = "";
try {
String[] ip_pair = request.getOld_host().split(":");
if (ip_pair.length != 2) {
resp.setStatus(101);
resp.setMessage("Request host error!");
return resp;
}
ServerInfo old_server;
//get plan_id;
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());
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;
}
sql = "select ip, port, set_id, group_id, memory, master from t_service_info where first_level_service_name=? and second_level_service_name=? and ip=? and port=?";
params.add(ip_pair[0]);
params.add(Integer.parseInt(ip_pair[1]));
old_server = util.findSimpleRefResult(sql, params, ServerInfo.class);
if (old_server == null) {
resp.setStatus(101);
resp.setMessage("Recover plan error!");
return resp;
}
sql = "select distinct set_id from t_service_info where first_level_service_name=? and second_level_service_name=? and ip=?";
params.clear();
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
params.add(request.getNew_ip());
ArrayList<Map<String, Object>> set_infos = util.findModeResult(sql, params);
if (set_infos.size() > 1 || (set_infos.size() == 1 && !set_infos.get(0).get("set_id").toString().equals(old_server.getSet_id()))) {
resp.setStatus(101);
resp.setMessage("Request IP already exists in other set!");
return resp;
}
plan_id = AddSecondLevelServiceIPInfo.newPlanID();
//use the same settings except ip
server = old_server;
server.setFirst_level_service_name(request.getFirst_level_service_name());
server.setSecond_level_service_name(request.getSecond_level_service_name());
server.setIp(request.getNew_ip());
server.setStatus("Planning");
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, recover_host) 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("rec");
params.add(request.getOld_host());
int addNum = util.updateByPreparedStatement(sql, params);
if (addNum < 0) {
resp.setMessage("Failed to insert plan.");
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(plan_id);
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
addNum = util.updateByPreparedStatement(sql, params);
if (addNum < 0) {
resp.setMessage("Failed to insert plan.");
resp.setStatus(100);
return resp;
}
InstallPlan.IPInfo info = new InstallPlan().new IPInfo(server.getIp(), server.getMemory());
info.getPort_status_map().put(server.getPort(), server.getStatus());
ArrayList<InstallPlan.IPInfo> infos = new ArrayList<>();
infos.add(info);
cluster_info.setPlan_id(plan_id);
new Thread(new InstallServerProc(infos, server, cluster_info, "recover", getServlet().getServletContext())).start();
} catch (Exception e) {
resp.setStatus(100);
resp.setMessage("db query exception!");
e.printStackTrace();
return resp;
} finally {
util.releaseConn();
}
resp.setPlan_id(plan_id);
resp.setMessage("success");
resp.setStatus(0);
return resp;
}
use of msec.org.DBUtil in project MSEC by Tencent.
the class DelStaff method exec.
public DelStaffResponse exec(DelStaffRequest request) {
DelStaffResponse response = new DelStaffResponse();
response.setMessage("unkown error.");
response.setStatus(100);
String result = checkIdentity();
if (!result.equals("success")) {
response.setStatus(99);
response.setMessage(result);
return response;
}
if (request.getStaff_name() == null || request.getStaff_name().equals("")) {
response.setMessage("The name of staff to be deleted 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 = "delete from t_staff where staff_name=?";
List<Object> params = new ArrayList<Object>();
params.add(request.getStaff_name());
try {
int delNum = util.updateByPreparedStatement(sql, params);
if (delNum >= 0) {
response.setMessage("success");
response.setDeleteNumber(delNum);
response.setStatus(0);
return response;
}
} catch (SQLException e) {
response.setMessage("Delete record failed:" + e.toString());
response.setStatus(100);
e.printStackTrace();
return response;
} finally {
util.releaseConn();
}
return response;
}
use of msec.org.DBUtil in project MSEC by Tencent.
the class AddNewStaff method exec.
public AddNewStaffResponse exec(AddNewStaffRequest request) {
Logger logger = Logger.getLogger("AddNewStaff");
AddNewStaffResponse response = new AddNewStaffResponse();
response.setMessage("unkown error.");
response.setStatus(100);
//检查用户身份
String result = checkIdentity();
if (!result.equals("success")) {
response.setStatus(99);
response.setMessage(result);
return response;
}
if (request.getStaff_name() == null || request.getStaff_name().equals("") || request.getStaff_phone() == null || request.getStaff_phone().equals("") || request.getPassword() == null || request.getPassword().equals("")) {
response.setMessage("input 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;
}
try {
String salt = AddNewStaff.newSalt();
logger.info(String.format("p1:%s\nsalt:%s", request.getPassword(), salt));
String saltedPwd = AddNewStaff.geneSaltedPwd(request.getPassword(), salt);
if (saltedPwd.equals("")) {
response.setMessage("geneSaltedPwd() failed.");
response.setStatus(100);
return response;
}
logger.info(String.format("p2:%s", saltedPwd));
String sql = "insert into t_staff(staff_name, staff_phone, password, salt) values(?,?,?, ?)";
ArrayList<Object> params = new ArrayList<Object>();
params.add(request.getStaff_name());
params.add(request.getStaff_phone());
params.add(saltedPwd);
params.add(salt);
try {
int addNum = util.updateByPreparedStatement(sql, params);
if (addNum >= 0) {
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;
}
return response;
} finally {
util.releaseConn();
}
}
Aggregations