Merge branch 'master' into 'master'

Fix concurrency issue in push notification execution

See merge request entgra/carbon-device-mgt!536
reporting
Charitha Goonetilleke 5 years ago
commit 1d229a9fe3

@ -77,6 +77,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
/** /**
* This class implements all the functionality exposed as part of the OperationManager. Any transaction initiated * This class implements all the functionality exposed as part of the OperationManager. Any transaction initiated
@ -105,6 +107,8 @@ public class OperationManagerImpl implements OperationManager {
private final Map<Integer, Long> lastUpdatedTimeStamps; private final Map<Integer, Long> lastUpdatedTimeStamps;
private final ConcurrentMap<Integer, String> operationsInitBy; private final ConcurrentMap<Integer, String> operationsInitBy;
private final ThreadPoolExecutor notificationExecutor;
public OperationManagerImpl() { public OperationManagerImpl() {
commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO(); commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO();
configOperationDAO = OperationManagementDAOFactory.getConfigOperationDAO(); configOperationDAO = OperationManagementDAOFactory.getConfigOperationDAO();
@ -117,6 +121,7 @@ public class OperationManagerImpl implements OperationManager {
notificationStrategies = new HashMap<>(); notificationStrategies = new HashMap<>();
lastUpdatedTimeStamps = new HashMap<>(); lastUpdatedTimeStamps = new HashMap<>();
operationsInitBy = new ConcurrentHashMap<>(); operationsInitBy = new ConcurrentHashMap<>();
notificationExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
} }
public OperationManagerImpl(String deviceType, DeviceManagementService deviceManagementService) { public OperationManagerImpl(String deviceType, DeviceManagementService deviceManagementService) {
@ -422,6 +427,10 @@ public class OperationManagerImpl implements OperationManager {
* Otherwise device may call pending operation while DB is locked for write and deadlock can occur. * Otherwise device may call pending operation while DB is locked for write and deadlock can occur.
*/ */
if (notificationStrategy != null) { if (notificationStrategy != null) {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
notificationExecutor.execute(() -> {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Sending push notification to " + device.getDeviceIdentifier() + " from add operation method."); log.debug("Sending push notification to " + device.getDeviceIdentifier() + " from add operation method.");
} }
@ -466,6 +475,8 @@ public class OperationManagerImpl implements OperationManager {
log.error("Error occurred while sending notifications to " + device.getType() + log.error("Error occurred while sending notifications to " + device.getType() +
" device carrying id '" + device.getDeviceIdentifier() + "'", e); " device carrying id '" + device.getDeviceIdentifier() + "'", e);
} }
PrivilegedCarbonContext.endTenantFlow();
});
} }
} }

@ -46,7 +46,7 @@ import java.util.Map;
*/ */
public class PushNotificationSchedulerTask implements Runnable { public class PushNotificationSchedulerTask implements Runnable {
private static Log log = LogFactory.getLog(PushNotificationSchedulerTask.class); private static final Log log = LogFactory.getLog(PushNotificationSchedulerTask.class);
private final OperationDAO operationDAO = OperationManagementDAOFactory.getOperationDAO(); private final OperationDAO operationDAO = OperationManagementDAOFactory.getOperationDAO();
private final OperationMappingDAO operationMappingDAO = OperationManagementDAOFactory.getOperationMappingDAO(); private final OperationMappingDAO operationMappingDAO = OperationManagementDAOFactory.getOperationMappingDAO();
private final DeviceManagementProviderService provider = DeviceManagementDataHolder.getInstance() private final DeviceManagementProviderService provider = DeviceManagementDataHolder.getInstance()

@ -140,7 +140,7 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT_OP_MAPPING (
DEVICE_ID INTEGER DEFAULT NULL, DEVICE_ID INTEGER DEFAULT NULL,
DEVICE_TYPE VARCHAR(300) NOT NULL, DEVICE_TYPE VARCHAR(300) NOT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL, DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL,
TENANT_ID INTEGER DEFAULT 0; TENANT_ID INTEGER DEFAULT 0,
PRIMARY KEY (ID), PRIMARY KEY (ID),
KEY `fk_dm_device_operation_mapping_operation` (`OPERATION_ID`), KEY `fk_dm_device_operation_mapping_operation` (`OPERATION_ID`),
KEY `IDX_DM_ENROLMENT_OP_MAPPING` (`ENROLMENT_ID`,`OPERATION_ID`), KEY `IDX_DM_ENROLMENT_OP_MAPPING` (`ENROLMENT_ID`,`OPERATION_ID`),
@ -677,9 +677,3 @@ ORDER BY TENANT_ID, DEVICE_ID;
-- END OF DASHBOARD RELATED VIEWS -- -- END OF DASHBOARD RELATED VIEWS --
-- TEMP TABLE REQUIRED FOR DATA ARCHIVAL JOB
CREATE TABLE IF NOT EXISTS DM_ARCHIVED_OPERATIONS (
ID INTEGER NOT NULL,
CREATED_TIMESTAMP TIMESTAMP NOT NULL,
PRIMARY KEY (ID)
)ENGINE = InnoDB;

@ -668,10 +668,3 @@ WHERE
ORDER BY TENANT_ID, DEVICE_ID; ORDER BY TENANT_ID, DEVICE_ID;
-- END OF DASHBOARD RELATED VIEWS -- -- END OF DASHBOARD RELATED VIEWS --
-- TEMP TABLE REQUIRED FOR DATA ARCHIVAL JOB
CREATE TABLE IF NOT EXISTS DM_ARCHIVED_OPERATIONS (
ID INTEGER NOT NULL,
CREATED_TIMESTAMP TIMESTAMP(0) NOT NULL,
PRIMARY KEY (ID)
);
Loading…
Cancel
Save