Fix UnsignedLongSet entry lifecycle 92/98392/2
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 10 Nov 2021 17:51:05 +0000 (18:51 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 10 Nov 2021 17:55:57 +0000 (18:55 +0100)
When we are copying things around, we have to ensure we copy Entries,
because they cannot be shared.

JIRA: CONTROLLER-2015
Change-Id: I8a484a3bf52ffeaf4f4d0e770bc77dba31dbb221
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/ImmutableUnsignedLongSet.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/MutableUnsignedLongSet.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/UnsignedLongSet.java

index 47b8a8b..af49065 100644 (file)
@@ -36,9 +36,9 @@ public final class ImmutableUnsignedLongSet extends UnsignedLongSet implements I
             return of();
         }
         if (mutable.size() <= ARRAY_MAX_ELEMENTS) {
-            return new ImmutableUnsignedLongSet(ImmutableSortedSet.copyOfSorted(mutable.trustedRanges()));
+            return new ImmutableUnsignedLongSet(ImmutableSortedSet.copyOf(mutable.copiedRanges()));
         }
-        return new ImmutableUnsignedLongSet(new TreeSet<>(mutable.trustedRanges()));
+        return new ImmutableUnsignedLongSet(new TreeSet<>(mutable.copiedRanges()));
     }
 
     public static @NonNull ImmutableUnsignedLongSet of() {
index 2b0dec3..582193e 100644 (file)
@@ -40,21 +40,22 @@ public final class MutableUnsignedLongSet extends UnsignedLongSet implements Mut
     }
 
     public void add(final long longBits) {
-        addOne(trustedRanges(), Entry.of(longBits), longBits);
+        addOne(trustedRanges(), longBits);
     }
 
     public void addAll(final UnsignedLongSet other) {
         final var ranges = trustedRanges();
         for (var range : other.trustedRanges()) {
             if (range.lowerBits == range.upperBits) {
-                addOne(ranges, range, range.lowerBits);
+                addOne(ranges, range.lowerBits);
             } else {
                 addRange(ranges, range);
             }
         }
     }
 
-    private static void addOne(final NavigableSet<Entry> ranges, final Entry range, final long longBits) {
+    private static void addOne(final NavigableSet<Entry> ranges, final long longBits) {
+        final var range = Entry.of(longBits);
         // We need Iterator.remove() to perform efficient merge below
         final var headIt = ranges.headSet(range, true).descendingIterator();
         if (headIt.hasNext()) {
@@ -116,7 +117,7 @@ public final class MutableUnsignedLongSet extends UnsignedLongSet implements Mut
         }
 
         // No luck, insert
-        ranges.add(range);
+        ranges.add(range.copy());
     }
 
     private static void expandLower(final NavigableSet<Entry> ranges, final Entry entry, final long upperBits) {
index 14e3b6a..e3fa626 100644 (file)
@@ -19,6 +19,7 @@ import com.google.common.primitives.UnsignedLong;
 import java.io.DataInput;
 import java.io.DataOutput;
 import java.io.IOException;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.NavigableSet;
 import java.util.TreeSet;
@@ -151,7 +152,7 @@ abstract class UnsignedLongSet {
     public abstract @NonNull ImmutableUnsignedLongSet immutableCopy();
 
     public final @NonNull MutableUnsignedLongSet mutableCopy() {
-        return new MutableUnsignedLongSet(new TreeSet<>(Collections2.transform(ranges, Entry::copy)));
+        return new MutableUnsignedLongSet(new TreeSet<>(copiedRanges()));
     }
 
     public final @NonNull NavigableSet<Entry> ranges() {
@@ -162,6 +163,10 @@ abstract class UnsignedLongSet {
         return ranges;
     }
 
+    final @NonNull Collection<Entry> copiedRanges() {
+        return Collections2.transform(ranges, Entry::copy);
+    }
+
     @Override
     public final int hashCode() {
         return ranges.hashCode();