ObjectDirectory: Reload alternates when objects/info/alternates changes ObjectDirectory read objects/info/alternates once and cached the result for the lifetime of the repository. If the file did not exist at first access, the resulting FileNotFoundException was swallowed and an empty array was cached. An alternates file created afterwards (or edited or deleted) was never noticed until the repository was closed, e.g. by a server restart. Add tests for an alternates file added and removed after the first object lookup. Change-Id: I4e0b892915ff791a323a7b7b13de71c1ee99a29c Signed-off-by: Adithya Chakilam <achakilam@nvidia.com>
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/AlternatesTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/AlternatesTest.java index f3a367c..560cf11 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/AlternatesTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/AlternatesTest.java
@@ -10,6 +10,7 @@ package org.eclipse.jgit.internal.storage.file; import static org.eclipse.jgit.lib.Constants.INFO_ALTERNATES; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.io.File; @@ -61,6 +62,31 @@ public void testAlternate() throws Exception { } @Test + public void testAlternateAddedAfterFirstAccess() throws Exception { + ObjectId id = ObjectId + .fromString("49322bb17d3acc9146f98c97d078513228bbf3c0"); + ObjectDirectory od = db2.getObjectDatabase(); + assertFalse(od.has(id)); + + setAlternate(db2, db); + assertTrue(od.has(id)); + assertAlternateObjects(db2); + } + + @Test + public void testAlternateRemovedAfterFirstAccess() throws Exception { + setAlternate(db2, db); + assertAlternateObjects(db2); + + File alt = new File(db2.getObjectDatabase().getDirectory(), + INFO_ALTERNATES); + assertTrue(alt.delete()); + ObjectId id = ObjectId + .fromString("49322bb17d3acc9146f98c97d078513228bbf3c0"); + assertFalse(db2.getObjectDatabase().has(id)); + } + + @Test public void testAlternateCyclic2() throws Exception { setAlternate(db2, db); setAlternate(db, db2);
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java index f7eeed0..8c7daa7 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java
@@ -101,6 +101,12 @@ private interface TriFunctionThrowsException<A, A2, A3, R, E extends Exception> private final AtomicReference<AlternateHandle[]> alternates; + /** + * Snapshot of the alternates file taken when it was last loaded, or null + * if the alternates were given explicitly and must not be refreshed. + */ + private volatile FileSnapshot alternatesSnapshot; + private final File shallowFile; private FileSnapshot shallowFileSnapshot = FileSnapshot.DIRTY; @@ -148,6 +154,8 @@ public ObjectDirectory(final Config cfg, final File dir, for (int i = 0; i < alternatePaths.length; i++) alt[i] = openAlternate(alternatePaths[i]); alternates.set(alt); + } else { + alternatesSnapshot = FileSnapshot.DIRTY; } } @@ -702,22 +710,49 @@ void closeAllPackHandles(File packFile) { AlternateHandle[] myAlternates() { AlternateHandle[] alt = alternates.get(); - if (alt == null) { - synchronized (alternates) { - alt = alternates.get(); - if (alt == null) { - try { - alt = loadAlternates(); - } catch (IOException e) { - alt = new AlternateHandle[0]; - } - alternates.set(alt); - } + FileSnapshot snap = alternatesSnapshot; + if (alt != null && (snap == null || !snap.isModified(alternatesFile))) { + return alt; + } + synchronized (alternates) { + alt = alternates.get(); + snap = alternatesSnapshot; + if (alt != null + && (snap == null || !snap.isModified(alternatesFile))) { + return alt; + } + AlternateHandle[] old = alt; + FileSnapshot newSnap = FileSnapshot.save(alternatesFile); + try { + alt = loadAlternates(old); + } catch (IOException e) { + alt = new AlternateHandle[0]; + } + alternates.set(alt); + alternatesSnapshot = newSnap; + if (old != null) { + closeUnused(old, alt); } } return alt; } + private static void closeUnused(AlternateHandle[] old, + AlternateHandle[] current) { + for (AlternateHandle o : old) { + boolean kept = false; + for (AlternateHandle c : current) { + if (c == o) { + kept = true; + break; + } + } + if (!kept) { + o.close(); + } + } + } + Set<AlternateHandle.Id> addMe(Set<AlternateHandle.Id> skips) { if (skips == null) { skips = new HashSet<>(); @@ -726,12 +761,23 @@ Set<AlternateHandle.Id> addMe(Set<AlternateHandle.Id> skips) { return skips; } - private AlternateHandle[] loadAlternates() throws IOException { + private AlternateHandle[] loadAlternates(AlternateHandle[] previous) + throws IOException { final List<AlternateHandle> l = new ArrayList<>(4); try (BufferedReader br = open(alternatesFile)) { String line; while ((line = br.readLine()) != null) { - l.add(openAlternate(line)); + File objdir = fs.resolve(objects, line); + AlternateHandle reuse = null; + if (previous != null) { + for (AlternateHandle p : previous) { + if (p.db.objects.equals(objdir)) { + reuse = p; + break; + } + } + } + l.add(reuse != null ? reuse : openAlternate(objdir)); } } return l.toArray(new AlternateHandle[0]); @@ -742,12 +788,6 @@ private static BufferedReader open(File f) return Files.newBufferedReader(f.toPath(), UTF_8); } - private AlternateHandle openAlternate(String location) - throws IOException { - final File objdir = fs.resolve(objects, location); - return openAlternate(objdir); - } - private AlternateHandle openAlternate(File objdir) throws IOException { final File parent = objdir.getParentFile(); if (FileKey.isGitRepository(parent, fs)) {