Demogorgon314 commented on code in PR #21854:
URL: https://github.com/apache/pulsar/pull/21854#discussion_r1444027857
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/manager/UnloadManager.java:
##########
@@ -38,13 +42,77 @@ public class UnloadManager implements StateChangeListener {
private final UnloadCounter counter;
private final Map<String, CompletableFuture<Void>> inFlightUnloadRequest;
+ private final String lookupServiceAddress;
- public UnloadManager(UnloadCounter counter) {
+ @VisibleForTesting
+ public enum LatencyMetric {
+ UNLOAD(buildHistogram(
+ "brk_lb_unload_latency", "Total time duration of unload operations
on source brokers"), true, false),
+ ASSIGN(buildHistogram(
+ "brk_lb_assign_latency", "Time spent in the load balancing ASSIGN
state on destination brokers"),
+ false, true),
+ RELEASE(buildHistogram(
+ "brk_lb_release_latency", "Time spent in the load balancing
RELEASE state on source brokers"), true, false),
+ DISCONNECT(buildHistogram(
+ "brk_lb_disconnect_latency", "Time spent in the load balancing
disconnected state on source brokers"),
+ true, false);
+
+ private static Histogram buildHistogram(String name, String help) {
+ return Histogram.build(name, help).unit("ms").labelNames("broker",
"metric").
+ buckets(new double[] {1.0, 10.0, 100.0, 200.0,
1000.0}).register();
+ }
+ private static final long OP_TIMEOUT_NS = TimeUnit.HOURS.toNanos(1);
+
+ private final Histogram histogram;
+ private final Map<String, CompletableFuture<Void>> futures = new
ConcurrentHashMap<>();
+ private final boolean isSourceBrokerMetric;
+ private final boolean isDestinationBrokerMetric;
+
+ LatencyMetric(Histogram histogram, boolean isSourceBrokerMetric,
boolean isDestinationBrokerMetric) {
+ this.histogram = histogram;
+ this.isSourceBrokerMetric = isSourceBrokerMetric;
+ this.isDestinationBrokerMetric = isDestinationBrokerMetric;
+ }
+
+ public void beginMeasurement(String serviceUnit, String
lookupServiceAddress, ServiceUnitStateData data) {
+ if ((isSourceBrokerMetric &&
lookupServiceAddress.equals(data.sourceBroker()))
+ || (isDestinationBrokerMetric &&
lookupServiceAddress.equals(data.dstBroker()))) {
+ var startTimeNs = System.nanoTime();
+ futures.computeIfAbsent(serviceUnit, ignore -> {
+ var future = new CompletableFuture<Void>();
+ future.completeOnTimeout(null, OP_TIMEOUT_NS,
TimeUnit.NANOSECONDS).
+ thenAccept(__ -> {
+ var durationMs =
TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTimeNs);
+ log.info("Operation {} for service unit {}
took {} ms", this, serviceUnit, durationMs);
+ histogram.labels(lookupServiceAddress,
"bundleUnloading").observe(durationMs);
+ }).whenComplete((__, throwable) ->
futures.remove(serviceUnit, future));
+ return future;
+ });
+ }
+ }
+
+ public void endMeasurement(String serviceUnit) {
+ var future = futures.get(serviceUnit);
+ if (future != null) {
+ future.complete(null);
+ }
+ }
+ }
+
+ public UnloadManager(PulsarService pulsar, UnloadCounter counter) {
this.counter = counter;
- this.inFlightUnloadRequest = new ConcurrentHashMap<>();
+ inFlightUnloadRequest = new ConcurrentHashMap<>();
+ lookupServiceAddress =
Objects.requireNonNull(pulsar.getLookupServiceAddress());
Review Comment:
Why pass a `PulsarService` here? It is only used to get the
`lookupServiceAddress`.
--
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]