summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test
diff options
context:
space:
mode:
authorChristian Halstrick <christian.halstrick@sap.com>2016-05-30 16:09:58 +0200
committerMatthias Sohn <matthias.sohn@sap.com>2016-07-12 11:32:49 +0200
commit7ffe547da79bf26301c5e5a0665d19a233b44818 (patch)
treea56af114e97af32529548eda812c557d068dc28d /org.eclipse.jgit.test
parentc1ca9cc80004b4eaa90f18a8fea251d565304e56 (diff)
downloadjgit-7ffe547da79bf26301c5e5a0665d19a233b44818.tar.gz
jgit-7ffe547da79bf26301c5e5a0665d19a233b44818.zip
Time based eviction strategy for repository cache
When Repository.close() decrements the useCount to 0 currently the cache immediately evicts the repository from WindowCache and RepositoryCache. This leads to I/O overhead on busy repositories because pack files and references are inserted and deleted from the cache frequently. This commit defers the eviction of a repository from the caches until last use of the repository is older than time to live. The eviction is handled by a background task running periodically. Add two new configuration parameters: * core.repositoryCacheExpireAfter: cache entries are evicted if the cache entry wasn't accessed longer than this time in milliseconds * core.repositoryCacheCleanupDelay: defines the interval in milliseconds for running a background task evicting expired cache entries. If set to -1 the delay is set to min(repositoryCacheExpireAfter, 10 minutes). If set to 0 the time based eviction is switched off and no background task is started. If time based eviction is switched off the JVM can still evict cache entries if heap memory is running low. Change-Id: I4a0214ad8b4a193985dda6a0ade63b70bdb948d7 Also-by: Matthias Sohn <matthias.sohn@sap.com> Also-by: Hugo Arès <hugo.ares@ericsson.com> Also-by: Sasa Zivkov <sasa.zivkov@sap.com>
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheConfigTest.java107
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheTest.java72
2 files changed, 175 insertions, 4 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheConfigTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheConfigTest.java
new file mode 100644
index 0000000000..52cc9fb030
--- /dev/null
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheConfigTest.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2016 Ericsson
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.eclipse.jgit.lib;
+
+import static org.eclipse.jgit.lib.RepositoryCacheConfig.AUTO_CLEANUP_DELAY;
+import static org.eclipse.jgit.lib.RepositoryCacheConfig.NO_CLEANUP;
+import static org.junit.Assert.assertEquals;
+
+import java.util.concurrent.TimeUnit;
+
+import org.eclipse.jgit.errors.ConfigInvalidException;
+import org.junit.Before;
+import org.junit.Test;
+
+public class RepositoryCacheConfigTest {
+
+ private RepositoryCacheConfig config;
+
+ @Before
+ public void setUp() {
+ config = new RepositoryCacheConfig();
+ }
+
+ @Test
+ public void testDefaultValues() {
+ assertEquals(TimeUnit.HOURS.toMillis(1), config.getExpireAfter());
+ assertEquals(config.getExpireAfter() / 10, config.getCleanupDelay());
+ }
+
+ @Test
+ public void testCleanupDelay() {
+ config.setCleanupDelay(TimeUnit.HOURS.toMillis(1));
+ assertEquals(TimeUnit.HOURS.toMillis(1), config.getCleanupDelay());
+ }
+
+ @Test
+ public void testAutoCleanupDelay() {
+ config.setExpireAfter(TimeUnit.MINUTES.toMillis(20));
+ config.setCleanupDelay(AUTO_CLEANUP_DELAY);
+ assertEquals(TimeUnit.MINUTES.toMillis(20), config.getExpireAfter());
+ assertEquals(config.getExpireAfter() / 10, config.getCleanupDelay());
+ }
+
+ @Test
+ public void testAutoCleanupDelayShouldBeMax10minutes() {
+ config.setExpireAfter(TimeUnit.HOURS.toMillis(10));
+ assertEquals(TimeUnit.HOURS.toMillis(10), config.getExpireAfter());
+ assertEquals(TimeUnit.MINUTES.toMillis(10), config.getCleanupDelay());
+ }
+
+ @Test
+ public void testDisabledCleanupDelay() {
+ config.setCleanupDelay(NO_CLEANUP);
+ assertEquals(NO_CLEANUP, config.getCleanupDelay());
+ }
+
+ @Test
+ public void testFromConfig() throws ConfigInvalidException {
+ Config otherConfig = new Config();
+ otherConfig.fromText("[core]\nrepositoryCacheExpireAfter=1000\n"
+ + "repositoryCacheCleanupDelay=500");
+ config.fromConfig(otherConfig);
+ assertEquals(1000, config.getExpireAfter());
+ assertEquals(500, config.getCleanupDelay());
+ }
+}
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 a1cec2d914..6bea320120 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
@@ -195,17 +195,81 @@ public class RepositoryCacheTest extends RepositoryTestCase {
assertEquals(0, ((Repository) db).useCnt.get());
}
- public void testRepositoryUnregisteringWhenClosing() throws Exception {
+ @Test
+ public void testRepositoryNotUnregisteringWhenClosing() throws Exception {
FileKey loc = FileKey.exact(db.getDirectory(), db.getFS());
Repository d2 = RepositoryCache.open(loc);
assertEquals(1, d2.useCnt.get());
assertThat(RepositoryCache.getRegisteredKeys(),
hasItem(FileKey.exact(db.getDirectory(), db.getFS())));
assertEquals(1, RepositoryCache.getRegisteredKeys().size());
-
d2.close();
-
assertEquals(0, d2.useCnt.get());
- assertEquals(0, RepositoryCache.getRegisteredKeys().size());
+ assertEquals(1, RepositoryCache.getRegisteredKeys().size());
+ assertTrue(RepositoryCache.isCached(d2));
+ }
+
+ @Test
+ public void testRepositoryUnregisteringWhenExpired() throws Exception {
+ Repository repoA = createBareRepository();
+ Repository repoB = createBareRepository();
+ Repository repoC = createBareRepository();
+ RepositoryCache.register(repoA);
+ RepositoryCache.register(repoB);
+ RepositoryCache.register(repoC);
+
+ assertEquals(3, RepositoryCache.getRegisteredKeys().size());
+ assertTrue(RepositoryCache.isCached(repoA));
+ assertTrue(RepositoryCache.isCached(repoB));
+ assertTrue(RepositoryCache.isCached(repoC));
+
+ // fake that repoA was closed more than 1 hour ago (default expiration
+ // time)
+ repoA.close();
+ repoA.closedAt.set(System.currentTimeMillis() - 65 * 60 * 1000);
+ // close repoB but this one will not be expired
+ repoB.close();
+
+ assertEquals(3, RepositoryCache.getRegisteredKeys().size());
+ assertTrue(RepositoryCache.isCached(repoA));
+ assertTrue(RepositoryCache.isCached(repoB));
+ assertTrue(RepositoryCache.isCached(repoC));
+
+ RepositoryCache.clearExpired();
+
+ assertEquals(2, RepositoryCache.getRegisteredKeys().size());
+ assertFalse(RepositoryCache.isCached(repoA));
+ assertTrue(RepositoryCache.isCached(repoB));
+ assertTrue(RepositoryCache.isCached(repoC));
+ }
+
+ @Test
+ public void testReconfigure() throws InterruptedException {
+ RepositoryCache.register(db);
+ assertTrue(RepositoryCache.isCached(db));
+ db.close();
+ assertTrue(RepositoryCache.isCached(db));
+
+ // Actually, we would only need to validate that
+ // WorkQueue.getExecutor().scheduleWithFixedDelay is called with proper
+ // values but since we do not have a mock library, we test
+ // reconfiguration from a black box perspective. I.e. reconfigure
+ // expireAfter and cleanupDelay to 1 ms and wait until the Repository
+ // is evicted to prove that reconfiguration worked.
+ RepositoryCacheConfig config = new RepositoryCacheConfig();
+ config.setExpireAfter(1);
+ config.setCleanupDelay(1);
+ config.install();
+
+ // Instead of using a fixed waiting time, start with small and increase:
+ // sleep 1, 2, 4, 8, 16, ..., 1024 ms
+ // This wait will time out after 2048 ms
+ for (int i = 0; i <= 10; i++) {
+ Thread.sleep(1 << i);
+ if (!RepositoryCache.isCached(db)) {
+ return;
+ }
+ }
+ fail("Repository should have been evicted from cache");
}
}