You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ozone.apache.org by el...@apache.org on 2021/03/01 12:39:57 UTC

[ozone] branch master updated: HDDS-4769. Simplify insert operation of ContainerAttribute (#1865)

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

elek 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 9a1a5ad  HDDS-4769. Simplify insert operation of ContainerAttribute (#1865)
9a1a5ad is described below

commit 9a1a5add1cf97119b0bb80d65a2c29d27e95b65c
Author: Symious <yi...@foxmail.com>
AuthorDate: Mon Mar 1 20:39:42 2021 +0800

    HDDS-4769. Simplify insert operation of ContainerAttribute (#1865)
---
 .../scm/container/states/ContainerAttribute.java   | 29 +++-------------------
 .../container/states/TestContainerAttribute.java   |  2 +-
 2 files changed, 5 insertions(+), 26 deletions(-)

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 49467e5..8500063 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 class ContainerAttribute<T> {
   }
 
   /**
-   * 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 class ContainerAttribute<T> {
   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 63cc9bf..a2426d1 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 class TestContainerAttribute {
         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);


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@ozone.apache.org
For additional commands, e-mail: commits-help@ozone.apache.org