Michael Kublin has uploaded a new change for review.

Change subject: engine: Introducing using of Infinispan cache at TimeoutBase
......................................................................

engine: Introducing using of Infinispan cache at TimeoutBase

The TimeoutBase was ysed for the following purpose:
1. The audit event which has expiration time was put to it internal cache
2. If the same event was tried to put again and it is expired, it was writtent 
to DB

Sound simple but a following implementation contains:
1. Memory leak - if we never tried to put event when it was already expired, 
internal cache
   never will be cleared
2. Restart - restart in the middle , event will be never send to DB

Solution:
I decided to use infinispan cache, from now if event has expiration time and it 
not contains in cache
it will be written to DB.
If event is contains in cache it will be skipped.
The cache has putIfAbsent() API which is similar to ConcurrentHashMap API, the 
entries are added to cache
with lifespan and will be cleared automatically by cache.

Change-Id: I2fbebb10c08c87d2c7fd4d7443d3e3b374541ed7
Signed-off-by: Michael Kublin <[email protected]>
---
M backend/manager/conf/standalone.xml
M backend/manager/modules/dal/pom.xml
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/TimeoutBase.java
M 
backend/manager/modules/dal/src/main/modules/org/ovirt/engine/core/dal/main/module.xml
M 
backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/TimeoutBaseTest.java
M backend/manager/modules/utils/pom.xml
M 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ejb/BeanType.java
M 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ejb/EngineEJBUtilsStrategy.java
M 
backend/manager/modules/utils/src/main/modules/org/ovirt/engine/core/utils/main/module.xml
M ear/pom.xml
M ear/src/main/resources/META-INF/MANIFEST.MF
M pom.xml
13 files changed, 100 insertions(+), 136 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/17/14117/1

diff --git a/backend/manager/conf/standalone.xml 
b/backend/manager/conf/standalone.xml
index 5067ceb..f573019 100644
--- a/backend/manager/conf/standalone.xml
+++ b/backend/manager/conf/standalone.xml
@@ -224,21 +224,12 @@
                 </thread-pool>
             </thread-pools>
         </subsystem>
-        <subsystem xmlns="urn:jboss:domain:infinispan:1.1" 
default-cache-container="hibernate">
-            <cache-container name="hibernate" default-cache="local-query">
-                <local-cache name="entity">
-                    <transaction mode="NON_XA"/>
-                    <eviction strategy="LRU" max-entries="10000"/>
-                    <expiration max-idle="100000"/>
-                </local-cache>
-                <local-cache name="local-query">
+        <subsystem xmlns="urn:jboss:domain:infinispan:1.1" 
default-cache-container="ovirt-engine">
+            <cache-container name="ovirt-engine" default-cache="timeout-base" 
jndi-name="java:jboss/infinispan/ovirt-engine" start="EAGER">
+                <local-cache name="timeout-base">
                     <transaction mode="NONE"/>
-                    <eviction strategy="LRU" max-entries="10000"/>
-                    <expiration max-idle="100000"/>
-                </local-cache>
-                <local-cache name="timestamps">
-                    <transaction mode="NONE"/>
-                    <eviction strategy="NONE"/>
+                    <eviction max-entries="10000"/>
+                    <expiration interval="60000"/>
                 </local-cache>
             </cache-container>
         </subsystem>
diff --git a/backend/manager/modules/dal/pom.xml 
b/backend/manager/modules/dal/pom.xml
index a0ed073..cfce673 100644
--- a/backend/manager/modules/dal/pom.xml
+++ b/backend/manager/modules/dal/pom.xml
@@ -85,6 +85,10 @@
       <artifactId>javassist</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+         <groupId>org.infinispan</groupId>
