Fix db connection pool exhaustion due to too many queries

remotes/1712966534876109980/master
Charitha Goonetilleke 8 months ago
parent dd03a073b2
commit 8f1781f4b6

@ -22,7 +22,6 @@ import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.HeartBeatBeaconC
import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.HeartBeatBeaconUtils; import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.HeartBeatBeaconUtils;
import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.config.datasource.DataSourceConfig; import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.config.datasource.DataSourceConfig;
import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.exception.InvalidConfigurationStateException; import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.exception.InvalidConfigurationStateException;
import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.ClusterFormationChangedNotifier;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.CarbonUtils;

@ -187,7 +187,7 @@ public class GenericHeartBeatDAOImpl implements HeartBeatDAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred checking existense of UUID" + uuid + String msg = "Error occurred checking existence of UUID" + uuid +
" amongst heartbeat meta info."; " amongst heartbeat meta info.";
log.error(msg, e); log.error(msg, e);
throw new HeartBeatDAOException(msg, e); throw new HeartBeatDAOException(msg, e);

@ -65,7 +65,7 @@ public class HeartBeatExecutor implements ServerStartupObserver {
Executors.newSingleThreadScheduledExecutor(); Executors.newSingleThreadScheduledExecutor();
if (CONFIG == null) { if (CONFIG == null) {
String msg = "Error while initiating schedule taks for recording heartbeats."; String msg = "Error while initiating schedule tasks for recording heartbeats.";
log.error(msg); log.error(msg);
throw new HeartBeatBeaconConfigurationException(msg); throw new HeartBeatBeaconConfigurationException(msg);
} }
@ -78,15 +78,15 @@ public class HeartBeatExecutor implements ServerStartupObserver {
} }
int timeOutIntervalInSeconds = CONFIG.getServerTimeOutIntervalInSeconds(); int timeOutIntervalInSeconds = CONFIG.getServerTimeOutIntervalInSeconds();
int timeSkew = CONFIG.getTimeSkew(); int timeSkew = CONFIG.getTimeSkew();
int cumilativeTimeOut = timeOutIntervalInSeconds + timeSkew; int cumulativeTimeOut = timeOutIntervalInSeconds + timeSkew;
final String designatedUUID = uuid; final String designatedUUID = uuid;
HeartBeatBeaconDataHolder.getInstance().setLocalServerUUID(designatedUUID); HeartBeatBeaconDataHolder.getInstance().setLocalServerUUID(designatedUUID);
Runnable periodicTask = new Runnable() { Runnable periodicTask = new Runnable() {
public void run() { public void run() {
try { try {
recordHeartBeat(designatedUUID); recordHeartBeat(designatedUUID);
electDynamicTaskExecutionCandidate(cumilativeTimeOut); electDynamicTaskExecutionCandidate(cumulativeTimeOut);
notifyClusterFormationChanged(cumilativeTimeOut); notifyClusterFormationChanged(cumulativeTimeOut);
} catch (Exception e) { } catch (Exception e) {
log.error("Error while executing record heart beat task. This will result in schedule operation malfunction.", e); log.error("Error while executing record heart beat task. This will result in schedule operation malfunction.", e);
} }
@ -97,7 +97,7 @@ public class HeartBeatExecutor implements ServerStartupObserver {
CONFIG.getNotifierFrequency() != 0 ? CONFIG.getNotifierFrequency() : DEFAULT__NOTIFIER_INTERVAL, CONFIG.getNotifierFrequency() != 0 ? CONFIG.getNotifierFrequency() : DEFAULT__NOTIFIER_INTERVAL,
TimeUnit.SECONDS); TimeUnit.SECONDS);
} catch (HeartBeatManagementException e) { } catch (HeartBeatManagementException e) {
String msg = "Error occured while updating initial server context."; String msg = "Error occurred while updating initial server context.";
log.error(msg); log.error(msg);
throw new HeartBeatBeaconConfigurationException(msg, e); throw new HeartBeatBeaconConfigurationException(msg, e);
} catch (IOException e) { } catch (IOException e) {
@ -111,13 +111,14 @@ public class HeartBeatExecutor implements ServerStartupObserver {
HeartBeatBeaconDataHolder.getInstance().getHeartBeatManagementService().recordHeartBeat(new HeartBeatEvent(uuid)); HeartBeatBeaconDataHolder.getInstance().getHeartBeatManagementService().recordHeartBeat(new HeartBeatEvent(uuid));
} }
static void electDynamicTaskExecutionCandidate(int cumilativeTimeOut) static void electDynamicTaskExecutionCandidate(int cumulativeTimeOut)
throws HeartBeatManagementException { throws HeartBeatManagementException {
HeartBeatBeaconDataHolder.getInstance().getHeartBeatManagementService().electCandidate(cumilativeTimeOut); HeartBeatBeaconDataHolder.getInstance().getHeartBeatManagementService().electCandidate(cumulativeTimeOut);
} }
static void notifyClusterFormationChanged(int cumilativeTimeOut) throws HeartBeatManagementException { static void notifyClusterFormationChanged(int cumulativeTimeOut) throws HeartBeatManagementException {
HeartBeatBeaconDataHolder.getInstance().getHeartBeatManagementService().notifyClusterFormationChanged(cumilativeTimeOut); HeartBeatBeaconDataHolder.getInstance().getHeartBeatManagementService()
.notifyClusterFormationChanged(cumulativeTimeOut);
} }
} }

