Liran Zelkha has uploaded a new change for review. Change subject: engine: Add caching capabilities to DAOs ......................................................................
engine: Add caching capabilities to DAOs This patch creates the ability to cache DAOs, and specifically VdsStatic, VdsGroup and vds_spm_id_map. Change-Id: Id3354858d67f20661363cccc60380119c30e12ba Bug-Url: https://bugzilla.redhat.com/?????? Signed-off-by: [email protected] <[email protected]> --- A backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/CachedEntity.java M backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/VDSGroup.java M backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/VdsStatic.java M backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/vds_spm_id_map.java A backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/EntityCache.java A backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CachedDao.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsDAODbFacadeImpl.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsGroupDAO.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsGroupDAODbFacadeImpl.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsSpmIdMapDAO.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsSpmIdMapDAODbFacadeImpl.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsStaticDAO.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsStaticDAODbFacadeImpl.java M frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml 14 files changed, 201 insertions(+), 21 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/52/21952/1 diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/CachedEntity.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/CachedEntity.java new file mode 100644 index 0000000..9b7368e --- /dev/null +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/CachedEntity.java @@ -0,0 +1,9 @@ +package org.ovirt.engine.core.common.businessentities; + + +public interface CachedEntity { + public long getExpiration(); + + public static final long LONG_LIVED_OBJECT = 5 * 60 * 1000; + public static final long SHORT_LIVED_OBJECT = 1 * 60 * 1000; +} diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/VDSGroup.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/VDSGroup.java index c8c8883..130d153 100644 --- a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/VDSGroup.java +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/VDSGroup.java @@ -16,7 +16,7 @@ @ValidVdsGroup(groups = { CreateEntity.class }) -public class VDSGroup extends IVdcQueryable implements Serializable, BusinessEntity<Guid>, HasStoragePool<Guid>, Nameable, Commented { +public class VDSGroup extends IVdcQueryable implements Serializable, BusinessEntity<Guid>, HasStoragePool<Guid>, Nameable, Commented, CachedEntity { private static final long serialVersionUID = 5659359762655478095L; @@ -337,4 +337,8 @@ && detectEmulatedMachine == other.detectEmulatedMachine); } + @Override + public long getExpiration() { + return CachedEntity.LONG_LIVED_OBJECT; + } } diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/VdsStatic.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/VdsStatic.java index 08c61fd..71f77e0 100644 --- a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/VdsStatic.java +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/VdsStatic.java @@ -18,7 +18,7 @@ import org.ovirt.engine.core.common.validation.group.UpdateEntity; import org.ovirt.engine.core.compat.Guid; -public class VdsStatic implements BusinessEntity<Guid>, Commented { +public class VdsStatic implements BusinessEntity<Guid>, Commented, CachedEntity { private static final long serialVersionUID = -1425566208615075937L; private static final int HOST_DEFAULT_SPM_PRIORITY = 5; @@ -600,4 +600,9 @@ && vdsType == other.vdsType && ObjectUtils.objectsEqual(sshKeyFingerprint, other.sshKeyFingerprint)); } + + @Override + public long getExpiration() { + return CachedEntity.LONG_LIVED_OBJECT; + } } diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/vds_spm_id_map.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/vds_spm_id_map.java index d9552c3..8b6f521 100644 --- a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/vds_spm_id_map.java +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/vds_spm_id_map.java @@ -5,7 +5,7 @@ import org.ovirt.engine.core.common.utils.ObjectUtils; import org.ovirt.engine.core.compat.Guid; -public class vds_spm_id_map implements Serializable, BusinessEntity<Guid> { +public class vds_spm_id_map implements Serializable, BusinessEntity<Guid>, CachedEntity { private static final long serialVersionUID = 308472653338744675L; public vds_spm_id_map() { @@ -76,4 +76,9 @@ && ObjectUtils.objectsEqual(vds_idField, other.vds_idField) && vds_spm_idField == other.vds_spm_idField); } + + @Override + public long getExpiration() { + return CachedEntity.LONG_LIVED_OBJECT; + } } diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/EntityCache.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/EntityCache.java new file mode 100644 index 0000000..f45bfd2 --- /dev/null +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/EntityCache.java @@ -0,0 +1,133 @@ +package org.ovirt.engine.core.dal; + +import java.util.HashMap; +import java.util.Map; + +import org.ovirt.engine.core.common.businessentities.CachedEntity; +import org.ovirt.engine.core.compat.Guid; +import org.ovirt.engine.core.dao.ReadDao; + +public class EntityCache { + + private static final EntityCache instance = new EntityCache(); + private Map<EntityCacheKey, EntityCacheItem> cache = new HashMap<EntityCacheKey, EntityCacheItem>(); + + private EntityCache() { + + } + + public static EntityCache getInstance() { + return instance; + } + + public <T extends CachedEntity> T getFromCache(Guid id, ReadDao dao) { + EntityCacheKey key = new EntityCacheKey(id, dao); + T instance = EntityCache.getInstance().get(key); + if (instance == null) { + instance = (T) dao.get(id); + EntityCache.getInstance().put(key, instance); + } + return instance; + + } + + private <T extends CachedEntity> T get(EntityCacheKey key) { + EntityCacheItem item = cache.get(key); + + if (item == null) + return null; + + if (System.currentTimeMillis() - item.getPlacedInCache() > item.getEntity().getExpiration()) + return null; + + return (T) item.getEntity(); + } + + private void put(EntityCacheKey key, CachedEntity entity) { + cache.put(key, new EntityCacheItem(entity)); + } + + private class EntityCacheItem { + private CachedEntity entity; + private long placedInCache; + + public EntityCacheItem(CachedEntity entity) { + this.entity = entity; + placedInCache = System.currentTimeMillis(); + } + + public CachedEntity getEntity() { + return entity; + } + + public long getPlacedInCache() { + return placedInCache; + } + } + + private class EntityCacheKey { + private Guid key; + private ReadDao dao; + + public EntityCacheKey(Guid key, ReadDao dao) { + super(); + this.key = key; + this.dao = dao; + } + + public Guid getKey() { + return key; + } + + public void setKey(Guid key) { + this.key = key; + } + + public ReadDao getDao() { + return dao; + } + + public void setDao(ReadDao dao) { + this.dao = dao; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + getOuterType().hashCode(); + result = prime * result + ((dao == null) ? 0 : dao.getClass().hashCode()); + result = prime * result + ((key == null) ? 0 : key.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + EntityCacheKey other = (EntityCacheKey) obj; + if (!getOuterType().equals(other.getOuterType())) + return false; + if (dao == null) { + if (other.dao != null) + return false; + } else if (!dao.getClass().equals(other.dao.getClass())) + return false; + if (key == null) { + if (other.key != null) + return false; + } else if (!key.equals(other.key)) + return false; + return true; + } + + private EntityCache getOuterType() { + return EntityCache.this; + } + + } +} diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CachedDao.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CachedDao.java new file mode 100644 index 0000000..caa5b44 --- /dev/null +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CachedDao.java @@ -0,0 +1,16 @@ +package org.ovirt.engine.core.dao; + +import java.io.Serializable; + +import org.ovirt.engine.core.common.businessentities.BusinessEntity; + +public interface CachedDao<T extends BusinessEntity<ID>, ID extends Serializable> { + /** + * Retrieves the entity with the given id from the cache. + * @param id + * The id to look by (can't be <code>null</code>). + * @return The entity instance, or <code>null</code> if not found. + */ + public T getFromCache(ID id); + +} diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsDAODbFacadeImpl.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsDAODbFacadeImpl.java index c0b1ac8..6213781 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsDAODbFacadeImpl.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsDAODbFacadeImpl.java @@ -350,12 +350,9 @@ public VDS getPartial(Guid vdsId) { VDS vdsPartial = new VDS(); - // TODO: A future patch will load this from cache - vdsPartial.setStaticData(DbFacade.getInstance().getVdsStaticDao().get(vdsId)); + vdsPartial.setStaticData(DbFacade.getInstance().getVdsStaticDao().getFromCache(vdsId)); vdsPartial.setDynamicData(DbFacade.getInstance().getVdsDynamicDao().get(vdsId)); - - // TODO: A future patch will load this from cache - VDSGroup group = DbFacade.getInstance().getVdsGroupDao().get(vdsPartial.getVdsGroupId()); + VDSGroup group = DbFacade.getInstance().getVdsGroupDao().getFromCache(vdsPartial.getVdsGroupId()); vdsPartial.setVdsGroupCompatibilityVersion(group.getcompatibility_version()); vdsPartial.setVdsGroupCpuName(group.getcpu_name()); vdsPartial.setVdsGroupDescription(group.getdescription()); @@ -370,9 +367,7 @@ @Override public void reloadPartial(VDS vds) { vds.setStatisticsData(DbFacade.getInstance().getVdsStatisticsDao().get(vds.getId())); - - // TODO: A future patch will load this from cache - vds.setVdsSpmId(DbFacade.getInstance().getVdsSpmIdMapDao().get(vds.getId()).getvds_spm_id()); + vds.setVdsSpmId(DbFacade.getInstance().getVdsSpmIdMapDao().getFromCache(vds.getId()).getvds_spm_id()); } } diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsGroupDAO.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsGroupDAO.java index f4479dc..4a0e79b 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsGroupDAO.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsGroupDAO.java @@ -10,7 +10,7 @@ * <code>VdsGroupDAO</code> defines a type that performs CRUD operations on instances of {@link VDSGroup}. * */ -public interface VdsGroupDAO extends DAO, SearchDAO<VDSGroup> { +public interface VdsGroupDAO extends ReadDao<VDSGroup, Guid>, SearchDAO<VDSGroup>, CachedDao<VDSGroup, Guid> { /** * Gets the group with the specified id. * @@ -95,13 +95,6 @@ * @return the list of groups */ List<VDSGroup> getAllForStoragePool(Guid id, Guid userID, boolean isFiltered); - - /** - * Retrieves all VDS groups. - * - * @return the list of groups - */ - List<VDSGroup> getAll(); /** * Retrieves all VDS groups. diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsGroupDAODbFacadeImpl.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsGroupDAODbFacadeImpl.java index af94bab..e50af61 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsGroupDAODbFacadeImpl.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsGroupDAODbFacadeImpl.java @@ -10,6 +10,7 @@ import org.ovirt.engine.core.common.businessentities.VDSGroup; import org.ovirt.engine.core.compat.Guid; import org.ovirt.engine.core.compat.Version; +import org.ovirt.engine.core.dal.EntityCache; import org.ovirt.engine.core.dal.dbbroker.DbFacadeUtils; import org.ovirt.engine.core.utils.SerializationFactory; import org.springframework.jdbc.core.RowMapper; @@ -234,4 +235,9 @@ getCustomMapSqlParameterSource() .addValue("cluster_policy_id", clusterPolicyId)); } + + @Override + public VDSGroup getFromCache(Guid id) { + return EntityCache.getInstance().getFromCache(id, this); + } } diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsSpmIdMapDAO.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsSpmIdMapDAO.java index 7423ccb..62b733b 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsSpmIdMapDAO.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsSpmIdMapDAO.java @@ -8,7 +8,7 @@ /** * <code>VdsSpmIdMapDAO</code> defines a type that performs CRUD operations on instances of {@link vds_spm_iso_map}. */ -public interface VdsSpmIdMapDAO extends GenericDao<vds_spm_id_map, Guid> { +public interface VdsSpmIdMapDAO extends GenericDao<vds_spm_id_map, Guid>, CachedDao<vds_spm_id_map, Guid> { /** * Gets the map for a given storage pool and vds id * diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsSpmIdMapDAODbFacadeImpl.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsSpmIdMapDAODbFacadeImpl.java index a61fc62..e90aae9 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsSpmIdMapDAODbFacadeImpl.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsSpmIdMapDAODbFacadeImpl.java @@ -7,6 +7,7 @@ import org.apache.commons.lang.NotImplementedException; import org.ovirt.engine.core.common.businessentities.vds_spm_id_map; import org.ovirt.engine.core.compat.Guid; +import org.ovirt.engine.core.dal.EntityCache; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; @@ -89,4 +90,10 @@ return entity; } } + + @Override + public vds_spm_id_map getFromCache(Guid id) { + return EntityCache.getInstance().getFromCache(id, this); + } + } diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsStaticDAO.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsStaticDAO.java index d24b406..5b13e79 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsStaticDAO.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsStaticDAO.java @@ -10,7 +10,7 @@ * * */ -public interface VdsStaticDAO extends GenericDao<VdsStatic, Guid> { +public interface VdsStaticDAO extends GenericDao<VdsStatic, Guid>, CachedDao<VdsStatic, Guid> { /** * Retrieves the instance for the given host name. * diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsStaticDAODbFacadeImpl.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsStaticDAODbFacadeImpl.java index eb42f14..254c0a1 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsStaticDAODbFacadeImpl.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VdsStaticDAODbFacadeImpl.java @@ -8,6 +8,7 @@ import org.ovirt.engine.core.common.businessentities.VDSType; import org.ovirt.engine.core.common.businessentities.VdsStatic; import org.ovirt.engine.core.compat.Guid; +import org.ovirt.engine.core.dal.EntityCache; import org.ovirt.engine.core.dal.dbbroker.DbFacadeUtils; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; @@ -161,4 +162,9 @@ } } + @Override + public VdsStatic getFromCache(Guid id) { + return EntityCache.getInstance().getFromCache(id, this); + } + } diff --git a/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml b/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml index f9cddc6..5bc64dc 100644 --- a/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml +++ b/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml @@ -48,6 +48,7 @@ <include name="common/businessentities/OpenStackImageProviderProperties.java" /> <include name="common/businessentities/VmBalloonInfo.java" /> <include name="common/businessentities/ArchitectureType.java" /> + <include name="common/businessentities/CachedEntity.java" /> <!-- Network business entities --> <include name="common/businessentities/network/VdsNetworkInterface.java" /> -- To view, visit http://gerrit.ovirt.org/21952 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Id3354858d67f20661363cccc60380119c30e12ba Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Liran Zelkha <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
