Skip to content
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

SLCORE-858 SonarLint storage is growing after restart #1156

Merged
merged 4 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -26,6 +26,7 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -54,6 +55,8 @@

public class XodusLocalOnlyIssueStore {

private static final String LOCAL_ONLY_ISSUE = "xodus-local-only-issue-store";
private static final Integer PURGE_NUMBER_OF_DAYS = 3;
private static final String CONFIGURATION_SCOPE_ID_ENTITY_TYPE = "Scope";
private static final String CONFIGURATION_SCOPE_ID_TO_FILES_LINK_NAME = "files";
private static final String FILE_ENTITY_TYPE = "File";
Expand Down Expand Up @@ -81,7 +84,8 @@ public class XodusLocalOnlyIssueStore {
private static final SonarLintLogger LOG = SonarLintLogger.get();

public XodusLocalOnlyIssueStore(Path backupDir, Path workDir) throws IOException {
xodusDbDir = Files.createTempDirectory(workDir, "xodus-local-only-issue-store");
xodusDbDir = Files.createTempDirectory(workDir, LOCAL_ONLY_ISSUE);
purgeOldTemporaryFiles(workDir);
backupFile = backupDir.resolve(BACKUP_TAR_GZ);
if (Files.isRegularFile(backupFile)) {
LOG.debug("Restoring previous local-only issue database from {}", backupFile);
Expand All @@ -100,6 +104,23 @@ public XodusLocalOnlyIssueStore(Path backupDir, Path workDir) throws IOException
});
}

private static void purgeOldTemporaryFiles(Path workDir) {
if (Files.exists(workDir)) {
try (var stream = Files.newDirectoryStream(workDir, LOCAL_ONLY_ISSUE + "*")) {
for (var path : stream) {
var file = path.toFile();
var diff = new Date().getTime() - file.lastModified();
if (diff > PURGE_NUMBER_OF_DAYS * 24 * 60 * 60 * 1000) {
FileUtils.deleteQuietly(file);
LOG.debug("Successfully purged " + path);
}
}
} catch (Exception e) {
LOG.error("Unable to purge old temporary files for findings");
}
}
}

public List<LocalOnlyIssue> loadForFile(String configurationScopeId, Path filePath) {
return entityStore.computeInReadonlyTransaction(txn -> findUnique(txn, CONFIGURATION_SCOPE_ID_ENTITY_TYPE, NAME_PROPERTY_NAME, configurationScopeId)
.map(configScopeId -> configScopeId.getLinks(CONFIGURATION_SCOPE_ID_TO_FILES_LINK_NAME))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.nio.file.StandardCopyOption;
import java.time.Instant;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand All @@ -51,6 +52,8 @@

public class XodusKnownFindingsStore {

private static final String KNOWN_FINDINGS_STORE = "known-findings-store";
private static final Integer PURGE_NUMBER_OF_DAYS = 3;
private static final String CONFIGURATION_SCOPE_ID_ENTITY_TYPE = "Scope";
private static final String CONFIGURATION_SCOPE_ID_TO_FILES_LINK_NAME = "files";
private static final String FILE_ENTITY_TYPE = "File";
Expand Down Expand Up @@ -78,7 +81,8 @@ public class XodusKnownFindingsStore {
private static final SonarLintLogger LOG = SonarLintLogger.get();

public XodusKnownFindingsStore(Path backupDir, Path workDir) throws IOException {
xodusDbDir = Files.createTempDirectory(workDir, "known-findings-store");
xodusDbDir = Files.createTempDirectory(workDir, KNOWN_FINDINGS_STORE);
purgeOldTemporaryFiles(workDir);
backupFile = backupDir.resolve(BACKUP_TAR_GZ);
if (Files.isRegularFile(backupFile)) {
LOG.debug("Restoring previous known findings database from {}", backupFile);
Expand All @@ -96,6 +100,23 @@ public XodusKnownFindingsStore(Path backupDir, Path workDir) throws IOException
});
}

private static void purgeOldTemporaryFiles(Path workDir) {
nquinquenel marked this conversation as resolved.
Show resolved Hide resolved
if (Files.exists(workDir)) {
try (var stream = Files.newDirectoryStream(workDir, KNOWN_FINDINGS_STORE + "*")) {
for (var path : stream) {
var file = path.toFile();
var diff = new Date().getTime() - file.lastModified();
if (diff > PURGE_NUMBER_OF_DAYS * 24 * 60 * 60 * 1000) {
FileUtils.deleteQuietly(file);
LOG.debug("Successfully purged " + path);
}
}
} catch (Exception e) {
LOG.error("Unable to purge old temporary files for findings");
}
}
}

public List<KnownFinding> loadIssuesForFile(String configurationScopeId, Path filePath) {
return loadFindingsForFile(configurationScopeId, filePath, FILE_TO_ISSUES_LINK_NAME);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.sonarsource.sonarlint.core.local.only;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
Expand Down Expand Up @@ -173,6 +174,19 @@ void should_not_load_a_known_hotspot_if_config_scope_id_is_wrong() {
assertThat(storedSecurityHotspot).isEmpty();
}

@Test
void should_purge_old_folders() throws IOException {
store.close();
var oldFile = Files.createTempFile(workDir, "known-findings-store", UUID.randomUUID().toString());
var file = oldFile.toFile();
var oneWeekAgo = System.currentTimeMillis() - 7L * 24 * 60 * 60 * 1000;
file.setLastModified(oneWeekAgo);

store = new XodusKnownFindingsStore(backupDir, workDir);

assertThat(Files.exists(oldFile)).isFalse();
}

@NotNull
private static KnownFinding aKnownFinding(String serverKey) {
return new KnownFinding(UUID.randomUUID(), serverKey, new TextRangeWithHash(1, 2, 3, 4, "hash"), new LineWithHash(1, "lineHash"), "ruleKey", "message",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
package org.sonarsource.sonarlint.core.local.only;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -135,5 +137,17 @@ void should_reopen_all_issues_for_file() {
assertThat(store.loadForFile("configScopeId", Path.of("file/path"))).isEmpty();
}

@Test
void should_purge_old_folders() throws IOException {
store.close();
var oldFile = Files.createTempFile(workDir, "xodus-local-only-issue-store", UUID.randomUUID().toString());
var file = oldFile.toFile();
var oneWeekAgo = System.currentTimeMillis() - 7L * 24 * 60 * 60 * 1000;
file.setLastModified(oneWeekAgo);

store = new XodusLocalOnlyIssueStore(backupDir, workDir);

assertThat(Files.exists(oldFile)).isFalse();
}

}