From c5a1c9fa659ccd482bb4de24531344c015f00d40 Mon Sep 17 00:00:00 2001 From: Colby Ranger Date: Thu, 9 May 2013 12:00:51 -0700 Subject: [PATCH] Add checkedGet() to ProjectCache. Currently get() squashes all exceptions to a null return value, making client unable to distinguish between not found or a transient error. Add a checkedGet() method, that raises IOException, when the exception is anything other than not found. Eventually, checkedGet() will replace get(), after all the callers have been updated to deal with the exception. Change-Id: I55294d0438cfc6164ca975a228006ad046d1961b --- .../gerrit/server/project/ProjectCache.java | 14 ++++++++++- .../server/project/ProjectCacheImpl.java | 23 +++++++++++++------ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/project/ProjectCache.java b/gerrit-server/src/main/java/com/google/gerrit/server/project/ProjectCache.java index 697b838bee..e7f2e3a747 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/project/ProjectCache.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/project/ProjectCache.java @@ -17,6 +17,7 @@ package com.google.gerrit.server.project; import com.google.gerrit.reviewdb.client.AccountGroup; import com.google.gerrit.reviewdb.client.Project; +import java.io.IOException; import java.util.Set; /** Cache of project information, including access rights. */ @@ -28,10 +29,21 @@ public interface ProjectCache { * Get the cached data for a project by its unique name. * * @param projectName name of the project. - * @return the cached data; null if no such project exists. + * @return the cached data; null if no such project exists or a error occured. + * @see #checkedGet(com.google.gerrit.reviewdb.client.Project.NameKey) */ public ProjectState get(Project.NameKey projectName); + /** + * Get the cached data for a project by its unique name. + * + * @param projectName name of the project. + * @throws IOException when there was an error. + * @return the cached data; null if no such project exists. + */ + public ProjectState checkedGet(Project.NameKey projectName) + throws IOException; + /** Invalidate the cached information about the given project. */ public void evict(Project p); diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/project/ProjectCacheImpl.java b/gerrit-server/src/main/java/com/google/gerrit/server/project/ProjectCacheImpl.java index 272c128b3c..58af4e12ff 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/project/ProjectCacheImpl.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/project/ProjectCacheImpl.java @@ -14,6 +14,7 @@ package com.google.gerrit.server.project; +import com.google.common.base.Throwables; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; import com.google.common.collect.Sets; @@ -34,6 +35,7 @@ import org.eclipse.jgit.lib.Repository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; import java.util.Collections; import java.util.Iterator; import java.util.NoSuchElementException; @@ -101,13 +103,18 @@ public class ProjectCacheImpl implements ProjectCache { return state; } - /** - * Get the cached data for a project by its unique name. - * - * @param projectName name of the project. - * @return the cached data; null if no such project exists. - */ + @Override public ProjectState get(final Project.NameKey projectName) { + try { + return checkedGet(projectName); + } catch (IOException e) { + return null; + } + } + + @Override + public ProjectState checkedGet(Project.NameKey projectName) + throws IOException { if (projectName == null) { return null; } @@ -121,12 +128,14 @@ public class ProjectCacheImpl implements ProjectCache { } catch (ExecutionException e) { if (!(e.getCause() instanceof RepositoryNotFoundException)) { log.warn(String.format("Cannot read project %s", projectName.get()), e); + Throwables.propagateIfInstanceOf(e.getCause(), IOException.class); + throw new IOException(e); } return null; } } - /** Invalidate the cached information about the given project. */ + @Override public void evict(final Project p) { if (p != null) { byName.invalidate(p.getNameKey().get());