incoorporating review comments

revert-70ac1926
Ace 4 years ago
parent bac6e3ba67
commit 4df57ede61

@ -298,11 +298,7 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService
}
activityList.setList(activities);
activityList.setCount(count);
// if (activities == null || activities.size() == 0) {
// if (isIfModifiedSinceSet) {
// return Response.notModified().build();
// }
// }
return Response.ok().entity(activityList).build();
} catch (OperationManagementException e) {
String msg

@ -797,13 +797,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
@Override
public List<Device> getAllocatedDevices(String type, int tenantId, int activeServerCount, int serverIndex) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
public List<Device> getAllocatedDevices(String type, int tenantId, int activeServerCount,
int serverIndex) throws DeviceManagementDAOException {
List<Device> devices = null;
try {
conn = this.getConnection();
Connection conn = this.getConnection();
String sql = "SELECT d1.ID AS DEVICE_ID," +
" d1.DESCRIPTION," +
" d1.NAME AS DEVICE_NAME," +
@ -832,24 +830,31 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
" AND MOD(d1.ID, ?) = ? " +
"ORDER BY e.DATE_OF_LAST_UPDATE DESC";
stmt = conn.prepareStatement(sql);
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, type);
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, activeServerCount);
stmt.setInt(5, serverIndex);
rs = stmt.executeQuery();
devices = new ArrayList<>();
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
Device device = DeviceManagementDAOUtil.loadActiveDevice(rs, false);
if (device != null) {
devices.add(device);
}
}
} catch (Exception e) {
log.error("Error encountered while populating allocated active devices for server with index : " + serverIndex +
" active-server-count " + activeServerCount + " device-type " + type + " tenant-id " + tenantId);
throw new DeviceManagementDAOException("Error occurred while populating active devices '" + type + "'", e);
}
}
} catch (SQLException e) {
log.error("Error encountered while retrieving allocated devices for server with index : " + serverIndex +
" active-server-count " + activeServerCount + " device-type " + type + " tenant-id " + tenantId);
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return devices;
}

