Skip to content

Commit 3cab5e4

Browse files
committed
do not modify history cache in FileHistoryCache#get()
1 parent d6d8512 commit 3cab5e4

File tree

7 files changed

+26
-103
lines changed

7 files changed

+26
-103
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/Configuration.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,6 @@ public final class Configuration {
107107
* Should the history log be cached?
108108
*/
109109
private boolean historyCache;
110-
/**
111-
* The maximum time in milliseconds {@code HistoryCache.get()} can take
112-
* before its result is cached.
113-
*/
114-
private int historyCacheTime;
115110
/**
116111
* flag to generate history. This is bigger hammer than @{code historyCache}
117112
* above. If set to false, no history query will be ever made and the webapp
@@ -543,7 +538,6 @@ public Configuration() {
543538
setGroupsCollapseThreshold(4);
544539
setHandleHistoryOfRenamedFiles(false);
545540
setHistoryCache(true);
546-
setHistoryCacheTime(30);
547541
setHistoryEnabled(true);
548542
setHitsPerPage(25);
549543
setIgnoredNames(new IgnoredNames());
@@ -807,28 +801,6 @@ public void setHistoryCache(boolean historyCache) {
807801
this.historyCache = historyCache;
808802
}
809803

810-
/**
811-
* How long can a history request take before it's cached? If the time is
812-
* exceeded, the result is cached. This setting only affects
813-
* {@code FileHistoryCache}.
814-
*
815-
* @return the maximum time in milliseconds a history request can take
816-
* before it's cached
817-
*/
818-
public int getHistoryCacheTime() {
819-
return historyCacheTime;
820-
}
821-
822-
/**
823-
* Set the maximum time a history request can take before it's cached. This
824-
* setting is only respected if {@code FileHistoryCache} is used.
825-
*
826-
* @param historyCacheTime maximum time in milliseconds
827-
*/
828-
public void setHistoryCacheTime(int historyCacheTime) {
829-
this.historyCacheTime = historyCacheTime;
830-
}
831-
832804
public boolean isFetchHistoryWhenNotInCache() {
833805
return fetchHistoryWhenNotInCache;
834806
}

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/RuntimeEnvironment.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -656,25 +656,6 @@ public Set<String> getCtagsLanguages() {
656656
return Collections.unmodifiableSet(ctagsLanguages);
657657
}
658658

659-
/**
660-
* Get the max time a SCM operation may use to avoid being cached.
661-
*
662-
* @return the maximum time in milliseconds
663-
*/
664-
public int getHistoryReaderTimeLimit() {
665-
return syncReadConfiguration(Configuration::getHistoryCacheTime);
666-
}
667-
668-
/**
669-
* Specify the maximum time a SCM operation should take before it will be
670-
* cached (in ms).
671-
*
672-
* @param historyCacheTime the max time in ms before it is cached
673-
*/
674-
public void setHistoryReaderTimeLimit(int historyCacheTime) {
675-
syncWriteConfiguration(historyCacheTime, Configuration::setHistoryCacheTime);
676-
}
677-
678659
/**
679660
* Is history cache currently enabled?
680661
*

opengrok-indexer/src/main/java/org/opengrok/indexer/history/FileHistoryCache.java

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ public void initialize() {
177177
}
178178
}
179179

180+
double getFileHistoryCacheHits() {
181+
return fileHistoryCacheHits.count();
182+
}
183+
180184
@Override
181185
public void optimize() {
182186
// nothing to do
@@ -669,39 +673,14 @@ public History get(File file, Repository repository, boolean withFiles)
669673
}
670674

671675
final History history;
672-
long time;
673676
try {
674-
time = System.currentTimeMillis();
675677
history = repository.getHistory(file);
676-
time = System.currentTimeMillis() - time;
677678
} catch (UnsupportedOperationException e) {
678679
// In this case, we've found a file for which the SCM has no history
679680
// An example is a non-SCCS file somewhere in an SCCS-controlled workspace.
680681
return null;
681682
}
682683

683-
// Don't cache history-information for directories, since the
684-
// history information on the directory may change if a file in
685-
// a sub-directory change. This will cause us to present a stale
686-
// history log until the current directory is updated and
687-
// invalidates the cache entry.
688-
if (!file.isDirectory()) {
689-
// Either the cache is stale or retrieving the history took too long, cache it!
690-
if (cacheFile.exists()) {
691-
if (LOGGER.isLoggable(Level.FINEST)) {
692-
LOGGER.log(Level.FINEST, "refreshing history for ''{0}'': {1}",
693-
new Object[]{file, history.getRevisionList()});
694-
}
695-
storeFile(history, file, repository);
696-
} else if (time > env.getHistoryReaderTimeLimit()) {
697-
if (LOGGER.isLoggable(Level.FINEST)) {
698-
LOGGER.log(Level.FINEST, "getting history for ''{0}'' took longer than {1} ms, caching it: {2}",
699-
new Object[]{file, env.getHistoryReaderTimeLimit(), history.getRevisionList()});
700-
}
701-
storeFile(history, file, repository);
702-
}
703-
}
704-
705684
return history;
706685
}
707686

opengrok-indexer/src/test/java/org/opengrok/indexer/configuration/ConfigMergeTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
*
3232
* @author vkotal
3333
*/
34-
public class ConfigMergeTest {
34+
class ConfigMergeTest {
3535
@Test
36-
public void basicTest() throws Exception {
36+
void basicTest() throws Exception {
3737

3838
String srcRoot = "/foo";
3939
String dataRoot = "/bar";

opengrok-indexer/src/test/java/org/opengrok/indexer/configuration/RuntimeEnvironmentTest.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,6 @@ public void testCtags() {
215215
"instance ctags should equals 'ctags' or the sys property");
216216
}
217217

218-
@Test
219-
public void testHistoryReaderTimeLimit() {
220-
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
221-
assertEquals(30, instance.getHistoryReaderTimeLimit());
222-
instance.setHistoryReaderTimeLimit(50);
223-
assertEquals(50, instance.getHistoryReaderTimeLimit());
224-
}
225-
226218
@Test
227219
public void testFetchHistoryWhenNotInCache() {
228220
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();

opengrok-indexer/src/test/java/org/opengrok/indexer/history/FileHistoryCacheTest.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
import org.opengrok.indexer.configuration.IgnoredNames;
6666
import org.opengrok.indexer.configuration.RuntimeEnvironment;
6767
import org.opengrok.indexer.util.IOUtils;
68-
import org.opengrok.indexer.util.TandemPath;
6968
import org.opengrok.indexer.util.TestRepository;
7069

7170
/**
@@ -157,8 +156,9 @@ private void assertSameEntry(HistoryEntry expected, HistoryEntry actual, boolean
157156

158157
/**
159158
* {@link FileHistoryCache#get(File, Repository, boolean)} should not disturb history cache
160-
* if run after repository update and reindex.
159+
* if run between repository update and reindex.
161160
*/
161+
@EnabledOnOs({OS.LINUX, OS.MAC, OS.SOLARIS, OS.AIX, OS.OTHER})
162162
@EnabledForRepository(MERCURIAL)
163163
@Test
164164
void testStoreTouchGet() throws Exception {
@@ -192,14 +192,17 @@ void testStoreTouchGet() throws Exception {
192192
// This makes sure that the file which contains the latest revision has indeed been created.
193193
assertEquals("11:bbb3ce75e1b8", cache.getLatestCachedRevision(repo));
194194

195-
// The history should not be disturbed.
196-
// Make sure that get() retrieved the history from cache.
195+
/*
196+
* The history should not be disturbed.
197+
* Make sure that get() retrieved the history from cache. Mocking/spying static methods
198+
* (FileHistoryCache#readCache() in this case) is tricky so use the cache hits metric.
199+
*/
197200
double cacheHitsBeforeGet = cache.getFileHistoryCacheHits();
198201
History historyAfterReindex = cache.get(file, repo, false);
199202
double cacheHitsAfterGet = cache.getFileHistoryCacheHits();
200203
assertNotNull(historyAfterReindex);
201204
assertEquals(historyAfterImport, historyAfterReindex);
202-
assertTrue(cacheHitsAfterGet - cacheHitsBeforeGet == 1);
205+
assertEquals(1, cacheHitsAfterGet - cacheHitsBeforeGet);
203206
}
204207

205208
/**
@@ -884,8 +887,7 @@ void testRenamedFile() throws Exception {
884887
updatedHistory.getHistoryEntries(), false);
885888
}
886889

887-
private void checkNoHistoryFetchRepo(String reponame, String filename,
888-
boolean hasHistory, boolean historyFileExists) throws Exception {
890+
private void checkNoHistoryFetchRepo(String reponame, String filename, boolean hasHistory) throws Exception {
889891

890892
File reposRoot = new File(repositories.getSourceRoot(), reponame);
891893
Repository repo = RepositoryFactory.getRepository(reposRoot);
@@ -899,13 +901,6 @@ private void checkNoHistoryFetchRepo(String reponame, String filename,
899901
// in history cache.
900902
History retrievedHistory = cache.get(repoFile, repo, true);
901903
assertEquals(hasHistory, retrievedHistory != null);
902-
903-
// The file in history cache should not exist since
904-
// FetchHistoryWhenNotInCache is set to false.
905-
File dataRoot = new File(repositories.getDataRoot(),
906-
"historycache" + File.separatorChar + reponame);
907-
File fileHistory = new File(dataRoot, TandemPath.join(filename, ".gz"));
908-
assertEquals(historyFileExists, fileHistory.exists());
909904
}
910905

911906
/*
@@ -921,9 +916,9 @@ void testNoHistoryFetch() throws Exception {
921916
cache.setHistoryIndexDone();
922917

923918
// First try repo with ability to fetch history for directories.
924-
checkNoHistoryFetchRepo("mercurial", "main.c", false, false);
919+
checkNoHistoryFetchRepo("mercurial", "main.c", false);
925920
// Second try repo which can fetch history of individual files only.
926-
checkNoHistoryFetchRepo("teamware", "header.h", true, true);
921+
checkNoHistoryFetchRepo("teamware", "header.h", true);
927922
}
928923

929924
/**

opengrok-indexer/src/test/java/org/opengrok/indexer/index/IndexerTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,35 +320,39 @@ public void reset() {
320320
/**
321321
* Test that reindex after changing a file does not wipe out history index
322322
* for this file. This is important for the incremental history indexing.
323-
* @throws Exception
324323
*/
325324
@Test
326325
@EnabledForRepository(MERCURIAL)
327326
void testRemoveFileOnFileChange() throws Exception {
328327
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
329328

329+
String path = "/mercurial/bar.txt";
330+
330331
TestRepository testrepo = new TestRepository();
331332
testrepo.create(HistoryGuru.class.getResourceAsStream("repositories.zip"));
332333

333334
env.setSourceRoot(testrepo.getSourceRoot());
334335
env.setDataRoot(testrepo.getDataRoot());
335336
env.setRepositories(testrepo.getSourceRoot());
336337

338+
// Create history cache.
339+
Indexer.getInstance().prepareIndexer(env, true, true,
340+
false, null, List.of("mercurial"));
341+
File historyFile = new File(env.getDataRootPath(),
342+
TandemPath.join("historycache" + path, ".gz"));
343+
assertTrue(historyFile.exists(), String.format("history cache for %s has to exist", path));
344+
337345
// create index
338346
Project project = new Project("mercurial", "/mercurial");
339347
IndexDatabase idb = new IndexDatabase(project);
340348
assertNotNull(idb);
341-
String path = "/mercurial/bar.txt";
342349
RemoveIndexChangeListener listener = new RemoveIndexChangeListener(path);
343350
idb.addIndexChangedListener(listener);
344351
idb.update();
345352
assertEquals(5, listener.filesToAdd.size());
346353
listener.reset();
347354

348355
// Change a file so that it gets picked up by the indexer.
349-
File historyFile = new File(env.getDataRootPath(),
350-
TandemPath.join("historycache" + path, ".gz"));
351-
assertTrue(historyFile.exists(), String.format("history cache for %s has to exist", path));
352356
File bar = new File(testrepo.getSourceRoot() + File.separator + "mercurial", "bar.txt");
353357
assertTrue(bar.exists());
354358
try (BufferedWriter bw = new BufferedWriter(new FileWriter(bar, true))) {

0 commit comments

Comments
 (0)