sumitagrawl commented on code in PR #3783:
URL: https://github.com/apache/ozone/pull/3783#discussion_r996477938


##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/server/ContainerReportQueue.java:
##########
@@ -0,0 +1,399 @@
+/*
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.hdds.scm.server;
+
+import org.apache.hadoop.hdds.protocol.DatanodeDetails;
+import 
org.apache.hadoop.hdds.scm.server.SCMDatanodeHeartbeatDispatcher.ContainerReportBase;
+import 
org.apache.hadoop.hdds.scm.server.SCMDatanodeHeartbeatDispatcher.ContainerReportFromDatanode;
+import 
org.apache.hadoop.hdds.scm.server.SCMDatanodeHeartbeatDispatcher.IncrementalContainerReportFromDatanode;
+import 
org.apache.hadoop.hdds.server.events.FixedThreadPoolWithAffinityExecutor.IQueueMetrics;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Customized queue to handle FCR and ICR from datanode optimally,
+ * avoiding duplicate FCR reports.
+ */
+public class ContainerReportQueue<VALUE extends ContainerReportBase>
+    implements BlockingQueue<VALUE>, IQueueMetrics {
+
+  private final Integer maxCapacity;
+
+  /* ordering queue provides ordering of execution in fair manner
+   * i.e. report execution from multiple datanode will be executed in same
+   * order as added to queue.
+   */
+  private LinkedBlockingQueue<String> orderingQueue
+      = new LinkedBlockingQueue<>();
+  private Map<String, List<VALUE>> dataMap = new HashMap<>();
+
+  private int capacity = 0;
+
+  private AtomicInteger droppedCount = new AtomicInteger();
+
+  public ContainerReportQueue() {
+    this(100000);
+  }
+  
+  public ContainerReportQueue(int maxCapacity) {
+    super();
+    this.maxCapacity = maxCapacity;
+  }
+
+  private boolean addContainerReport(VALUE val) {
+    ContainerReportFromDatanode report
+        = (ContainerReportFromDatanode) val;
+    synchronized (this) {
+      // 1. check if no previous report available, else add the report
+      DatanodeDetails dn = report.getDatanodeDetails();
+      if (!dataMap.containsKey(dn.getUuidString())) {
+        ArrayList<VALUE> dataList = new ArrayList<>();
+        dataList.add(val);
+        ++capacity;
+        dataMap.put(dn.getUuidString(), dataList);
+        orderingQueue.add(dn.getUuidString());
+        return true;
+      }
+
+      // 2. FCR report available
+      List<VALUE> dataList = dataMap.get(dn.getUuidString());
+      boolean isReportRemoved = false;
+      if (!dataList.isEmpty()) {
+        // remove FCR if present
+        for (int i = dataList.size() - 1; i >= 0; --i) {
+          ContainerReportBase reportInfo = dataList.get(i);
+          // if FCR, its last FCR report, remove directly
+          if (isFCRReport(reportInfo)) {
+            dataList.remove(i);
+            --capacity;
+            droppedCount.incrementAndGet();
+            isReportRemoved = true;
+            break;
+          }
+        }
+      }
+
+      dataList.add(val);
+      ++capacity;
+      if (!isReportRemoved) {
+        orderingQueue.add(dn.getUuidString());
+      }
+    }
+    return true;
+  }
+
+  private boolean addIncrementalReport(VALUE val) {
+    IncrementalContainerReportFromDatanode report
+        = (IncrementalContainerReportFromDatanode) val;
+    synchronized (this) {
+      // 1. check if no previous report available, else add the report
+      DatanodeDetails dn = report.getDatanodeDetails();
+      if (!dataMap.containsKey(dn.getUuidString())) {
+        ArrayList<VALUE> dataList = new ArrayList<>();
+        dataList.add(val);
+        ++capacity;
+        dataMap.put(dn.getUuidString(), dataList);
+        orderingQueue.add(dn.getUuidString());
+        return true;
+      }
+
+      // 2. Add ICR report or merge to previous ICR
+      List<VALUE> dataList = dataMap.get(dn.getUuidString());
+      dataList.add(val);
+      ++capacity;
+      orderingQueue.add(dn.getUuidString());
+    }
+    return true;
+  }
+
+  private boolean isFCRReport(Object report) {
+    return report instanceof ContainerReportFromDatanode;
+  }
+
+  private boolean isICRReport(Object report) {
+    return report instanceof IncrementalContainerReportFromDatanode;
+  }
+
+  private VALUE removeAndGet(String uuid) {
+    if (uuid == null) {
+      return null;
+    }
+
+    List<VALUE> dataList = dataMap.get(uuid);
+    VALUE report = null;
+    if (dataList != null && !dataList.isEmpty()) {
+      report = dataList.remove(0);
+      --capacity;
+      if (dataList.isEmpty()) {
+        dataMap.remove(uuid);
+      }
+    }
+    return report;
+  }
+
+  private VALUE getReport(String uuid) {
+    if (uuid == null) {
+      return null;
+    }
+
+    List<VALUE> dataList = dataMap.get(uuid);
+    if (dataList != null && !dataList.isEmpty()) {
+      return dataList.get(0);
+    }
+    return null;
+  }
+
+  private static void checkNotNull(Object v) {
+    if (v == null) {
+      throw new NullPointerException();
+    }
+  }
+
+  public boolean addValue(@NotNull VALUE value) {
+    synchronized (this) {
+      if (remainingCapacity() == 0) {
+        return false;
+      }
+
+      if (isFCRReport(value)) {
+        return addContainerReport(value);
+      } else if (isICRReport(value)) {
+        return addIncrementalReport(value);
+      }
+      return false;
+    }
+  }
+
+  @Override
+  public boolean add(@NotNull VALUE value) {
+    checkNotNull(value);
+    synchronized (this) {
+      if (remainingCapacity() == 0) {
+        throw new IllegalStateException("capacity not available");
+      }
+
+      return addValue(value);
+    }
+  }
+
+  @Override
+  public boolean offer(@NotNull VALUE value) {
+    checkNotNull(value);
+    synchronized (this) {
+      return addValue(value);
+    }
+  }
+
+  @Override
+  public VALUE remove() {
+    synchronized (this) {
+      String uuid = orderingQueue.remove();
+      if (null == uuid) {
+        throw new NoSuchElementException();
+      }
+      return removeAndGet(uuid);
+    }
+  }
+
+  @Override
+  public VALUE poll() {
+    synchronized (this) {
+      String uuid = orderingQueue.poll();
+      return removeAndGet(uuid);
+    }
+  }
+
+  @Override
+  public VALUE element() {
+    synchronized (this) {
+      String uuid = orderingQueue.element();
+      if (null == uuid) {
+        throw new NoSuchElementException();
+      }
+      return getReport(uuid);
+    }
+  }
+
+  @Override
+  public VALUE peek() {
+    synchronized (this) {
+      String uuid = orderingQueue.peek();
+      return getReport(uuid);
+    }
+  }
+
+  @Override
+  public void put(@NotNull VALUE value) throws InterruptedException {
+    checkNotNull(value);
+    while (!addValue(value)) {
+      Thread.currentThread().sleep(10);
+    }
+  }
+
+  @Override
+  public boolean offer(VALUE value, long timeout, @NotNull TimeUnit unit)
+      throws InterruptedException {
+    checkNotNull(value);
+    long timeoutMillis = unit.toMillis(timeout);
+    while (timeoutMillis > 0) {
+      if (addValue(value)) {
+        return true;
+      }
+      Thread.currentThread().sleep(10);
+      timeoutMillis -= 10;

Review Comment:
   used startTime and end time to get actual difference and updated same.



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