ProjectCacheImpl: Harden against null UUIDs

Since Ie51b350d GroupList#uuids may include null UUIDs. Regardless of
whether or not this is a good idea, these UUIDs are never relevant as
defined by ProjectCache#guessRelevantGroupUUIDs. Harden
ProjectCacheImpl against this state of affairs as well as future bugs.

Change-Id: I74c62a5009123d5d95fea7d39e08addfd29f816c
This commit is contained in:
Dave Borowitz
2016-10-19 16:49:36 -04:00
parent b1d431d3e4
commit ec2ef4f0c4

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.server.project; package com.google.gerrit.server.project;
import static java.util.stream.Collectors.toSet;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
@@ -40,9 +42,9 @@ import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@@ -200,25 +202,24 @@ public class ProjectCacheImpl implements ProjectCache {
} }
@Override @Override
public Iterable<Project.NameKey> all() { public SortedSet<Project.NameKey> all() {
try { try {
return list.get(ListKey.ALL); return list.get(ListKey.ALL);
} catch (ExecutionException e) { } catch (ExecutionException e) {
log.warn("Cannot list available projects", e); log.warn("Cannot list available projects", e);
return Collections.emptyList(); return Collections.emptySortedSet();
} }
} }
@Override @Override
public Set<AccountGroup.UUID> guessRelevantGroupUUIDs() { public Set<AccountGroup.UUID> guessRelevantGroupUUIDs() {
Set<AccountGroup.UUID> groups = new HashSet<>(); return all().stream().map(n -> byName.getIfPresent(n.get()))
for (Project.NameKey n : all()) { .filter(Objects::nonNull)
ProjectState p = byName.getIfPresent(n.get()); .flatMap(p -> p.getConfig().getAllGroupUUIDs().stream())
if (p != null) { // getAllGroupUUIDs shouldn't really return null UUIDs, but harden
groups.addAll(p.getConfig().getAllGroupUUIDs()); // against them just in case there is a bug or corner case.
} .filter(id -> id != null && id.get() != null)
} .collect(toSet());
return groups;
} }
@Override @Override