+         <artifactId>infinispan-core</artifactId>
+    </dependency>
   </dependencies>
   <build>
     <filters>
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java
index eebd957..f450f68 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java
@@ -806,57 +806,61 @@
         updateTimeoutLogableObject(auditLogable, logType);
 
         if (auditLogable.getLegal()) {
-            String message = null;
-            String resolvedMessage = null;
-            AuditLogSeverity severity = severities.get(logType);
-            if (severity == null) {
-                severity = AuditLogSeverity.NORMAL;
-                log.infoFormat("No severity for {0} audit log type, assuming 
Normal severity", logType);
-            }
-            AuditLog auditLog = null;
-            // handle external log messages invoked by plugins via the API
-            if (auditLogable.isExternal()) {
-                resolvedMessage = message = loggerString; // message is sent 
as an argument, no need to resolve.
-                auditLog = new AuditLog(logType,
-                        severity,
-                        resolvedMessage,
-                        auditLogable.getUserId(),
-                        auditLogable.getUserName(),
-                        auditLogable.getVmIdRef(),
-                        auditLogable.getVmName(),
-                        auditLogable.getVdsIdRef(),
-                        auditLogable.getVdsName(),
-                        auditLogable.getVmTemplateIdRef(),
-                        auditLogable.getVmTemplateName(),
-                        auditLogable.getOrigin(),
-                        auditLogable.getCustomEventId(),
-                        auditLogable.getEventFloodInSec(),
-                        auditLogable.getCustomData());
-            } else if ((message = messages.get(logType)) != null) { // 
Application log message from AuditLogMessages
-                resolvedMessage = resolveMessage(message, auditLogable);
-                auditLog = new AuditLog(logType, severity, resolvedMessage, 
auditLogable.getUserId(),
-                        auditLogable.getUserName(), auditLogable.getVmIdRef(), 
auditLogable.getVmName(),
-                        auditLogable.getVdsIdRef(), auditLogable.getVdsName(), 
auditLogable.getVmTemplateIdRef(),
-                        auditLogable.getVmTemplateName());
-            }
-            if (auditLog != null) {
-                
auditLog.setstorage_domain_id(auditLogable.getStorageDomainId());
-                
auditLog.setstorage_domain_name(auditLogable.getStorageDomainName());
-                auditLog.setstorage_pool_id(auditLogable.getStoragePoolId());
-                
auditLog.setstorage_pool_name(auditLogable.getStoragePoolName());
-                auditLog.setvds_group_id(auditLogable.getVdsGroupId());
-                auditLog.setvds_group_name(auditLogable.getVdsGroupName());
-                auditLog.setCorrelationId(auditLogable.getCorrelationId());
-                auditLog.setJobId(auditLogable.getJobId());
-                auditLog.setGlusterVolumeId(auditLogable.getGlusterVolumeId());
-                
auditLog.setGlusterVolumeName(auditLogable.getGlusterVolumeName());
-                auditLog.setExternal(auditLogable.isExternal());
-                auditLog.setQuotaId(auditLogable.getQuotaIdForLog());
-                auditLog.setQuotaName(auditLogable.getQuotaNameForLog());
-                getDbFacadeInstance().getAuditLogDao().save(auditLog);
-                if (!"".equals(loggerString)) {
-                    log.infoFormat(loggerString, resolvedMessage);
-                }
+            saveToDb(auditLogable, logType, loggerString);
+        }
+    }
+
+    public static void saveToDb(AuditLogableBase auditLogable, AuditLogType 
logType, String loggerString) {
+        String message = null;
+        String resolvedMessage = null;
+        AuditLogSeverity severity = severities.get(logType);
+        if (severity == null) {
+            severity = AuditLogSeverity.NORMAL;
+            log.infoFormat("No severity for {0} audit log type, assuming 
Normal severity", logType);
+        }
+        AuditLog auditLog = null;
+        // handle external log messages invoked by plugins via the API
+        if (auditLogable.isExternal()) {
+            resolvedMessage = message = loggerString; // message is sent as an 
argument, no need to resolve.
+            auditLog = new AuditLog(logType,
+                    severity,
+                    resolvedMessage,
+                    auditLogable.getUserId(),
+                    auditLogable.getUserName(),
+                    auditLogable.getVmIdRef(),
+                    auditLogable.getVmName(),
+                    auditLogable.getVdsIdRef(),
+                    auditLogable.getVdsName(),
+                    auditLogable.getVmTemplateIdRef(),
+                    auditLogable.getVmTemplateName(),
+                    auditLogable.getOrigin(),
+                    auditLogable.getCustomEventId(),
+                    auditLogable.getEventFloodInSec(),
+                    auditLogable.getCustomData());
+        } else if ((message = messages.get(logType)) != null) { // Application 
log message from AuditLogMessages
+            resolvedMessage = resolveMessage(message, auditLogable);
+            auditLog = new AuditLog(logType, severity, resolvedMessage, 
auditLogable.getUserId(),
+                    auditLogable.getUserName(), auditLogable.getVmIdRef(), 
auditLogable.getVmName(),
+                    auditLogable.getVdsIdRef(), auditLogable.getVdsName(), 
auditLogable.getVmTemplateIdRef(),
+                    auditLogable.getVmTemplateName());
+        }
+        if (auditLog != null) {
+            auditLog.setstorage_domain_id(auditLogable.getStorageDomainId());
+            
auditLog.setstorage_domain_name(auditLogable.getStorageDomainName());
+            auditLog.setstorage_pool_id(auditLogable.getStoragePoolId());
+            auditLog.setstorage_pool_name(auditLogable.getStoragePoolName());
+            auditLog.setvds_group_id(auditLogable.getVdsGroupId());
+            auditLog.setvds_group_name(auditLogable.getVdsGroupName());
+            auditLog.setCorrelationId(auditLogable.getCorrelationId());
+            auditLog.setJobId(auditLogable.getJobId());
+            auditLog.setGlusterVolumeId(auditLogable.getGlusterVolumeId());
+            auditLog.setGlusterVolumeName(auditLogable.getGlusterVolumeName());
+            auditLog.setExternal(auditLogable.isExternal());
+            auditLog.setQuotaId(auditLogable.getQuotaIdForLog());
+            auditLog.setQuotaName(auditLogable.getQuotaNameForLog());
+            getDbFacadeInstance().getAuditLogDao().save(auditLog);
+            if (!"".equals(loggerString)) {
+                log.infoFormat(loggerString, resolvedMessage);
             }
         }
     }
@@ -875,7 +879,7 @@
                 :
                 logType.getDuplicateEventsIntervalValue();
         if (duplicateEventsIntrvalValue > 0) {
-            auditLogable.setEndTime(System.currentTimeMillis() + 
TimeUnit.SECONDS.toMillis(duplicateEventsIntrvalValue));
+            
auditLogable.setEndTime(TimeUnit.SECONDS.toMillis(duplicateEventsIntrvalValue));
             auditLogable.setTimeoutObjectId(composeObjectId(auditLogable, 
logType));
         }
     }
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/TimeoutBase.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/TimeoutBase.java
index cca7820..3dfe67d 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/TimeoutBase.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/TimeoutBase.java
@@ -1,13 +1,12 @@
 package org.ovirt.engine.core.dal.dbbroker.auditloghandling;
 
 import java.io.Serializable;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import org.ovirt.engine.core.utils.cache.CacheManager;
 
 public abstract class TimeoutBase implements Serializable {
     private static final long serialVersionUID = -4969034051659487755L;
-    private static final Map<String, TimeoutBase> mHandler = new 
HashMap<String, TimeoutBase>();
-    private static final Object mLock = new Object();
     private boolean mUseTimeout;
     private long mEndTime = 0L;
 
@@ -48,28 +47,19 @@
      * Checks if timeout is used and if it is, checks the timeout. If no 
timeout set, then it will set this object as timeout.
      * @return should the action be logged again
      */
+    @SuppressWarnings("unchecked")
     public boolean getLegal() {
-        boolean returnValue = true;
         if (getUseTimout()) {
-            synchronized (mLock) {
-                String keyForCheck = getkeyForCheck();
-                TimeoutBase timeoutBase = mHandler.get(keyForCheck);
-
-                if (timeoutBase != null) {
-                    // not first try. check if timeout passed
-                    if (System.currentTimeMillis() < timeoutBase.getEndTime()) 
{
-                        returnValue = false;
-                    } else {
-                        // timeout over. Clean data
-                        mHandler.remove(keyForCheck);
-                    }
-                } else {
-                    // first try, add value
-                    mHandler.put(keyForCheck, this);
-                }
+            String keyForCheck = getkeyForCheck();
+            if (CacheManager.getTimeoutBaseCache().putIfAbsent(keyForCheck,
+                    this,
+                    this.getEndTime(),
+                    TimeUnit.MILLISECONDS) == null) {
+                return true;
             }
+            return false;
         }
 
-        return returnValue;
+        return true;
     }
 }
diff --git 
a/backend/manager/modules/dal/src/main/modules/org/ovirt/engine/core/dal/main/module.xml
 
b/backend/manager/modules/dal/src/main/modules/org/ovirt/engine/core/dal/main/module.xml
index 3ebd787..02eb7a8 100644
--- 
a/backend/manager/modules/dal/src/main/modules/org/ovirt/engine/core/dal/main/module.xml
+++ 
b/backend/manager/modules/dal/src/main/modules/org/ovirt/engine/core/dal/main/module.xml
@@ -14,6 +14,7 @@
       <module name="org.ovirt.engine.core.compat"/>
       <module name="org.ovirt.engine.core.dependencies"/>
       <module name="org.ovirt.engine.core.utils"/>
+      <module name="org.infinispan"/>
    </dependencies>
 
 </module>
diff --git 
a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/TimeoutBaseTest.java
 
b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/TimeoutBaseTest.java
index 75dc626..a402958 100644
--- 
a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/TimeoutBaseTest.java
+++ 
b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/TimeoutBaseTest.java
@@ -68,53 +68,6 @@
         assertTrue(result);
     }
 
-    @Test
-    public void legalFirstTime() {
-        final TestTimeoutBase t = new TestTimeoutBase();
-        t.setUseTimout(true);
-        final boolean result = t.getLegal();
-        assertTrue(result);
-    }
-
-    @Test
-    public void legalNullObjectId() {
-        final TestTimeoutBase t = new TestTimeoutBase();
-        t.setUseTimout(true);
-        t.setTimeoutObjectId(null);
-        final boolean result = t.getLegal();
-        assertTrue(result);
-    }
-
-    @Test
-    public void legalTimedOut() {
-        final TestTimeoutBase t = new TestTimeoutBase();
-        t.setUseTimout(true);
-        long c = System.currentTimeMillis();
-        c -= 1000;
-        t.setEndTime(c);
-        final String s = "legal-timeout";
-        t.setTimeoutObjectId(s);
-        // get it into the hashtable
-        t.getLegal();
-        final boolean result = t.getLegal();
-        assertTrue(result);
-    }
-
-    @Test
-    public void legalNotTimedOut() {
-        final TestTimeoutBase t = new TestTimeoutBase();
-        t.setUseTimout(true);
-        final String s = "illegal-timeout";
-        t.setTimeoutObjectId(s);
-        long c = System.currentTimeMillis();
-        c += 5000;
-        t.setEndTime(c);
-        // get it into the hashtable
-        t.getLegal();
-        final boolean result = t.getLegal();
-        assertFalse(result);
-    }
-
     public class TestTimeoutBase extends TimeoutBase {
 
         @Override
diff --git a/backend/manager/modules/utils/pom.xml 
b/backend/manager/modules/utils/pom.xml
index f98bf6e..a6942d2 100644
--- a/backend/manager/modules/utils/pom.xml
+++ b/backend/manager/modules/utils/pom.xml
@@ -133,6 +133,11 @@
       <artifactId>log4j</artifactId>
       <scope>provided</scope>
     </dependency>
+    
+    <dependency>
+         <groupId>org.infinispan</groupId>
+         <artifactId>infinispan-core</artifactId>
+    </dependency>
 
   </dependencies>
 
diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ejb/BeanType.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ejb/BeanType.java
index 8203e2d..1186080 100644
--- 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ejb/BeanType.java
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ejb/BeanType.java
@@ -11,6 +11,7 @@
     USERS_DOMAINS_CACHE,
     VDS_EVENT_LISTENER,
     LOCK_MANAGER,
-    EVENTQUEUE_MANAGER;
+    EVENTQUEUE_MANAGER,
+    CACHE_CONTAINER;
 
 }
diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ejb/EngineEJBUtilsStrategy.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ejb/EngineEJBUtilsStrategy.java
index 5659d31..4c37350 100644
--- 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ejb/EngineEJBUtilsStrategy.java
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ejb/EngineEJBUtilsStrategy.java
@@ -20,6 +20,7 @@
         addBeanJNDIName(BeanType.VDS_EVENT_LISTENER, 
ENGINE_CONTEXT_PREFIX.concat("bll/VdsEventListener"));
         addBeanJNDIName(BeanType.LOCK_MANAGER, 
ENGINE_CONTEXT_PREFIX.concat("bll/LockManager"));
         addBeanJNDIName(BeanType.EVENTQUEUE_MANAGER,  
ENGINE_CONTEXT_PREFIX.concat("bll/EventQueue"));
+        addBeanJNDIName(BeanType.CACHE_CONTAINER,  
"java:jboss/infinispan/ovirt-engine");
     }
 
     @Override
