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

jnioche pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/storm.git


The following commit(s) were added to refs/heads/master by this push:
     new 0eaced227 synchronize access to map in Multi*Metric; fixes STORM-3944
     new d7851dcac Merge pull request #3559 from DigitalPebble/3944
0eaced227 is described below

commit 0eaced227cda6635bee3d32a9824f8c9cf376092
Author: Julien Nioche <jul...@digitalpebble.com>
AuthorDate: Tue Aug 8 11:43:16 2023 +0100

    synchronize access to map in Multi*Metric; fixes STORM-3944
    
    Signed-off-by: Julien Nioche <jul...@digitalpebble.com>
---
 .../apache/storm/metric/api/MultiCountMetric.java  | 18 +++++++++-------
 .../storm/metric/api/MultiReducedMetric.java       | 24 +++++++++++++---------
 2 files changed, 25 insertions(+), 17 deletions(-)

diff --git 
a/storm-client/src/jvm/org/apache/storm/metric/api/MultiCountMetric.java 
b/storm-client/src/jvm/org/apache/storm/metric/api/MultiCountMetric.java
index d1336656a..21d89402e 100644
--- a/storm-client/src/jvm/org/apache/storm/metric/api/MultiCountMetric.java
+++ b/storm-client/src/jvm/org/apache/storm/metric/api/MultiCountMetric.java
@@ -16,24 +16,28 @@ import java.util.HashMap;
 import java.util.Map;
 
 public class MultiCountMetric implements IMetric {
-    Map<String, CountMetric> value = new HashMap<>();
+    final Map<String, CountMetric> value = new HashMap<>();
 
     public MultiCountMetric() {
     }
 
     public CountMetric scope(String key) {
-        CountMetric val = value.get(key);
-        if (val == null) {
-            value.put(key, val = new CountMetric());
+        synchronized (value) {
+            CountMetric val = value.get(key);
+            if (val == null) {
+                value.put(key, val = new CountMetric());
+            }
+            return val;
         }
-        return val;
     }
 
     @Override
     public Map<String, Object> getValueAndReset() {
         Map<String, Object> ret = new HashMap<>();
-        for (Map.Entry<String, CountMetric> e : value.entrySet()) {
-            ret.put(e.getKey(), e.getValue().getValueAndReset());
+        synchronized (value) {
+            for (Map.Entry<String, CountMetric> e : value.entrySet()) {
+                ret.put(e.getKey(), e.getValue().getValueAndReset());
+            }
         }
         return ret;
     }
diff --git 
a/storm-client/src/jvm/org/apache/storm/metric/api/MultiReducedMetric.java 
b/storm-client/src/jvm/org/apache/storm/metric/api/MultiReducedMetric.java
index c9c8590d3..9b0efe0a2 100644
--- a/storm-client/src/jvm/org/apache/storm/metric/api/MultiReducedMetric.java
+++ b/storm-client/src/jvm/org/apache/storm/metric/api/MultiReducedMetric.java
@@ -16,28 +16,32 @@ import java.util.HashMap;
 import java.util.Map;
 
 public class MultiReducedMetric implements IMetric {
-    Map<String, ReducedMetric> value = new HashMap<>();
-    IReducer reducer;
+    final Map<String, ReducedMetric> value = new HashMap<>();
+    final IReducer reducer;
 
     public MultiReducedMetric(IReducer reducer) {
         this.reducer = reducer;
     }
 
     public ReducedMetric scope(String key) {
-        ReducedMetric val = value.get(key);
-        if (val == null) {
-            value.put(key, val = new ReducedMetric(reducer));
+        synchronized (value) {
+            ReducedMetric val = value.get(key);
+            if (val == null) {
+                value.put(key, val = new ReducedMetric(reducer));
+            }
+            return val;
         }
-        return val;
     }
 
     @Override
     public Map<String, Object> getValueAndReset() {
         Map<String, Object> ret = new HashMap<>();
-        for (Map.Entry<String, ReducedMetric> e : value.entrySet()) {
-            Object val = e.getValue().getValueAndReset();
-            if (val != null) {
-                ret.put(e.getKey(), val);
+        synchronized (value) {
+            for (Map.Entry<String, ReducedMetric> e : value.entrySet()) {
+                Object val = e.getValue().getValueAndReset();
+                if (val != null) {
+                    ret.put(e.getKey(), val);
+                }
             }
         }
         return ret;

Reply via email to