sodonnel commented on PR #8990:
URL: https://github.com/apache/ozone/pull/8990#issuecomment-3248516175
I was thinking something like this would work:
```
---
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/states/ContainerEntry.java
+++
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/states/ContainerEntry.java
@@ -17,6 +17,7 @@
package org.apache.hadoop.hdds.scm.container.states;
+import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -31,7 +32,7 @@
*/
public class ContainerEntry {
private final ContainerInfo info;
- private final Map<DatanodeID, ContainerReplica> replicas = new
HashMap<>();
+ private Map<ContainerReplica, ContainerReplica> replicas = new
HashMap<>();
ContainerEntry(ContainerInfo info) {
this.info = info;
@@ -42,14 +43,22 @@ public ContainerInfo getInfo() {
}
public Set<ContainerReplica> getReplicas() {
- return new HashSet<>(replicas.values());
+ return Collections.unmodifiableSet(replicas.keySet());
}
public ContainerReplica put(ContainerReplica r) {
- return replicas.put(r.getDatanodeDetails().getID(), r);
+ // Modifications are rare compared to reads, so to optimize for read we
return and unmodifable view of the
+ // map in getReplicas. That means for any modification we need to copy
the map.
+ Map<ContainerReplica, ContainerReplica> map = new HashMap<>(8);
+ map.putAll(this.replicas);
+ replicas = map;
+ return replicas.put(r, r);
}
public ContainerReplica removeReplica(DatanodeID datanodeID) {
+ Map<ContainerReplica, ContainerReplica> map = new HashMap<>(8);
+ map.putAll(this.replicas);
+ replicas = map;
return replicas.remove(datanodeID);
}
}
```
However, what I am not sure about, is whether any of the caller of
getReplicas() would assume they have their own copy they can modify, and
therefore that code would fail. So while this would be efficient, it really
constrains how the return value can be used, so it may be too risky to change.
--
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]