Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor detectMultipleHolderNames for efficient use of RAM. #7008

Merged
merged 1 commit into from Feb 25, 2024
Merged

Refactor detectMultipleHolderNames for efficient use of RAM. #7008

merged 1 commit into from Feb 25, 2024

Conversation

ghost
Copy link

@ghost ghost commented Jan 24, 2024

Fixes #7006 (Out Of Memory Exception thrown)

Purpose of the method is to (a) maintain a list of flagged disputes, and (b) notify listeners if new items were added.
Using toString() on a large map of String to List caused an OOM exception.
For (b) notifying listeners it is only the change in flagged dispute count that is relevant (so the UI can display an alert icon). The same effect can be obtained by counting identified records.

Verification

User opens a dispute using a payment account that has a different holder name than in a previous dispute. Mediator clicks on the alert icon.

image

Copy link
Contributor

@alvasw alvasw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

Good catch! The toString() comparison is indeed bad.

I saw that you're using a AtomicLongs to count the item. Does this imply that detectMultipleHolderNames() is called from multiple threads? If that's the case suspiciousDisputesByTraderMap shouldn't be a HashMap.

Otherwise the following change is simpler and faster:

diff --git a/core/src/main/java/bisq/core/support/dispute/agent/MultipleHolderNameDetection.java b/core/src/main/java/bisq/core/support/dispute/agent/MultipleHolderNameDetection.java
index 1be96332ef..09c722eed5 100644
--- a/core/src/main/java/bisq/core/support/dispute/agent/MultipleHolderNameDetection.java
+++ b/core/src/main/java/bisq/core/support/dispute/agent/MultipleHolderNameDetection.java
@@ -146,7 +146,10 @@ public class MultipleHolderNameDetection {
     ///////////////////////////////////////////////////////////////////////////////////////////
 
     public void detectMultipleHolderNames() {
-        String previous = suspiciousDisputesByTraderMap.toString();
+        int previous = suspiciousDisputesByTraderMap.values().stream()
+                .mapToInt(List::size)
+                .sum();
+
         getAllDisputesByTraderMap().forEach((key, value) -> {
             Set<String> userNames = value.stream()
                     .map(dispute -> {
@@ -161,8 +164,12 @@ public class MultipleHolderNameDetection {
                 suspiciousDisputesByTraderMap.put(key, value);
             }
         });
-        String updated = suspiciousDisputesByTraderMap.toString();
-        if (!previous.equals(updated)) {
+
+        int updated = suspiciousDisputesByTraderMap.values().stream()
+                .mapToInt(List::size)
+                .sum();
+
+        if (previous != updated) {
             listeners.forEach(Listener::onSuspiciousDisputeDetected);
         }
     }

Fixes issue #7006 (Out Of Memory Exception thrown)
@ghost
Copy link
Author

ghost commented Feb 7, 2024

Review change to use int applied.

Copy link
Contributor

@alvasw alvasw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

Copy link
Contributor

@alejandrogarcia83 alejandrogarcia83 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

@alejandrogarcia83 alejandrogarcia83 merged commit 29c4999 into bisq-network:master Feb 25, 2024
3 checks passed
@ghost ghost mentioned this pull request Feb 29, 2024
@alejandrogarcia83 alejandrogarcia83 modified the milestones: v1.9.15, v1.9.16 Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mediation ticket opening failed due to OOM at detectMultipleHolderNames
2 participants