From 5a1e147eaa729ba0ba75bdeebcc745a55bff6232 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Ar=C3=A8s?= Date: Wed, 12 Oct 2016 06:54:52 -0400 Subject: [PATCH 1/2] Fix eviction of repositories with negative usage count MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the repository close method was called twice (or more) for one open, the usage count became negative and the repository was never be evicted from the cache because the method checking if repository is expired was not considering negative usage count. Change-Id: I18a80c415c54c37d1b9def2b311ff2d0afa455ca Signed-off-by: Hugo Arès --- .../eclipse/jgit/lib/RepositoryCacheTest.java | 21 +++++++++++++++++++ .../org/eclipse/jgit/lib/RepositoryCache.java | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheTest.java index 6bea32012..b44b4c34a 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheTest.java @@ -209,6 +209,27 @@ public class RepositoryCacheTest extends RepositoryTestCase { assertTrue(RepositoryCache.isCached(d2)); } + @Test + public void testRepositoryUnregisteringWhenExpiredAndUsageCountNegative() + throws Exception { + Repository repoA = createBareRepository(); + RepositoryCache.register(repoA); + + assertEquals(1, RepositoryCache.getRegisteredKeys().size()); + assertTrue(RepositoryCache.isCached(repoA)); + + // close the repo twice to make usage count negative + repoA.close(); + repoA.close(); + // fake that repoA was closed more than 1 hour ago (default expiration + // time) + repoA.closedAt.set(System.currentTimeMillis() - 65 * 60 * 1000); + + RepositoryCache.clearExpired(); + + assertEquals(0, RepositoryCache.getRegisteredKeys().size()); + } + @Test public void testRepositoryUnregisteringWhenExpired() throws Exception { Repository repoA = createBareRepository(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java index 2e0ed16a5..98ac60113 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java @@ -298,7 +298,7 @@ public class RepositoryCache { } private boolean isExpired(Repository db) { - return db != null && db.useCnt.get() == 0 + return db != null && db.useCnt.get() <= 0 && (System.currentTimeMillis() - db.closedAt.get() > expireAfter); } From 535f0afd13b3df42be5cfc9e93fddcbb223aa2f6 Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Tue, 11 Oct 2016 15:28:41 +0200 Subject: [PATCH 2/2] Unconditionally close repositories in RepositoryCache.clear() Earlier we tried to close the repository before removing it from the cache, so close only reduced refcount but didn't close it. Now that we no longer leak usage count on purpose and the usage count is now ignored anyway, there is no longer a need to run the removal twice. Change-Id: I8b62cec6d8a3e88c096d1f37a1f7f5a5066c90a0 Signed-off-by: Matthias Sohn --- .../src/org/eclipse/jgit/lib/RepositoryCache.java | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java index 98ac60113..7a8d246df 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java @@ -326,15 +326,9 @@ public class RepositoryCache { } private void clearAll() { - for (int stage = 0; stage < 2; stage++) { - for (Iterator>> i = cacheMap - .entrySet().iterator(); i.hasNext();) { - final Map.Entry> e = i.next(); - final Repository db = e.getValue().get(); - if (db != null) - db.close(); - i.remove(); - } + for (Iterator>> i = cacheMap + .entrySet().iterator(); i.hasNext();) { + unregisterAndCloseRepository(i.next().getKey(), null); } }