Skip to content

convert File History cache serialization to Smile #4268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
*/
package org.opengrok.indexer.history;

Expand Down Expand Up @@ -132,6 +132,6 @@ public void clearFile(String path) {
}

public String getCacheFileSuffix() {
return ".gz";
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@
import org.opengrok.indexer.util.ForbiddenSymlinkException;
import org.opengrok.indexer.util.IOUtils;

import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPOutputStream;

/**
* Helper functions for {@link HistoryCache} and {@link AnnotationCache} implementations.
Expand All @@ -51,20 +47,6 @@ private CacheUtil() {
// private to enforce static
}

/**
* Write serialized object to file.
* @param object object to be stored
* @param output output file
* @throws IOException on error
*/
public static void writeCache(Object object, File output) throws IOException {
try (FileOutputStream out = new FileOutputStream(output);
XMLEncoder e = new XMLEncoder(new GZIPOutputStream(new BufferedOutputStream(out)))) {
e.setPersistenceDelegate(File.class, new FileHistoryCache.FilePersistenceDelegate());
e.writeObject(object);
}
}

/**
*
* @param repository {@link RepositoryInfo} instance
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2006, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2023, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2019, Chris Fraire <cfraire@me.com>.
*/
package org.opengrok.indexer.history;
Expand All @@ -32,6 +32,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.jetbrains.annotations.VisibleForTesting;
import org.opengrok.indexer.logger.LoggerFactory;

Expand All @@ -55,6 +56,7 @@ public class HistoryEntry implements Serializable {
private final StringBuffer message;

private boolean active;
@JsonIgnore
private SortedSet<String> files;

/** Creates a new instance of HistoryEntry. */
Expand Down Expand Up @@ -94,6 +96,7 @@ public HistoryEntry(String revision, Date date, String author, String message, b
this.revision = revision;
}

@JsonIgnore
public String getLine() {
return String.join(" ",
getRevision(), getDate().toString(), getAuthor(), message, "\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ protected void doCreateCache(HistoryCache cache, String sinceRevision, File dire
if (fileCollector != null) {
visitors.add(fileCollector);
}
try (Progress progress = new Progress(LOGGER, String.format(" changesets traversed of %s", this))) {
try (Progress progress = new Progress(LOGGER, String.format("changesets traversed of %s", this),
Level.FINER)) {
ProgressVisitor progressVisitor = new ProgressVisitor(progress);
visitors.add(progressVisitor);
traverseHistory(directory, sinceRevision, null, null, visitors);
Expand Down Expand Up @@ -186,7 +187,8 @@ protected void doCreateCache(HistoryCache cache, String sinceRevision, File dire
}

try (Progress progress = new Progress(LOGGER,
String.format(" changesets traversed of %s (range %s %s)", this, sinceRevision, tillRevision))) {
String.format("changesets traversed of %s (range %s %s)", this, sinceRevision, tillRevision),
Level.FINER)) {
ProgressVisitor progressVisitor = new ProgressVisitor(progress);
visitors.add(progressVisitor);
traverseHistory(directory, sinceRevision, tillRevision, null, visitors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public Progress(Logger logger, String suffix, long totalCount, Level logLevel) {
Level.FINE, Level.FINER, Level.FINEST, Level.ALL);
int i = standardLevels.indexOf(baseLogLevel);
for (int num : new int[]{100, 50, 10, 1}) {
if (i >= standardLevels.size()) {
break;
}

Level level = standardLevels.get(i);
if (level == null) {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2018, 2020, Chris Fraire <cfraire@me.com>.
* Portions Copyright (c) 2020, 2023, Ric Harris <harrisric@users.noreply.github.com>.
*/
Expand All @@ -28,7 +28,6 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opengrok.indexer.condition.RepositoryInstalled.Type.MERCURIAL;
import static org.opengrok.indexer.condition.RepositoryInstalled.Type.SCCS;
Expand All @@ -38,18 +37,26 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import com.fasterxml.jackson.dataformat.smile.SmileParser;
import com.fasterxml.jackson.dataformat.smile.databind.SmileMapper;
import org.apache.commons.lang3.time.DateUtils;
import org.eclipse.jgit.api.Git;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -196,7 +203,7 @@ void testStoreTouchGet() throws Exception {
/*
* The history should not be disturbed.
* Make sure that get() retrieved the history from cache. Mocking/spying static methods
* (FileHistoryCache#readCache() in this case) is tricky so use the cache hits metric.
* (FileHistoryCache#readHistory() in this case) is tricky so use the cache hits metric.
*/
double cacheHitsBeforeGet = cache.getFileHistoryCacheHits();
History historyAfterReindex = cache.get(file, repo, false);
Expand Down Expand Up @@ -251,7 +258,7 @@ void testStoreAndGetIncrementalTags() throws Exception {
// Avoid uncommitted changes.
MercurialRepositoryTest.runHgCommand(reposRoot, "revert", "--all");

// Add bunch of changesets with file based changes and tags.
// Add a bunch of changesets with file based changes and tags.
MercurialRepositoryTest.runHgCommand(reposRoot, "import",
Paths.get(getClass().getResource("/history/hg-export-tag.txt").toURI()).toString());

Expand Down Expand Up @@ -396,14 +403,15 @@ void testStoreAndGet() throws Exception {
/**
* Check how incremental reindex behaves when indexing changesets that
* rename+change file.
*
* <p>
* The scenario goes as follows:
* - create Mercurial repository
* - perform full reindex
* - add changesets which renamed and modify a file
* - perform incremental reindex
* - change+rename the file again
* - incremental reindex
* </p>
*/
@EnabledOnOs({OS.LINUX, OS.MAC, OS.SOLARIS, OS.AIX, OS.OTHER})
@EnabledForRepository(MERCURIAL)
Expand Down Expand Up @@ -984,59 +992,40 @@ void testStoreAndTryToGetIgnored() throws Exception {
assertNotNull(retrievedHistory, "history for Makefile should not be null");
}

@ParameterizedTest
@ValueSource(strings = {
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<java version=\"11.0.8\" class=\"java.beans.XMLDecoder\">\n" +
" <object class=\"java.lang.Runtime\" method=\"getRuntime\">\n" +
" <void method=\"exec\">\n" +
" <array class=\"java.lang.String\" length=\"2\">\n" +
" <void index=\"0\">\n" +
" <string>/usr/bin/nc</string>\n" +
" </void>\n" +
" <void index=\"1\">\n" +
" <string>-l</string>\n" +
" </void>\n" +
" </array>\n" +
" </void>\n" +
" </object>\n" +
"</java>",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<java version=\"11.0.8\" class=\"java.beans.XMLDecoder\">\n" +
" <object class=\"java.lang.ProcessBuilder\">\n" +
" <array class=\"java.lang.String\" length=\"1\" >\n" +
" <void index=\"0\"> \n" +
" <string>/usr/bin/curl https://oracle.com</string>\n" +
" </void>\n" +
" </array>\n" +
" <void method=\"start\"/>\n" +
" </object>\n" +
"</java>",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<java version=\"11.0.8\" class=\"java.beans.XMLDecoder\">\n" +
" <object class = \"java.io.FileOutputStream\"> \n" +
" <string>opengrok_test.txt</string>\n" +
" <method name = \"write\">\n" +
" <array class=\"byte\" length=\"3\">\n" +
" <void index=\"0\"><byte>96</byte></void>\n" +
" <void index=\"1\"><byte>96</byte></void>\n" +
" <void index=\"2\"><byte>96</byte></void>\n" +
" </array>\n" +
" </method>\n" +
" <method name=\"close\"/>\n" +
" </object>\n" +
"</java>"
})
void testDeserializationOfNotWhiteListedClassThrowsError(final String exploit) {
assertThrows(IllegalAccessError.class, () -> FileHistoryCache.readCache(exploit));
}

// TODO
@Test
void testReadCacheValid() throws IOException {
File testFile = new File(FileHistoryCacheTest.class.getClassLoader().
getResource("history/FileHistoryCache.java.gz").getFile());
History history = FileHistoryCache.readCache(testFile);
assertNotNull(history);
assertEquals(30, history.getHistoryEntries().size());
void testSmile() throws Exception {
ObjectMapper mapper = new SmileMapper();
File outputFile = new File("/tmp/histentry");
ObjectWriter objectWriter = mapper.writer().forType(HistoryEntry.class);

try (OutputStream outputStream = new FileOutputStream(outputFile)) {
HistoryEntry historyEntry = new HistoryEntry("1.1.1", "1",
new Date(1245446973L / 60 * 60 * 1000),
"xyz",
"Return failure when executed with no arguments",
true, Collections.emptyList());
byte[] bytes = objectWriter.writeValueAsBytes(historyEntry);
outputStream.write(bytes);

historyEntry = new HistoryEntry("2.2.2", "2",
new Date(1245446973L / 60 * 60 * 1000),
"xyz",
"Return failure when executed with no arguments",
true, Collections.emptyList());
bytes = objectWriter.writeValueAsBytes(historyEntry);
outputStream.write(bytes);
}

SmileFactory factory = new SmileFactory();
List<HistoryEntry> historyEntryList = new ArrayList<>();
try (SmileParser parser = factory.createParser(outputFile)) {
parser.setCodec(mapper);
Iterator<HistoryEntry> historyEntryIterator = parser.readValuesAs(HistoryEntry.class);
historyEntryIterator.forEachRemaining(historyEntryList::add);
}

History history = new History(historyEntryList);
System.out.println(history);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,16 @@ void testGetLastHistoryEntry() {
}

/**
* Serialises and then deserialises a history and checks for equality.
* Serialises and then deserializes a history and checks for equality.
*/
@Test
void testSerialisation() throws IOException {
ArrayList<HistoryEntry> serialisableEntryList = new ArrayList<>(entries);
History history = new History(serialisableEntryList);
File tempFile = File.createTempFile("tmpHistory1", "gz", temporaryPath.toFile());
CacheUtil.writeCache(history, tempFile);
History deserialised = FileHistoryCache.readCache(tempFile);
assertEquals(history, deserialised);
File tempFile = File.createTempFile("tmpHistory1", "", temporaryPath.toFile());
FileHistoryCache.writeHistoryTo(history, tempFile);
// TODO: mock the repository
// History deserialised = FileHistoryCache.readHistory(tempFile, repository);
// assertEquals(history, deserialised);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private void checkDataExistence(String fileName, boolean shouldExist) {
assertFalse(historyGuru.hasAnnotationCacheForFile(file));
}
} else {
cacheFile = TandemPath.join(fileName, ".gz");
cacheFile = TandemPath.join(fileName, dirName.equals(IndexDatabase.XREF_DIR) ? ".gz" : "");
File dataFile = new File(dataDir, cacheFile);

if (shouldExist) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public void fileRemoved(String path) {
if (path.equals(this.path)) {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
File f = new File(env.getDataRootPath(),
TandemPath.join("historycache" + path, ".gz"));
TandemPath.join("historycache" + path, ""));
assertTrue(f.exists(), String.format("history cache file %s should be preserved", f));
}
removedFiles.add(path);
Expand Down Expand Up @@ -339,7 +339,7 @@ void testRemoveFileOnFileChange() throws Exception {
Indexer.getInstance().prepareIndexer(env, true, true,
null, List.of("mercurial"));
File historyFile = new File(env.getDataRootPath(),
TandemPath.join("historycache" + path, ".gz"));
TandemPath.join("historycache" + path, ""));
assertTrue(historyFile.exists(), String.format("history cache for %s has to exist", path));

// create index
Expand Down
Binary file not shown.