From 99e5f9f876ddc10571ab9d883657bbeee1e38fa9 Mon Sep 17 00:00:00 2001 From: Charitha Goonetilleke Date: Sat, 10 Apr 2021 16:18:06 +0000 Subject: [PATCH] Fix executing policy monitoring continuously --- .../task/impl/DeviceStatusMonitoringTask.java | 1 + .../task/impl/DeviceDetailsRetrieverTask.java | 10 +- .../impl/DynamicPartitionedScheduleTask.java | 21 +++- .../mgt/core/enforcement/DelegationTask.java | 22 ++--- .../policy/mgt/core/task/MonitoringTask.java | 96 ++++++++----------- .../core/task/TaskScheduleServiceImpl.java | 12 +-- 6 files changed, 77 insertions(+), 85 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java index 7bc831c3137..5182fc36516 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java @@ -52,6 +52,7 @@ public class DeviceStatusMonitoringTask extends DynamicPartitionedScheduleTask { @Override public void setProperties(Map properties) { + super.setProperties(properties); deviceType = properties.get(DeviceStatusTaskManagerServiceImpl.DEVICE_TYPE); deviceTypeId = Integer.parseInt(properties.get(DeviceStatusTaskManagerServiceImpl.DEVICE_TYPE_ID)); String deviceStatusTaskConfigStr = properties.get(DeviceStatusTaskManagerServiceImpl.DEVICE_STATUS_TASK_CONFIG); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java index 360325e9ab9..0c6f77c15de 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java @@ -39,28 +39,26 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.common.DynamicTaskContext; -import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.common.StartupOperationConfig; +import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.task.DeviceMgtTaskException; import org.wso2.carbon.device.mgt.core.task.DeviceTaskManager; -import org.wso2.carbon.ntask.core.Task; -import org.wso2.carbon.user.api.UserStoreException; import java.util.List; import java.util.Map; public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask { - private static Log log = LogFactory.getLog(DeviceDetailsRetrieverTask.class); + private static final Log log = LogFactory.getLog(DeviceDetailsRetrieverTask.class); private String deviceType; private DeviceManagementProviderService deviceManagementProviderService; @Override public void setProperties(Map map) { + super.setProperties(map); deviceType = map.get("DEVICE_TYPE"); } @@ -122,7 +120,7 @@ public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask { //pass the configurations also from here, monitoring tasks try { if (deviceManagementProviderService.isDeviceMonitoringEnabled(deviceType)) { - deviceTaskManager.addOperations(super.getTaskContext()); + deviceTaskManager.addOperations(getTaskContext()); } } catch (DeviceMgtTaskException e) { log.error("Error occurred while trying to add the operations to " + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DynamicPartitionedScheduleTask.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DynamicPartitionedScheduleTask.java index ef11b3bda21..0bbb59d2bda 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DynamicPartitionedScheduleTask.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DynamicPartitionedScheduleTask.java @@ -26,12 +26,27 @@ import org.wso2.carbon.device.mgt.common.DynamicTaskContext; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.ntask.core.Task; +import java.util.Map; + public abstract class DynamicPartitionedScheduleTask implements Task { private static final Log log = LogFactory.getLog(DynamicPartitionedScheduleTask.class); private static DynamicTaskContext taskContext = null; + private Map properties; + + @Override + public void setProperties(Map properties) { + this.properties = properties; + } + + public String getProperty(String name) { + if (properties == null) { + return null; + } + return properties.get(name); + } @Override public final void init() { @@ -97,11 +112,7 @@ public abstract class DynamicPartitionedScheduleTask implements Task { } public static boolean isDynamicTaskEligible(){ - if(taskContext != null && taskContext.isPartitioningEnabled()) { - return true; - } else { - return false; - } + return taskContext != null && taskContext.isPartitioningEnabled(); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java index b24e84f0292..ba4cfd971cd 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java @@ -21,8 +21,8 @@ package org.wso2.carbon.policy.mgt.core.enforcement; 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.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; +import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; @@ -36,19 +36,13 @@ import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; import java.util.ArrayList; import java.util.List; -import java.util.Map; public class DelegationTask extends DynamicPartitionedScheduleTask { private static final Log log = LogFactory.getLog(DelegationTask.class); - private PolicyConfiguration policyConfiguration = DeviceConfigurationManager.getInstance() + private final PolicyConfiguration policyConfiguration = DeviceConfigurationManager.getInstance() .getDeviceManagementConfig().getPolicyConfiguration(); - @Override - public void setProperties(Map map) { - - } - @Override public void executeDynamicTask() { try { @@ -70,10 +64,10 @@ public class DelegationTask extends DynamicPartitionedScheduleTask { try { devices = new ArrayList<>(); toBeNotified = new ArrayList<>(); - if (super.isDynamicTaskEligible()) { + if (isDynamicTaskEligible()) { devices.addAll(service.getAllocatedDevices(deviceType, - super.getTaskContext().getActiveServerCount(), - super.getTaskContext().getServerHashIndex())); + getTaskContext().getActiveServerCount(), + getTaskContext().getServerHashIndex())); } else { devices.addAll(service.getAllDevices(deviceType, false)); } @@ -81,9 +75,9 @@ public class DelegationTask extends DynamicPartitionedScheduleTask { if (device != null && device.getEnrolmentInfo() != null && device.getEnrolmentInfo().getStatus() != EnrolmentInfo.Status.REMOVED) { toBeNotified.add(device); - } - if (log.isDebugEnabled()) { - log.debug("Adding policy operation to device : " + device.getDeviceIdentifier()); + if (log.isDebugEnabled()) { + log.debug("Adding policy operation to device : " + device.getDeviceIdentifier()); + } } } if (!toBeNotified.isEmpty()) { 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 d51f283a8e7..c93ff3d5631 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 @@ -24,33 +24,22 @@ import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; 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; import org.wso2.carbon.device.mgt.core.task.impl.DynamicPartitionedScheduleTask; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; import java.util.ArrayList; import java.util.List; -import java.util.Map; public class MonitoringTask extends DynamicPartitionedScheduleTask { private static final Log log = LogFactory.getLog(MonitoringTask.class); - @Override - public void setProperties(Map map) { - } - - @Override - public void executeDynamicTask() { - if (log.isDebugEnabled()) { - log.debug("Monitoring task started to run."); - } - this.executeforAllTenants(); - } + private String tenant; /** * Check whether Device platform (ex: android) is exist in the cdm-config.xml file before adding a @@ -66,30 +55,27 @@ public class MonitoringTask extends DynamicPartitionedScheduleTask { return policyMonitoringManager != null; } - private void executeforAllTenants() { - + @Override + public void executeDynamicTask() { + tenant = getProperty(PolicyManagementConstants.TENANT_ID); + if (tenant == null) { + log.warn("Tenant id of the Monitoring Task got null"); + return; + } + int tenantId = Integer.parseInt(tenant); if (log.isDebugEnabled()) { - log.debug("Monitoring task started to run for all tenants."); + log.debug("Monitoring task started to run for tenant: " + tenant); + } + if (MultitenantConstants.SUPER_TENANT_ID == tenantId) { + this.executeTask(); + return; } try { - DeviceManagementProviderService deviceManagementService = PolicyManagementDataHolder - .getInstance().getDeviceManagementService(); - List tenants = deviceManagementService.getDeviceEnrolledTenants(); - for (Integer tenant : tenants) { - if (MultitenantConstants.SUPER_TENANT_ID == tenant) { - this.executeTask(); - continue; - } - try { - PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant, true); - this.executeTask(); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - } - } catch (DeviceManagementException e) { - log.error("Error occurred while trying to get the available tenants from device manager service ", e); + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true); + this.executeTask(); + } finally { + PrivilegedCarbonContext.endTenantFlow(); } } @@ -105,7 +91,7 @@ public class MonitoringTask extends DynamicPartitionedScheduleTask { } } } catch (PolicyComplianceException e) { - log.error("Error occurred while getting the device types."); + log.error("TID:[" + tenant + "] Error occurred while getting the device types."); } if (!deviceTypes.isEmpty()) { try { @@ -113,42 +99,42 @@ public class MonitoringTask extends DynamicPartitionedScheduleTask { PolicyManagementDataHolder.getInstance().getDeviceManagementService(); for (String deviceType : configDeviceTypes) { if (log.isDebugEnabled()) { - log.debug("Running task for device type : " + deviceType); + log.debug("TID:[" + tenant + "] Running task for device type : " + deviceType); } PolicyMonitoringManager monitoringService = PolicyManagementDataHolder.getInstance().getDeviceManagementService() .getPolicyMonitoringManager(deviceType); List devices; - if(super.isDynamicTaskEligible()){ - devices = deviceManagementProviderService.getAllocatedDevices(deviceType, - super.getTaskContext().getActiveServerCount(), - super.getTaskContext().getServerHashIndex()); + if(isDynamicTaskEligible()){ + devices = deviceManagementProviderService + .getAllocatedDevices(deviceType, getTaskContext().getActiveServerCount(), + getTaskContext().getServerHashIndex()); } else { devices = deviceManagementProviderService.getAllDevices(deviceType, false); } if (monitoringService != null && !devices.isEmpty()) { List notifiableDevices = new ArrayList<>(); if (log.isDebugEnabled()) { - log.debug("Removing inactive and blocked devices from the list for the device type : " + - deviceType); + log.debug("TID:[" + tenant + "] Removing inactive and blocked devices from " + + "the list for the device type : " + deviceType); } + StringBuilder sb = new StringBuilder(); for (Device device : devices) { EnrolmentInfo.Status status = device.getEnrolmentInfo().getStatus(); if (status.equals(EnrolmentInfo.Status.ACTIVE) || - status.equals(EnrolmentInfo.Status.INACTIVE) || status.equals(EnrolmentInfo.Status.UNREACHABLE)) { notifiableDevices.add(device); - } - if (log.isDebugEnabled()) { - log.debug("Adding monitoring operation to device : " + device.getDeviceIdentifier()); + if (log.isDebugEnabled()) { + if (sb.length() > 0) { + sb.append(", "); + } + sb.append(device.getDeviceIdentifier()); + } } } if (log.isDebugEnabled()) { - log.debug("Following '" + deviceType + "' devices selected to send the notification " + - "for policy monitoring"); - for (Device device : notifiableDevices) { - log.debug(device.getDeviceIdentifier()); - } + log.debug("TID:[" + tenant + "] Sending monitoring to '" + deviceType + + "' devices with ids [" + sb + "]"); } if (!notifiableDevices.isEmpty()) { monitoringManager.addMonitoringOperation(deviceType, notifiableDevices); @@ -156,13 +142,14 @@ public class MonitoringTask extends DynamicPartitionedScheduleTask { } } if (log.isDebugEnabled()) { - log.debug("Monitoring task running completed."); + log.debug("TID:[" + tenant + "] Monitoring task running completed."); } } catch (Exception e) { - log.error("Error occurred while trying to run a task.", e); + log.error("TID:[" + tenant + "] Error occurred while trying to run a task.", e); } } else { - log.info("No device types registered currently. So did not run the monitoring task."); + log.info("TID:[" + tenant + "] No device types registered currently. " + + "So did not run the monitoring task."); } } @@ -170,4 +157,5 @@ public class MonitoringTask extends DynamicPartitionedScheduleTask { protected void setup() { } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java index b4b358bc320..d6aefcee118 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java @@ -39,7 +39,7 @@ import java.util.Map; public class TaskScheduleServiceImpl implements TaskScheduleService { private static final Log log = LogFactory.getLog(TaskScheduleServiceImpl.class); - private PolicyConfiguration policyConfiguration; + private final PolicyConfiguration policyConfiguration; public TaskScheduleServiceImpl() { @@ -72,7 +72,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { Map properties = new HashMap<>(); properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId)); - String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); + String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + tenantId; if (!taskManager.isTaskScheduled(taskName)) { @@ -101,7 +101,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { public void stopTask() throws PolicyMonitoringTaskException { int tenantId = getTenantId(); try { - String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); + String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + tenantId; TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); if (taskService.isServerInit()) { TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); @@ -117,7 +117,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { public void updateTask(int monitoringFrequency) throws PolicyMonitoringTaskException { int tenantId = getTenantId(); try { - String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); + String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + tenantId; TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); @@ -129,7 +129,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { triggerInfo.setRepeatCount(-1); Map properties = new HashMap<>(); - properties.put("tenantId", String.valueOf(tenantId)); + properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId)); TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ, properties, triggerInfo); @@ -150,7 +150,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { @Override public boolean isTaskScheduled() throws PolicyMonitoringTaskException { int tenantId = getTenantId(); - String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); + String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + tenantId; TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); TaskManager taskManager; try {