|
|
@ -27,6 +27,7 @@ import io.entgra.server.bootup.heartbeat.beacon.exception.HeartBeatManagementExc
|
|
|
|
import io.entgra.server.bootup.heartbeat.beacon.dto.ServerContext;
|
|
|
|
import io.entgra.server.bootup.heartbeat.beacon.dto.ServerContext;
|
|
|
|
import io.entgra.server.bootup.heartbeat.beacon.internal.HeartBeatBeaconDataHolder;
|
|
|
|
import io.entgra.server.bootup.heartbeat.beacon.internal.HeartBeatBeaconDataHolder;
|
|
|
|
import org.wso2.carbon.device.mgt.common.ServerCtxInfo;
|
|
|
|
import org.wso2.carbon.device.mgt.common.ServerCtxInfo;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
|
|
|
|
|
|
|
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
@ -39,30 +40,35 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
|
|
|
|
int hashIndex = -1;
|
|
|
|
int hashIndex = -1;
|
|
|
|
ServerContext localServerCtx = null;
|
|
|
|
ServerContext localServerCtx = null;
|
|
|
|
ServerCtxInfo serverCtxInfo = null;
|
|
|
|
ServerCtxInfo serverCtxInfo = null;
|
|
|
|
try {
|
|
|
|
if(HeartBeatBeaconConfig.getInstance().isEnabled()) {
|
|
|
|
HeartBeatBeaconDAOFactory.openConnection();
|
|
|
|
try {
|
|
|
|
heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO();
|
|
|
|
HeartBeatBeaconDAOFactory.openConnection();
|
|
|
|
|
|
|
|
heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO();
|
|
|
|
|
|
|
|
|
|
|
|
int timeOutIntervalInSeconds = HeartBeatBeaconConfig.getInstance().getServerTimeOutIntervalInSeconds();
|
|
|
|
int timeOutIntervalInSeconds = HeartBeatBeaconConfig.getInstance().getServerTimeOutIntervalInSeconds();
|
|
|
|
int timeSkew = HeartBeatBeaconConfig.getInstance().getTimeSkew();
|
|
|
|
int timeSkew = HeartBeatBeaconConfig.getInstance().getTimeSkew();
|
|
|
|
int cumilativeTimeOut = timeOutIntervalInSeconds + timeSkew;
|
|
|
|
int cumilativeTimeOut = timeOutIntervalInSeconds + timeSkew;
|
|
|
|
String localServerUUID = HeartBeatBeaconDataHolder.getInstance().getLocalServerUUID();
|
|
|
|
String localServerUUID = HeartBeatBeaconDataHolder.getInstance().getLocalServerUUID();
|
|
|
|
Map<String, ServerContext> serverCtxMap = heartBeatDAO.getActiveServerDetails(cumilativeTimeOut);
|
|
|
|
Map<String, ServerContext> serverCtxMap = heartBeatDAO.getActiveServerDetails(cumilativeTimeOut);
|
|
|
|
if(!serverCtxMap.isEmpty()) {
|
|
|
|
if (!serverCtxMap.isEmpty()) {
|
|
|
|
localServerCtx = serverCtxMap.get(localServerUUID);
|
|
|
|
localServerCtx = serverCtxMap.get(localServerUUID);
|
|
|
|
if (localServerCtx != null) {
|
|
|
|
if (localServerCtx != null) {
|
|
|
|
hashIndex = localServerCtx.getIndex();
|
|
|
|
hashIndex = localServerCtx.getIndex();
|
|
|
|
serverCtxInfo = new ServerCtxInfo(serverCtxMap.size(), hashIndex);
|
|
|
|
serverCtxInfo = new ServerCtxInfo(serverCtxMap.size(), hashIndex);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
String msg = "Error occurred while opening a connection to the underlying data source";
|
|
|
|
|
|
|
|
throw new HeartBeatManagementException(msg, e);
|
|
|
|
|
|
|
|
} catch (HeartBeatDAOException e) {
|
|
|
|
|
|
|
|
String msg = "Error occurred while retrieving active server count.";
|
|
|
|
|
|
|
|
throw new HeartBeatManagementException(msg, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
HeartBeatBeaconDAOFactory.closeConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
} else {
|
|
|
|
String msg = "Error occurred while opening a connection to the underlying data source";
|
|
|
|
String msg = "Heart Beat Configuration Disabled. Server Context Information Not available.";
|
|
|
|
throw new HeartBeatManagementException(msg, e);
|
|
|
|
throw new HeartBeatManagementException(msg);
|
|
|
|
} catch (HeartBeatDAOException e) {
|
|
|
|
|
|
|
|
String msg = "Error occurred while retrieving active server count.";
|
|
|
|
|
|
|
|
throw new HeartBeatManagementException(msg, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
HeartBeatBeaconDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return serverCtxInfo;
|
|
|
|
return serverCtxInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -71,22 +77,29 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
|
|
|
|
public String updateServerContext(ServerContext ctx) throws HeartBeatManagementException {
|
|
|
|
public String updateServerContext(ServerContext ctx) throws HeartBeatManagementException {
|
|
|
|
HeartBeatDAO heartBeatDAO;
|
|
|
|
HeartBeatDAO heartBeatDAO;
|
|
|
|
String uuid = null;
|
|
|
|
String uuid = null;
|
|
|
|
try {
|
|
|
|
if(HeartBeatBeaconConfig.getInstance().isEnabled()) {
|
|
|
|
HeartBeatBeaconDAOFactory.openConnection();
|
|
|
|
try {
|
|
|
|
heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO();
|
|
|
|
HeartBeatBeaconDAOFactory.beginTransaction();
|
|
|
|
|
|
|
|
heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO();
|
|
|
|
|
|
|
|
|
|
|
|
uuid = heartBeatDAO.retrieveExistingServerCtx(ctx);
|
|
|
|
uuid = heartBeatDAO.retrieveExistingServerCtx(ctx);
|
|
|
|
if(uuid == null){
|
|
|
|
if (uuid == null) {
|
|
|
|
uuid = heartBeatDAO.recordServerCtx(ctx);
|
|
|
|
uuid = heartBeatDAO.recordServerCtx(ctx);
|
|
|
|
|
|
|
|
HeartBeatBeaconDAOFactory.commitTransaction();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (HeartBeatDAOException e) {
|
|
|
|
|
|
|
|
String msg = "Error Occured while retrieving server context.";
|
|
|
|
|
|
|
|
throw new HeartBeatManagementException(msg, e);
|
|
|
|
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
|
|
|
|
HeartBeatBeaconDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
String msg = "Error occurred while updating server context. Issue in opening a connection to the underlying data source";
|
|
|
|
|
|
|
|
throw new HeartBeatManagementException(msg, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
HeartBeatBeaconDAOFactory.closeConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
} else {
|
|
|
|
String msg = "Error occurred while opening a connection to the underlying data source";
|
|
|
|
String msg = "Heart Beat Configuration Disabled. Updating Server Context Failed.";
|
|
|
|
throw new HeartBeatManagementException(msg, e);
|
|
|
|
throw new HeartBeatManagementException(msg);
|
|
|
|
} catch (HeartBeatDAOException e) {
|
|
|
|
|
|
|
|
String msg = "Error Occured while retrieving active server count.";
|
|
|
|
|
|
|
|
throw new HeartBeatManagementException(msg, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
HeartBeatBeaconDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return uuid;
|
|
|
|
return uuid;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -96,18 +109,26 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
|
|
|
|
public boolean recordHeartBeat(HeartBeatEvent event) throws HeartBeatManagementException {
|
|
|
|
public boolean recordHeartBeat(HeartBeatEvent event) throws HeartBeatManagementException {
|
|
|
|
HeartBeatDAO heartBeatDAO;
|
|
|
|
HeartBeatDAO heartBeatDAO;
|
|
|
|
boolean operationSuccess = false;
|
|
|
|
boolean operationSuccess = false;
|
|
|
|
try {
|
|
|
|
if (HeartBeatBeaconConfig.getInstance().isEnabled()) {
|
|
|
|
HeartBeatBeaconDAOFactory.openConnection();
|
|
|
|
try {
|
|
|
|
heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO();
|
|
|
|
HeartBeatBeaconDAOFactory.beginTransaction();
|
|
|
|
operationSuccess = heartBeatDAO.recordHeatBeat(event);
|
|
|
|
heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO();
|
|
|
|
} catch (SQLException e) {
|
|
|
|
operationSuccess = heartBeatDAO.recordHeatBeat(event);
|
|
|
|
String msg = "Error occurred while opening a connection to the underlying data source";
|
|
|
|
HeartBeatBeaconDAOFactory.commitTransaction();
|
|
|
|
throw new HeartBeatManagementException(msg, e);
|
|
|
|
} catch (HeartBeatDAOException e) {
|
|
|
|
} catch (HeartBeatDAOException e) {
|
|
|
|
String msg = "Error occurred while recording heart beat.";
|
|
|
|
String msg = "Error Occured while retrieving active server count.";
|
|
|
|
throw new HeartBeatManagementException(msg, e);
|
|
|
|
throw new HeartBeatManagementException(msg, e);
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
} finally {
|
|
|
|
HeartBeatBeaconDAOFactory.rollbackTransaction();
|
|
|
|
HeartBeatBeaconDAOFactory.closeConnection();
|
|
|
|
String msg = "Error occurred performing heart beat record transaction. " +
|
|
|
|
|
|
|
|
"Transaction rolled back.";
|
|
|
|
|
|
|
|
throw new HeartBeatManagementException(msg, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
HeartBeatBeaconDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
String msg = "Heart Beat Configuration Disabled. Recording Heart Beat Failed.";
|
|
|
|
|
|
|
|
throw new HeartBeatManagementException(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return operationSuccess;
|
|
|
|
return operationSuccess;
|
|
|
|
}
|
|
|
|
}
|
|
|
|