diff --git 
a/backend/manager/modules/utils/src/main/modules/org/ovirt/engine/core/utils/main/module.xml
 
b/backend/manager/modules/utils/src/main/modules/org/ovirt/engine/core/utils/main/module.xml
index 723b5b2..b89e075 100644
--- 
a/backend/manager/modules/utils/src/main/modules/org/ovirt/engine/core/utils/main/module.xml
+++ 
b/backend/manager/modules/utils/src/main/modules/org/ovirt/engine/core/utils/main/module.xml
@@ -21,6 +21,7 @@
       <module name="org.ovirt.engine.core.common"/>
       <module name="org.ovirt.engine.core.compat"/>
       <module name="org.ovirt.engine.core.dependencies"/>
+      <module name="org.infinispan"/>
    </dependencies>
 
 </module>
diff --git a/ear/pom.xml b/ear/pom.xml
index 6dc83378..1c47312 100644
--- a/ear/pom.xml
+++ b/ear/pom.xml
@@ -110,6 +110,12 @@
       <scope>provided</scope>
     </dependency>
 
+       <dependency>
+          <groupId>org.infinispan</groupId>
+          <artifactId>infinispan-core</artifactId>
+          <scope>provided</scope>
+       </dependency>
+
   </dependencies>
 
   <build>
