aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-migration
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2023-01-06 17:42:28 -0600
committersonartech <sonartech@sonarsource.com>2023-01-12 20:02:51 +0000
commit8b2213ef7a6777709d981a83c4d875951f225cc5 (patch)
treef32d286387e7919c2721e259a2380754a9041040 /server/sonar-db-migration
parent62bfeeb68576a0a81427e12d9168dad3cec18f5b (diff)
downloadsonarqube-8b2213ef7a6777709d981a83c4d875951f225cc5.tar.gz
sonarqube-8b2213ef7a6777709d981a83c4d875951f225cc5.zip
SONAR-18174 Analyzer cache should be kept in the file system to decrease memory use
Diffstat (limited to 'server/sonar-db-migration')
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/DbVersion99.java3
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/DeleteAnalysisCache.java37
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v99/DeleteAnalysisCacheTest.java74
-rw-r--r--server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v99/DeleteAnalysisCacheTest/schema.sql5
4 files changed, 118 insertions, 1 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/DbVersion99.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/DbVersion99.java
index 6ce8b9338a6..c4ea40e2ca9 100644
--- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/DbVersion99.java
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/DbVersion99.java
@@ -26,6 +26,7 @@ public class DbVersion99 implements DbVersion {
@Override
public void addSteps(MigrationStepRegistry registry) {
registry
- .add(6800, "Add node_name column to ce_activity table", AddNodeNameColumnToCeActivityTable.class);
+ .add(6800, "Add node_name column to ce_activity table", AddNodeNameColumnToCeActivityTable.class)
+ .add(6801, "Delete all analysis cache", DeleteAnalysisCache.class);
}
}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/DeleteAnalysisCache.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/DeleteAnalysisCache.java
new file mode 100644
index 00000000000..6089d4740de
--- /dev/null
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/DeleteAnalysisCache.java
@@ -0,0 +1,37 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.sonar.server.platform.db.migration.version.v99;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.step.DataChange;
+
+public class DeleteAnalysisCache extends DataChange {
+ public DeleteAnalysisCache(Database db) {
+ super(db);
+ }
+
+ @Override
+ protected void execute(Context context) throws SQLException {
+ context.prepareUpsert("delete from scanner_analysis_cache")
+ .execute()
+ .commit();
+ }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v99/DeleteAnalysisCacheTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v99/DeleteAnalysisCacheTest.java
new file mode 100644
index 00000000000..11a1ada1c8d
--- /dev/null
+++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v99/DeleteAnalysisCacheTest.java
@@ -0,0 +1,74 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.sonar.server.platform.db.migration.version.v99;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DeleteAnalysisCacheTest {
+ @Rule
+ public final CoreDbTester db = CoreDbTester.createForSchema(DeleteAnalysisCacheTest.class, "schema.sql");
+
+ private final DeleteAnalysisCache underTest = new DeleteAnalysisCache(db.database());
+
+ @Test
+ public void no_op_if_no_data() throws SQLException {
+ assertThatCacheIsEmpty();
+ underTest.execute();
+ assertThatCacheIsEmpty();
+ }
+
+ @Test
+ public void deletes_all_data_in_table() throws SQLException {
+ insertCache("b1", "d1");
+ insertCache("b2", "d2");
+ assertThatCacheIsNotEmpty();
+ underTest.execute();
+ assertThatCacheIsEmpty();
+ }
+
+ @Test
+ public void migration_is_reentrant() throws SQLException {
+ insertCache("b1", "d1");
+ insertCache("b2", "d2");
+ assertThatCacheIsNotEmpty();
+ underTest.execute();
+ underTest.execute();
+ assertThatCacheIsEmpty();
+ }
+
+ private void assertThatCacheIsEmpty() {
+ assertThat(db.countRowsOfTable("scanner_analysis_cache")).isZero();
+ }
+
+ private void assertThatCacheIsNotEmpty() {
+ assertThat(db.countRowsOfTable("scanner_analysis_cache")).isNotZero();
+ }
+
+ private void insertCache(String branch, String data) {
+ db.executeInsert("scanner_analysis_cache",
+ "branch_uuid", branch,
+ "data", data);
+ }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v99/DeleteAnalysisCacheTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v99/DeleteAnalysisCacheTest/schema.sql
new file mode 100644
index 00000000000..4ea9eac962a
--- /dev/null
+++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v99/DeleteAnalysisCacheTest/schema.sql
@@ -0,0 +1,5 @@
+CREATE TABLE "SCANNER_ANALYSIS_CACHE"(
+ "BRANCH_UUID" CHARACTER VARYING(40) NOT NULL,
+ "DATA" BINARY LARGE OBJECT NOT NULL
+);
+ALTER TABLE "SCANNER_ANALYSIS_CACHE" ADD CONSTRAINT "PK_SCANNER_ANALYSIS_CACHE" PRIMARY KEY("BRANCH_UUID");