@ -165,6 +165,7 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
}
@Override
public List<Device> getAllocatedDevices(PaginationRequest request, int tenantId,
int activeServerCount, int serverIndex)
throws DeviceManagementDAOException {

@ -18,6 +18,8 @@
*/
package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
@ -39,6 +41,8 @@ import java.util.Map;
public class OperationMappingDAOImpl implements OperationMappingDAO {
private static final Log log = LogFactory.getLog(OperationMappingDAOImpl.class);
@Override
public void addOperationMapping(Operation operation, Integer deviceId, boolean isScheduled, Device device, Integer tenantId) throws
OperationManagementDAOException {
@ -228,11 +232,10 @@ public class OperationMappingDAOImpl implements OperationMappingDAO {
}
@Override
public List<OperationEnrolmentMapping> getFirstPendingOperationMappingsForActiveEnrolments(long minDuration,
public List<OperationEnrolmentMapping> getFirstPendingOperationMappingsForActiveEnrolments(
long minDuration,
long maxDuration, int deviceTypeId,
int activeServerCount, int serverHashIndex) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<OperationEnrolmentMapping> enrolmentOperationMappingList;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
@ -242,27 +245,27 @@ public class OperationMappingDAOImpl implements OperationMappingDAO {
"AS CREATED_TIMESTAMP, E.STATUS AS ENROLMENT_STATUS, E.TENANT_ID FROM " +
"DM_ENROLMENT_OP_MAPPING OP INNER JOIN DM_ENROLMENT E ON OP.ENROLMENT_ID = E.ID INNER JOIN " +
"DM_DEVICE D ON E.DEVICE_ID = D.ID WHERE " +
"OP.STATUS IN ('"+ Operation.Status.PENDING.name() + "','" + Operation.Status.REPEATED.name() + "') " +
"OP.STATUS IN ('" + Operation.Status.PENDING.name() + "','" + Operation.Status.REPEATED.name() + "') " +
"AND OP.CREATED_TIMESTAMP BETWEEN ? AND ? AND E.STATUS IN ('" + EnrolmentInfo.Status.ACTIVE.name() +
"','" + EnrolmentInfo.Status.UNREACHABLE.name() + "') AND D.DEVICE_TYPE_ID = ? AND MOD(D.ID, ?) = ? GROUP BY ENROLMENT_ID," +
" D.DEVICE_IDENTIFICATION, E.STATUS, E.TENANT_ID";
stmt = conn.prepareStatement(sql);
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setLong(1, maxDuration);
stmt.setLong(2, minDuration);
stmt.setInt(3, deviceTypeId);
stmt.setInt(4, activeServerCount);
stmt.setInt(5, serverHashIndex);
rs = stmt.executeQuery();
try (ResultSet rs = stmt.executeQuery()) {
enrolmentOperationMappingList = new ArrayList<>();
while (rs.next()) {
OperationEnrolmentMapping enrolmentOperationMapping = this.getEnrolmentOpMapping(rs);
enrolmentOperationMappingList.add(enrolmentOperationMapping);
}
}
}
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while fetching pending operation mappings for " +
"active devices of type '" + deviceTypeId + "'", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return enrolmentOperationMappingList;
}
@ -300,9 +303,11 @@ public class OperationMappingDAOImpl implements OperationMappingDAO {
}
@Override
public Map<Integer, Long> getLastConnectedTimeForActiveEnrolments(long timeStamp, int deviceTypeId, int activeServerCount, int serverHashIndex) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
public Map<Integer, Long> getLastConnectedTimeForActiveEnrolments(long timeStamp,
int deviceTypeId,
int activeServerCount,
int serverHashIndex)
throws OperationManagementDAOException {
Map<Integer, Long> lastConnectedTimeMap = null;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
@ -314,21 +319,23 @@ public class OperationMappingDAOImpl implements OperationMappingDAO {
"OP.STATUS = '" + Operation.Status.COMPLETED.name() + "'" +
"AND OP.UPDATED_TIMESTAMP >= ? AND E.STATUS IN ('" + EnrolmentInfo.Status.ACTIVE.name() +
"','" + EnrolmentInfo.Status.UNREACHABLE.name() + "') AND D.DEVICE_TYPE_ID = ? AND MOD(D.ID, ?) = ? GROUP BY ENROLMENT_ID";
stmt = conn.prepareStatement(sql);
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setLong(1, timeStamp);
stmt.setInt(2, deviceTypeId);
stmt.setInt(3, activeServerCount);
stmt.setInt(4, serverHashIndex);
rs = stmt.executeQuery();
try (ResultSet rs = stmt.executeQuery()) {
lastConnectedTimeMap = new HashMap<>();
while (rs.next()) {
lastConnectedTimeMap.put(rs.getInt("EID"), rs.getLong("LAST_CONNECTED_TIME"));
}
}
}
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while fetching last connected time for " +
"active devices of type '" + deviceTypeId + "'", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
String msg = "Error occurred while fetching last connected time for " +
"active devices of type '" + deviceTypeId + "'";
log.error(msg, e);
throw new OperationManagementDAOException(msg, e);
}
return lastConnectedTimeMap;
}

@ -231,7 +231,7 @@ public class OperationDAOUtil {
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(rs.getInt("OP_RES_ID"));
} else {
if(activityStatus.getResponses() == null){
if (activityStatus.getResponses() == null) {
List<OperationResponse> operationResponses = new ArrayList<>();
activityStatus.setResponses(operationResponses);
}

@ -795,7 +795,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving all devices of type '" +
deviceType + "' that are being managed within the scope of current tenant";
log.error(msg);
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source";

@ -1,3 +1,21 @@
/*
* Copyright (c) 2020, Entgra Pvt Ltd. (http://www.wso2.org) All Rights Reserved.
*
* Entgra Pvt Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core;
import io.entgra.server.bootup.heartbeat.beacon.dto.HeartBeatEvent;

@ -1,21 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~
~ WSO2 Inc. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
~ Copyright (c) 2020, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved.
~
~ Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@ -31,7 +31,7 @@
<packaging>bundle</packaging>
<name>Entgra - Heartbeat Beacon</name>
<description>Entgra - Server Startup and Heartbeat Monitoring Component</description>
<url>http://wso2.org</url>
<url>http://www.entgra.io</url>
<build>
<plugins>

@ -55,8 +55,10 @@ public class HeartBeatBeaconUtils {
DocumentBuilder docBuilder = factory.newDocumentBuilder();
return docBuilder.parse(file);
} catch (Exception e) {
throw new HeartBeatBeaconConfigurationException("Error occurred while parsing file, while converting " +
"to a org.w3c.dom.Document", e);
String msg = "Error occurred while parsing file, while converting " +
"to a org.w3c.dom.Document";
log.error(msg, e);
throw new HeartBeatBeaconConfigurationException(msg, e);
}
}
@ -76,7 +78,9 @@ public class HeartBeatBeaconUtils {
final InitialContext context = new InitialContext(jndiProperties);
return (DataSource) context.doLookup(dataSourceName);
} catch (Exception e) {
throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
String msg = "Error in looking up data source: " + e.getMessage();
log.error(msg);
throw new RuntimeException(msg, e);
}
}
@ -110,7 +114,11 @@ public class HeartBeatBeaconUtils {
uuid = props.getProperty("server.uuid");
input.close();
} catch (FileNotFoundException e) {
log.info("File : server-credentials.properties does not exist, new UUID will be generated for server.");
String msg = "File : server-credentials.properties does not exist, new UUID will be generated for server.";
if(log.isDebugEnabled()){
log.debug(msg, e);
}
log.info(msg);
} catch (IOException e) {
log.error("Error accessing server-credentials.properties to locate server.uuid.", e);
}

@ -49,7 +49,6 @@ public class GenericHeartBeatDAOImpl implements HeartBeatDAO {
@Override
public String recordServerCtx(ServerContext ctx) throws HeartBeatDAOException {
PreparedStatement stmt = null;
String uuid = null;
try {
Connection conn = HeartBeatBeaconDAOFactory.getConnection();
@ -57,167 +56,167 @@ public class GenericHeartBeatDAOImpl implements HeartBeatDAO {
String sql;
sql = "INSERT INTO SERVER_HEART_BEAT_EVENTS(HOST_NAME, UUID, SERVER_PORT) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
stmt.setString(1, ctx.getHostName());
stmt.setString(2, serverUUID);
stmt.setInt(3, ctx.getCarbonServerPort());
if(stmt.executeUpdate() > 0){
if (stmt.executeUpdate() > 0) {
uuid = serverUUID;
}
}
} catch (SQLException e) {
throw new HeartBeatDAOException("Error occurred while persisting server context for : '" +
String msg = "Error occurred while persisting server context for : '" +
"port '" + ctx.getCarbonServerPort() + "' " +
"hostname : '" + ctx.getHostName() + "' ", e);
} finally {
HeartBeatBeaconDAOUtil.cleanupResources(stmt, null);
"hostname : '" + ctx.getHostName() + "'";
log.error(msg, e);
throw new HeartBeatDAOException(msg, e);
}
return uuid;
}
@Override
public boolean recordElectedCandidate(String serverUUID) throws HeartBeatDAOException {
PreparedStatement stmt = null;
try {
Connection conn = HeartBeatBeaconDAOFactory.getConnection();
String sql;
sql = "INSERT INTO ELECTED_LEADER_META_INFO(UUID) VALUES (?)";
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
stmt.setString(1, serverUUID);
return stmt.executeUpdate() > 0;
}
} catch (SQLException e) {
throw new HeartBeatDAOException("Error occurred while persisting UUID of chosen " +
"elected dynamic task execution candidate : " + serverUUID , e);
} finally {
HeartBeatBeaconDAOUtil.cleanupResources(stmt, null);
String msg = "Error occurred while persisting UUID of chosen " +
"elected dynamic task execution candidate : " + serverUUID;
log.error(msg, e);
throw new HeartBeatDAOException(msg, e);
}
}
@Override
public void purgeCandidates() throws HeartBeatDAOException {
Statement stmt = null;
try {
Connection conn = HeartBeatBeaconDAOFactory.getConnection();
conn.setAutoCommit(false);
String sql = "TRUNCATE TABLE ELECTED_LEADER_META_INFO";
stmt = conn.createStatement();
try (Statement stmt = conn.createStatement()) {
stmt.execute(sql);
conn.commit();
}
} catch (SQLException e) {
throw new HeartBeatDAOException("Error occurred while truncating ELECTED_LEADER_META_INFO table.", e);
} finally {
HeartBeatBeaconDAOUtil.cleanupResources(stmt, null);
String msg = "Error occurred while truncating ELECTED_LEADER_META_INFO table.";
log.error(msg, e);
throw new HeartBeatDAOException(msg, e);
}
}
@Override
public ElectedCandidate retrieveCandidate() throws HeartBeatDAOException {
Statement stmt = null;
ResultSet resultSet = null;
ElectedCandidate candidate = null;
try {
Connection conn = HeartBeatBeaconDAOFactory.getConnection();
String sql = "SELECT * from ELECTED_LEADER_META_INFO";
stmt = conn.createStatement();
resultSet = stmt.executeQuery(sql);
while (resultSet.next()){
try (Statement stmt = conn.createStatement()) {
try (ResultSet resultSet = stmt.executeQuery(sql)) {
while (resultSet.next()) {
candidate = HeartBeatBeaconDAOUtil.populateCandidate(resultSet);
}
}
}
} catch (SQLException e) {
throw new HeartBeatDAOException("Error occurred while retrieving meta information of elected candidate", e);
} finally {
HeartBeatBeaconDAOUtil.cleanupResources(stmt, resultSet);
String msg = "Error occurred while retrieving meta information of elected candidate";
log.error(msg, e);
throw new HeartBeatDAOException(msg, e);
}
return candidate;
}
@Override
public boolean acknowledgeTask(String uuid, List<String> taskList) throws HeartBeatDAOException {
PreparedStatement stmt = null;
public boolean acknowledgeTask(String uuid, List<String> taskList)
throws HeartBeatDAOException {
try {
Connection conn = HeartBeatBeaconDAOFactory.getConnection();
String sql;
sql = "UPDATE ELECTED_LEADER_META_INFO SET ACKNOWLEDGED_TASK_LIST = ? WHERE UUID = ?";
stmt = conn.prepareStatement(sql, new String[]{"UUID"});
try (PreparedStatement stmt = conn.prepareStatement(sql, new String[]{"UUID"})) {
stmt.setString(1, String.join(",", taskList));
stmt.setString(2, uuid);
return stmt.executeUpdate() > 0;
}
} catch (SQLException e) {
throw new HeartBeatDAOException("Error occurred while updating task list of elected server : '" +
uuid + "' and task list " + taskList, e);
} finally {
HeartBeatBeaconDAOUtil.cleanupResources(stmt, null);
String msg = "Error occurred while updating task list of elected server : '" +
uuid + "' and task list " + taskList;
log.error(msg, e);
throw new HeartBeatDAOException(msg, e);
}
}
@Override
public boolean recordHeatBeat(HeartBeatEvent event) throws HeartBeatDAOException {
PreparedStatement stmt = null;
try {
Connection conn = HeartBeatBeaconDAOFactory.getConnection();
String sql;
sql = "UPDATE SERVER_HEART_BEAT_EVENTS SET LAST_UPDATED_TIMESTAMP = ? WHERE UUID = ?";
stmt = conn.prepareStatement(sql, new String[]{"ID"});
try (PreparedStatement stmt = conn.prepareStatement(sql, new String[]{"ID"})) {
stmt.setTimestamp(1, event.getTime());
stmt.setString(2, event.getServerUUID());
return stmt.executeUpdate() > 0;
}
} catch (SQLException e) {
throw new HeartBeatDAOException("Error occurred while updating heartbeat event against server with UUID : '" +
event.getServerUUID() + "' and timestamp " + event.getTime(), e);
} finally {
HeartBeatBeaconDAOUtil.cleanupResources(stmt, null);
String msg = "Error occurred while updating heartbeat event against server with UUID : '" +
event.getServerUUID() + "' and timestamp " + event.getTime();
log.error(msg, e);
throw new HeartBeatDAOException(msg, e);
}
}
@Override
public boolean checkUUIDValidity(String uuid) throws HeartBeatDAOException {
PreparedStatement stmt = null;
ResultSet resultSet = null;
boolean result = false;
try {
Connection conn = HeartBeatBeaconDAOFactory.getConnection();
String sql = "SELECT ID FROM SERVER_HEART_BEAT_EVENTS WHERE UUID = ?";
stmt = conn.prepareStatement(sql);
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, uuid);
resultSet = stmt.executeQuery();
if(resultSet.next()){
try (ResultSet resultSet = stmt.executeQuery()) {
if (resultSet.next()) {
result = true;
}
}
}
} catch (SQLException e) {
throw new HeartBeatDAOException("Error occurred checking existense of UUID" + uuid +
" amongst heartbeat meta info ", e);
} finally {
HeartBeatBeaconDAOUtil.cleanupResources(stmt, resultSet);
String msg = "Error occurred checking existense of UUID" + uuid +
" amongst heartbeat meta info.";
log.error(msg, e);
throw new HeartBeatDAOException(msg, e);
}
return result;
}
@Override
public String retrieveExistingServerCtx(ServerContext ctx) throws HeartBeatDAOException {
PreparedStatement stmt = null;
ResultSet resultSet = null;
String uuid = null;
try {
Connection conn = HeartBeatBeaconDAOFactory.getConnection();
String sql = "SELECT UUID FROM SERVER_HEART_BEAT_EVENTS WHERE HOST_NAME = ? AND SERVER_PORT = ?";
stmt = conn.prepareStatement(sql, new String[]{"UUID"});
try (PreparedStatement stmt = conn.prepareStatement(sql, new String[]{"UUID"})) {
stmt.setString(1, ctx.getHostName());
stmt.setInt(2, ctx.getCarbonServerPort());
resultSet = stmt.executeQuery();
if (resultSet.next()){
try (ResultSet resultSet = stmt.executeQuery()) {
if (resultSet.next()) {
uuid = resultSet.getString("UUID");
}
}
}
} catch (SQLException e) {
throw new HeartBeatDAOException("Error occurred while retrieving meta information for heart beat event from " +
String msg = "Error occurred while retrieving meta information for heart beat event from " +
"port '" + ctx.getCarbonServerPort() + "' " +
"hostname : '" + ctx.getHostName() + "' ", e);
} finally {
HeartBeatBeaconDAOUtil.cleanupResources(stmt, resultSet);
"hostname : '" + ctx.getHostName() + "'";
log.error(msg, e);
throw new HeartBeatDAOException(msg, e);
}
return uuid;
}
@ -225,8 +224,6 @@ public class GenericHeartBeatDAOImpl implements HeartBeatDAO {
@Override
public Map<String, ServerContext> getActiveServerDetails(int elapsedTimeInSeconds)
throws HeartBeatDAOException {
PreparedStatement stmt = null;
ResultSet resultSet = null;
Map<String, ServerContext> ctxList = new HashMap<>();
try {
Connection conn = HeartBeatBeaconDAOFactory.getConnection();
@ -234,17 +231,19 @@ public class GenericHeartBeatDAOImpl implements HeartBeatDAO {
"SERVER_HEART_BEAT_EVENTS, (SELECT @row_number:=-1) AS TEMP " +
"WHERE LAST_UPDATED_TIMESTAMP > ? " +
"ORDER BY UUID";
stmt = conn.prepareStatement(sql);
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setTimestamp(1, new Timestamp(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(elapsedTimeInSeconds)));
resultSet = stmt.executeQuery();
try (ResultSet resultSet = stmt.executeQuery()) {
while (resultSet.next()) {
ctxList.put(resultSet.getString("UUID"), HeartBeatBeaconDAOUtil.populateContext(resultSet));
}
}
}
} catch (SQLException e) {
throw new HeartBeatDAOException("Error occurred while retrieving acting server count with " +
"heartbeat updates within " + elapsedTimeInSeconds + " seconds.", e);
} finally {
HeartBeatBeaconDAOUtil.cleanupResources(stmt, resultSet);
String msg = "Error occurred while retrieving acting server count with " +
"heartbeat updates within " + elapsedTimeInSeconds + " seconds.";
log.error(msg, e);
throw new HeartBeatDAOException(msg, e);
}
return ctxList;
}

@ -47,13 +47,15 @@ public class HeartBeatExecutor {
ScheduledExecutorService executor =
Executors.newSingleThreadScheduledExecutor();
if(CONFIG == null){
throw new HeartBeatBeaconConfigurationException("Error while initiating schedule taks for recording heartbeats.");
if (CONFIG == null) {
String msg = "Error while initiating schedule taks for recording heartbeats.";
log.error(msg);
throw new HeartBeatBeaconConfigurationException(msg);
}
try {
String uuid = HeartBeatBeaconUtils.readUUID();
if(uuid == null){
if (uuid == null) {
uuid = HeartBeatBeaconDataHolder.getInstance().getHeartBeatManagementService().updateServerContext(ctx);
HeartBeatBeaconUtils.saveUUID(uuid);
}
@ -77,9 +79,13 @@ public class HeartBeatExecutor {
CONFIG.getNotifierFrequency() != 0 ? CONFIG.getNotifierFrequency() : DEFAULT__NOTIFIER_INTERVAL,
TimeUnit.SECONDS);
} catch (HeartBeatManagementException e) {
throw new HeartBeatBeaconConfigurationException("Error occured while updating initial server context.", e);
String msg = "Error occured while updating initial server context.";
log.error(msg);
throw new HeartBeatBeaconConfigurationException(msg, e);
} catch (IOException e) {
throw new HeartBeatBeaconConfigurationException("Error while persisting UUID of server.", e);
String msg = "Error while persisting UUID of server.";
log.error(msg);
throw new HeartBeatBeaconConfigurationException(msg, e);
}
}
@ -87,7 +93,8 @@ public class HeartBeatExecutor {
HeartBeatBeaconDataHolder.getInstance().getHeartBeatManagementService().recordHeartBeat(new HeartBeatEvent(uuid));
}
static void electDynamicTaskExecutionCandidate(int cumilativeTimeOut) throws HeartBeatManagementException {
static void electDynamicTaskExecutionCandidate(int cumilativeTimeOut)
throws HeartBeatManagementException {
HeartBeatBeaconDataHolder.getInstance().getHeartBeatManagementService().electCandidate(cumilativeTimeOut);
}

@ -18,7 +18,6 @@
package io.entgra.server.bootup.heartbeat.beacon.service;
import io.entgra.server.bootup.heartbeat.beacon.dto.ElectedCandidate;
import io.entgra.server.bootup.heartbeat.beacon.dto.HeartBeatEvent;
import io.entgra.server.bootup.heartbeat.beacon.dto.ServerContext;
import io.entgra.server.bootup.heartbeat.beacon.exception.HeartBeatManagementException;

@ -47,7 +47,7 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
private final HeartBeatDAO heartBeatDAO;
public HeartBeatManagementServiceImpl(){
public HeartBeatManagementServiceImpl() {
this.heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO();
}
@ -57,7 +57,7 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
int hashIndex = -1;
ServerContext localServerCtx = null;
ServerCtxInfo serverCtxInfo = null;
if(HeartBeatBeaconConfig.getInstance().isEnabled()) {
if (HeartBeatBeaconConfig.getInstance().isEnabled()) {
try {
HeartBeatBeaconDAOFactory.openConnection();
int timeOutIntervalInSeconds = HeartBeatBeaconConfig.getInstance().getServerTimeOutIntervalInSeconds();
@ -74,9 +74,11 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
}
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the underlying data source";
log.error(msg, e);
throw new HeartBeatManagementException(msg, e);
} catch (HeartBeatDAOException e) {
String msg = "Error occurred while retrieving active server count.";
log.error(msg, e);
throw new HeartBeatManagementException(msg, e);
} finally {
HeartBeatBeaconDAOFactory.closeConnection();
@ -91,10 +93,11 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
@Override
public boolean isTaskPartitioningEnabled() throws HeartBeatManagementException {
boolean enabled = false;
if(HeartBeatBeaconConfig.getInstance() != null){
if (HeartBeatBeaconConfig.getInstance() != null) {
enabled = HeartBeatBeaconConfig.getInstance().isEnabled();
} else {
String msg = "Issue instantiating heart beat config.";
log.error(msg);
throw new HeartBeatManagementException(msg);
}
return enabled;
@ -104,7 +107,7 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
@Override
public String updateServerContext(ServerContext ctx) throws HeartBeatManagementException {
String uuid = null;
if(HeartBeatBeaconConfig.getInstance().isEnabled()) {
if (HeartBeatBeaconConfig.getInstance().isEnabled()) {
try {
HeartBeatBeaconDAOFactory.beginTransaction();
uuid = heartBeatDAO.retrieveExistingServerCtx(ctx);
@ -114,10 +117,12 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
}
} catch (HeartBeatDAOException e) {
String msg = "Error Occured while retrieving server context.";
log.error(msg, e);
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";
log.error(msg, e);
throw new HeartBeatManagementException(msg, e);
} finally {
HeartBeatBeaconDAOFactory.closeConnection();
@ -132,22 +137,24 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
@Override
public boolean isQualifiedToExecuteTask() throws HeartBeatManagementException {
boolean isQualified = false;
if(HeartBeatBeaconConfig.getInstance().isEnabled()) {
if (HeartBeatBeaconConfig.getInstance().isEnabled()) {
try {
String localServerUUID = HeartBeatBeaconDataHolder.getInstance().getLocalServerUUID();
HeartBeatBeaconDAOFactory.openConnection();
ElectedCandidate candidate = heartBeatDAO.retrieveCandidate();
if(candidate != null && candidate.getServerUUID().equalsIgnoreCase(localServerUUID)){
if (candidate != null && candidate.getServerUUID().equalsIgnoreCase(localServerUUID)) {
isQualified = true;
if(log.isDebugEnabled()){
if (log.isDebugEnabled()) {
log.debug("Node : " + localServerUUID + " Qualified to execute randomly assigned task.");
}
}
} catch (HeartBeatDAOException e) {
String msg = "Error occurred while checking if server is qualified to execute randomly designated task.";
log.error(msg, e);
throw new HeartBeatManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the underlying data source";
log.error(msg, e);
throw new HeartBeatManagementException(msg, e);
} finally {
HeartBeatBeaconDAOFactory.closeConnection();
@ -160,23 +167,24 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
}
@Override
public boolean updateTaskExecutionAcknowledgement(String newTask) throws HeartBeatManagementException {
public boolean updateTaskExecutionAcknowledgement(String newTask)
throws HeartBeatManagementException {
boolean result = false;
if(HeartBeatBeaconConfig.getInstance().isEnabled()) {
if (HeartBeatBeaconConfig.getInstance().isEnabled()) {
try {
String serverUUID = HeartBeatBeaconDataHolder.getInstance().getLocalServerUUID();
HeartBeatBeaconDAOFactory.beginTransaction();
ElectedCandidate candidate = heartBeatDAO.retrieveCandidate();
if(candidate != null && candidate.getServerUUID().equals(serverUUID)){
if (candidate != null && candidate.getServerUUID().equals(serverUUID)) {
List<String> taskList = candidate.getAcknowledgedTaskList();
boolean taskExecuted = false;
for(String task : taskList){
if(task.equalsIgnoreCase(newTask)){
for (String task : taskList) {
if (task.equalsIgnoreCase(newTask)) {
taskExecuted = true;
break;
}
}
if(!taskExecuted) {
if (!taskExecuted) {
taskList.add(newTask);
result = heartBeatDAO.acknowledgeTask(serverUUID, taskList);
HeartBeatBeaconDAOFactory.commitTransaction();
@ -184,10 +192,12 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
}
} catch (HeartBeatDAOException e) {
String msg = "Error occurred while updating acknowledged task.";
log.error(msg, e);
throw new HeartBeatManagementException(msg, e);
} catch (TransactionManagementException e) {
HeartBeatBeaconDAOFactory.rollbackTransaction();
String msg = "Error occurred while updating acknowledged task.. Issue in opening a connection to the underlying data source";
log.error(msg, e);
throw new HeartBeatManagementException(msg, e);
} finally {
HeartBeatBeaconDAOFactory.closeConnection();
@ -223,10 +233,12 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
}
} catch (HeartBeatDAOException e) {
String msg = "Error occurred while electing candidate for dynamic task execution.";
log.error(msg, e);
throw new HeartBeatManagementException(msg, e);
} catch (TransactionManagementException e) {
HeartBeatBeaconDAOFactory.rollbackTransaction();
String msg = "Error occurred while electing candidate for dynamic task execution. Issue in opening a connection to the underlying data source";
log.error(msg, e);
throw new HeartBeatManagementException(msg, e);
} finally {
HeartBeatBeaconDAOFactory.closeConnection();
@ -243,8 +255,7 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
}
private String getRandomElement(Set<String> valueSet)
{
private String getRandomElement(Set<String> valueSet) {
Random rand = new Random();
List<String> items = new ArrayList<>(valueSet);
return items.get(rand.nextInt(items.size()));
@ -257,11 +268,12 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
if (HeartBeatBeaconConfig.getInstance().isEnabled()) {
try {
HeartBeatBeaconDAOFactory.beginTransaction();
if(heartBeatDAO.checkUUIDValidity(event.getServerUUID())){
if (heartBeatDAO.checkUUIDValidity(event.getServerUUID())) {
operationSuccess = heartBeatDAO.recordHeatBeat(event);
HeartBeatBeaconDAOFactory.commitTransaction();
} else {
String msg = "Server UUID Does not exist, heartbeat not recorded.";
log.error(msg);
throw new HeartBeatManagementException(msg);
}
} catch (HeartBeatDAOException e) {
@ -271,12 +283,14 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
HeartBeatBeaconDAOFactory.rollbackTransaction();
String msg = "Error occurred performing heart beat record transaction. " +
"Transaction rolled back.";
log.error(msg, e);
throw new HeartBeatManagementException(msg, e);
} finally {
HeartBeatBeaconDAOFactory.closeConnection();
}
} else {
String msg = "Heart Beat Configuration Disabled. Recording Heart Beat Failed.";
log.error(msg);
throw new HeartBeatManagementException(msg);
}
return operationSuccess;

@ -1,5 +1,5 @@
#
# Copyright 2009 WSO2, Inc. (http://wso2.com)
# Copyright 2020 Entgra Pvt. Ltd.. (http://entgra.io)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -15,7 +15,7 @@
#
#
# This is the log4j configuration file used by WSO2 Carbon
# This is the log4j configuration file used by Entgra Pvt. Ltd.
#
# IMPORTANT : Please do not remove or change the names of any
# of the Appenders defined here. The layout pattern & log file

@ -1,11 +1,11 @@
<!--
~ Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~ Copyright (c) 2020, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved.
~
~ WSO2 Inc. licenses this file to you under the Apache License,
~ Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ you may obtain a copy of the License at
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~

@ -1,21 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~
~ WSO2 Inc. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
~ Copyright (c) 2020, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved.
~
~ Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

@ -70,7 +70,7 @@ public class DelegationTask extends DynamicPartitionedScheduleTask {
try {
devices = new ArrayList<>();
toBeNotified = new ArrayList<>();
if(super.isDynamicTaskEligible()) {
if (super.isDynamicTaskEligible()) {
devices.addAll(service.getAllocatedDevices(deviceType,
super.getTaskContext().getActiveServerCount(),
super.getTaskContext().getServerHashIndex()));
@ -82,8 +82,7 @@ public class DelegationTask extends DynamicPartitionedScheduleTask {
&& device.getEnrolmentInfo().getStatus() != EnrolmentInfo.Status.REMOVED) {
toBeNotified.add(device);
}
// }
if(log.isDebugEnabled()){
if (log.isDebugEnabled()) {
log.debug("Adding policy operation to device : " + device.getDeviceIdentifier());
}
}

@ -139,7 +139,7 @@ public class MonitoringTask extends DynamicPartitionedScheduleTask {
status.equals(EnrolmentInfo.Status.UNREACHABLE)) {
notifiableDevices.add(device);
}
if(log.isDebugEnabled()){
if (log.isDebugEnabled()) {
log.debug("Adding monitoring operation to device : " + device.getDeviceIdentifier());
}
}

@ -1,3 +1,21 @@
/*
* Copyright (c) 2020, Entgra Pvt Ltd. (http://www.wso2.org) All Rights Reserved.
*
* Entgra Pvt Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.policy.mgt.core.mock;
import io.entgra.server.bootup.heartbeat.beacon.dto.HeartBeatEvent;

@ -1,14 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2018 - 2020 Entgra (Pvt) Ltd, Inc - All Rights Reserved.
~ Copyright (c) 2020, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved.
~
~ Unauthorised copying/redistribution of this file, via any medium is strictly prohibited.
~
~ Licensed under the Entgra Commercial License, Version 1.0 (the "License");
~ you may not use this file except in compliance with the License.
~ Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://entgra.io/licenses/entgra-commercial/1.0
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an

@ -1,21 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~
~ WSO2 Inc. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
~ Copyright (c) 2020, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved.
~
~ Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@ -32,7 +32,7 @@
<version>4.1.11-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Entgra - Heart Beat Feature</name>
<url>http://wso2.org</url>
<url>http://entgra.io</url>
<modules>
<module>io.entgra.server.heart.beat.feature</module>

Loading…
Cancel
Save