jkevan commented on a change in pull request #237:
URL: https://github.com/apache/unomi/pull/237#discussion_r576383862
##########
File path:
services/src/main/java/org/apache/unomi/services/impl/segments/SegmentServiceImpl.java
##########
@@ -919,74 +943,93 @@ private void updateExistingProfilesForSegment(Segment
segment) {
profilesToRemoveSubConditions.add(notNewSegmentCondition);
profilesToRemoveCondition.setParameter("subConditions",
profilesToRemoveSubConditions);
- PartialList<Profile> profilesToRemove =
persistenceService.query(profilesToRemoveCondition, null, Profile.class, 0,
segmentUpdateBatchSize, "10m");
- PartialList<Profile> profilesToAdd =
persistenceService.query(profilesToAddCondition, null, Profile.class, 0,
segmentUpdateBatchSize, "10m");
-
- while (profilesToAdd.getList().size() > 0) {
- long profilesToAddStartTime = System.currentTimeMillis();
- for (Profile profileToAdd : profilesToAdd.getList()) {
- profileToAdd.getSegments().add(segment.getItemId());
- Map<String,Object> sourceMap = new HashMap<>();
- sourceMap.put("segments", profileToAdd.getSegments());
- profileToAdd.setSystemProperty("lastUpdated", new Date());
- sourceMap.put("systemProperties",
profileToAdd.getSystemProperties());
- persistenceService.update(profileToAdd, null,
Profile.class, sourceMap);
- Event profileUpdated = new Event("profileUpdated", null,
profileToAdd, null, null, profileToAdd, new Date());
- profileUpdated.setPersistent(false);
- eventService.send(profileUpdated);
- updatedProfileCount++;
- }
- logger.info("{} profiles added to segment in {}ms",
profilesToAdd.size(), System.currentTimeMillis() - profilesToAddStartTime);
- profilesToAdd =
persistenceService.continueScrollQuery(Profile.class,
profilesToAdd.getScrollIdentifier(), profilesToAdd.getScrollTimeValidity());
- if (profilesToAdd == null || profilesToAdd.getList().size() ==
0) {
- break;
- }
+ updatedProfileCount +=
updateProfilesSegment(profilesToAddCondition, segmentId, true);
+ updatedProfileCount +=
updateProfilesSegment(profilesToRemoveCondition, segmentId, false);
+ } else {
+ updatedProfileCount += updateProfilesSegment(segmentCondition,
segmentId, false);
+ }
+ logger.info("{} profiles updated in {}ms", updatedProfileCount,
System.currentTimeMillis() - updateProfilesForSegmentStartTime);
+ }
+
+ private long updateProfilesSegment(Condition profilesToUpdateCondition,
String segmentId, boolean isAdd){
+ long updatedProfileCount= 0;
+ PartialList<Profile> profiles =
persistenceService.query(profilesToUpdateCondition, null, Profile.class, 0,
segmentUpdateBatchSize, "10m");
+
+ while (profiles != null && profiles.getList().size() > 0) {
+ long startTime = System.currentTimeMillis();
+ if (batchSegmentProfileUpdate) {
+ batchUpdateProfilesSegment(segmentId, profiles.getList(),
isAdd);
}
- while (profilesToRemove.getList().size() > 0) {
- long profilesToRemoveStartTime = System.currentTimeMillis();
- for (Profile profileToRemove : profilesToRemove.getList()) {
- profileToRemove.getSegments().remove(segment.getItemId());
- Map<String,Object> sourceMap = new HashMap<>();
- sourceMap.put("segments", profileToRemove.getSegments());
- profileToRemove.setSystemProperty("lastUpdated", new
Date());
- sourceMap.put("systemProperties",
profileToRemove.getSystemProperties());
- persistenceService.update(profileToRemove, null,
Profile.class, sourceMap);
- Event profileUpdated = new Event("profileUpdated", null,
profileToRemove, null, null, profileToRemove, new Date());
- profileUpdated.setPersistent(false);
- eventService.send(profileUpdated);
- updatedProfileCount++;
- }
- logger.info("{} profiles removed from segment in {}ms",
profilesToRemove.size(), System.currentTimeMillis() - profilesToRemoveStartTime
);
- profilesToRemove =
persistenceService.continueScrollQuery(Profile.class,
profilesToRemove.getScrollIdentifier(),
profilesToRemove.getScrollTimeValidity());
- if (profilesToRemove == null ||
profilesToRemove.getList().size() == 0) {
- break;
+ else { //send update profile one by one
+ for (Profile profileToUpdate : profiles.getList()) {
+ Map<String, Object> sourceMap =
buildPropertiesMapForUpdateSegment(profileToUpdate, segmentId, isAdd);
+ persistenceService.update(profileToUpdate, null,
Profile.class, sourceMap);
}
}
+ if (sendProfileUpdateEventForSegmentUpdate)
+ sendProfileUpdatedEvent(profiles.getList());
- } else {
- PartialList<Profile> profilesToRemove =
persistenceService.query(segmentCondition, null, Profile.class, 0, 200, "10m");
- while (profilesToRemove.getList().size() > 0) {
- long profilesToRemoveStartTime = System.currentTimeMillis();
- for (Profile profileToRemove : profilesToRemove.getList()) {
- profileToRemove.getSegments().remove(segment.getItemId());
- Map<String,Object> sourceMap = new HashMap<>();
- sourceMap.put("segments", profileToRemove.getSegments());
- profileToRemove.setSystemProperty("lastUpdated", new
Date());
- sourceMap.put("systemProperties",
profileToRemove.getSystemProperties());
- persistenceService.update(profileToRemove, null,
Profile.class, sourceMap);
- Event profileUpdated = new Event("profileUpdated", null,
profileToRemove, null, null, profileToRemove, new Date());
- profileUpdated.setPersistent(false);
- eventService.send(profileUpdated);
- updatedProfileCount++;
- }
- logger.info("{} profiles removed from segment in {}ms",
profilesToRemove.size(), System.currentTimeMillis() -
profilesToRemoveStartTime);
- profilesToRemove =
persistenceService.continueScrollQuery(Profile.class,
profilesToRemove.getScrollIdentifier(),
profilesToRemove.getScrollTimeValidity());
- if (profilesToRemove == null ||
profilesToRemove.getList().size() == 0) {
- break;
- }
- }
+ updatedProfileCount += profiles.size();
Review comment:
since the retry is synchron we could also get the failed ids from the
retry, and send the events for only successful updated events, and udpate the
count accordingly.
In term of implementation, event it was the case before it's sounds not
correct to considered a failed update profile as counted in the number of
profiled updated, and also send the updated event for a profile that was not
updated.
But again, since it was already the case before, we can keep this behavior.
it could be done in a separate unomi PR and ticket.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]