You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "Technoboy- (via GitHub)" <gi...@apache.org> on 2023/02/09 08:47:37 UTC

[GitHub] [pulsar] Technoboy- commented on a diff in pull request #19471: [improve][broker] PIP-192 Added broker and top-bundles load reporters

Technoboy- commented on code in PR #19471:
URL: https://github.com/apache/pulsar/pull/19471#discussion_r1101140514


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/reporter/TopBundleLoadDataReporter.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.loadbalance.extensions.reporter;
+
+import java.util.concurrent.CompletableFuture;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.PulsarService;
+import org.apache.pulsar.broker.loadbalance.extensions.data.TopBundlesLoadData;
+import org.apache.pulsar.broker.loadbalance.extensions.models.TopKBundles;
+import org.apache.pulsar.broker.loadbalance.extensions.store.LoadDataStore;
+
+@Slf4j
+public class TopBundleLoadDataReporter implements LoadDataReporter<TopBundlesLoadData> {
+
+    private final PulsarService pulsar;
+
+    private final String lookupServiceAddress;
+
+    private final LoadDataStore<TopBundlesLoadData> bundleLoadDataStore;
+
+    private final TopKBundles topKBundles;
+
+    private long lastBundleStatsUpdatedAt;
+
+    public TopBundleLoadDataReporter(PulsarService pulsar,
+                                     String lookupServiceAddress,
+                                     LoadDataStore<TopBundlesLoadData> bundleLoadDataStore) {
+        this.pulsar = pulsar;
+        this.lookupServiceAddress = lookupServiceAddress;
+        this.bundleLoadDataStore = bundleLoadDataStore;
+        this.lastBundleStatsUpdatedAt = 0;
+        this.topKBundles = new TopKBundles();
+    }
+
+    @Override
+    public TopBundlesLoadData generateLoadData() {
+
+        var pulsarStats = pulsar.getBrokerService().getPulsarStats();
+        TopBundlesLoadData result = null;
+        synchronized (pulsarStats) {
+            var pulsarStatsUpdatedAt = pulsarStats.getUpdatedAt();
+            if (pulsarStatsUpdatedAt > lastBundleStatsUpdatedAt) {
+                var bundleStats = pulsar.getBrokerService().getBundleStats();
+                double percentage = pulsar.getConfiguration().getLoadBalancerBundleLoadReportPercentage();
+                int topk = Math.max(1, (int) (bundleStats.size() * percentage / 100.0));
+                topKBundles.update(bundleStats, topk);
+                lastBundleStatsUpdatedAt = pulsarStatsUpdatedAt;
+                result = topKBundles.getLoadData();
+            }
+        }
+        return result;
+    }
+
+    @Override
+    public CompletableFuture<Void> reportAsync(boolean force) {
+        try {
+            var topBundlesLoadData = generateLoadData();
+            if (topBundlesLoadData != null || force) {
+                return this.bundleLoadDataStore.pushAsync(lookupServiceAddress, topKBundles.getLoadData())
+                        .exceptionally(e -> {
+                            log.error("Failed to report top-bundles load data.", e);
+                            return null;
+                        });
+            } else {
+                return CompletableFuture.completedFuture(null);
+            }
+        } catch (Throwable e) {
+            log.error("Failed to report top-bundles load data.", e);
+            return CompletableFuture.failedFuture(e);

Review Comment:
   Why do we need to catch exception ?



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org