From 5ee8c0f0654ffee90282f2c66e912884249f8a17 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Wed, 2 Sep 2015 14:47:36 +0530 Subject: [PATCH] Fixing the cache manager issues --- .../operation/mgt/OperationManagerImpl.java | 2 +- .../org.wso2.carbon.policy.mgt.core/pom.xml | 3 +- .../cache/impl/PolicyCacheManagerImpl.java | 233 ++++++++++++++---- .../core/util/PolicyManagementConstants.java | 1 + .../mgt/core/util/PolicyManagerUtil.java | 30 ++- 5 files changed, 212 insertions(+), 57 deletions(-) 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 0315ded100..c26eea0de4 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 @@ -184,7 +184,7 @@ public class OperationManagerImpl implements OperationManager { } finally { DeviceManagementDAOFactory.closeConnection(); } - + OperationManagementDAOFactory.openConnection(); if (enrolmentId < 0) { throw new OperationManagementException("Device not found for the given device Identifier:" + 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 a2333b475d..7e7a50ecd8 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 @@ -71,7 +71,8 @@ org.wso2.carbon.user.api.*, org.wso2.carbon.device.mgt.core.*, org.wso2.carbon.device.mgt.common.*, - org.wso2.carbon.ntask.* + org.wso2.carbon.ntask.*, + org.wso2.carbon.caching.* !org.wso2.carbon.policy.mgt.core.internal, diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java index 677b30920d..28096de9c1 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java @@ -27,11 +27,13 @@ import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.cache.PolicyCacheManager; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; -import java.util.ArrayList; +import javax.cache.Cache; import java.util.HashMap; +import java.util.Iterator; import java.util.List; -import java.util.Set; public class PolicyCacheManagerImpl implements PolicyCacheManager { @@ -41,6 +43,14 @@ public class PolicyCacheManagerImpl implements PolicyCacheManager { private static PolicyCacheManagerImpl policyCacheManager; +// private static Cache getPolicyCache() { +// return PolicyManagerUtil.getPolicyCache(PolicyManagementConstants.DM_CACHE); +// } + + private static Cache> getPolicyListCache() { + return PolicyManagerUtil.getPolicyListCache(PolicyManagementConstants.DM_CACHE_LIST); + } + private PolicyCacheManagerImpl() { } @@ -57,43 +67,87 @@ public class PolicyCacheManagerImpl implements PolicyCacheManager { @Override public void addAllPolicies(List policies) { - HashMap map = this.getTenantRelatedMap(); - if (map.isEmpty()) { - for (Policy policy : policies) { - map.put(policy.getId(), policy); - } - } + // HashMap map = this.getTenantRelatedMap(); +// Cache cache = getPolicyCache(); +// if (cache.isEmpty()) { + +// for (Policy policy : policies) { +// cache.put(policy.getId(), policy); +// } +// } + + Cache> lCache = getPolicyListCache(); + lCache.put(1, policies); } @Override public void updateAllPolicies(List policies) { - HashMap map = this.getTenantRelatedMap(); - map.clear(); - if (map.isEmpty()) { - for (Policy policy : policies) { - map.put(policy.getId(), policy); - } - } +//// HashMap map = this.getTenantRelatedMap(); +// Cache cache = getPolicyCache(); +// cache.removeAll(); +//// map.clear(); +//// if (map.isEmpty()) { +// for (Policy policy : policies) { +//// map.put(policy.getId(), policy); +// cache.put(policy.getId(), policy); +// } +//// } + + Cache> lCache = getPolicyListCache(); + lCache.removeAll(); + lCache.put(1, policies); } @Override public List getAllPolicies() throws PolicyManagementException { - HashMap map = this.getTenantRelatedMap(); - if (map.isEmpty()) { +// // HashMap map = this.getTenantRelatedMap(); +// Cache cache = getPolicyCache(); +// Iterator iterator = cache.iterator(); +//// iterator.hasNext() +// if (!iterator.hasNext()) { +// PolicyManager policyManager = new PolicyManagerImpl(); +// this.addAllPolicies(policyManager.getPolicies()); +// } +// if (log.isDebugEnabled()) { +// //log.debug("No of policies stored in the cache .. : " + map.size()); +// +// //Set keySet = map.keySet(); +// Iterator iterator2 = cache.iterator(); +// while (iterator2.hasNext()) { +// org.wso2.carbon.caching.impl.CacheEntry thisEntry = (org.wso2.carbon.caching.impl.CacheEntry) +// iterator2.next(); +// log.debug("Policy id in maps .. : " + thisEntry.getKey() + " policy name : " + ((Policy) thisEntry +// .getValue()). +// getPolicyName() + " Activated : " + ((Policy) thisEntry.getValue()).isActive()); +// } +// } +// +// List policies = new ArrayList<>(); +// while (iterator.hasNext()){ +// CacheEntry thisEntry = (CacheEntry) iterator.next(); +// policies.add((Policy) thisEntry.getValue()); +// } +// +// return policies; +//// return new ArrayList<>(map.values()); + + Cache> lCache = getPolicyListCache(); + if (!lCache.containsKey(1)) { PolicyManager policyManager = new PolicyManagerImpl(); this.addAllPolicies(policyManager.getPolicies()); } - if (log.isDebugEnabled()) { - log.debug("No of policies stored in the cache .. : " + map.size()); - Set keySet = map.keySet(); - for (Integer x : keySet) { - log.debug("Policy id in maps .. : " + map.get(x).getId() + " policy name : " + map.get(x). - getPolicyName() + " Activated : " + map.get(x).isActive()); + if (log.isDebugEnabled()) { + List cachedPolicy = lCache.get(1); + for (Policy policy : cachedPolicy) { + log.debug("Policy id in cache .. : " + policy.getId() + " policy name : " + policy. + getPolicyName() + " Activated : " + policy.isActive()); } } - return new ArrayList<>(map.values()); + + return lCache.get(1); + } @Override @@ -105,59 +159,136 @@ public class PolicyCacheManagerImpl implements PolicyCacheManager { @Override public void removeAllPolicies() { - HashMap map = this.getTenantRelatedMap(); - map.clear(); +// Cache cache = getPolicyCache(); +// cache.removeAll(); + //HashMap map = this.getTenantRelatedMap(); + //map.clear(); + + Cache> lCache = getPolicyListCache(); + lCache.removeAll(); } @Override public void addPolicy(Policy policy) { - HashMap map = this.getTenantRelatedMap(); - if (!map.containsKey(policy.getId())) { - map.put(policy.getId(), policy); - } else { - log.warn("Policy id (" + policy.getId() + ") already exist in the map. hence not attempted to store."); + //HashMap map = this.getTenantRelatedMap(); +// Cache cache = getPolicyCache(); +// if (!cache.containsKey(policy.getId())) { +// cache.put(policy.getId(), policy); +// } else { +// log.warn("Policy id (" + policy.getId() + ") already exist in the map. hence not attempted to store."); +// } + + Cache> lCache = getPolicyListCache(); + if (lCache.containsKey(1)) { + List cachedPolicy = lCache.get(1); + + for (Policy pol : cachedPolicy) { + if (pol.getId() == policy.getId()) { + return; + } + } + cachedPolicy.add(policy); } + } @Override public void updatePolicy(Policy policy) { - HashMap map = this.getTenantRelatedMap(); - if (map.containsKey(policy.getId())) { - map.remove(policy.getId()); - map.put(policy.getId(), policy); + // HashMap map = this.getTenantRelatedMap(); +// Cache cache = getPolicyCache(); +// if (cache.containsKey(policy.getId())) { +// cache.remove(policy.getId()); +// cache.put(policy.getId(), policy); +// } + + Cache> lCache = getPolicyListCache(); + if (lCache.containsKey(1)) { + List cachedPolicy = lCache.get(1); + Iterator iterator = cachedPolicy.iterator(); + while (iterator.hasNext()) { + Policy pol = (Policy) iterator.next(); + if (pol.getId() == policy.getId()) { + iterator.remove(); + break; + } + } + cachedPolicy.add(policy); + lCache.replace(1, cachedPolicy); } + } @Override public void updatePolicy(int policyId) throws PolicyManagementException { - HashMap map = this.getTenantRelatedMap(); - if (map.containsKey(policyId)) { - this.removePolicy(policyId); + // HashMap map = this.getTenantRelatedMap(); +// Cache cache = getPolicyCache(); +// if (cache.containsKey(policyId)) { +// this.removePolicy(policyId); +// } +// PolicyManager policyManager = new PolicyManagerImpl(); +// Policy policy = policyManager.getPolicy(policyId); +// cache.put(policyId, policy); + + Cache> lCache = getPolicyListCache(); + if (lCache.containsKey(1)) { + PolicyManager policyManager = new PolicyManagerImpl(); + Policy policy = policyManager.getPolicy(policyId); + this.updatePolicy(policy); } - PolicyManager policyManager = new PolicyManagerImpl(); - Policy policy = policyManager.getPolicy(policyId); - map.put(policyId, policy); + } @Override public void removePolicy(int policyId) { - HashMap map = this.getTenantRelatedMap(); - if (map.containsKey(policyId)) { - map.remove(policyId); - } else { - log.warn("Policy id (" + policyId + ") does not exist in the cache. Hence not removed."); + // HashMap map = this.getTenantRelatedMap(); +// Cache cache = getPolicyCache(); +// if (cache.containsKey(policyId)) { +// cache.remove(policyId); +// } else { +// log.warn("Policy id (" + policyId + ") does not exist in the cache. Hence not removed."); +// } + + Cache> lCache = getPolicyListCache(); + if (lCache.containsKey(1)) { + List cachedPolicy = lCache.get(1); + Iterator iterator = cachedPolicy.iterator(); + while (iterator.hasNext()) { + Policy pol = (Policy) iterator.next(); + if (pol.getId() == policyId) { + iterator.remove(); + break; + } + } + lCache.replace(1, cachedPolicy); } } @Override public Policy getPolicy(int policyId) throws PolicyManagementException { - HashMap map = this.getTenantRelatedMap(); - if (!map.containsKey(policyId)) { + //HashMap map = this.getTenantRelatedMap(); +// Cache cache = getPolicyCache(); +// if (!cache.containsKey(policyId)) { +// this.removeAllPolicies(); +// this.getAllPolicies(); +// } +// return cache.get(policyId); + + Cache> lCache = getPolicyListCache(); + if (!lCache.containsKey(1)) { this.removeAllPolicies(); this.getAllPolicies(); } - return map.get(policyId); + Policy policy = null; + List cachedPolicy = lCache.get(1); + Iterator iterator = cachedPolicy.iterator(); + while (iterator.hasNext()) { + Policy pol = (Policy) iterator.next(); + if (pol.getId() == policyId) { + policy = pol; + } + } + return policy; } @Override @@ -175,10 +306,10 @@ public class PolicyCacheManagerImpl implements PolicyCacheManager { return 0; } - private HashMap getTenantRelatedMap(){ + private HashMap getTenantRelatedMap() { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - if(!tenantedPolicyMap.containsKey(tenantId)){ + if (!tenantedPolicyMap.containsKey(tenantId)) { HashMap policyMap = new HashMap<>(); tenantedPolicyMap.put(tenantId, policyMap); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java index e8354b3e99..13a2df3442 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java @@ -39,6 +39,7 @@ public final class PolicyManagementConstants { public static final String DM_CACHE_MANAGER = "DM_CACHE_MANAGER"; public static final String DM_CACHE = "DM_CACHE"; + public static final String DM_CACHE_LIST = "DM_CACHE_LIST"; public static final String DELEGATION_TASK_TYPE = "DELEGATION__TASK"; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index 198913c055..ed14a3c6ea 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -31,6 +31,10 @@ import org.wso2.carbon.policy.mgt.common.ProfileFeature; import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.policy.mgt.core.config.datasource.JNDILookupDefinition; import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; +import org.wso2.carbon.registry.api.GhostResource; +import org.wso2.carbon.registry.core.RegistryConstants; +import org.wso2.carbon.registry.core.caching.RegistryCacheEntry; +import org.wso2.carbon.registry.core.caching.RegistryCacheKey; import javax.cache.Cache; import javax.cache.CacheManager; @@ -152,10 +156,28 @@ public class PolicyManagerUtil { } - public static Cache getCacheManagerImpl() { - return Caching.getCacheManagerFactory() - .getCacheManager(PolicyManagementConstants.DM_CACHE_MANAGER).getCache(PolicyManagementConstants - .DM_CACHE); +// public static Cache getCacheManagerImpl() { +// return Caching.getCacheManagerFactory() +// .getCacheManager(PolicyManagementConstants.DM_CACHE_MANAGER).getCache(PolicyManagementConstants +// .DM_CACHE); +// } + + + public static Cache getPolicyCache(String name){ + CacheManager manager = getCacheManager(); + return (manager != null) ? manager.getCache(name) : + Caching.getCacheManager().getCache(name); + } + + public static Cache> getPolicyListCache(String name){ + CacheManager manager = getCacheManager(); + return (manager != null) ? manager.>getCache(name) : + Caching.getCacheManager().>getCache(name); + } + + private static CacheManager getCacheManager() { + return Caching.getCacheManagerFactory().getCacheManager( + PolicyManagementConstants.DM_CACHE_MANAGER); }