Propagate cause of a project not found in cache
Propagate the original exception when a project failed to be loaded into cache. That allows the callers to have a better understanding of what is going on and adjust the compensation logic to recover from it when possible. For instance, the caller may need to filter out the project if the exception isn't a critical one (e.g. RepositoryNotFoundException) Change-Id: Iaa4d050c376176b60c6feb7ad5cd3cac45f02b17
This commit is contained in:
@@ -47,6 +47,17 @@ public interface ProjectCache {
|
||||
*/
|
||||
ProjectState checkedGet(@Nullable Project.NameKey projectName) throws IOException;
|
||||
|
||||
/**
|
||||
* Get the cached data for a project by its unique name.
|
||||
*
|
||||
* @param projectName name of the project.
|
||||
* @param strict true when any error generates an exception
|
||||
* @throws Exception in case of any error (strict = true) or only for I/O or other internal
|
||||
* errors.
|
||||
* @return the cached data or null when strict = false
|
||||
*/
|
||||
public ProjectState checkedGet(Project.NameKey projectName, boolean strict) throws Exception;
|
||||
|
||||
/** Invalidate the cached information about the given project. */
|
||||
void evict(Project p);
|
||||
|
||||
|
||||
@@ -139,13 +139,8 @@ public class ProjectCacheImpl implements ProjectCache {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
ProjectState state = byName.get(projectName.get());
|
||||
if (state != null && state.needsRefresh(clock.read())) {
|
||||
byName.invalidate(projectName.get());
|
||||
state = byName.get(projectName.get());
|
||||
}
|
||||
return state;
|
||||
} catch (ExecutionException e) {
|
||||
return strictCheckedGet(projectName);
|
||||
} catch (Exception e) {
|
||||
if (!(e.getCause() instanceof RepositoryNotFoundException)) {
|
||||
log.warn(String.format("Cannot read project %s", projectName.get()), e);
|
||||
Throwables.throwIfInstanceOf(e.getCause(), IOException.class);
|
||||
@@ -156,6 +151,20 @@ public class ProjectCacheImpl implements ProjectCache {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProjectState checkedGet(Project.NameKey projectName, boolean strict) throws Exception {
|
||||
return strict ? strictCheckedGet(projectName) : checkedGet(projectName);
|
||||
}
|
||||
|
||||
private ProjectState strictCheckedGet(Project.NameKey projectName) throws Exception {
|
||||
ProjectState state = byName.get(projectName.get());
|
||||
if (state != null && state.needsRefresh(clock.read())) {
|
||||
byName.invalidate(projectName.get());
|
||||
state = byName.get(projectName.get());
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evict(Project p) {
|
||||
if (p != null) {
|
||||
|
||||
@@ -262,6 +262,12 @@ public class RefControlTest {
|
||||
|
||||
@Override
|
||||
public void evict(Project.NameKey p) {}
|
||||
|
||||
@Override
|
||||
public ProjectState checkedGet(Project.NameKey projectName, boolean strict)
|
||||
throws Exception {
|
||||
return all.get(projectName);
|
||||
}
|
||||
};
|
||||
|
||||
Injector injector = Guice.createInjector(new InMemoryModule());
|
||||
|
||||
Reference in New Issue
Block a user