heesung-sn commented on code in PR #18807:
URL: https://github.com/apache/pulsar/pull/18807#discussion_r1050174671


##########
pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/stats/TopicStatsImpl.java:
##########
@@ -252,19 +253,15 @@ public TopicStatsImpl add(TopicStats ts) {
                    return newStats;
                }).add((PublisherStatsImpl) s);
            } else {
-               if (this.publishers.size() != stats.publishers.size()) {
-                   for (int i = 0; i < stats.publishers.size(); i++) {
-                       PublisherStatsImpl newStats = new PublisherStatsImpl();
-                       newStats.setSupportsPartialProducer(false);
-                       
this.publishers.add(newStats.add(stats.publishers.get(i)));
-                   }
-               } else {
-                   for (int i = 0; i < stats.publishers.size(); i++) {
-                       this.publishers.get(i).add(stats.publishers.get(i));
-                   }
+               if (index == this.publishers.size()) {
+                   PublisherStatsImpl newStats = new PublisherStatsImpl();
+                   newStats.setSupportsPartialProducer(false);
+                   this.publishers.add(newStats);
                }
+               this.publishers.get(index)
+                       .add((PublisherStatsImpl) s);

Review Comment:
   Hi,
   
   I see that `*TopicStatsImpl` is not a thread-safe class. Currently, the 
caller needs to externally synchronize the access if concurrent threads access 
the same instance.
   
   > Potentially access to it from concurrent threads.
   
   
   Could you provide this example? I see that `PartitionedTopicStatsImpl` is 
instantiated by a single thread, and `add(..) ` gets called from the same 
thread.
   
   
   ```
   PartitionedTopicStatsImpl stats = new 
PartitionedTopicStatsImpl(partitionMetadata);
   ...
               FutureUtil.waitForAll(topicStatsFutureList).handle((result, 
exception) -> {
   ...
                               stats.add(statFuture.get());
                               if (perPartition) {
                                   
stats.getPartitions().put(topicName.getPartition(i).toString(), 
statFuture.get());
   ...
                       }
                   }
   ```



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to