diff --git a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/states/ContainerAttribute.java b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/states/ContainerAttribute.java index af44a8a043e5..08b065c534af 100644 --- a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/states/ContainerAttribute.java +++ b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/states/ContainerAttribute.java @@ -84,7 +84,8 @@ public ContainerAttribute() { } /** - * Insert or update the value in the Attribute map. + * Insert the value in the Attribute map, keep the original value if it exists + * already. * * @param key - The key to the set where the ContainerID should exist. * @param value - Actual Container ID. @@ -93,30 +94,8 @@ public ContainerAttribute() { public boolean insert(T key, ContainerID value) throws SCMException { Preconditions.checkNotNull(key); Preconditions.checkNotNull(value); - - if (attributeMap.containsKey(key)) { - if (attributeMap.get(key).add(value)) { - return true; //we inserted the value as it doesn’t exist in the set. - } else { // Failure indicates that this ContainerID exists in the Set - if (!attributeMap.get(key).remove(value)) { - LOG.error("Failure to remove the object from the Map.Key:{}, " + - "ContainerID: {}", key, value); - throw new SCMException("Failure to remove the object from the Map", - FAILED_TO_CHANGE_CONTAINER_STATE); - } - attributeMap.get(key).add(value); - return true; - } - } else { - // This key does not exist, we need to allocate this key in the map. - // TODO: Replace TreeSet with FoldedTreeSet from HDFS Utils. - // Skipping for now, since FoldedTreeSet does not have implementations - // for headSet and TailSet. We need those calls. - this.attributeMap.put(key, new TreeSet<>()); - // This should not fail, we just allocated this object. - attributeMap.get(key).add(value); - return true; - } + attributeMap.computeIfAbsent(key, any -> new TreeSet<>()).add(value); + return true; } /** diff --git a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/states/TestContainerAttribute.java b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/states/TestContainerAttribute.java index 63cc9bfd7893..a2426d1efb48 100644 --- a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/states/TestContainerAttribute.java +++ b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/states/TestContainerAttribute.java @@ -45,7 +45,7 @@ public void testInsert() throws SCMException { containerAttribute.getCollection(1).size()); Assert.assertTrue(containerAttribute.getCollection(1).contains(id)); - // Insert again and verify that it overwrites an existing value. + // Insert again and verify that the new ContainerId is inserted. ContainerID newId = new ContainerID(42); containerAttribute.insert(1, newId);