anmolanmol1234 commented on code in PR #8137:
URL: https://github.com/apache/hadoop/pull/8137#discussion_r2650644827


##########
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/MetricsBucket.java:
##########
@@ -0,0 +1,196 @@
+/**
+ * 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.hadoop.fs.azurebfs.services;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.hadoop.fs.azurebfs.utils.SimpleRateLimiter;
+
+/**
+ * MetricsBucket holds metrics for multiple AbfsClients and
+ * dispatches them in batches, respecting rate limits.
+ */
+final class MetricsBucket {
+
+  // Rate limiter to control the rate of dispatching metrics.
+  private final SimpleRateLimiter rateLimiter;
+
+  // Buffer to hold metrics before sending.
+  private final AtomicReference<ConcurrentLinkedQueue<String>> metricsBuffer =
+      new AtomicReference<>(new ConcurrentLinkedQueue<>());
+
+  // Set of registered AbfsClients.
+  private final Set<AbfsClient> clients =
+      ConcurrentHashMap.newKeySet();
+
+  // Maximum size of metrics header in characters.
+  private static final long MAX_HEADER_SIZE = 1024;
+
+  // Constructor
+  MetricsBucket(SimpleRateLimiter rateLimiter) {
+    this.rateLimiter = rateLimiter;
+  }
+
+  /**
+   * Register a new AbfsClient.
+   * @param client the AbfsClient to register
+   */
+  public void registerClient(AbfsClient client) {
+    if (client != null) {
+      clients.add(client);
+    }
+  }
+
+  /**
+   * Deregister an AbfsClient. If this is the last client, drain and send
+   * any remaining metrics.
+   * @param client the AbfsClient to deregister
+   * @return true if the client was deregistered, false otherwise
+   */
+  public boolean deregisterClient(AbfsClient client) {
+    if (client == null) {
+      return false;
+    }
+    ConcurrentLinkedQueue<String> batchToSend = null;
+    boolean isLastClient = false;
+
+    synchronized (this) {
+      if (!clients.contains(client)) {
+        return false;
+      }
+
+      if (clients.size() == 1) {

Review Comment:
   The code checks whether the client is the last one before removing it, so 
another thread can change the client list in between, causing metrics to be 
sent at the wrong time or using a closing client.
   Solution: first remove the client, then check if the client list is empty, 
and only then drain and send the remaining metrics.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to