You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by jk...@apache.org on 2022/06/01 10:09:41 UTC

[unomi] branch optimizeProfileQueryDuringSegmentRemove created (now 85212a837)

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

jkevan pushed a change to branch optimizeProfileQueryDuringSegmentRemove
in repository https://gitbox.apache.org/repos/asf/unomi.git


      at 85212a837 UNOMI-557: Optimize the profile update during segment deletion

This branch includes the following new commits:

     new 85212a837 UNOMI-557: Optimize the profile update during segment deletion

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[unomi] 01/01: UNOMI-557: Optimize the profile update during segment deletion

Posted by jk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jkevan pushed a commit to branch optimizeProfileQueryDuringSegmentRemove
in repository https://gitbox.apache.org/repos/asf/unomi.git

commit 85212a8373932b6b36eb4b8b03e687e12373eb34
Author: Kevan <ke...@jahia.com>
AuthorDate: Wed Jun 1 12:09:22 2022 +0200

    UNOMI-557: Optimize the profile update during segment deletion
---
 .../java/org/apache/unomi/itests/SegmentIT.java    | 33 ++++++++++++++++++++++
 .../services/impl/segments/SegmentServiceImpl.java | 16 +----------
 2 files changed, 34 insertions(+), 15 deletions(-)

diff --git a/itests/src/test/java/org/apache/unomi/itests/SegmentIT.java b/itests/src/test/java/org/apache/unomi/itests/SegmentIT.java
index 221f975ac..17698fad7 100644
--- a/itests/src/test/java/org/apache/unomi/itests/SegmentIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/SegmentIT.java
@@ -161,6 +161,39 @@ public class SegmentIT extends BaseIT {
         segmentService.removeSegmentDefinition(SEGMENT_ID, false);
     }
 
+    @Test
+    public void testProfileEngagedSegmentAddedRemoved() throws InterruptedException {
+        // create Profile
+        Profile profile = new Profile();
+        profile.setItemId("test_profile_id");
+        profile.setProperty("age", 42);
+        profileService.save(profile);
+        persistenceService.refreshIndex(Profile.class, null);
+
+        keepTrying("Profile should not be engaged in the segment yet", () -> profileService.load("test_profile_id"),
+                loadedProfile -> loadedProfile != null && (loadedProfile.getSegments() == null || !loadedProfile.getSegments().contains("add-delete-segment-test")), 1000, 20);
+
+        // create the segment
+        Metadata segmentMetadata = new Metadata("add-delete-segment-test");
+        Segment segment = new Segment(segmentMetadata);
+        Condition segmentCondition = new Condition(definitionsService.getConditionType("profilePropertyCondition"));
+        segmentCondition.setParameter("propertyName", "properties.age");
+        segmentCondition.setParameter("comparisonOperator", "exists");
+        segment.setCondition(segmentCondition);
+        segmentService.setSegmentDefinition(segment);
+
+        // insure the profile that did the past event condition is correctly engaged in the segment.
+        keepTrying("Profile should be engaged in the segment", () -> profileService.load("test_profile_id"),
+                updatedProfile -> updatedProfile.getSegments().contains("add-delete-segment-test"), 1000, 20);
+
+        // delete the segment
+        segmentService.removeSegmentDefinition("add-delete-segment-test", false);
+
+        // insure the profile is not engaged anymore after segment deleted
+        keepTrying("Profile should not be engaged in the segment anymore after the segment have been deleted", () -> profileService.load("test_profile_id"),
+                loadedProfile -> loadedProfile != null && (loadedProfile.getSegments() == null || !loadedProfile.getSegments().contains("add-delete-segment-test")), 1000, 20);
+    }
+
     @Test
     public void testSegmentWithPastEventCondition() throws InterruptedException {
         // create Profile
diff --git a/services/src/main/java/org/apache/unomi/services/impl/segments/SegmentServiceImpl.java b/services/src/main/java/org/apache/unomi/services/impl/segments/SegmentServiceImpl.java
index dc1333b42..6a86a8c44 100644
--- a/services/src/main/java/org/apache/unomi/services/impl/segments/SegmentServiceImpl.java
+++ b/services/src/main/java/org/apache/unomi/services/impl/segments/SegmentServiceImpl.java
@@ -391,21 +391,7 @@ public class SegmentServiceImpl extends AbstractServiceImpl implements SegmentSe
             segmentCondition.setParameter("propertyName", "segments");
             segmentCondition.setParameter("comparisonOperator", "equals");
             segmentCondition.setParameter("propertyValue", segmentId);
-
-            List<Profile> previousProfiles = persistenceService.query(segmentCondition, null, Profile.class);
-            long updatedProfileCount = 0;
-            long profileRemovalStartTime = System.currentTimeMillis();
-            if (batchSegmentProfileUpdate && previousProfiles.size() > 0) {
-                batchUpdateProfilesSegment(segmentId, previousProfiles, false);
-            } else {
-                for (Profile profileToRemove : previousProfiles) {
-                    Map<String, Object> sourceMap = buildPropertiesMapForUpdateSegment(profileToRemove, segmentId, false);
-                    persistenceService.update(profileToRemove, null, Profile.class, sourceMap);
-                }
-            }
-
-            updatedProfileCount += previousProfiles.size();
-            logger.info("Removed segment from {} profiles in {} ms", updatedProfileCount, System.currentTimeMillis() - profileRemovalStartTime);
+            updateProfilesSegment(segmentCondition, segmentId, false);
 
             // update impacted segments
             for (Segment segment : impactedSegments) {