You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by sh...@apache.org on 2021/07/20 10:32:15 UTC

[unomi] branch UNOMI-188-rule-event-type-optimization updated: UNOMI-188 Rule event type optimization - Fixed some issues with NullPointerException in the RestServer when it was restarting - Moved the update of the rules by event type into a separate method - Added the missing call to the postVisit method in visitConditions.

This is an automated email from the ASF dual-hosted git repository.

shuber pushed a commit to branch UNOMI-188-rule-event-type-optimization
in repository https://gitbox.apache.org/repos/asf/unomi.git


The following commit(s) were added to refs/heads/UNOMI-188-rule-event-type-optimization by this push:
     new 9ed34da  UNOMI-188 Rule event type optimization - Fixed some issues with NullPointerException in the RestServer when it was restarting - Moved the update of the rules by event type into a separate method - Added the missing call to the postVisit method in visitConditions.
9ed34da is described below

commit 9ed34da1f7e23afaa1dc73e1d15798817eb1a95f
Author: Serge Huber <sh...@jahia.com>
AuthorDate: Tue Jul 20 12:32:07 2021 +0200

    UNOMI-188 Rule event type optimization
    - Fixed some issues with NullPointerException in the RestServer when it was restarting
    - Moved the update of the rules by event type into a separate method
    - Added the missing call to the postVisit method in visitConditions.
---
 .../org/apache/unomi/rest/server/RestServer.java   | 14 ++++++++++---
 .../apache/unomi/services/impl/ParserHelper.java   |  2 +-
 .../services/impl/rules/RulesServiceImpl.java      | 23 ++++++++++++++--------
 3 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/rest/src/main/java/org/apache/unomi/rest/server/RestServer.java b/rest/src/main/java/org/apache/unomi/rest/server/RestServer.java
index d910012..c260ffe 100644
--- a/rest/src/main/java/org/apache/unomi/rest/server/RestServer.java
+++ b/rest/src/main/java/org/apache/unomi/rest/server/RestServer.java
@@ -125,6 +125,15 @@ public class RestServer {
             @Override
             public Object addingService(ServiceReference reference) {
                 Object serviceBean = bundleContext.getService(reference);
+                while (serviceBean == null) {
+                    logger.info("Waiting for service " + reference.getProperty("objectClass") + " to become available...");
+                    serviceBean = bundleContext.getService(reference);
+                    try {
+                        Thread.sleep(100);
+                    } catch (InterruptedException e) {
+                        logger.warn("Interrupted thread exception", e);
+                    }
+                }
                 logger.info("Registering JAX RS service " + serviceBean.getClass().getName());
                 serviceBeans.add(serviceBean);
                 timeOfLastUpdate = System.currentTimeMillis();
@@ -141,9 +150,8 @@ public class RestServer {
 
             @Override
             public void removedService(ServiceReference reference, Object service) {
-                Object serviceBean = bundleContext.getService(reference);
-                logger.info("Removing JAX RS service " + serviceBean.getClass().getName());
-                serviceBeans.remove(serviceBean);
+                logger.info("Removing JAX RS service " + service.getClass().getName());
+                serviceBeans.remove(service);
                 timeOfLastUpdate = System.currentTimeMillis();
                 refreshServer();
             }
diff --git a/services/src/main/java/org/apache/unomi/services/impl/ParserHelper.java b/services/src/main/java/org/apache/unomi/services/impl/ParserHelper.java
index 381786d..0b90ec5 100644
--- a/services/src/main/java/org/apache/unomi/services/impl/ParserHelper.java
+++ b/services/src/main/java/org/apache/unomi/services/impl/ParserHelper.java
@@ -81,7 +81,6 @@ public class ParserHelper {
 
             @Override
             public void postVisit(Condition condition) {
-
             }
         });
         return result;
@@ -105,6 +104,7 @@ public class ParserHelper {
                 }
             }
         }
+        visitor.postVisit(rootCondition);
     }
 
     public static boolean resolveActionTypes(DefinitionsService definitionsService, Rule rule) {
diff --git a/services/src/main/java/org/apache/unomi/services/impl/rules/RulesServiceImpl.java b/services/src/main/java/org/apache/unomi/services/impl/rules/RulesServiceImpl.java
index c314032..01ae985 100644
--- a/services/src/main/java/org/apache/unomi/services/impl/rules/RulesServiceImpl.java
+++ b/services/src/main/java/org/apache/unomi/services/impl/rules/RulesServiceImpl.java
@@ -36,7 +36,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
-import java.lang.reflect.Field;
 import java.net.URL;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
@@ -264,24 +263,32 @@ public class RulesServiceImpl implements RulesService, EventListenerService, Syn
 
     public void refreshRules() {
         try {
-            allRules = getAllRules();
+            // we use local variables to make sure we quickly switch the collections since the refresh is called often
+            // we want to avoid concurrency issues with the shared collections
+            List<Rule> newAllRules = getAllRules();
+            this.rulesByEventType = getRulesByEventType(newAllRules);
+            this.allRules = newAllRules;
         } catch (Throwable t) {
             logger.error("Error loading rules from persistence back-end", t);
         }
     }
 
     private List<Rule> getAllRules() {
-        List<Rule> allItems = persistenceService.getAllItems(Rule.class, 0, -1, "priority").getList();
-        Map<String,Set<Rule>> newRulesByEventType = new HashMap<>();
-        for (Rule rule : allItems) {
+        List<Rule> rules = persistenceService.getAllItems(Rule.class, 0, -1, "priority").getList();
+        for (Rule rule : rules) {
             ParserHelper.resolveConditionType(definitionsService, rule.getCondition(), "rule " + rule.getItemId());
-            updateRulesByEventType(newRulesByEventType, rule);
             ParserHelper.resolveActionTypes(definitionsService, rule);
         }
-        this.rulesByEventType = newRulesByEventType;
-        return allItems;
+        return rules;
     }
 
+    private Map<String,Set<Rule>> getRulesByEventType(List<Rule> rules) {
+        Map<String,Set<Rule>> newRulesByEventType = new HashMap<>();
+        for (Rule rule : rules) {
+            updateRulesByEventType(newRulesByEventType, rule);
+        }
+        return newRulesByEventType;
+    }
 
     public boolean canHandle(Event event) {
         return true;