fix(storage): skip keys deleted mid-iteration in StorageBackedMap views (#6017)

* fix(storage): skip keys deleted mid-iteration in StorageBackedMap views

The entry iterator walks a key snapshot and each entry's getValue() re-read
storage, so a key deleted after the snapshot made values()/entrySet() yield
null. Callers streaming values() (AutoScaling was patched per-site in #5751;
many other services do the same) then hit NullPointerExceptions.

Fix it once in the map: the iterator looks one element ahead and skips keys
whose value is gone, and entries carry the value read at iteration time.
Because size() is a separate snapshot, the entrySet/keySet/values views now
report unknown-size spliterators; a SIZED stream would otherwise throw
"End size N is less than fixed size M" when a key disappears mid-stream.

* test(storage): cover iterator remove after lookahead, exhaustion and entry setValue

* fix(storage): bind view spliterators lazily at first traversal

The view spliterators wrapped iterator() at spliterator() time, so the key
snapshot was taken when stream() was called rather than when the terminal
operation ran. Collection.spliterator()'s default, which these views used
before, is late-binding. Defer taking the iterator to the first tryAdvance
or split, still reporting unknown size.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-10-19 18:21:18 -04:00
1 parent 5774e7db8d
commit 1b89d5da39
2 files changed
+315 -6

No files matched your search

@@ -1,14 +1,21 @@
package io.github.hectorvent.floci.core.storage;
import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* A mutable {@link Map} facade over {@link StorageBackend}.
@@ -103,20 +110,36 @@ public class StorageBackedMap<V>
return new Iterator<>()
{
private String currentKey;
// One-element lookahead: a key in the snapshot may be deleted before
// we reach it, so skip keys whose value is gone instead of yielding null.
private String nextKey;
private V nextValue;
@Override
public boolean hasNext()
{
return keys.hasNext();
while (nextValue == null && keys.hasNext()) {
nextKey = keys.next();
nextValue = StorageBackedMap.this.get(nextKey);
}
return nextValue != null;
}
@Override
public Entry<String, V> next()
{
currentKey = keys.next();
String entryKey = currentKey;
if (!hasNext()) {
throw new NoSuchElementException();
}
currentKey = nextKey;
String entryKey = nextKey;
V entryValue = nextValue;
nextKey = null;
nextValue = null;
return new Entry<>()
{
private V value = entryValue;
@Override
public String getKey()
{
@@ -126,13 +149,15 @@ public class StorageBackedMap<V>
@Override
public V getValue()
{
return StorageBackedMap.this.get(entryKey);
return value;
}
@Override
public V setValue(V value)
public V setValue(V newValue)
{
return StorageBackedMap.this.put(entryKey, value);
V previous = StorageBackedMap.this.put(entryKey, newValue);
value = newValue;
return previous;
}
};
}
@@ -154,6 +179,134 @@ public class StorageBackedMap<V>
{
return snapshotKeys().size();
}
@Override
public Spliterator<Entry<String, V>> spliterator()
{
return unsized(this::iterator);
}
};
}
@Override
public Set<String> keySet()
{
return new AbstractSet<>()
{
@Override
public Iterator<String> iterator()
{
Iterator<Entry<String, V>> entries = entrySet().iterator();
return new Iterator<>()
{
@Override
public boolean hasNext()
{
return entries.hasNext();
}
@Override
public String next()
{
return entries.next().getKey();
}
@Override
public void remove()
{
entries.remove();
}
};
}
@Override
public int size()
{
return StorageBackedMap.this.size();
}
@Override
public boolean contains(Object key)
{
return containsKey(key);
}
@Override
public Spliterator<String> spliterator()
{
return unsized(this::iterator);
}
};
}
@Override
public Collection<V> values()
{
return new AbstractCollection<>()
{
@Override
public Iterator<V> iterator()
{
Iterator<Entry<String, V>> entries = entrySet().iterator();
return new Iterator<>()
{
@Override
public boolean hasNext()
{
return entries.hasNext();
}
@Override
public V next()
{
return entries.next().getValue();
}
@Override
public void remove()
{
entries.remove();
}
};
}
@Override
public int size()
{
return StorageBackedMap.this.size();
}
@Override
public Spliterator<V> spliterator()
{
return unsized(this::iterator);
}
};
}
// size() is a separate key snapshot from the one an iterator walks, and the
// iterator skips keys deleted in between, so the views must not report SIZED:
// a sized stream throws "End size N is less than fixed size M" on a mismatch.
// The iterator is taken on first traversal or split, not here, so the
// spliterator stays late-binding like the Collection.spliterator() default.
private static <T> Spliterator<T> unsized(Supplier<Iterator<T>> iterators)
{
return new Spliterators.AbstractSpliterator<>(Long.MAX_VALUE, Spliterator.NONNULL)
{
private Iterator<T> iterator;
@Override
public boolean tryAdvance(Consumer<? super T> action)
{
if (iterator == null) {
iterator = iterators.get();
}
if (!iterator.hasNext()) {
return false;
}
action.accept(iterator.next());
return true;
}
};
}
@@ -2,8 +2,12 @@ package io.github.hectorvent.floci.core.storage;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
@@ -12,9 +16,12 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -152,6 +159,155 @@ class StorageBackedMapTest
assertTrue(map.isEmpty());
}
@Test
void valuesSkipsKeyDeletedAfterIterationStarted()
{
StorageBackedMap<String> map = mapOf("alpha", "beta", "gamma");
List<String> seen = new ArrayList<>();
Iterator<String> iterator = map.values().iterator();
while (iterator.hasNext()) {
String value = iterator.next();
if (seen.isEmpty()) {
map.remove(otherKey(value));
}
seen.add(value);
}
assertEquals(2, seen.size());
assertFalse(seen.contains(null));
}
@Test
void entrySetSkipsKeyDeletedAfterIterationStarted()
{
StorageBackedMap<String> map = mapOf("alpha", "beta", "gamma");
List<Map.Entry<String, String>> seen = new ArrayList<>();
for (Map.Entry<String, String> entry : map.entrySet()) {
if (seen.isEmpty()) {
map.remove(otherKey(entry.getKey()));
}
seen.add(entry);
}
assertEquals(2, seen.size());
for (Map.Entry<String, String> entry : seen) {
assertNotNull(entry.getValue());
assertEquals(entry.getKey() + "-value", entry.getValue());
}
}
@Test
void valuesStreamSurvivesKeyDeletedDuringStream()
{
StorageBackedMap<String> map = mapOf("alpha", "beta", "gamma");
AtomicInteger calls = new AtomicInteger();
List<String> values = map.values().stream()
.map(value -> {
if (calls.getAndIncrement() == 0) {
map.remove(otherKey(value));
}
return value;
})
.toList();
assertEquals(2, values.size());
assertFalse(values.contains(null));
}
@Test
void keySetStreamSurvivesKeyDeletedDuringStream()
{
StorageBackedMap<String> map = mapOf("alpha", "beta", "gamma");
AtomicInteger calls = new AtomicInteger();
List<String> keys = map.keySet().stream()
.map(key -> {
if (calls.getAndIncrement() == 0) {
map.remove(otherKey(key));
}
return key;
})
.toList();
assertEquals(2, keys.size());
assertFalse(keys.contains(null));
}
@Test
void viewStreamsSeeKeyAddedBeforeTerminalOperation()
{
StorageBackedMap<String> map = mapOf("alpha");
Stream<String> keys = map.keySet().stream();
Stream<String> values = map.values().stream();
Stream<Map.Entry<String, String>> entries = map.entrySet().stream();
map.put("beta", "beta-value");
assertEquals(Set.of("alpha", "beta"), keys.collect(Collectors.toSet()));
assertEquals(Set.of("alpha-value", "beta-value"), values.collect(Collectors.toSet()));
assertEquals(Set.of("alpha", "beta"),
entries.map(Map.Entry::getKey).collect(Collectors.toSet()));
}
@Test
void iteratorRemoveAfterLookaheadRemovesTheYieldedKey()
{
StorageBackedMap<String> map = mapOf("alpha", "beta", "gamma");
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
String removed = iterator.next().getKey();
// hasNext() reads one element ahead; remove() must still hit the yielded key.
assertTrue(iterator.hasNext());
iterator.remove();
assertFalse(map.containsKey(removed));
assertEquals(2, map.size());
}
@Test
void iteratorThrowsNoSuchElementAtExhaustion()
{
StorageBackedMap<String> map = mapOf("alpha");
Iterator<String> iterator = map.values().iterator();
iterator.next();
assertFalse(iterator.hasNext());
assertThrows(NoSuchElementException.class, iterator::next);
}
@Test
void entrySetValueWritesThrough()
{
StorageBackedMap<String> map = mapOf("alpha");
Map.Entry<String, String> entry = map.entrySet().iterator().next();
assertEquals("alpha-value", entry.setValue("updated"));
assertEquals("updated", entry.getValue());
assertEquals("updated", map.get("alpha"));
}
private static StorageBackedMap<String> mapOf(String... keys)
{
StorageBackedMap<String> map = new StorageBackedMap<>(new InMemoryStorage<>());
for (String key : keys) {
map.put(key, key + "-value");
}
return map;
}
// Some key other than the one just yielded, so the deletion lands on a key the
// iterator has snapshotted but not reached, whatever order storage returns keys in.
private static String otherKey(String yielded)
{
String key = yielded.replace("-value", "");
return key.equals("gamma") ? "alpha" : "gamma";
}
private static void await(CountDownLatch latch)
{
try {