diff --git a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/safemode/ContainerSafeModeRule.java b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/safemode/ContainerSafeModeRule.java index 496d481b61..8eadeb3554 100644 --- a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/safemode/ContainerSafeModeRule.java +++ b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/safemode/ContainerSafeModeRule.java @@ -19,6 +19,7 @@ import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; @@ -63,19 +64,18 @@ public ContainerSafeModeRule(String ruleName, EventQueue eventQueue, " value should be >= 0.0 and <= 1.0"); containerMap = new ConcurrentHashMap<>(); - if(containers != null) { - containers.forEach(c -> { - // TODO: There can be containers in OPEN state which were never - // created by the client. We are not considering these containers for - // now. These containers can be handled by tracking pipelines. - if (c != null && c.getState() != null && - !c.getState().equals(HddsProtos.LifeCycleState.OPEN)) { - containerMap.put(c.getContainerID(), c); - } - }); - maxContainer = containerMap.size(); - } + containers.forEach(container -> { + // There can be containers in OPEN/CLOSING state which were never + // created by the client. We are not considering these containers for + // now. These containers can be handled by tracking pipelines. + Optional.ofNullable(container.getState()) + .filter(state -> state != HddsProtos.LifeCycleState.OPEN) + .filter(state -> state != HddsProtos.LifeCycleState.CLOSING) + .ifPresent(s -> containerMap.put(container.getContainerID(), + container)); + }); + maxContainer = containerMap.size(); long cutOff = (long) Math.ceil(maxContainer * safeModeCutoff); getSafeModeMetrics().setNumContainerWithOneReplicaReportedThreshold(cutOff); } diff --git a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/safemode/TestSCMSafeModeManager.java b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/safemode/TestSCMSafeModeManager.java index ba92035466..247b38afc7 100644 --- a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/safemode/TestSCMSafeModeManager.java +++ b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/safemode/TestSCMSafeModeManager.java @@ -23,6 +23,7 @@ import java.io.File; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.UUID; @@ -60,7 +61,7 @@ public class TestSCMSafeModeManager { private static EventQueue queue; private SCMSafeModeManager scmSafeModeManager; private static Configuration config; - private List containers; + private List containers = Collections.emptyList(); @Rule public Timeout timeout = new Timeout(1000 * 300); @@ -85,7 +86,8 @@ public void testSafeModeState() throws Exception { @Test public void testSafeModeStateWithNullContainers() { - new SCMSafeModeManager(config, null, null, queue); + new SCMSafeModeManager(config, Collections.emptyList(), + null, queue); } private void testSafeMode(int numContainers) throws Exception {