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
This commit is contained in:
Colby Ranger
2013-05-09 12:00:51 -07:00
parent de57ac051d
commit c5a1c9fa65
2 changed files with 29 additions and 8 deletions

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.server.project;
import com.google.gerrit.reviewdb.client.AccountGroup; import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.Project;
import java.io.IOException;
import java.util.Set; import java.util.Set;
/** Cache of project information, including access rights. */ /** 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. * Get the cached data for a project by its unique name.
* *
* @param projectName name of the project. * @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); 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. */ /** Invalidate the cached information about the given project. */
public void evict(Project p); public void evict(Project p);

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.server.project; package com.google.gerrit.server.project;
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;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
@@ -34,6 +35,7 @@ import org.eclipse.jgit.lib.Repository;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
@@ -101,13 +103,18 @@ public class ProjectCacheImpl implements ProjectCache {
return state; return state;
} }
/** @Override
* 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.
*/
public ProjectState get(final Project.NameKey projectName) { 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) { if (projectName == null) {
return null; return null;
} }
@@ -121,12 +128,14 @@ public class ProjectCacheImpl implements ProjectCache {
} catch (ExecutionException e) { } catch (ExecutionException e) {
if (!(e.getCause() instanceof RepositoryNotFoundException)) { if (!(e.getCause() instanceof RepositoryNotFoundException)) {
log.warn(String.format("Cannot read project %s", projectName.get()), e); log.warn(String.format("Cannot read project %s", projectName.get()), e);
Throwables.propagateIfInstanceOf(e.getCause(), IOException.class);
throw new IOException(e);
} }
return null; return null;
} }
} }
/** Invalidate the cached information about the given project. */ @Override
public void evict(final Project p) { public void evict(final Project p) {
if (p != null) { if (p != null) {
byName.invalidate(p.getNameKey().get()); byName.invalidate(p.getNameKey().get());