diff --git a/ear/src/main/resources/META-INF/MANIFEST.MF 
b/ear/src/main/resources/META-INF/MANIFEST.MF
index b045a89..3686aef 100644
--- a/ear/src/main/resources/META-INF/MANIFEST.MF
+++ b/ear/src/main/resources/META-INF/MANIFEST.MF
@@ -10,6 +10,7 @@
  org.codehaus.jackson.jackson-mapper-asl,
  org.dom4j,
  org.hibernate.validator,
+ org.infinispan,
  org.ovirt.engine.core.common,
  org.ovirt.engine.core.compat,
  org.ovirt.engine.core.dal,
diff --git a/pom.xml b/pom.xml
index 7dd0ad9..a43ebf2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -88,6 +88,7 @@
     <jaxb-impl.version>2.2</jaxb-impl.version>
     <jbosssx-bare.version>2.0.4</jbosssx-bare.version>
     <log4j.version>1.2.16</log4j.version>
+    <infinispan.version>5.2.5.Final</infinispan.version>
   </properties>
   <dependencyManagement>
     <dependencies>
@@ -276,6 +277,11 @@
         <version>${javax.jstl.api.version}</version>
         <scope>provided</scope>
       </dependency>
+      <dependency>
+           <groupId>org.infinispan</groupId>
+           <artifactId>infinispan-core</artifactId>
+           <version>${infinispan.version}</version>
+      </dependency>
     </dependencies>
   </dependencyManagement>
   <dependencies>


--
To view, visit http://gerrit.ovirt.org/14117
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2fbebb10c08c87d2c7fd4d7443d3e3b374541ed7
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Michael Kublin <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to