From a1a313608f441d78cd10ded61485201dafd80183 Mon Sep 17 00:00:00 2001 From: charitha Date: Tue, 12 May 2020 15:34:55 +0530 Subject: [PATCH 1/5] Remove attempt count incrementing logic to optimize performance --- .../policy/mgt/monitor/NonComplianceData.java | 9 -- .../org.wso2.carbon.policy.mgt.core/pom.xml | 2 +- .../policy/mgt/core/dao/MonitoringDAO.java | 6 +- .../mgt/core/dao/impl/MonitoringDAOImpl.java | 114 +++----------- .../mgt/core/dao/impl/PolicyDAOImpl.java | 6 +- .../mgt/core/mgt/MonitoringManager.java | 9 +- .../core/mgt/impl/MonitoringManagerImpl.java | 145 +++++------------- .../policy/mgt/core/task/MonitoringTask.java | 21 ++- .../core/PolicyManagerServiceImplTest.java | 4 +- .../mgt/impl/MonitoringManagerImplTest.java | 2 +- 10 files changed, 79 insertions(+), 239 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/monitor/NonComplianceData.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/monitor/NonComplianceData.java index ffbe36cb8d..09f835bd92 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/monitor/NonComplianceData.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/monitor/NonComplianceData.java @@ -35,7 +35,6 @@ public class NonComplianceData { private Timestamp lastRequestedTime; private Timestamp lastSucceededTime; private Timestamp lastFailedTime; - private int attempts; private String message; /** @@ -85,14 +84,6 @@ public class NonComplianceData { this.lastFailedTime = lastFailedTime; } - public int getAttempts() { - return attempts; - } - - public void setAttempts(int attempts) { - this.attempts = attempts; - } - public int getDeviceId() { return deviceId; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml index 869d0ead09..1460c114cb 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml @@ -61,7 +61,7 @@ javax.cache, javax.naming, javax.xml.*, - javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional, + javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional, org.w3c.dom, org.wso2.carbon.policy.mgt.common.*, org.wso2.carbon.user.core.*, diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java index d7af966f28..ad9f9e02eb 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java @@ -55,7 +55,7 @@ public interface MonitoringDAO { List getCompliance(List deviceIds) throws MonitoringDAOException; - List getCompliance() throws MonitoringDAOException; + Map getCompliance() throws MonitoringDAOException; List getAllComplianceDevices( PaginationRequest paginationRequest, String policyId, boolean complianceStatus, boolean isPending, String fromDate, String toDate) @@ -65,8 +65,4 @@ public interface MonitoringDAO { void deleteNoneComplianceData(int policyComplianceStatusId) throws MonitoringDAOException; - boolean updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException; - - void updateAttempts(List deviceId, boolean reset) throws MonitoringDAOException; - } \ No newline at end of file diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java index 662d3ac349..e9f8abcf77 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java @@ -49,15 +49,14 @@ public class MonitoringDAOImpl implements MonitoringDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, ATTEMPTS, " + - "LAST_REQUESTED_TIME, TENANT_ID) VALUES (?, ?, ?,?, ?, ?) "; + String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, " + + "LAST_REQUESTED_TIME, TENANT_ID) VALUES (?, ?, ?,?, ?) "; stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt.setInt(1, deviceId); stmt.setInt(2, policyId); stmt.setInt(3, 1); - stmt.setInt(4, 1); - stmt.setTimestamp(5, currentTimestamp); - stmt.setInt(6, tenantId); + stmt.setTimestamp(4, currentTimestamp); + stmt.setInt(5, tenantId); stmt.executeUpdate(); generatedKeys = stmt.getGeneratedKeys(); @@ -90,16 +89,15 @@ public class MonitoringDAOImpl implements MonitoringDAO { } try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, ATTEMPTS, " + - "LAST_REQUESTED_TIME, TENANT_ID) VALUES (?, ?, ?,?, ?, ?) "; + String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, " + + "LAST_REQUESTED_TIME, TENANT_ID) VALUES (?, ?, ?, ?, ?) "; stmt = conn.prepareStatement(query); for (Map.Entry map : devicePolicyMap.entrySet()) { stmt.setInt(1, map.getKey()); stmt.setInt(2, map.getValue()); stmt.setInt(3, 1); - stmt.setInt(4, 1); - stmt.setTimestamp(5, currentTimestamp); - stmt.setInt(6, tenantId); + stmt.setTimestamp(4, currentTimestamp); + stmt.setInt(5, tenantId); stmt.addBatch(); } stmt.executeBatch(); @@ -119,23 +117,22 @@ public class MonitoringDAOImpl implements MonitoringDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); if (log.isDebugEnabled()) { - for (PolicyDeviceWrapper wrapper : policyDeviceWrapper){ + for (PolicyDeviceWrapper wrapper : policyDeviceWrapper) { log.debug("Policy Id : " + wrapper.getPolicyId() + " - " + " Device Id : " + wrapper.getDeviceId()); } } try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, ATTEMPTS, " + - "LAST_REQUESTED_TIME, TENANT_ID, ENROLMENT_ID) VALUES (?, ?, ?, ?, ?, ?, ?) "; + String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, " + + "LAST_REQUESTED_TIME, TENANT_ID, ENROLMENT_ID) VALUES (?, ?, ?, ?, ?, ?) "; stmt = conn.prepareStatement(query); for (PolicyDeviceWrapper wrapper : policyDeviceWrapper) { stmt.setInt(1, wrapper.getDeviceId()); stmt.setInt(2, wrapper.getPolicyId()); stmt.setInt(3, 1); - stmt.setInt(4, 1); - stmt.setTimestamp(5, currentTimestamp); - stmt.setInt(6, tenantId); - stmt.setInt(7, wrapper.getEnrolmentId()); + stmt.setTimestamp(4, currentTimestamp); + stmt.setInt(5, tenantId); + stmt.setInt(6, wrapper.getEnrolmentId()); stmt.addBatch(); } stmt.executeBatch(); @@ -155,8 +152,8 @@ public class MonitoringDAOImpl implements MonitoringDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = 0, LAST_FAILED_TIME = ?, POLICY_ID = ?," + - " ATTEMPTS=0 WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; + String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = 0, LAST_FAILED_TIME = ?, POLICY_ID = ? " + + "WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setTimestamp(1, currentTimestamp); stmt.setInt(2, policyId); @@ -182,8 +179,8 @@ public class MonitoringDAOImpl implements MonitoringDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = ?, ATTEMPTS=0, LAST_SUCCESS_TIME = ?" + - " WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; + String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = ?, LAST_SUCCESS_TIME = ?" + + " WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, 1); stmt.setTimestamp(2, currentTimestamp); @@ -260,9 +257,9 @@ public class MonitoringDAOImpl implements MonitoringDAO { while (resultSet.next()) { complianceData.setId(resultSet.getInt("ID")); complianceData.setDeviceId(resultSet.getInt("DEVICE_ID")); + complianceData.setEnrolmentId(resultSet.getInt("ENROLMENT_ID")); complianceData.setPolicyId(resultSet.getInt("POLICY_ID")); complianceData.setStatus(resultSet.getBoolean("STATUS")); - complianceData.setAttempts(resultSet.getInt("ATTEMPTS")); complianceData.setLastRequestedTime(resultSet.getTimestamp("LAST_REQUESTED_TIME")); complianceData.setLastSucceededTime(resultSet.getTimestamp("LAST_SUCCESS_TIME")); complianceData.setLastFailedTime(resultSet.getTimestamp("LAST_FAILED_TIME")); @@ -298,7 +295,6 @@ public class MonitoringDAOImpl implements MonitoringDAO { complianceData.setEnrolmentId(resultSet.getInt("ENROLMENT_ID")); complianceData.setPolicyId(resultSet.getInt("POLICY_ID")); complianceData.setStatus(resultSet.getBoolean("STATUS")); - complianceData.setAttempts(resultSet.getInt("ATTEMPTS")); complianceData.setLastRequestedTime(resultSet.getTimestamp("LAST_REQUESTED_TIME")); complianceData.setLastSucceededTime(resultSet.getTimestamp("LAST_SUCCESS_TIME")); complianceData.setLastFailedTime(resultSet.getTimestamp("LAST_FAILED_TIME")); @@ -314,11 +310,11 @@ public class MonitoringDAOImpl implements MonitoringDAO { } @Override - public List getCompliance() throws MonitoringDAOException { + public Map getCompliance() throws MonitoringDAOException { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - List complianceDataList = new ArrayList<>(); + Map complianceDataList = new HashMap<>(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); @@ -327,19 +323,19 @@ public class MonitoringDAOImpl implements MonitoringDAO { stmt.setInt(1, tenantId); resultSet = stmt.executeQuery(); + NonComplianceData complianceData; while (resultSet.next()) { - NonComplianceData complianceData = new NonComplianceData(); + complianceData = new NonComplianceData(); complianceData.setId(resultSet.getInt("ID")); complianceData.setDeviceId(resultSet.getInt("DEVICE_ID")); complianceData.setEnrolmentId(resultSet.getInt("ENROLMENT_ID")); complianceData.setPolicyId(resultSet.getInt("POLICY_ID")); complianceData.setStatus(resultSet.getBoolean("STATUS")); - complianceData.setAttempts(resultSet.getInt("ATTEMPTS")); complianceData.setLastRequestedTime(resultSet.getTimestamp("LAST_REQUESTED_TIME")); complianceData.setLastSucceededTime(resultSet.getTimestamp("LAST_SUCCESS_TIME")); complianceData.setLastFailedTime(resultSet.getTimestamp("LAST_FAILED_TIME")); - complianceDataList.add(complianceData); + complianceDataList.put(complianceData.getEnrolmentId(), complianceData); } return complianceDataList; } catch (SQLException e) { @@ -492,68 +488,6 @@ public class MonitoringDAOImpl implements MonitoringDAO { } - @Override - public boolean updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException { - boolean status = false; - Connection conn; - PreparedStatement stmt = null; - Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { - conn = this.getConnection(); - String query = ""; - if (reset) { - query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = 0, LAST_REQUESTED_TIME = ? " + - "WHERE DEVICE_ID = ? AND TENANT_ID = ?"; - } else { - query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = ATTEMPTS + 1, LAST_REQUESTED_TIME = ? " + - "WHERE DEVICE_ID = ? AND TENANT_ID = ?"; - } - stmt = conn.prepareStatement(query); - stmt.setTimestamp(1, currentTimestamp); - stmt.setInt(2, deviceId); - stmt.setInt(3, tenantId); - stmt.executeUpdate(); - status = true; - } catch (SQLException e) { - throw new MonitoringDAOException("Unable to update the attempts data in database.", e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, null); - } - return status; - } - - @Override - public void updateAttempts(List deviceIds, boolean reset) throws MonitoringDAOException { - Connection conn; - PreparedStatement stmt = null; - Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { - conn = this.getConnection(); - String query = ""; - if (reset) { - query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = 0, LAST_REQUESTED_TIME = ? " + - "WHERE DEVICE_ID = ? AND TENANT_ID = ?"; - } else { - query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = ATTEMPTS + 1, LAST_REQUESTED_TIME = ? " + - "WHERE DEVICE_ID = ? AND TENANT_ID = ?"; - } - stmt = conn.prepareStatement(query); - for (int deviceId : deviceIds) { - stmt.setTimestamp(1, currentTimestamp); - stmt.setInt(2, deviceId); - stmt.setInt(3, tenantId); - stmt.addBatch(); - } - stmt.executeBatch(); - } catch (SQLException e) { - throw new MonitoringDAOException("Unable to update the attempts data in database.", e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, null); - } - } - private Connection getConnection() throws MonitoringDAOException { return PolicyManagementDAOFactory.getConnection(); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index a244d3b6a6..766c512eb0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -1778,13 +1778,13 @@ public class PolicyDAOImpl implements PolicyDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE TENANT_ID = ?"; + String query = "SELECT POLICY_ID, ENROLMENT_ID FROM DM_DEVICE_POLICY_APPLIED WHERE TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, tenantId); resultSet = stmt.executeQuery(); while (resultSet.next()) { - devicePolicyIds.put(resultSet.getInt("DEVICE_ID"), resultSet.getInt("POLICY_ID")); + devicePolicyIds.put(resultSet.getInt("ENROLMENT_ID"), resultSet.getInt("POLICY_ID")); } } catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while getting the applied policy", e); @@ -1805,7 +1805,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); - String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE TENANT_ID = ?"; + String query = "SELECT POLICY_ID, DEVICE_ID FROM DM_DEVICE_POLICY_APPLIED WHERE TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, tenantId); resultSet = stmt.executeQuery(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java index b058ff5fcb..c0773fa080 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java @@ -44,15 +44,8 @@ public interface MonitoringManager { NonComplianceData getDevicePolicyCompliance(Device device) throws PolicyComplianceException; - void addMonitoringOperation(List devices) throws PolicyComplianceException; + void addMonitoringOperation(String deviceType, List devices) throws PolicyComplianceException; List getDeviceTypes() throws PolicyComplianceException; - PaginationResult getPolicyCompliance( - PaginationRequest paginationRequest, String policyId, boolean complianceStatus, boolean isPending, - String fromDate, String toDate) throws PolicyComplianceException; - - List getNoneComplianceFeatures(int complianceStatusId) - throws PolicyComplianceException; - } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index a2d4039826..1edca6715c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -32,6 +32,7 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.ComplianceData; +import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.ComplianceFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.NonComplianceData; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; @@ -58,9 +59,9 @@ import java.util.Map; public class MonitoringManagerImpl implements MonitoringManager { - private PolicyDAO policyDAO; - private MonitoringDAO monitoringDAO; - private ComplianceDecisionPoint complianceDecisionPoint; + private final PolicyDAO policyDAO; + private final MonitoringDAO monitoringDAO; + private final ComplianceDecisionPoint complianceDecisionPoint; private static final Log log = LogFactory.getLog(MonitoringManagerImpl.class); private static final String OPERATION_MONITOR = "MONITOR"; @@ -113,6 +114,7 @@ public class MonitoringManagerImpl implements MonitoringManager { complianceData.setPolicy(policy); complianceFeatures = complianceData.getComplianceFeatures(); complianceData.setDeviceId(device.getId()); + complianceData.setEnrolmentId(cmd.getEnrolmentId()); complianceData.setPolicyId(policy.getId()); } @@ -121,7 +123,7 @@ public class MonitoringManagerImpl implements MonitoringManager { } catch (MonitoringDAOException e) { throw new PolicyComplianceException( "Unable to add the none compliance features to database for device " + - device.getDeviceIdentifier() + " - " + device.getType(), e); + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } finally { PolicyManagementDAOFactory.closeConnection(); } @@ -145,7 +147,7 @@ public class MonitoringManagerImpl implements MonitoringManager { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyComplianceException( "Unable to add the none compliance features to database for device " + - device.getDeviceIdentifier() + " - " + device.getType(), e); + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } finally { PolicyManagementDAOFactory.closeConnection(); } @@ -253,106 +255,46 @@ public class MonitoringManagerImpl implements MonitoringManager { } @Override - public void addMonitoringOperation(List devices) throws PolicyComplianceException { + public void addMonitoringOperation(String deviceType, List devices) throws PolicyComplianceException { - //int tenantId = PolicyManagerUtil.getTenantId(); - Map deviceIds = new HashMap<>(); - List complianceDatas = new ArrayList<>(); - HashMap devicePolicyIdMap = new HashMap<>(); + Map notifiableDeviceEnrollments = new HashMap<>(); + List firstTimeComplianceData = new ArrayList<>(); try { PolicyManagementDAOFactory.openConnection(); - //TODO: Return a map from getCompliance to reduce O(n^2) -> O(n) - List cd = monitoringDAO.getCompliance(); + Map persistedComplianceData = monitoringDAO.getCompliance(); + HashMap appliedPolicyIds = policyDAO.getAppliedPolicyIds(); + int enrollmentId; for (Device device : devices) { - deviceIds.put(device.getId(), device); - - for (NonComplianceData data : cd) { - if (device.getId() == data.getDeviceId() && device.getEnrolmentInfo().getId() == data - .getEnrolmentId()) { - complianceDatas.add(data); - } - } - } - List deviceIDs = new ArrayList<>(deviceIds.keySet()); - - HashMap temp = policyDAO.getAppliedPolicyIds(); - for (Integer id : deviceIDs) { - if (temp != null && !temp.isEmpty() && temp.containsKey(id)) { - devicePolicyIdMap.put(id, temp.get(id)); + enrollmentId = device.getEnrolmentInfo().getId(); + if (persistedComplianceData.containsKey(enrollmentId)) { + notifiableDeviceEnrollments.put(enrollmentId, device); + } else if (appliedPolicyIds.containsKey(enrollmentId)){ + PolicyDeviceWrapper policyDeviceWrapper = new PolicyDeviceWrapper(); + policyDeviceWrapper.setDeviceId(device.getId()); + policyDeviceWrapper.setEnrolmentId(device.getEnrolmentInfo().getId()); + policyDeviceWrapper.setPolicyId(appliedPolicyIds.get(enrollmentId)); + firstTimeComplianceData.add(policyDeviceWrapper); + notifiableDeviceEnrollments.put(enrollmentId, device); } } - } catch (SQLException e) { throw new PolicyComplianceException("SQL error occurred while getting monitoring details.", e); } catch (MonitoringDAOException e) { - throw new PolicyComplianceException("SQL error occurred while getting monitoring details.", e); + throw new PolicyComplianceException("Error occurred while getting monitoring details.", e); } catch (PolicyManagerDAOException e) { throw new PolicyComplianceException("SQL error occurred while getting policy details.", e); } finally { PolicyManagementDAOFactory.closeConnection(); } - Map deviceIdsToAddOperation = new HashMap<>(); - Map deviceIdsWithExistingOperation = new HashMap<>(); - - List firstTimeDevices = new ArrayList<>(); - - Map tempMap = new HashMap<>(); - try { - if (complianceDatas != null || !complianceDatas.isEmpty()) { - for (NonComplianceData complianceData : complianceDatas) { - - tempMap.put(complianceData.getDeviceId(), complianceData); - - if (complianceData.getAttempts() == 0) { - deviceIdsToAddOperation.put(complianceData.getDeviceId(), - deviceIds.get(complianceData.getDeviceId())); - } else { - deviceIdsWithExistingOperation.put(complianceData.getDeviceId(), - deviceIds.get(complianceData.getDeviceId())); - } - } - } - - for (Device device : devices) { - if ((!tempMap.containsKey(device.getId())) && (devicePolicyIdMap.containsKey(device.getId()))) { - deviceIdsToAddOperation.put(device.getId(), device); - - PolicyDeviceWrapper policyDeviceWrapper = new PolicyDeviceWrapper(); - policyDeviceWrapper.setDeviceId(device.getId()); - policyDeviceWrapper.setEnrolmentId(device.getEnrolmentInfo().getId()); - policyDeviceWrapper.setPolicyId(devicePolicyIdMap.get(device.getId())); - - firstTimeDevices.add(policyDeviceWrapper); - - // firstTimeDeviceIdsWithPolicyIds.put(device.getId(), devicePolicyIdMap.get(device.getId())); - } - } - - if (log.isDebugEnabled()) { - log.debug("These devices are in the system for the first time"); - for (PolicyDeviceWrapper wrapper : firstTimeDevices) { - log.debug("First time device primary key : " + wrapper.getDeviceId() + " & policy id " + - wrapper.getPolicyId()); - } - } - PolicyManagementDAOFactory.beginTransaction(); - - if (!deviceIdsToAddOperation.isEmpty()) { - // monitoringDAO.addComplianceDetails(firstTimeDeviceIdsWithPolicyIds); - monitoringDAO.addComplianceDetails(firstTimeDevices); - monitoringDAO.updateAttempts(new ArrayList<>(deviceIdsToAddOperation.keySet()), false); + if (!firstTimeComplianceData.isEmpty()) { + monitoringDAO.addComplianceDetails(firstTimeComplianceData); + PolicyManagementDAOFactory.commitTransaction(); } - - if (!deviceIdsWithExistingOperation.isEmpty()) { - monitoringDAO.updateAttempts(new ArrayList<>(deviceIdsWithExistingOperation.keySet()), false); - } - PolicyManagementDAOFactory.commitTransaction(); - } catch (MonitoringDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyComplianceException("Error occurred from monitoring dao.", e); @@ -363,9 +305,9 @@ public class MonitoringManagerImpl implements MonitoringManager { PolicyManagementDAOFactory.closeConnection(); } - if (!deviceIdsToAddOperation.isEmpty()) { + if (!notifiableDeviceEnrollments.isEmpty()) { try { - this.addMonitoringOperationsToDatabase(new ArrayList<>(deviceIdsToAddOperation.values())); + this.addMonitoringOperationsToDatabase(deviceType, new ArrayList<>(notifiableDeviceEnrollments.values())); } catch (InvalidDeviceException e) { throw new PolicyComplianceException("Invalid Device Identifiers found.", e); } catch (OperationManagementException e) { @@ -381,8 +323,8 @@ public class MonitoringManagerImpl implements MonitoringManager { try { //when shutdown, it sets DeviceManagementService to null, therefore need to have a null check if (PolicyManagementDataHolder.getInstance().getDeviceManagementService() != null) { - deviceTypes = - PolicyManagementDataHolder.getInstance().getDeviceManagementService().getPolicyMonitoringEnableDeviceTypes(); + deviceTypes = PolicyManagementDataHolder.getInstance().getDeviceManagementService() + .getPolicyMonitoringEnableDeviceTypes(); } } catch (DeviceManagementException e) { throw new PolicyComplianceException("Error occurred while getting the device types.", e); @@ -436,32 +378,17 @@ public class MonitoringManagerImpl implements MonitoringManager { return complianceFeatureList; } - private void addMonitoringOperationsToDatabase(List devices) - throws PolicyComplianceException, OperationManagementException, InvalidDeviceException { + private void addMonitoringOperationsToDatabase(String deviceType, List devices) + throws OperationManagementException, InvalidDeviceException { List deviceIdentifiers = this.getDeviceIdentifiersFromDevices(devices); CommandOperation monitoringOperation = new CommandOperation(); monitoringOperation.setEnabled(true); monitoringOperation.setType(Operation.Type.COMMAND); monitoringOperation.setCode(OPERATION_MONITOR); - // CommandOperation infoOperation = new CommandOperation(); - // infoOperation.setEnabled(true); - // infoOperation.setType(Operation.Type.COMMAND);\\ - // infoOperation.setCode(OPERATION_INFO); - // CommandOperation appListOperation = new CommandOperation(); - // appListOperation.setEnabled(true); - // appListOperation.setType(Operation.Type.COMMAND); - // appListOperation.setCode(OPERATION_APP_LIST); - - //TODO: Fix this properly later adding device type to be passed in when the task manage executes "addOperations()" - String type = null; - if (deviceIdentifiers.size() > 0) { - type = deviceIdentifiers.get(0).getType(); - } - DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); - service.addOperation(type, monitoringOperation, deviceIdentifiers); - // service.addOperation(infoOperation, deviceIdentifiers); - // service.addOperation(appListOperation, deviceIdentifiers); + monitoringOperation.setControl(Operation.Control.NO_REPEAT); + PolicyManagementDataHolder.getInstance().getDeviceManagementService() + .addOperation(deviceType, monitoringOperation, deviceIdentifiers); } private List getDeviceIdentifiersFromDevices(List devices) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java index 3fbe2d5834..7ed5e683d3 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java @@ -23,7 +23,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; @@ -40,7 +40,7 @@ import java.util.Map; public class MonitoringTask implements Task { - private static Log log = LogFactory.getLog(MonitoringTask.class); + private static final Log log = LogFactory.getLog(MonitoringTask.class); Map properties; private boolean executeForTenants = false; @@ -62,10 +62,10 @@ public class MonitoringTask implements Task { if (log.isDebugEnabled()) { log.debug("Monitoring task started to run."); } - if(System.getProperty(IS_CLOUD) != null && Boolean.parseBoolean(System.getProperty(IS_CLOUD))){ + if (System.getProperty(IS_CLOUD) != null && Boolean.parseBoolean(System.getProperty(IS_CLOUD))) { executeForTenants = true; } - if(executeForTenants) { + if (executeForTenants) { this.executeforAllTenants(); } else { this.executeTask(); @@ -83,10 +83,7 @@ public class MonitoringTask implements Task { private boolean isPlatformExist(String deviceType) { PolicyMonitoringManager policyMonitoringManager = PolicyManagementDataHolder.getInstance() .getDeviceManagementService().getPolicyMonitoringManager(deviceType); - if (policyMonitoringManager != null) { - return true; - } - return false; + return policyMonitoringManager != null; } private void executeforAllTenants() { @@ -116,7 +113,7 @@ public class MonitoringTask implements Task { } } - private void executeTask(){ + private void executeTask() { MonitoringManager monitoringManager = PolicyManagementDataHolder.getInstance().getMonitoringManager(); List deviceTypes = new ArrayList<>(); @@ -158,13 +155,14 @@ public class MonitoringTask implements Task { } } if (log.isDebugEnabled()) { - log.debug("Following devices selected to send the notification for " + deviceType); + log.debug("Following '" + deviceType + "' devices selected to send the notification " + + "for policy monitoring"); for (Device device : notifiableDevices) { log.debug(device.getDeviceIdentifier()); } } if (!notifiableDevices.isEmpty()) { - monitoringManager.addMonitoringOperation(notifiableDevices); + monitoringManager.addMonitoringOperation(deviceType, notifiableDevices); } } } @@ -178,4 +176,5 @@ public class MonitoringTask implements Task { log.info("No device types registered currently. So did not run the monitoring task."); } } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java index 1bd4d6591e..2ffacebbac 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java @@ -259,7 +259,7 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { deviceList.add(device); MonitoringManager manager = new MonitoringManagerImpl(); - manager.addMonitoringOperation(deviceList); + manager.addMonitoringOperation(DEVICE_TYPE_A, deviceList); policyManagerService.checkCompliance(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), complianceFeatures); boolean deviceCompliance = policyManagerService.isCompliant(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); @@ -289,7 +289,7 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { deviceList.add(device); MonitoringManager manager = new MonitoringManagerImpl(); - manager.addMonitoringOperation(deviceList); + manager.addMonitoringOperation(DEVICE_TYPE_A, deviceList); policyManagerService.checkCompliance(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), complianceFeatures); boolean deviceCompliance = policyManagerService.isCompliant(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImplTest.java index 07dc2dd992..1faed27398 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImplTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImplTest.java @@ -316,7 +316,7 @@ public class MonitoringManagerImplTest extends BasePolicyManagementDAOTest{ @Test public void testAddMonitoringOperation() throws Exception { - monitoringManager.addMonitoringOperation(deviceMgtService.getAllDevices()); + monitoringManager.addMonitoringOperation(DEVICE_TYPE_E, deviceMgtService.getAllDevices()); } @Test From 3ccc383d126baaaacbfcfe8795d4d552a3ec873e Mon Sep 17 00:00:00 2001 From: charitha Date: Thu, 14 May 2020 14:43:37 +0530 Subject: [PATCH 2/5] Add missing interfaces --- .../wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java | 7 +++++++ .../wso2/carbon/policy/mgt/core/task/MonitoringTask.java | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java index c0773fa080..234b176b14 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java @@ -48,4 +48,11 @@ public interface MonitoringManager { List getDeviceTypes() throws PolicyComplianceException; + PaginationResult getPolicyCompliance( + PaginationRequest paginationRequest, String policyId, boolean complianceStatus, boolean isPending, + String fromDate, String toDate) throws PolicyComplianceException; + + List getNoneComplianceFeatures(int complianceStatusId) + throws PolicyComplianceException; + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java index 7ed5e683d3..d70c06858b 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java @@ -23,8 +23,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; +import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; From dac5c2b511bcd6b543e63bede7eb5a5963d72dd5 Mon Sep 17 00:00:00 2001 From: charitha Date: Mon, 18 May 2020 09:22:23 +0530 Subject: [PATCH 3/5] Fix deadlock in device application list update --- .../mgt/core/dao/impl/AbstractDeviceDAOImpl.java | 3 ++- .../mgt/core/dao/impl/ApplicationDAOImpl.java | 15 ++++----------- .../core/operation/mgt/OperationManagerImpl.java | 10 +++++++--- .../src/main/resources/dbscripts/cdm/mssql.sql | 11 +++++++++-- .../src/main/resources/dbscripts/cdm/mysql.sql | 2 ++ 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 1c6ebebae3..ff56439f04 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -2650,7 +2650,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(e.DEVICE_ID) AS DEVICE_COUNT FROM DM_ENROLMENT e WHERE STATUS != 'REMOVED'"; + String sql = "SELECT COUNT(e.DEVICE_ID) AS DEVICE_COUNT FROM DM_ENROLMENT e WHERE STATUS = 'ACTIVE' " + + "OR STATUS = 'UNREACHABLE'"; stmt = conn.prepareStatement(sql); ResultSet rs = stmt.executeQuery(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java index aa44f0996a..5a89b613be 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java @@ -88,7 +88,7 @@ public class ApplicationDAOImpl implements ApplicationDAO { conn = this.getConnection(); stmt = conn.prepareStatement("UPDATE DM_APPLICATION SET NAME = ?, PLATFORM = ?, CATEGORY = ?, " + "VERSION = ?, TYPE = ?, LOCATION_URL = ?, IMAGE_URL = ?, MEMORY_USAGE = ?, IS_ACTIVE = ? " + - "WHERE APP_IDENTIFIER = ? AND DEVICE_ID = ? AND ENROLMENT_ID = ? AND TENANT_ID = ?"); + "WHERE ID = ?"); for (Application application : applications) { stmt.setString(1, application.getName()); @@ -100,10 +100,7 @@ public class ApplicationDAOImpl implements ApplicationDAO { stmt.setString(7, application.getImageUrl()); stmt.setInt(8, application.getMemoryUsage()); stmt.setBoolean(9, application.isActive()); - stmt.setString(10, application.getApplicationIdentifier()); - stmt.setInt(11, deviceId); - stmt.setInt(12, enrolmentId); - stmt.setInt(13, tenantId); + stmt.setInt(10, application.getId()); stmt.addBatch(); } stmt.executeBatch(); @@ -123,14 +120,10 @@ public class ApplicationDAOImpl implements ApplicationDAO { try { conn = this.getConnection(); conn.setAutoCommit(false); - stmt = conn.prepareStatement("DELETE FROM DM_APPLICATION WHERE APP_IDENTIFIER = ? AND DEVICE_ID = ? " + - "AND ENROLMENT_ID = ? AND TENANT_ID = ?"); + stmt = conn.prepareStatement("DELETE FROM DM_APPLICATION WHERE ID = ?"); for (Application app : apps) { - stmt.setString(1, app.getApplicationIdentifier()); - stmt.setInt(2, deviceId); - stmt.setInt(3, enrolmentId); - stmt.setInt(4, tenantId); + stmt.setInt(1, app.getId()); stmt.addBatch(); } stmt.executeBatch(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index ca7659f1e3..8620657a18 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -834,13 +834,14 @@ public class OperationManagerImpl implements OperationManager { public void updateOperation(int enrolmentId, Operation operation, DeviceIdentifier deviceId) throws OperationManagementException { int operationId = operation.getId(); + boolean isOperationUpdated = false; try { OperationManagementDAOFactory.beginTransaction(); if (operation.getStatus() != null) { int failAttempts = 0; while (true) { try { - operationDAO.updateOperationStatus(enrolmentId, operationId, + isOperationUpdated = operationDAO.updateOperationStatus(enrolmentId, operationId, org.wso2.carbon.device.mgt.core.dto.operation.mgt. Operation.Status.valueOf(operation.getStatus(). toString())); @@ -865,7 +866,10 @@ public class OperationManagerImpl implements OperationManager { } } } - if (operation.getOperationResponse() != null) { + if (!isOperationUpdated) { + log.warn("Operation " + operationId + "'s status is not updated"); + } + if (isOperationUpdated && operation.getOperationResponse() != null) { OperationMonitoringTaskConfig operationMonitoringTaskConfig = DeviceManagementDataHolder .getInstance().getDeviceManagementProvider().getDeviceMonitoringConfig(deviceId.getType()); List monitoringOperations = operationMonitoringTaskConfig.getMonitoringOperation(); @@ -901,7 +905,7 @@ public class OperationManagerImpl implements OperationManager { } } OperationResponseMeta responseMeta = null; - if (operation.getOperationResponse() != null) { + if (isOperationUpdated && operation.getOperationResponse() != null) { int failAttempts = 0; while (true) { try { diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 6ba14be3d2..5f437350e5 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -182,6 +182,9 @@ CREATE TABLE DM_DEVICE_OPERATION_RESPONSE ( DM_ENROLMENT_OP_MAPPING (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_DM_RES_RT' AND OBJECT_ID = OBJECT_ID('DM_DEVICE_OPERATION_RESPONSE')) +CREATE INDEX IDX_DM_RES_RT ON DM_DEVICE_OPERATION_RESPONSE(RECEIVED_TIMESTAMP); + IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_ENID_OPID' AND OBJECT_ID = OBJECT_ID('DM_DEVICE_OPERATION_RESPONSE')) CREATE INDEX IDX_ENID_OPID ON DM_DEVICE_OPERATION_RESPONSE(OPERATION_ID, ENROLMENT_ID); @@ -190,11 +193,12 @@ CREATE INDEX IDX_DM_EN_OP_MAP_RES ON DM_DEVICE_OPERATION_RESPONSE(EN_OP_MAP_ID); IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_OPERATION_RESPONSE_LARGE]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_OPERATION_RESPONSE_LARGE ( - ID INTEGER IDENTITY(1,1) NOT NULL, + ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL, EN_OP_MAP_ID INTEGER NOT NULL, OPERATION_RESPONSE VARBINARY(MAX) DEFAULT NULL, - RECEIVED_TIMESTAMP DATETIME2 DEFAULT NULL + RECEIVED_TIMESTAMP DATETIME2 DEFAULT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL PRIMARY KEY (ID), CONSTRAINT FK_DM_DEVICE_OPERATION_RESP_LARGE_OPERATION FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, @@ -419,6 +423,9 @@ CREATE TABLE DM_APPLICATION ( DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_DM_APPLICATION' AND OBJECT_ID = OBJECT_ID('DM_APPLICATION')) +CREATE INDEX IDX_DM_APPLICATION ON DM_APPLICATION(DEVICE_ID, ENROLMENT_ID, TENANT_ID); + -- POLICY RELATED TABLES FINISHED -- -- POLICY AND DEVICE GROUP MAPPING -- diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 90c4e8a00d..dceb96f4e8 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -439,6 +439,8 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION ( DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION )ENGINE = InnoDB; +CREATE INDEX IDX_DM_APPLICATION ON DM_APPLICATION(DEVICE_ID, ENROLMENT_ID, TENANT_ID); + -- END OF POLICY RELATED TABLES -- -- POLICY AND DEVICE GROUP MAPPING -- From f59242d6b82e028151f1f1afdb4b1a2f29cc3821 Mon Sep 17 00:00:00 2001 From: charitha Date: Mon, 18 May 2020 09:49:11 +0530 Subject: [PATCH 4/5] Fix formatting issue --- .../org.wso2.carbon.policy.mgt.core/pom.xml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml index 1460c114cb..4b32c251bc 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml @@ -18,7 +18,8 @@ ~ under the License. --> - + org.wso2.carbon.devicemgt policy-mgt @@ -27,7 +28,6 @@ 4.0.0 - org.wso2.carbon.devicemgt org.wso2.carbon.policy.mgt.core 4.1.7-SNAPSHOT bundle @@ -61,7 +61,7 @@ javax.cache, javax.naming, javax.xml.*, - javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional, + javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional, org.w3c.dom, org.wso2.carbon.policy.mgt.common.*, org.wso2.carbon.user.core.*, @@ -142,8 +142,8 @@ org.wso2.carbon.utils - - + + org.wso2.carbon @@ -188,6 +188,7 @@ org.testng testng + test org.apache.tomcat.wso2 From 9280810b2402eda3da857489466643cfed4e6e61 Mon Sep 17 00:00:00 2001 From: charitha Date: Mon, 18 May 2020 10:11:25 +0530 Subject: [PATCH 5/5] Avoid unnecessary db connection --- .../core/mgt/impl/MonitoringManagerImpl.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index 1edca6715c..351da8495e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -289,20 +289,20 @@ public class MonitoringManagerImpl implements MonitoringManager { PolicyManagementDAOFactory.closeConnection(); } - try { - PolicyManagementDAOFactory.beginTransaction(); - if (!firstTimeComplianceData.isEmpty()) { + if (!firstTimeComplianceData.isEmpty()) { + try { + PolicyManagementDAOFactory.beginTransaction(); monitoringDAO.addComplianceDetails(firstTimeComplianceData); PolicyManagementDAOFactory.commitTransaction(); + } catch (MonitoringDAOException e) { + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyComplianceException("Error occurred from monitoring dao.", e); + } catch (PolicyManagerDAOException e) { + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyComplianceException("Error occurred reading the applied policies to devices.", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } - } catch (MonitoringDAOException e) { - PolicyManagementDAOFactory.rollbackTransaction(); - throw new PolicyComplianceException("Error occurred from monitoring dao.", e); - } catch (PolicyManagerDAOException e) { - PolicyManagementDAOFactory.rollbackTransaction(); - throw new PolicyComplianceException("Error occurred reading the applied policies to devices.", e); - } finally { - PolicyManagementDAOFactory.closeConnection(); } if (!notifiableDeviceEnrollments.isEmpty()) {