From d1fe1baad0e11198bb9d780aabc54729c27ad8d9 Mon Sep 17 00:00:00 2001 From: Charitha Goonetilleke Date: Fri, 10 May 2024 14:43:41 +0530 Subject: [PATCH] Fix issues with caching --- .../device/mgt/common/DeviceIdentifier.java | 19 +++++++++--- .../device/mgt/core/cache/DeviceCacheKey.java | 31 +++++++------------ .../beacon/dao/HeartBeatBeaconDAOFactory.java | 9 ++---- .../cache/OperationTemplateCacheLoader.java | 5 ++- .../dto/OperationTemplateCacheKey.java | 19 ++++++++++++ .../mgt/dto/DeviceSubTypeCacheKey.java | 8 +++++ .../mgt/impl/DeviceSubTypeServiceImpl.java | 4 ++- .../main/resources/dbscripts/cdm/mysql.sql | 2 +- 8 files changed, 62 insertions(+), 35 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/DeviceIdentifier.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/DeviceIdentifier.java index a1bb3fe51e..d02f844071 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/DeviceIdentifier.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/DeviceIdentifier.java @@ -69,9 +69,20 @@ public class DeviceIdentifier implements Serializable{ @Override public String toString() { - return "deviceId {" + - "id='" + id + '\'' + - ", type='" + type + '\'' + - '}'; + return type + "|" + id; } + + @Override + public int hashCode() { + return toString().hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof DeviceIdentifier) { + return (this.hashCode() == obj.hashCode()); + } + return false; + } + } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/cache/DeviceCacheKey.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/cache/DeviceCacheKey.java index a5931f408e..d25cd2673a 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/cache/DeviceCacheKey.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/cache/DeviceCacheKey.java @@ -28,7 +28,6 @@ public class DeviceCacheKey { private String deviceId; private String deviceType; private int tenantId; - private volatile int hashCode; public String getDeviceId() { return deviceId; @@ -55,27 +54,21 @@ public class DeviceCacheKey { } @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (!DeviceCacheKey.class.isAssignableFrom(obj.getClass())) { - return false; - } - final DeviceCacheKey other = (DeviceCacheKey) obj; - String thisId = this.deviceId + "-" + this.deviceType + "_" + this.tenantId; - String otherId = other.deviceId + "-" + other.deviceType + "_" + other.tenantId; - if (!thisId.equals(otherId)) { - return false; - } - return true; + public int hashCode() { + return toString().hashCode(); } @Override - public int hashCode() { - if (hashCode == 0) { - hashCode = Objects.hash(deviceId, deviceType, tenantId); + public String toString() { + return tenantId + "|" + deviceType + "|" + deviceId; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof DeviceCacheKey) { + return (this.hashCode() == obj.hashCode()); } - return hashCode; + return false; } + } diff --git a/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/dao/HeartBeatBeaconDAOFactory.java b/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/dao/HeartBeatBeaconDAOFactory.java index 7130f07cbf..e3c3dd3381 100644 --- a/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/dao/HeartBeatBeaconDAOFactory.java +++ b/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/dao/HeartBeatBeaconDAOFactory.java @@ -24,10 +24,8 @@ import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.config.datasourc import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.dao.impl.GenericHeartBeatDAOImpl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants; import io.entgra.device.mgt.core.device.mgt.common.exceptions.IllegalTransactionStateException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException; -import io.entgra.device.mgt.core.device.mgt.common.exceptions.UnsupportedDatabaseEngineException; import javax.sql.DataSource; import java.sql.Connection; @@ -42,7 +40,7 @@ public class HeartBeatBeaconDAOFactory { private static final Log log = LogFactory.getLog(HeartBeatBeaconDAOFactory.class); private static DataSource dataSource; - private static ThreadLocal currentConnection = new ThreadLocal<>(); + private static final ThreadLocal currentConnection = new ThreadLocal<>(); /** * Get instance of GroupDAO @@ -64,7 +62,6 @@ public class HeartBeatBeaconDAOFactory { /** * Begin transaction with datasource for write data * - * @throws TransactionManagementException */ public static void beginTransaction() throws TransactionManagementException { Connection conn = currentConnection.get(); @@ -85,7 +82,6 @@ public class HeartBeatBeaconDAOFactory { /** * Open connection to the datasource for read data * - * @throws SQLException */ public static void openConnection() throws SQLException { Connection conn = currentConnection.get(); @@ -102,7 +98,6 @@ public class HeartBeatBeaconDAOFactory { * Get current connection to datasource * * @return current connection - * @throws SQLException */ public static Connection getConnection() throws SQLException { Connection conn = currentConnection.get(); @@ -189,7 +184,7 @@ public class HeartBeatBeaconDAOFactory { List jndiPropertyList = jndiConfig.getJndiProperties(); if (jndiPropertyList != null) { - Hashtable jndiProperties = new Hashtable(); + Hashtable jndiProperties = new Hashtable<>(); for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) { jndiProperties.put(prop.getName(), prop.getValue()); } diff --git a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/cache/OperationTemplateCacheLoader.java b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/cache/OperationTemplateCacheLoader.java index e92ffbadcc..4b9316fad9 100644 --- a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/cache/OperationTemplateCacheLoader.java +++ b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/cache/OperationTemplateCacheLoader.java @@ -59,9 +59,8 @@ public class OperationTemplateCacheLoader extends CacheLoader deviceSubTypeCache = CacheBuilder.newBuilder() .expireAfterWrite(15, TimeUnit.MINUTES) @@ -143,7 +145,7 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService { } @Override - public DeviceSubType getDeviceSubType(String subTypeId, int tenantId, String deviceType) + public synchronized DeviceSubType getDeviceSubType(String subTypeId, int tenantId, String deviceType) throws SubTypeMgtPluginException { try { DeviceSubTypeCacheKey key = DeviceSubTypeMgtUtil.getDeviceSubTypeCacheKey(tenantId, subTypeId, deviceType); diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 9d3751e5d7..7000513833 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -133,7 +133,7 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION )ENGINE = InnoDB; CREATE INDEX IDX_ENROLMENT_FK_DEVICE_ID ON DM_ENROLMENT(DEVICE_ID); -CREATE INDEX IDX_ENROLMENT_DEVICE_TYPE_ID ON DM_ENROLMENT(DEVICE_TYPE_ID); +CREATE INDEX IDX_ENROLMENT_DEVICE_TYPE ON DM_ENROLMENT(DEVICE_TYPE); CREATE INDEX IDX_ENROLMENT_DEVICE_IDENTIFICATION ON DM_ENROLMENT(DEVICE_IDENTIFICATION); CREATE INDEX IDX_ENROLMENT_STATUS ON DM_ENROLMENT(STATUS);