@ -50,6 +50,7 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
private static int lastActiveCount = -1; private static int lastActiveCount = -1;
private static int lastHashIndex = -1; private static int lastHashIndex = -1;
private static volatile boolean isQualified = false;
public HeartBeatManagementServiceImpl() { public HeartBeatManagementServiceImpl() {
this.heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO(); this.heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO();
@ -58,17 +59,17 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
@Override @Override
public ServerCtxInfo getServerCtxInfo() throws HeartBeatManagementException { public ServerCtxInfo getServerCtxInfo() throws HeartBeatManagementException {
int hashIndex = -1; int hashIndex;
ServerContext localServerCtx = null; ServerContext localServerCtx;
ServerCtxInfo serverCtxInfo = null; ServerCtxInfo serverCtxInfo = null;
if (HeartBeatBeaconConfig.getInstance().isEnabled()) { if (HeartBeatBeaconConfig.getInstance().isEnabled()) {
try { try {
HeartBeatBeaconDAOFactory.openConnection(); HeartBeatBeaconDAOFactory.openConnection();
int timeOutIntervalInSeconds = HeartBeatBeaconConfig.getInstance().getServerTimeOutIntervalInSeconds(); int timeOutIntervalInSeconds = HeartBeatBeaconConfig.getInstance().getServerTimeOutIntervalInSeconds();
int timeSkew = HeartBeatBeaconConfig.getInstance().getTimeSkew(); int timeSkew = HeartBeatBeaconConfig.getInstance().getTimeSkew();
int cumilativeTimeOut = timeOutIntervalInSeconds + timeSkew; int cumulativeTimeOut = timeOutIntervalInSeconds + timeSkew;
String localServerUUID = HeartBeatBeaconDataHolder.getInstance().getLocalServerUUID(); String localServerUUID = HeartBeatBeaconDataHolder.getInstance().getLocalServerUUID();
Map<String, ServerContext> serverCtxMap = heartBeatDAO.getActiveServerDetails(cumilativeTimeOut); Map<String, ServerContext> serverCtxMap = heartBeatDAO.getActiveServerDetails(cumulativeTimeOut);
if (!serverCtxMap.isEmpty()) { if (!serverCtxMap.isEmpty()) {
localServerCtx = serverCtxMap.get(localServerUUID); localServerCtx = serverCtxMap.get(localServerUUID);
if (localServerCtx != null) { if (localServerCtx != null) {
@ -97,7 +98,7 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
@Override @Override
public boolean isTaskPartitioningEnabled() throws HeartBeatManagementException { public boolean isTaskPartitioningEnabled() throws HeartBeatManagementException {
boolean enabled = false; boolean enabled;
if (HeartBeatBeaconConfig.getInstance() != null) { if (HeartBeatBeaconConfig.getInstance() != null) {
enabled = HeartBeatBeaconConfig.getInstance().isEnabled(); enabled = HeartBeatBeaconConfig.getInstance().isEnabled();
} else { } else {
@ -111,7 +112,7 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
@Override @Override
public String updateServerContext(ServerContext ctx) throws HeartBeatManagementException { public String updateServerContext(ServerContext ctx) throws HeartBeatManagementException {
String uuid = null; String uuid;
if (HeartBeatBeaconConfig.getInstance().isEnabled()) { if (HeartBeatBeaconConfig.getInstance().isEnabled()) {
try { try {
HeartBeatBeaconDAOFactory.beginTransaction(); HeartBeatBeaconDAOFactory.beginTransaction();
@ -121,12 +122,13 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
HeartBeatBeaconDAOFactory.commitTransaction(); HeartBeatBeaconDAOFactory.commitTransaction();
} }
} catch (HeartBeatDAOException e) { } catch (HeartBeatDAOException e) {
String msg = "Error Occured while retrieving server context."; String msg = "Error Occurred while retrieving server context.";
log.error(msg, e); log.error(msg, e);
throw new HeartBeatManagementException(msg, e); throw new HeartBeatManagementException(msg, e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
HeartBeatBeaconDAOFactory.rollbackTransaction(); HeartBeatBeaconDAOFactory.rollbackTransaction();
String msg = "Error occurred while updating server context. Issue in opening a connection to the underlying data source"; String msg = "Error occurred while updating server context. Issue in opening a connection to the " +
"underlying data source";
log.error(msg, e); log.error(msg, e);
throw new HeartBeatManagementException(msg, e); throw new HeartBeatManagementException(msg, e);
} finally { } finally {
@ -141,35 +143,7 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
} }
@Override @Override
public boolean isQualifiedToExecuteTask() throws HeartBeatManagementException { public boolean isQualifiedToExecuteTask() {
boolean isQualified = false;
if (HeartBeatBeaconConfig.getInstance().isEnabled()) {
try {
String localServerUUID = HeartBeatBeaconDataHolder.getInstance().getLocalServerUUID();
HeartBeatBeaconDAOFactory.openConnection();
ElectedCandidate candidate = heartBeatDAO.retrieveCandidate();
if (candidate != null && candidate.getServerUUID().equalsIgnoreCase(localServerUUID)) {
isQualified = true;
if (log.isDebugEnabled()) {
log.debug("Node : " + localServerUUID + " is 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();
}
} else {
String msg = "Heart Beat Configuration Disabled. Error occurred while checking if server is qualified to execute randomly designated task.";
log.error(msg);
throw new HeartBeatManagementException(msg);
}
return isQualified; return isQualified;
} }
@ -229,7 +203,7 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
if (presentCandidate != null) { if (presentCandidate != null) {
//if candidate is older than stipulated elapsed-time, purge and re-elect //if candidate is older than stipulated elapsed-time, purge and re-elect
if (presentCandidate.getTimeOfElection().before(new Timestamp(System.currentTimeMillis() if (presentCandidate.getTimeOfElection().before(new Timestamp(System.currentTimeMillis()
- TimeUnit.SECONDS.toMillis(elapsedTimeInSeconds)))) { - TimeUnit.SECONDS.toMillis(elapsedTimeInSeconds)))) {
heartBeatDAO.purgeCandidates(); heartBeatDAO.purgeCandidates();
electCandidate(servers); electCandidate(servers);
} }
@ -240,13 +214,24 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
} }
HeartBeatBeaconDAOFactory.commitTransaction(); HeartBeatBeaconDAOFactory.commitTransaction();
} }
ElectedCandidate candidate = heartBeatDAO.retrieveCandidate();
String localServerUUID = HeartBeatBeaconDataHolder.getInstance().getLocalServerUUID();
if (candidate != null && candidate.getServerUUID().equalsIgnoreCase(localServerUUID)) {
isQualified = true;
if (log.isDebugEnabled()) {
log.debug("Node : " + localServerUUID + " is qualified to execute randomly assigned task.");
}
} else {
isQualified = false;
}
} catch (HeartBeatDAOException e) { } catch (HeartBeatDAOException e) {
String msg = "Error occurred while electing candidate for dynamic task execution."; String msg = "Error occurred while electing candidate for dynamic task execution.";
log.error(msg, e); log.error(msg, e);
throw new HeartBeatManagementException(msg, e); throw new HeartBeatManagementException(msg, e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
HeartBeatBeaconDAOFactory.rollbackTransaction(); HeartBeatBeaconDAOFactory.rollbackTransaction();
String msg = "Error occurred while electing candidate for dynamic task execution. Issue in opening a connection to the underlying data source"; 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); log.error(msg, e);
throw new HeartBeatManagementException(msg, e); throw new HeartBeatManagementException(msg, e);
} finally { } finally {
@ -258,6 +243,7 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
throw new HeartBeatManagementException(msg); throw new HeartBeatManagementException(msg);
} }
} }
@Override @Override
public void notifyClusterFormationChanged(int elapsedTimeInSeconds) throws HeartBeatManagementException { public void notifyClusterFormationChanged(int elapsedTimeInSeconds) throws HeartBeatManagementException {
if (HeartBeatBeaconConfig.getInstance().isEnabled()) { if (HeartBeatBeaconConfig.getInstance().isEnabled()) {
@ -278,7 +264,8 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
lastHashIndex = serverContext.getIndex(); lastHashIndex = serverContext.getIndex();
lastActiveCount = servers.size(); lastActiveCount = servers.size();
ClusterFormationChangedNotifierRepository repository = HeartBeatBeaconDataHolder.getInstance().getClusterFormationChangedNotifierRepository(); ClusterFormationChangedNotifierRepository repository = HeartBeatBeaconDataHolder.getInstance()
.getClusterFormationChangedNotifierRepository();
Map<String, ClusterFormationChangedNotifier> notifiers = repository.getNotifiers(); Map<String, ClusterFormationChangedNotifier> notifiers = repository.getNotifiers();
for (String type : notifiers.keySet()) { for (String type : notifiers.keySet()) {
ClusterFormationChangedNotifier notifier = notifiers.get(type); ClusterFormationChangedNotifier notifier = notifiers.get(type);
@ -372,8 +359,8 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
HeartBeatBeaconDAOFactory.openConnection(); HeartBeatBeaconDAOFactory.openConnection();
int timeOutIntervalInSeconds = HeartBeatBeaconConfig.getInstance().getServerTimeOutIntervalInSeconds(); int timeOutIntervalInSeconds = HeartBeatBeaconConfig.getInstance().getServerTimeOutIntervalInSeconds();
int timeSkew = HeartBeatBeaconConfig.getInstance().getTimeSkew(); int timeSkew = HeartBeatBeaconConfig.getInstance().getTimeSkew();
int cumilativeTimeOut = timeOutIntervalInSeconds + timeSkew; int cumulativeTimeOut = timeOutIntervalInSeconds + timeSkew;
Map<String, ServerContext> serverCtxMap = heartBeatDAO.getActiveServerDetails(cumilativeTimeOut); Map<String, ServerContext> serverCtxMap = heartBeatDAO.getActiveServerDetails(cumulativeTimeOut);
for (String uuid : serverCtxMap.keySet()) { for (String uuid : serverCtxMap.keySet()) {
ServerContext serverContext = serverCtxMap.get(uuid); ServerContext serverContext = serverCtxMap.get(uuid);
activeServers.put(serverContext.getIndex(), serverContext); activeServers.put(serverContext.getIndex(), serverContext);
@ -396,4 +383,5 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic
} }
return activeServers; return activeServers;
} }
} }

@ -19,20 +19,19 @@
package io.entgra.device.mgt.core.subtype.mgt.cache; package io.entgra.device.mgt.core.subtype.mgt.cache;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import io.entgra.device.mgt.core.subtype.mgt.dto.DeviceSubTypeCacheKey;
import io.entgra.device.mgt.core.subtype.mgt.dao.DeviceSubTypeDAO; import io.entgra.device.mgt.core.subtype.mgt.dao.DeviceSubTypeDAO;
import io.entgra.device.mgt.core.subtype.mgt.dao.DeviceSubTypeDAOFactory; import io.entgra.device.mgt.core.subtype.mgt.dao.DeviceSubTypeDAOFactory;
import io.entgra.device.mgt.core.subtype.mgt.dao.util.ConnectionManagerUtil; import io.entgra.device.mgt.core.subtype.mgt.dao.util.ConnectionManagerUtil;
import io.entgra.device.mgt.core.subtype.mgt.dto.DeviceSubType; import io.entgra.device.mgt.core.subtype.mgt.dto.DeviceSubType;
import io.entgra.device.mgt.core.subtype.mgt.dto.DeviceSubTypeCacheKey;
import io.entgra.device.mgt.core.subtype.mgt.exception.DBConnectionException; import io.entgra.device.mgt.core.subtype.mgt.exception.DBConnectionException;
import io.entgra.device.mgt.core.subtype.mgt.exception.SubTypeMgtDAOException; import io.entgra.device.mgt.core.subtype.mgt.exception.SubTypeMgtDAOException;
import io.entgra.device.mgt.core.subtype.mgt.exception.SubTypeMgtPluginException; import io.entgra.device.mgt.core.subtype.mgt.exception.SubTypeMgtPluginException;
import io.entgra.device.mgt.core.subtype.mgt.util.DeviceSubTypeMgtUtil;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
public class GetDeviceSubTypeCacheLoader extends CacheLoader<String, DeviceSubType> { public class GetDeviceSubTypeCacheLoader extends CacheLoader<DeviceSubTypeCacheKey, DeviceSubType> {
private static final Log log = LogFactory.getLog(GetDeviceSubTypeCacheLoader.class); private static final Log log = LogFactory.getLog(GetDeviceSubTypeCacheLoader.class);
@ -43,14 +42,13 @@ public class GetDeviceSubTypeCacheLoader extends CacheLoader<String, DeviceSubTy
} }
@Override @Override
public DeviceSubType load(String key) throws SubTypeMgtPluginException { public DeviceSubType load(DeviceSubTypeCacheKey deviceSubTypeCacheKey) throws SubTypeMgtPluginException {
DeviceSubTypeCacheKey deviceSubTypeCacheKey = DeviceSubTypeMgtUtil.getDeviceSubTypeCacheKey(key);
int tenantId = deviceSubTypeCacheKey.getTenantId(); int tenantId = deviceSubTypeCacheKey.getTenantId();
String subTypeId = deviceSubTypeCacheKey.getSubTypeId(); String subTypeId = deviceSubTypeCacheKey.getSubTypeId();
String deviceType = deviceSubTypeCacheKey.getDeviceType(); String deviceType = deviceSubTypeCacheKey.getDeviceType();
if (log.isTraceEnabled()) { if (log.isDebugEnabled()) {
log.trace("Loading Device subtype for " + deviceType + " subtype & subtype Id : " + subTypeId); log.debug("Loading Device subtype for " + deviceType + " subtype & subtype Id : " + subTypeId);
} }
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();

@ -19,9 +19,10 @@
package io.entgra.device.mgt.core.subtype.mgt.dto; package io.entgra.device.mgt.core.subtype.mgt.dto;
public class DeviceSubTypeCacheKey { public class DeviceSubTypeCacheKey {
int tenantId;
String subTypeId; private int tenantId;
String deviceType; private String subTypeId;
private String deviceType;
public int getTenantId() { public int getTenantId() {
return tenantId; return tenantId;
@ -46,4 +47,15 @@ public class DeviceSubTypeCacheKey {
public void setDeviceType(String deviceType) { public void setDeviceType(String deviceType) {
this.deviceType = deviceType; this.deviceType = deviceType;
} }
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public String toString() {
return tenantId + "|" + subTypeId + "|" + deviceType;
}
} }

@ -22,6 +22,7 @@ import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
import io.entgra.device.mgt.core.subtype.mgt.cache.GetDeviceSubTypeCacheLoader; import io.entgra.device.mgt.core.subtype.mgt.cache.GetDeviceSubTypeCacheLoader;
import io.entgra.device.mgt.core.subtype.mgt.dto.DeviceSubTypeCacheKey;
import io.entgra.device.mgt.core.subtype.mgt.exception.BadRequestException; import io.entgra.device.mgt.core.subtype.mgt.exception.BadRequestException;
import io.entgra.device.mgt.core.subtype.mgt.exception.DBConnectionException; import io.entgra.device.mgt.core.subtype.mgt.exception.DBConnectionException;
import io.entgra.device.mgt.core.subtype.mgt.exception.SubTypeMgtDAOException; import io.entgra.device.mgt.core.subtype.mgt.exception.SubTypeMgtDAOException;
@ -42,8 +43,10 @@ import java.util.concurrent.TimeUnit;
public class DeviceSubTypeServiceImpl implements DeviceSubTypeService { public class DeviceSubTypeServiceImpl implements DeviceSubTypeService {
private static final Log log = LogFactory.getLog(DeviceSubTypeServiceImpl.class); private static final Log log = LogFactory.getLog(DeviceSubTypeServiceImpl.class);
private static final LoadingCache<String, DeviceSubType> deviceSubTypeCache = CacheBuilder.newBuilder() private static final LoadingCache<DeviceSubTypeCacheKey, DeviceSubType> deviceSubTypeCache
.expireAfterWrite(15, TimeUnit.MINUTES).build(new GetDeviceSubTypeCacheLoader()); = CacheBuilder.newBuilder()
.expireAfterWrite(15, TimeUnit.MINUTES)
.build(new GetDeviceSubTypeCacheLoader());
private final DeviceSubTypeDAO deviceSubTypeDAO; private final DeviceSubTypeDAO deviceSubTypeDAO;
public DeviceSubTypeServiceImpl() { public DeviceSubTypeServiceImpl() {
@ -52,7 +55,7 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService {
@Override @Override
public boolean addDeviceSubType(DeviceSubType deviceSubType) throws SubTypeMgtPluginException { public boolean addDeviceSubType(DeviceSubType deviceSubType) throws SubTypeMgtPluginException {
String msg = ""; String msg;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
deviceSubType.setTenantId(tenantId); deviceSubType.setTenantId(tenantId);
@ -72,9 +75,6 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService {
throw new SubTypeMgtPluginException(msg); throw new SubTypeMgtPluginException(msg);
} }
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
String key = DeviceSubTypeMgtUtil.setDeviceSubTypeCacheKey(tenantId, deviceSubType.getSubTypeId(),
deviceSubType.getDeviceType());
deviceSubTypeCache.put(key, deviceSubType);
return true; return true;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
msg = "Error occurred while obtaining the database connection to add device subtype for " + msg = "Error occurred while obtaining the database connection to add device subtype for " +
@ -89,6 +89,10 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService {
throw new SubTypeMgtPluginException(msg, e); throw new SubTypeMgtPluginException(msg, e);
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
DeviceSubTypeCacheKey key = DeviceSubTypeMgtUtil.getDeviceSubTypeCacheKey(tenantId,
deviceSubType.getSubTypeId(),
deviceSubType.getDeviceType());
deviceSubTypeCache.refresh(key);
} }
} }
@ -96,7 +100,7 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService {
public boolean updateDeviceSubType(String subTypeId, int tenantId, String deviceType, public boolean updateDeviceSubType(String subTypeId, int tenantId, String deviceType,
String subTypeName, String typeDefinition) String subTypeName, String typeDefinition)
throws SubTypeMgtPluginException { throws SubTypeMgtPluginException {
String msg = ""; String msg;
DeviceSubType deviceSubTypeOld = getDeviceSubType(subTypeId, tenantId, deviceType); DeviceSubType deviceSubTypeOld = getDeviceSubType(subTypeId, tenantId, deviceType);
if (deviceSubTypeOld == null) { if (deviceSubTypeOld == null) {
@ -133,7 +137,7 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService {
throw new SubTypeMgtPluginException(msg, e); throw new SubTypeMgtPluginException(msg, e);
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
String key = DeviceSubTypeMgtUtil.setDeviceSubTypeCacheKey(tenantId, subTypeId, deviceType); DeviceSubTypeCacheKey key = DeviceSubTypeMgtUtil.getDeviceSubTypeCacheKey(tenantId, subTypeId, deviceType);
deviceSubTypeCache.refresh(key); deviceSubTypeCache.refresh(key);
} }
} }
@ -142,7 +146,7 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService {
public DeviceSubType getDeviceSubType(String subTypeId, int tenantId, String deviceType) public DeviceSubType getDeviceSubType(String subTypeId, int tenantId, String deviceType)
throws SubTypeMgtPluginException { throws SubTypeMgtPluginException {
try { try {
String key = DeviceSubTypeMgtUtil.setDeviceSubTypeCacheKey(tenantId, subTypeId, deviceType); DeviceSubTypeCacheKey key = DeviceSubTypeMgtUtil.getDeviceSubTypeCacheKey(tenantId, subTypeId, deviceType);
return deviceSubTypeCache.get(key); return deviceSubTypeCache.get(key);
} catch (CacheLoader.InvalidCacheLoadException e) { } catch (CacheLoader.InvalidCacheLoadException e) {
String msg = "Not having any" + deviceType + " subtype for subtype id: " + subTypeId; String msg = "Not having any" + deviceType + " subtype for subtype id: " + subTypeId;
@ -179,12 +183,7 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService {
@Override @Override
public int getDeviceSubTypeCount(String deviceType) throws SubTypeMgtPluginException { public int getDeviceSubTypeCount(String deviceType) throws SubTypeMgtPluginException {
try { try {
int result = deviceSubTypeDAO.getDeviceSubTypeCount(deviceType); return deviceSubTypeDAO.getDeviceSubTypeCount(deviceType);
if (result <= 0) {
String msg = "There are no any subtypes for device type: " + deviceType;
log.error(msg);
}
return result;
} catch (SubTypeMgtDAOException e) { } catch (SubTypeMgtDAOException e) {
String msg = "Error occurred in the database level while retrieving device subtypes count for " + deviceType String msg = "Error occurred in the database level while retrieving device subtypes count for " + deviceType
+ " subtypes"; + " subtypes";

@ -19,23 +19,15 @@
package io.entgra.device.mgt.core.subtype.mgt.util; package io.entgra.device.mgt.core.subtype.mgt.util;
import io.entgra.device.mgt.core.subtype.mgt.dto.DeviceSubTypeCacheKey; import io.entgra.device.mgt.core.subtype.mgt.dto.DeviceSubTypeCacheKey;
import io.entgra.device.mgt.core.subtype.mgt.dto.DeviceSubType;
public class DeviceSubTypeMgtUtil { public class DeviceSubTypeMgtUtil {
public static String setDeviceSubTypeCacheKey(int tenantId, String subTypeId, String deviceType) {
return tenantId + "|" + subTypeId + "|" + deviceType.toString();
}
public static DeviceSubTypeCacheKey getDeviceSubTypeCacheKey(String key) {
String[] keys = key.split("\\|");
int tenantId = Integer.parseInt(keys[0]);
String subTypeId = keys[1];
String deviceType = keys[2];
public static DeviceSubTypeCacheKey getDeviceSubTypeCacheKey(int tenantId, String subTypeId, String deviceType) {
DeviceSubTypeCacheKey deviceSubTypesCacheKey = new DeviceSubTypeCacheKey(); DeviceSubTypeCacheKey deviceSubTypesCacheKey = new DeviceSubTypeCacheKey();
deviceSubTypesCacheKey.setTenantId(tenantId); deviceSubTypesCacheKey.setTenantId(tenantId);
deviceSubTypesCacheKey.setSubTypeId(subTypeId); deviceSubTypesCacheKey.setSubTypeId(subTypeId);
deviceSubTypesCacheKey.setDeviceType(deviceType); deviceSubTypesCacheKey.setDeviceType(deviceType);
return deviceSubTypesCacheKey; return deviceSubTypesCacheKey;
} }
} }

Loading…
Cancel
Save