siddhantsangwan commented on code in PR #5261:
URL: https://github.com/apache/ozone/pull/5261#discussion_r1321411776


##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/replication/LegacyReplicationManager.java:
##########
@@ -1141,8 +1141,103 @@ private void handleUnderReplicatedHealthy(final 
ContainerInfo container,
     List<ContainerReplica> replicationSources = 
getReplicationSources(container,
         replicaSet.getReplicas(), State.CLOSED, State.QUASI_CLOSED);
     // This method will handle topology even if replicasNeeded <= 0.
-    replicateAnyWithTopology(container, replicationSources,
-        placementStatus, replicasNeeded);
+    try {
+      replicateAnyWithTopology(container, replicationSources,
+          placementStatus, replicasNeeded);
+    } catch (SCMException e) {
+      if (e.getResult()
+          .equals(SCMException.ResultCodes.FAILED_TO_FIND_SUITABLE_NODE) &&
+          replicasNeeded > 0) {
+        /*
+        If we reach here, the container is under replicated but placement
+        policy could not find any target Datanodes to host new replicas.
+        We can try unblocking under replication handling by removing any
+        unhealthy replicas. This will free up those datanodes, so they can host
+        healthy replicas.
+         */
+        deleteUnhealthyReplicaIfNeeded(container, replicaSet);
+      }
+    }
+  }
+
+  /**
+   * Finds and deletes an unhealthy replica (UNHEALTHY or QUASI_CLOSED) under
+   * certain conditions.
+   */
+  private void deleteUnhealthyReplicaIfNeeded(ContainerInfo container,
+      RatisContainerReplicaCount replicaCount) {
+    LOG.info("Finding an unhealthy replica to delete for container {} with " +
+        "replicas {} to unblock under replication handling.", container,
+        replicaCount.getReplicas());
+
+    int pendingDeletes = getInflightDel(container.containerID());
+    if (pendingDeletes > 0) {
+      LOG.debug("Container {} has {} pending deletes. Will not delete an " +
+          "unhealthy replica for this container.", container, pendingDeletes);
+      return;
+    }
+
+    List<ContainerReplica> replicas = replicaCount.getReplicas();
+    if (replicas.size() < 3) {
+      LOG.debug("Container {} has only {} replicas. Will not delete an " +
+          "unhealthy replica for this container.", container, replicas.size());
+      return;
+    }
+
+    LifeCycleState containerState = container.getState();
+    boolean foundMatchingReplica = false;
+    for (ContainerReplica replica : replicas) {
+      if (compareState(containerState, replica.getState())) {
+        foundMatchingReplica = true;
+        break;
+      }
+    }
+    if (!foundMatchingReplica) {
+      LOG.debug("No matching replica found for container {} with replicas " +
+              "{}. Will not delete any unhealthy replica for this container.",
+          container, replicas);
+      return;
+    }
+
+    List<ContainerReplica> deleteCandidates = new ArrayList<>();
+    // collect unhealthy replicas on in-service, healthy nodes
+    for (ContainerReplica replica : replicas) {
+      try {
+        NodeStatus nodeStatus =
+            nodeManager.getNodeStatus(replica.getDatanodeDetails());
+        if (!nodeStatus.isHealthy() || !nodeStatus.isInService()) {
+          continue;
+        }
+      } catch (NodeNotFoundException e) {
+        LOG.warn("Skipping replica {} when trying to unblock under " +
+            "replication handling.", replica, e);
+        continue;
+      }
+
+      if (replica.getState() == State.QUASI_CLOSED) {
+        deleteCandidates.add(replica);
+      } else if (replica.getState() == State.UNHEALTHY) {
+        deleteCandidates.add(replica);
+      }

Review Comment:
   I'm avoiding this check as it'll also return true for cases like closed 
container with closing replica, quasi-closed container with closing replica 
etc. Instead, I'm now doing
   ```
         if (containerState != LifeCycleState.QUASI_CLOSED &&
             replica.getState() == State.QUASI_CLOSED) {
           // a quasi_closed replica is a candidate only if its seq id is less
           // than the container's
           if (replica.getSequenceId() < containerSeq) {
             deleteCandidates.add(replica);
           }
         } else if (replica.getState() == State.UNHEALTHY) {
           deleteCandidates.add(replica);
         }
       }
   ```
   which should do what we want.



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