lhotari commented on code in PR #24639:
URL: https://github.com/apache/pulsar/pull/24639#discussion_r2282018979


##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/resources/MetadataStoreCacheLoader.java:
##########
@@ -61,26 +63,28 @@ public MetadataStoreCacheLoader(PulsarResources 
pulsarResources, int operationTi
      * @throws Exception
      */
     public void init() throws Exception {
-        loadReportResources.getStore().registerListener((n) -> {
-            if (LOADBALANCE_BROKERS_ROOT.equals(n.getPath()) && 
NotificationType.ChildrenChanged.equals(n.getType())) {
-                
loadReportResources.getChildrenAsync(LOADBALANCE_BROKERS_ROOT).thenApplyAsync((brokerNodes)->{
-                    updateBrokerList(brokerNodes).thenRun(() -> {
-                        log.info("Successfully updated broker info {}", 
brokerNodes);
-                    }).exceptionally(ex -> {
-                        log.warn("Error updating broker info after broker list 
changed", ex);
-                        return null;
+        Runnable tryUpdate = () -> {
+            synchronized (this) {
+                updateFuture = updateFuture.thenCompose(__ -> {

Review Comment:
   There's already `org.apache.pulsar.common.util.FutureUtil.Sequencer` in the 
code base to handle this type of use cases. Please use it. There's an example 
of using `FutureUtil.Sequencer` in 
`org.apache.pulsar.metadata.coordination.impl.LockManagerImpl` class.



##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/resources/MetadataStoreCacheLoader.java:
##########
@@ -61,26 +63,28 @@ public MetadataStoreCacheLoader(PulsarResources 
pulsarResources, int operationTi
      * @throws Exception
      */
     public void init() throws Exception {
-        loadReportResources.getStore().registerListener((n) -> {
-            if (LOADBALANCE_BROKERS_ROOT.equals(n.getPath()) && 
NotificationType.ChildrenChanged.equals(n.getType())) {
-                
loadReportResources.getChildrenAsync(LOADBALANCE_BROKERS_ROOT).thenApplyAsync((brokerNodes)->{
-                    updateBrokerList(brokerNodes).thenRun(() -> {
-                        log.info("Successfully updated broker info {}", 
brokerNodes);
-                    }).exceptionally(ex -> {
-                        log.warn("Error updating broker info after broker list 
changed", ex);
-                        return null;
+        Runnable tryUpdate = () -> {
+            synchronized (this) {
+                updateFuture = updateFuture.thenCompose(__ -> {
+                    return 
loadReportResources.getChildrenAsync(LOADBALANCE_BROKERS_ROOT).thenComposeAsync((brokerNodes)
 -> {
+                        return updateBrokerList(brokerNodes).thenRun(() -> {
+                            log.info("Successfully updated broker info {}", 
brokerNodes);
+                        });
                     });
-                    return null;
                 }).exceptionally(ex -> {
                     log.warn("Error updating broker info after broker list 
changed", ex);
                     return null;
                 });
             }
+        };
+        loadReportResources.getStore().registerListener((n) -> {
+            if (LOADBALANCE_BROKERS_ROOT.equals(n.getPath()) && 
NotificationType.ChildrenChanged.equals(n.getType())) {
+                tryUpdate.run();
+            }
         });
 
-        // Do initial fetch of brokers list
-        
updateBrokerList(loadReportResources.getChildren(LOADBALANCE_BROKERS_ROOT)).get(operationTimeoutMs,
-                TimeUnit.SECONDS);
+        // Do periodic fetch of brokers list
+        orderedExecutor.scheduleAtFixedRate(tryUpdate, 0, 
UPDATE_INTERVAL_SECONDS, TimeUnit.SECONDS);

Review Comment:
   Please don't add a periodic refresh in this PR.
   Instead of adding a periodic refresh, it should be sufficient to add a 
session listener for `SessionEvent.SessionReestablished` and 
`SessionEvent.Reconnected` events and only refresh in those cases. There's an 
example of handling these events in 
`org.apache.pulsar.metadata.coordination.impl.LockManagerImpl`.
   



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