This is an automated email from the ASF dual-hosted git repository.

sodonnell pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new d0e6824eec HDDS-7614. Add subscription mechanism to 
ContainerReplicaPendingOps (#4064)
d0e6824eec is described below

commit d0e6824eec7c015fb5066fefae89408220de1583
Author: Siddhant Sangwan <[email protected]>
AuthorDate: Mon Dec 12 21:19:29 2022 +0530

    HDDS-7614. Add subscription mechanism to ContainerReplicaPendingOps (#4064)
---
 .../replication/ContainerReplicaPendingOps.java    | 45 +++++++++++++
 .../ContainerReplicaPendingOpsSubscriber.java      | 39 +++++++++++
 .../TestContainerReplicaPendingOps.java            | 77 ++++++++++++++++++++++
 3 files changed, 161 insertions(+)

diff --git 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/replication/ContainerReplicaPendingOps.java
 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/replication/ContainerReplicaPendingOps.java
index 6a329af97f..4d10b1fc71 100644
--- 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/replication/ContainerReplicaPendingOps.java
+++ 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/replication/ContainerReplicaPendingOps.java
@@ -52,6 +52,8 @@ public class ContainerReplicaPendingOps {
   private final ConcurrentHashMap<PendingOpType, AtomicLong>
       pendingOpCount = new ConcurrentHashMap<>();
   private ReplicationManagerMetrics replicationMetrics = null;
+  private List<ContainerReplicaPendingOpsSubscriber> subscribers =
+      new ArrayList<>();
 
   public ContainerReplicaPendingOps(final ConfigurationSource conf,
       Clock clock) {
@@ -168,6 +170,9 @@ public class ContainerReplicaPendingOps {
    */
   public void removeExpiredEntries(long expiryMilliSeconds) {
     for (ContainerID containerID : pendingOps.keySet()) {
+      // List of expired ops that subscribers will be notified about
+      List<ContainerReplicaOp> expiredOps = new ArrayList<>();
+
       // Rather than use an entry set, we get the map entry again. This is
       // to protect against another thread modifying the value after this
       // iterator started. Once we lock on the ContainerID object, no other
@@ -187,6 +192,7 @@ public class ContainerReplicaPendingOps {
           if (op.getScheduledEpochMillis() + expiryMilliSeconds
               < clock.millis()) {
             iterator.remove();
+            expiredOps.add(op);
             pendingOpCount.get(op.getOpType()).decrementAndGet();
             updateTimeoutMetrics(op);
           }
@@ -197,6 +203,11 @@ public class ContainerReplicaPendingOps {
       } finally {
         lock.unlock();
       }
+
+      // notify if there are expired ops
+      if (!expiredOps.isEmpty()) {
+        notifySubscribers(expiredOps, containerID, true);
+      }
     }
   }
 
@@ -234,6 +245,8 @@ public class ContainerReplicaPendingOps {
   private boolean completeOp(ContainerReplicaOp.PendingOpType opType,
       ContainerID containerID, DatanodeDetails target, int replicaIndex) {
     boolean found = false;
+    // List of completed ops that subscribers will be notified about
+    List<ContainerReplicaOp> completedOps = new ArrayList<>();
     Lock lock = writeLock(containerID);
     lock.lock();
     try {
@@ -246,6 +259,7 @@ public class ContainerReplicaPendingOps {
               && op.getTarget().equals(target)
               && op.getReplicaIndex() == replicaIndex) {
             found = true;
+            completedOps.add(op);
             iterator.remove();
             pendingOpCount.get(op.getOpType()).decrementAndGet();
           }
@@ -257,9 +271,40 @@ public class ContainerReplicaPendingOps {
     } finally {
       lock.unlock();
     }
+
+    if (found) {
+      notifySubscribers(completedOps, containerID, false);
+    }
     return found;
   }
 
+  /**
+   * Notifies subscribers about the specified ops by calling
+   * ContainerReplicaPendingOpsSubscriber#opCompleted.
+   *
+   * @param ops the ops to send notifications for
+   * @param containerID the container that ops belong to
+   * @param timedOut true if the ops (each one) expired, false if they 
completed
+   */
+  private void notifySubscribers(List<ContainerReplicaOp> ops,
+      ContainerID containerID, boolean timedOut) {
+    for (ContainerReplicaOp op : ops) {
+      for (ContainerReplicaPendingOpsSubscriber subscriber : subscribers) {
+        subscriber.opCompleted(op, containerID, timedOut);
+      }
+    }
+  }
+
+  /**
+   * Registers a subscriber that will be notified about completed ops.
+   *
+   * @param subscriber object that wants to subscribe
+   */
+  public void registerSubscriber(
+      ContainerReplicaPendingOpsSubscriber subscriber) {
+    subscribers.add(subscriber);
+  }
+
   private Lock writeLock(ContainerID containerID) {
     return stripedLock.get(containerID).writeLock();
   }
diff --git 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/replication/ContainerReplicaPendingOpsSubscriber.java
 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/replication/ContainerReplicaPendingOpsSubscriber.java
new file mode 100644
index 0000000000..96d4d3adf5
--- /dev/null
+++ 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/replication/ContainerReplicaPendingOpsSubscriber.java
@@ -0,0 +1,39 @@
+/**
+ * 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.container.replication;
+
+import org.apache.hadoop.hdds.scm.container.ContainerID;
+
+/**
+ * A subscriber can register with ContainerReplicaPendingOps to receive
+ * updates on pending ops.
+ */
+public interface ContainerReplicaPendingOpsSubscriber {
+
+  /**
+   * Notifies that the specified op has been completed for the specified
+   * containerID. Might have completed normally or timed out.
+   *
+   * @param op Add or Delete op
+   * @param containerID container on which the operation is being performed
+   * @param timedOut true if the timed out, else false
+   */
+  void opCompleted(ContainerReplicaOp op, ContainerID containerID,
+      boolean timedOut);
+}
diff --git 
a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/replication/TestContainerReplicaPendingOps.java
 
b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/replication/TestContainerReplicaPendingOps.java
index a86eb5ac74..915324195e 100644
--- 
a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/replication/TestContainerReplicaPendingOps.java
+++ 
b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/replication/TestContainerReplicaPendingOps.java
@@ -273,4 +273,81 @@ public class TestContainerReplicaPendingOps {
     Assertions.assertEquals(0, pendingOps.getPendingOpCount(DELETE));
   }
 
+  /**
+   * Tests that registered subscribers are notified about completed and expired
+   * ops.
+   */
+  @Test
+  public void testNotifySubscribers() {
+    // register subscribers
+    ContainerReplicaPendingOpsSubscriber subscriber1 = Mockito.mock(
+        ContainerReplicaPendingOpsSubscriber.class);
+    ContainerReplicaPendingOpsSubscriber subscriber2 = Mockito.mock(
+        ContainerReplicaPendingOpsSubscriber.class);
+    pendingOps.registerSubscriber(subscriber1);
+    pendingOps.registerSubscriber(subscriber2);
+
+    // schedule an ADD and a DELETE
+    ContainerID containerID = new ContainerID(1);
+    pendingOps.scheduleAddReplica(containerID, dn1, 0);
+    ContainerReplicaOp addOp = pendingOps.getPendingOps(containerID).get(0);
+    pendingOps.scheduleDeleteReplica(containerID, dn1, 0);
+
+    // complete the ADD and verify that subscribers were notified
+    pendingOps.completeAddReplica(containerID, dn1, 0);
+    Mockito.verify(subscriber1, Mockito.times(1)).opCompleted(addOp,
+        containerID, false);
+    Mockito.verify(subscriber2, Mockito.times(1)).opCompleted(addOp,
+        containerID, false);
+
+    // complete the DELETE and verify subscribers were notified
+    ContainerReplicaOp deleteOp = pendingOps.getPendingOps(containerID).get(0);
+    pendingOps.completeDeleteReplica(containerID, dn1, 0);
+    Mockito.verify(subscriber1, Mockito.times(1)).opCompleted(deleteOp,
+        containerID, false);
+    Mockito.verify(subscriber2, Mockito.times(1)).opCompleted(deleteOp,
+        containerID, false);
+
+    // now, test notification on expiration
+    pendingOps.scheduleDeleteReplica(containerID, dn1, 0);
+    pendingOps.scheduleAddReplica(containerID, dn2, 0);
+    for (ContainerReplicaOp op : pendingOps.getPendingOps(containerID)) {
+      if (op.getOpType() == ADD) {
+        addOp = op;
+      } else {
+        deleteOp = op;
+      }
+    }
+    clock.fastForward(1000);
+    pendingOps.removeExpiredEntries(500);
+    // the clock is at 1000 and commands expired at 500
+    Mockito.verify(subscriber1, Mockito.times(1)).opCompleted(addOp,
+        containerID, true);
+    Mockito.verify(subscriber1, Mockito.times(1)).opCompleted(deleteOp,
+        containerID, true);
+    Mockito.verify(subscriber2, Mockito.times(1)).opCompleted(addOp,
+        containerID, true);
+    Mockito.verify(subscriber2, Mockito.times(1)).opCompleted(deleteOp,
+        containerID, true);
+  }
+
+  @Test
+  public void subscribersShouldNotBeNotifiedWhenOpsHaveNotExpired() {
+    ContainerID containerID = new ContainerID(1);
+
+    // schedule ops
+    pendingOps.scheduleDeleteReplica(containerID, dn1, 0);
+    pendingOps.scheduleAddReplica(containerID, dn2, 0);
+
+    // register subscriber
+    ContainerReplicaPendingOpsSubscriber subscriber1 = Mockito.mock(
+        ContainerReplicaPendingOpsSubscriber.class);
+    pendingOps.registerSubscriber(subscriber1);
+
+    clock.fastForward(1000);
+    pendingOps.removeExpiredEntries(5000);
+    // no entries have expired, so there should be zero interactions with the
+    // subscriber
+    Mockito.verifyZeroInteractions(subscriber1);
+  }
 }


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

Reply via email to