Liran Zelkha has posted comments on this change.
Change subject: (WIP) Adding caching capabilities to database access
......................................................................
Patch Set 4: (21 inline comments)
Sorry it took so long to implement all changes
....................................................
File
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/StoragePool.java
Line 12: import org.ovirt.engine.core.compat.NGuid;
Line 13: import org.ovirt.engine.core.compat.Version;
Line 14:
Line 15: public class StoragePool extends IVdcQueryable implements
BusinessEntity<Guid> {
Line 16: public class storage_pool extends IVdcQueryable implements
BusinessEntity<Guid> {
True. Fixed in the next commit.
Line 17: private static final long serialVersionUID = 6162262095329980112L;
Line 18:
Line 19: private Guid id = new Guid();
Line 20:
....................................................
File
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/VdsDynamic.java
Line 13: import org.ovirt.engine.core.compat.StringHelper;
Line 14: import org.ovirt.engine.core.compat.Version;
Line 15: import org.ovirt.engine.core.compat.cache.TimeToLiveInCache;
Line 16:
Line 17: @TimeToLiveInCache(30)
Patch 5 introduces a DB table used to configure caching on a per entity level.
This annotation was removed.
Line 18: public class VdsDynamic implements BusinessEntity<Guid> {
Line 19: private static final long serialVersionUID = -6010035855157006935L;
Line 20:
Line 21: private Guid id;
....................................................
File
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/VmStatic.java
Line 11: import org.ovirt.engine.core.common.validation.group.UpdateEntity;
Line 12: import org.ovirt.engine.core.compat.Guid;
Line 13: import org.ovirt.engine.core.compat.cache.TimeToLiveInCache;
Line 14:
Line 15: @TimeToLiveInCache(3000)
Yes. Right now it's in the database, but 3000 for a static entity seems
reasonable.
Line 16: public class VmStatic extends VmBase {
Line 17: private static final long serialVersionUID = -2753306386502558044L;
Line 18:
Line 19: private Guid vmtGuid = new Guid();
....................................................
File
backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/cache/TimeToLiveInCache.java
Line 8: @Target(ElementType.TYPE)
Line 9: @Retention(RetentionPolicy.RUNTIME)
Line 10:
Line 11: public @interface TimeToLiveInCache {
Line 12: int value();
Removed in patchset 5.
Line 13:
....................................................
File
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/cache/CacheableJdbcTemplate.java
Line 18: import org.springframework.jdbc.core.RowMapper;
Line 19: import org.springframework.jdbc.core.SqlParameter;
Line 20: import org.springframework.jdbc.support.rowset.SqlRowSet;
Line 21:
Line 22: public class CacheableJdbcTemplate extends JdbcTemplate {
Done
Line 23:
Line 24: @SuppressWarnings("unused")
Line 25: private static final Log log = LogFactory
Line 26: .getLog(CacheableJdbcTemplate.class);
Line 108: return result;
Line 109: }
Line 110:
Line 111: @Override
Line 112: public long queryForLong(String sql) throws DataAccessException {
Not really - it's the definition of the Spring Jdbc spec...
Line 113: Long result;
Line 114: result = (Long) cacheManager.getFromCache(sql, null);
Line 115:
Line 116: if (result == null) {
Line 243: @Override
Line 244: public <T> List<T> query(String sql, PreparedStatementSetter pss,
Line 245: RowMapper<T> rowMapper) throws DataAccessException {
Line 246:
Line 247: List<T> result;
Done
Line 248: Object[] args = null;
Line 249:
Line 250: PreparedStatementMock psToCache = new PreparedStatementMock();
Line 251:
Line 252: try {
Line 253: pss.setValues(psToCache);
Line 254: args = psToCache.getArguments();
Line 255: } catch (SQLException e) {
Line 256: e.printStackTrace();
Done
Line 257: }
Line 258:
Line 259: result = (List<T>) cacheManager.getFromCache(sql, args);
Line 260:
....................................................
File
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/cache/CacheItemWrapper.java
Line 1: package org.ovirt.engine.core.dal.cache;
Line 2:
Line 3: public class CacheItemWrapper {
Not really, as we don't know that data type of the item we're caching (can be
any business entity, or any other object for that matter).
Line 4: private long cacheTime;
Line 5: private Object data;
Line 6:
Line 7: public CacheItemWrapper(long cacheTime, Object data) {
....................................................
File
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/cache/CacheManager.java
Line 12:
Line 13: @SuppressWarnings("rawtypes")
Line 14: public class CacheManager {
Line 15: private static final Log log = LogFactory
Line 16: .getLog(CacheableJdbcTemplate.class);
Good catch. Fixed.
Line 17: private static CacheManager instance = new CacheManager();
Line 18:
Line 19: private Map<CacheKey, CacheItemWrapper> cache = new
WeakHashMap<CacheKey, CacheItemWrapper>();
Line 20: private Map<Class, List<CacheKey>> typeToKeymapping = new
WeakHashMap<Class, List<CacheKey>>();
Line 13: @SuppressWarnings("rawtypes")
Line 14: public class CacheManager {
Line 15: private static final Log log = LogFactory
Line 16: .getLog(CacheableJdbcTemplate.class);
Line 17: private static CacheManager instance = new CacheManager();
Good catch. Fixed.
Line 18:
Line 19: private Map<CacheKey, CacheItemWrapper> cache = new
WeakHashMap<CacheKey, CacheItemWrapper>();
Line 20: private Map<Class, List<CacheKey>> typeToKeymapping = new
WeakHashMap<Class, List<CacheKey>>();
Line 21:
Line 26: public static CacheManager getInstance() {
Line 27: return instance;
Line 28: }
Line 29:
Line 30: public Object getFromCache(String sql, Object[] args) {
There is a ConfigValue to disable caching introduced in PatchSet 5. Open to
suggestions if you think code level refresh is also required.
Line 31:
Line 32: if (log.isDebugEnabled())
Line 33: log.debug("Getting from cache for query " + sql);
Line 34:
Line 28: }
Line 29:
Line 30: public Object getFromCache(String sql, Object[] args) {
Line 31:
Line 32: if (log.isDebugEnabled())
I'll use log.debugFormat
Line 33: log.debug("Getting from cache for query " + sql);
Line 34:
Line 35: CacheKey key = new CacheKey(sql, args);
Line 36:
Line 58: type = dataList.get(0).getClass();
Line 59: }
Line 60:
Line 61: // Check cache time vs object TimeToLiveInCache definition
Line 62: TimeToLiveInCache ttlic = (TimeToLiveInCache) type
Removed in PatchSet 5
Line 63: .getAnnotation(TimeToLiveInCache.class);
Line 64:
Line 65: // Calculate how long this object has been in cache
Line 66: long currentTime = System.currentTimeMillis();
Line 63: .getAnnotation(TimeToLiveInCache.class);
Line 64:
Line 65: // Calculate how long this object has been in cache
Line 66: long currentTime = System.currentTimeMillis();
Line 67: long diff = (currentTime - wrapper.getCacheTime()) / 1000;
1000 is for seconds, so I guess it is OK.I think using currentTime helps
understand the code, don't you?
Line 68:
Line 69: // Default behavior when no annotation exists is to disable
cache
Line 70: if (ttlic == null || ttlic.value() < diff) {
Line 71: if (log.isDebugEnabled())
Line 96: if (cacheKeys == null) {
Line 97: cacheKeys = new ArrayList<CacheKey>();
Line 98: }
Line 99: cacheKeys.add(key);
Line 100: typeToKeymapping.put(result.getClass(), cacheKeys);
True. Fixed.
Line 101:
Line 102: }
Line 103:
Line 104: // TODO: Should be synchronized for updatedType
Line 109: for (CacheKey key : cacheKeys) {
Line 110: cache.remove(key);
Line 111: }
Line 112: typeToKeymapping.remove(updatedType);
Line 113: }
Done
Line 114: }
Line 115:
....................................................
File
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/cache/PreparedStatementMock.java
Line 24: import java.util.ArrayList;
Line 25: import java.util.Calendar;
Line 26: import java.util.List;
Line 27:
Line 28: public class PreparedStatementMock implements PreparedStatement {
Fixed
Line 29: List<Object> params = new ArrayList<Object>();
Line 30:
Line 31: @Override
Line 32: public ResultSet executeQuery(String sql) throws SQLException {
....................................................
File
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/PostgresDbEngineDialect.java
Line 25:
Line 26: /**
Line 27: * Postgres db engine dialect.
Line 28: */
Line 29: public class PostgresDbEngineDialect implements DbEngineDialect {
All changes were moved to http://gerrit.ovirt.org/#/c/15039
Line 30: @SuppressWarnings("unused")
Line 31: private static final Log log = LogFactory
Line 32: .getLog(PostgresDbEngineDialect.class);
Line 33: /**
....................................................
File
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/SimpleJdbcCallsHandler.java
Line 99: private CallCreator createCallForModification(final String
procedureName) {
Line 100: return new CallCreator() {
Line 101: @Override
Line 102: public SimpleJdbcCall createCall() {
Line 103: // return new
SimpleJdbcCall(template).withProcedureName(procedureName);
Fixed
Line 104: return new
CacheableJdbcCall(template).withProcedureName(procedureName);
Line 105: }
Line 106: };
Line 107: }
Line 100: return new CallCreator() {
Line 101: @Override
Line 102: public SimpleJdbcCall createCall() {
Line 103: // return new
SimpleJdbcCall(template).withProcedureName(procedureName);
Line 104: return new
CacheableJdbcCall(template).withProcedureName(procedureName);
We plan to introduce an ability to clean the cache once modification on an item
was performed
Line 105: }
Line 106: };
Line 107: }
Line 108:
--
To view, visit http://gerrit.ovirt.org/14188
To unsubscribe, visit http://gerrit.ovirt.org/settings
Gerrit-MessageType: comment
Gerrit-Change-Id: I04d7edea6a52626c68f88362416abdb025e4b026
Gerrit-PatchSet: 4
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Liran Zelkha <[email protected]>
Gerrit-Reviewer: Allon Mureinik <[email protected]>
Gerrit-Reviewer: Eli Mesika <[email protected]>
Gerrit-Reviewer: Gilad Chaplik <[email protected]>
Gerrit-Reviewer: Laszlo Hornyak <[email protected]>
Gerrit-Reviewer: Liran Zelkha <[email protected]>
Gerrit-Reviewer: Maor Lipchuk <[email protected]>
Gerrit-Reviewer: Michael Kublin <[email protected]>
Gerrit-Reviewer: Mike Kolesnik <[email protected]>
Gerrit-Reviewer: Roy Golan <[email protected]>
Gerrit-Reviewer: Tal Nisan <[email protected]>
Gerrit-Reviewer: Yair Zaslavsky <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches