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

devmadhuu pushed a commit to branch HDDS-11233
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/HDDS-11233 by this push:
     new 9c28f7a4109 HDDS-17585. Rank SCM placement candidates by the requested 
storage type. (#11917)
9c28f7a4109 is described below

commit 9c28f7a41095ee793dc7ff4f7928024333f4b5ad
Author: Devesh Kumar Singh <[email protected]>
AuthorDate: Mon Nov 16 11:58:47 2026 +0530

    HDDS-17585. Rank SCM placement candidates by the requested storage type. 
(#11917)
---
 .../hadoop/hdds/scm/SCMCommonPlacementPolicy.java  | 36 +++++++-
 .../algorithms/SCMContainerPlacementCapacity.java  | 41 ++++++++--
 .../hdds/scm/TestSCMCommonPlacementPolicy.java     | 11 +++
 .../TestSCMContainerPlacementCapacity.java         | 95 ++++++++++++++++++++++
 4 files changed, 173 insertions(+), 10 deletions(-)

diff --git 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/SCMCommonPlacementPolicy.java
 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/SCMCommonPlacementPolicy.java
index e39ee266f97..2abcdc04d65 100644
--- 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/SCMCommonPlacementPolicy.java
+++ 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/SCMCommonPlacementPolicy.java
@@ -21,6 +21,7 @@
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
+import jakarta.annotation.Nullable;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -387,10 +388,27 @@ public static boolean hasEnoughSpace(DatanodeDetails 
datanodeDetails,
   public List<DatanodeDetails> getResultSet(
       int nodesRequired, List<DatanodeDetails> healthyNodes)
       throws SCMException {
+    return getResultSet(nodesRequired, healthyNodes, null);
+  }
+
+  /**
+   * Picks the required number of nodes, comparing candidates on the given
+   * storage type where the policy ranks nodes by usage.
+   *
+   * @param storageType storage type the container will be placed on, or null 
to
+   *                    compare nodes on their overall usage. The healthy node
+   *                    list is already filtered to this storage type by
+   *                    {@link #chooseDatanodesInternal}; this only affects how
+   *                    the remaining candidates are ranked against each other.
+   */
+  public List<DatanodeDetails> getResultSet(
+      int nodesRequired, List<DatanodeDetails> healthyNodes,
+      @Nullable StorageType storageType)
+      throws SCMException {
     List<DatanodeDetails> results = new ArrayList<>();
     for (int x = 0; x < nodesRequired; x++) {
       // invoke the choose function defined in the derived classes.
-      DatanodeDetails nodeId = chooseNode(healthyNodes);
+      DatanodeDetails nodeId = chooseNode(healthyNodes, storageType);
       if (nodeId != null) {
         removePeers(nodeId, healthyNodes);
         results.add(nodeId);
@@ -418,6 +436,22 @@ public List<DatanodeDetails> getResultSet(
   public abstract DatanodeDetails chooseNode(
       List<DatanodeDetails> healthyNodes);
 
+  /**
+   * Choose a datanode according to the policy. The default implementation 
ignores the storage type and delegates to
+   * {@link #chooseNode(List)}.
+   *
+   * Policies that rank nodes by usage should override this to compare on 
{@code storageType}.
+   *
+   * @param healthyNodes - Set of healthy nodes we can choose from.
+   * @param storageType  - storage type to compare node usage on, or null to
+   *                     compare overall usage.
+   * @return DatanodeDetails
+   */
+  public DatanodeDetails chooseNode(List<DatanodeDetails> healthyNodes,
+      @Nullable StorageType storageType) {
+    return chooseNode(healthyNodes);
+  }
+
   /**
    * Default implementation to return the number of racks containers should 
span
    * to meet the placement policy. For simple policies that are not rack aware
diff --git 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/placement/algorithms/SCMContainerPlacementCapacity.java
 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/placement/algorithms/SCMContainerPlacementCapacity.java
index 7de1e8f48a5..b3c9c52c3cd 100644
--- 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/placement/algorithms/SCMContainerPlacementCapacity.java
+++ 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/placement/algorithms/SCMContainerPlacementCapacity.java
@@ -114,7 +114,7 @@ protected List<DatanodeDetails> chooseDatanodesInternal(
     if (healthyNodes.size() == nodesRequired) {
       return healthyNodes;
     }
-    return getResultSet(nodesRequired, healthyNodes);
+    return getResultSet(nodesRequired, healthyNodes, storageType);
   }
 
   /**
@@ -127,6 +127,19 @@ protected List<DatanodeDetails> chooseDatanodesInternal(
    */
   @Override
   public DatanodeDetails chooseNode(List<DatanodeDetails> healthyNodes) {
+    return chooseNode(healthyNodes, null);
+  }
+
+  /**
+   * {@inheritDoc}
+   *
+   * When a storage type is given, the two candidates are compared on their 
usage
+   * of that storage type rather than their overall usage, so a node that is
+   * lightly used overall but nearly full on the requested tier is not 
preferred.
+   */
+  @Override
+  public DatanodeDetails chooseNode(List<DatanodeDetails> healthyNodes,
+      StorageType storageType) {
     metrics.incrDatanodeChooseAttemptCount();
     int firstNodeNdx = getRand().nextInt(healthyNodes.size());
     int secondNodeNdx = getRand().nextInt(healthyNodes.size());
@@ -137,17 +150,27 @@ public DatanodeDetails chooseNode(List<DatanodeDetails> 
healthyNodes) {
     if (firstNodeNdx == secondNodeNdx) {
       datanodeDetails = healthyNodes.get(firstNodeNdx);
     } else {
-      DatanodeDetails firstNodeDetails = healthyNodes.get(firstNodeNdx);
-      DatanodeDetails secondNodeDetails = healthyNodes.get(secondNodeNdx);
-      SCMNodeMetric firstNodeMetric =
-          getNodeManager().getNodeStat(firstNodeDetails);
-      SCMNodeMetric secondNodeMetric =
-          getNodeManager().getNodeStat(secondNodeDetails);
-      datanodeDetails = !firstNodeMetric.isGreater(secondNodeMetric.get())
-          ? firstNodeDetails : secondNodeDetails;
+      datanodeDetails = chooseNodeByCapacity(
+          healthyNodes.get(firstNodeNdx), healthyNodes.get(secondNodeNdx), 
storageType);
     }
     healthyNodes.remove(datanodeDetails);
     metrics.incrDatanodeChooseSuccessCount();
     return datanodeDetails;
   }
+
+  /**
+   * Chooses the less utilized of the two randomly sampled candidates. Compare 
usage on the requested storage type,
+   * or overall usage when no type is supplied. The existing metric comparison 
breaks similar utilization ratios
+   * by used bytes; keep the first candidate when neither is greater. 
Comparing a random pair favors less utilized
+   * nodes without directing every allocation to the least utilized node in 
the cluster.
+   */
+  private DatanodeDetails chooseNodeByCapacity(DatanodeDetails firstNode, 
DatanodeDetails secondNode,
+      StorageType storageType) {
+    SCMNodeMetric firstNodeMetric = getNodeManager().getNodeStat(firstNode);
+    SCMNodeMetric secondNodeMetric = getNodeManager().getNodeStat(secondNode);
+    boolean firstIsFuller = storageType == null
+        ? firstNodeMetric.isGreater(secondNodeMetric.get())
+        : firstNodeMetric.isGreater(secondNodeMetric.get(), storageType);
+    return firstIsFuller ? secondNode : firstNode;
+  }
 }
diff --git 
a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/TestSCMCommonPlacementPolicy.java
 
b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/TestSCMCommonPlacementPolicy.java
index 06d96e6712e..2305c298a26 100644
--- 
a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/TestSCMCommonPlacementPolicy.java
+++ 
b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/TestSCMCommonPlacementPolicy.java
@@ -101,6 +101,17 @@ public void testGetResultSet() throws SCMException {
     assertNotEquals(1, resultSet.size());
   }
 
+  @Test
+  void testStorageTypeSelectionDelegatesToExistingPolicy() throws SCMException 
{
+    DummyPlacementPolicy policy = Mockito.spy(new 
DummyPlacementPolicy(nodeManager, conf, 5));
+    List<DatanodeDetails> nodes = getAllNodes(nodeManager);
+
+    List<DatanodeDetails> result = policy.getResultSet(3, nodes, 
StorageType.SSD);
+
+    assertThat(result).hasSize(3).doesNotHaveDuplicates();
+    Mockito.verify(policy, Mockito.times(3)).chooseNode(Mockito.anyList());
+  }
+
   private Set<ContainerReplica> testReplicasToFixMisreplication(
           List<ContainerReplica> replicas,
           DummyPlacementPolicy placementPolicy,
diff --git 
a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/placement/algorithms/TestSCMContainerPlacementCapacity.java
 
b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/placement/algorithms/TestSCMContainerPlacementCapacity.java
index 96317aa8d43..fb914ca4581 100644
--- 
a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/placement/algorithms/TestSCMContainerPlacementCapacity.java
+++ 
b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/placement/algorithms/TestSCMContainerPlacementCapacity.java
@@ -23,14 +23,20 @@
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Random;
 import org.apache.hadoop.fs.StorageType;
 import org.apache.hadoop.hdds.conf.OzoneConfiguration;
 import org.apache.hadoop.hdds.conf.StorageUnit;
@@ -47,12 +53,85 @@
 import org.apache.hadoop.hdds.scm.node.NodeStatus;
 import org.apache.hadoop.ozone.container.upgrade.UpgradeUtils;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.ValueSource;
 
 /**
  * Test for the scm container placement.
  */
 public class TestSCMContainerPlacementCapacity {
 
+  @ParameterizedTest
+  @CsvSource({"SSD,0,1,1", "SSD,1,0,1", "DISK,0,1,0", "DISK,1,0,0", ",0,1,0", 
",1,0,0"})
+  void chooseNodeRanksOnRequestedStorageType(StorageType storageType, int 
firstIndex, int secondIndex,
+      int expectedIndex) {
+    List<DatanodeDetails> nodes = Arrays.asList(
+        MockDatanodeDetails.randomDatanodeDetails(), 
MockDatanodeDetails.randomDatanodeDetails());
+    NodeManager nodeManager = mock(NodeManager.class);
+    // Node 0: 10% used overall, 95% used on SSD. Node 1: 50% overall, 5% on 
SSD.
+    when(nodeManager.getNodeStat(nodes.get(0))).thenReturn(nodeMetric(95, 5));
+    when(nodeManager.getNodeStat(nodes.get(1))).thenReturn(nodeMetric(5, 495));
+    SCMContainerPlacementMetrics metrics = 
mock(SCMContainerPlacementMetrics.class);
+    SCMContainerPlacementCapacity policy = spy(new 
SCMContainerPlacementCapacity(
+        nodeManager, new OzoneConfiguration(), null, true, metrics));
+    Random random = mock(Random.class);
+    when(random.nextInt(2)).thenReturn(firstIndex, secondIndex);
+    doReturn(random).when(policy).getRand();
+    List<DatanodeDetails> candidates = new ArrayList<>(nodes);
+
+    DatanodeDetails selected = storageType == null
+        ? policy.chooseNode(candidates) : policy.chooseNode(candidates, 
storageType);
+
+    assertEquals(nodes.get(expectedIndex), selected);
+    assertThat(candidates).containsExactly(nodes.get(1 - expectedIndex));
+    verify(metrics).incrDatanodeChooseAttemptCount();
+    verify(metrics).incrDatanodeChooseSuccessCount();
+  }
+
+  @ParameterizedTest
+  @ValueSource(ints = {0, 1})
+  void chooseNodeKeepsSameIndexSelection(int index) {
+    NodeManager nodeManager = mock(NodeManager.class);
+    SCMContainerPlacementCapacity policy = spy(new 
SCMContainerPlacementCapacity(
+        nodeManager, new OzoneConfiguration(), null, true, 
mock(SCMContainerPlacementMetrics.class)));
+    Random random = mock(Random.class);
+    when(random.nextInt(2)).thenReturn(index);
+    doReturn(random).when(policy).getRand();
+    List<DatanodeDetails> nodes = Arrays.asList(
+        MockDatanodeDetails.randomDatanodeDetails(), 
MockDatanodeDetails.randomDatanodeDetails());
+    List<DatanodeDetails> candidates = new ArrayList<>(nodes);
+
+    assertEquals(nodes.get(index), policy.chooseNode(candidates, 
StorageType.SSD));
+    assertThat(candidates).containsExactly(nodes.get(1 - index));
+    verify(nodeManager, never()).getNodeStat(any());
+  }
+
+  @ParameterizedTest
+  @CsvSource({"SSD,1", "DISK,0", ",0"})
+  void chooseDatanodesUsesRequestedStorageType(StorageType storageType, int 
expectedIndex) throws SCMException {
+    List<DatanodeInfo> nodes = new ArrayList<>();
+    for (int i = 0; i < 2; i++) {
+      nodes.add(new DatanodeInfo(MockDatanodeDetails.randomDatanodeDetails(), 
NodeStatus.inServiceHealthy(),
+          UpgradeUtils.defaultVersionProto(), 
HddsTestUtils.ROLL_INTERVAL_MS_DEFAULT));
+    }
+    NodeManager nodeManager = mock(NodeManager.class);
+    
when(nodeManager.getNodes(NodeStatus.inServiceHealthy())).thenAnswer(ignored -> 
new ArrayList<>(nodes));
+    when(nodeManager.hasAvailableSpace(any(DatanodeInfo.class), 
any())).thenReturn(true);
+    when(nodeManager.getNodeStat(nodes.get(0))).thenReturn(nodeMetric(95, 5));
+    when(nodeManager.getNodeStat(nodes.get(1))).thenReturn(nodeMetric(5, 495));
+    SCMContainerPlacementCapacity policy = spy(new 
SCMContainerPlacementCapacity(
+        nodeManager, new OzoneConfiguration(), null, true, 
mock(SCMContainerPlacementMetrics.class)));
+    Random random = mock(Random.class);
+    when(random.nextInt(2)).thenReturn(0, 1);
+    doReturn(random).when(policy).getRand();
+
+    List<DatanodeDetails> selected = policy.chooseDatanodes(
+        Collections.emptyList(), Collections.emptyList(), null, 1, 0, 1, 
storageType);
+
+    assertThat(selected).containsExactly(nodes.get(expectedIndex));
+  }
+
   @Test
   public void chooseDatanodes() throws SCMException {
     //given
@@ -183,4 +262,20 @@ private static SCMNodeMetric createSCMNodeMetric(long 
capacity, long used, long
         singletonMap(StorageType.DEFAULT, freeSpaceToSpare),
         singletonMap(StorageType.DEFAULT, reserved));
   }
+
+  private static SCMNodeMetric nodeMetric(long ssdUsed, long diskUsed) {
+    Map<StorageType, Long> capacity = new HashMap<>();
+    capacity.put(StorageType.SSD, 100L);
+    capacity.put(StorageType.DISK, 900L);
+    Map<StorageType, Long> used = new HashMap<>();
+    used.put(StorageType.SSD, ssdUsed);
+    used.put(StorageType.DISK, diskUsed);
+    Map<StorageType, Long> remaining = new HashMap<>();
+    remaining.put(StorageType.SSD, 100L - ssdUsed);
+    remaining.put(StorageType.DISK, 900L - diskUsed);
+    Map<StorageType, Long> zeros = new HashMap<>();
+    zeros.put(StorageType.SSD, 0L);
+    zeros.put(StorageType.DISK, 0L);
+    return new SCMNodeMetric(capacity, used, remaining, zeros, zeros, zeros);
+  }
 }


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

Reply via email to