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 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 @@ -51,9 +51,12 @@
import org.sonarsource.sonarlint.core.serverconnection.storage.UuidBinding;

import static java.util.Objects.requireNonNull;
import static org.sonarsource.sonarlint.core.storage.XodusPurgeUtils.purgeOldTemporaryFiles;

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, PURGE_NUMBER_OF_DAYS, LOCAL_ONLY_ISSUE + "*");
backupFile = backupDir.resolve(BACKUP_TAR_GZ);
if (Files.isRegularFile(backupFile)) {
LOG.debug("Restoring previous local-only issue database from {}", backupFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* SonarLint Core - Implementation
* Copyright (C) 2016-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarsource.sonarlint.core.storage;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger;

public class XodusPurgeUtils {

private XodusPurgeUtils() {
// Static class
}

private static final SonarLintLogger LOG = SonarLintLogger.get();

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@
import org.sonarsource.sonarlint.core.serverconnection.storage.UuidBinding;

import static java.util.Objects.requireNonNull;
import static org.sonarsource.sonarlint.core.storage.XodusPurgeUtils.purgeOldTemporaryFiles;

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, PURGE_NUMBER_OF_DAYS, KNOWN_FINDINGS_STORE + "*");
backupFile = backupDir.resolve(BACKUP_TAR_GZ);
if (Files.isRegularFile(backupFile)) {
LOG.debug("Restoring previous known findings database from {}", backupFile);
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();
}

}