From: Teryk Bellahsene Date: Thu, 30 Oct 2014 19:58:43 +0000 (+0100) Subject: SONAR-5806 Invalidate database preview cache after snapshot switch on the server... X-Git-Tag: 5.0-RC1~470 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d8d5c8fd2d5d25822baa93d77adf02a34122a7c1;p=sonarqube.git SONAR-5806 Invalidate database preview cache after snapshot switch on the server side --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ComputationStepRegistry.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ComputationStepRegistry.java index e4a2d573bc4..0e392ca2aad 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/ComputationStepRegistry.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/ComputationStepRegistry.java @@ -33,8 +33,9 @@ public class ComputationStepRegistry implements ServerComponent { SynchronizeProjectPermissionsStep synchronizeProjectPermissionsStep, IndexProjectIssuesStep indexProjectIssuesStep, SwitchSnapshotStep switchSnapshotStep, - DataCleanerStep dataCleanerStep) { - steps = ImmutableList.of(synchronizeProjectPermissionsStep, switchSnapshotStep, dataCleanerStep, indexProjectIssuesStep); + DataCleanerStep dataCleanerStep, + InvalidatePreviewCacheStep invalidatePreviewCacheStep) { + steps = ImmutableList.of(synchronizeProjectPermissionsStep, switchSnapshotStep, invalidatePreviewCacheStep, dataCleanerStep, indexProjectIssuesStep); } public List steps() { diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/InvalidatePreviewCacheStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/InvalidatePreviewCacheStep.java new file mode 100644 index 00000000000..dcdf9c822d1 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/InvalidatePreviewCacheStep.java @@ -0,0 +1,52 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.computation; + +import org.sonar.core.component.ComponentDto; +import org.sonar.core.computation.db.AnalysisReportDto; +import org.sonar.core.persistence.DbSession; +import org.sonar.core.preview.PreviewCache; +import org.sonar.core.properties.PropertiesDao; +import org.sonar.core.properties.PropertyDto; + + +public class InvalidatePreviewCacheStep implements ComputationStep { + private final PropertiesDao propertiesDao; + + public InvalidatePreviewCacheStep(PropertiesDao propertiesDao) { + this.propertiesDao = propertiesDao; + } + + @Override + public void execute(DbSession session, AnalysisReportDto analysisReportDto, ComponentDto project) { + propertiesDao.setProperty(updatedProjectPreviewCacheProperty(project)); + } + + private PropertyDto updatedProjectPreviewCacheProperty(ComponentDto project) { + return new PropertyDto().setKey(PreviewCache.SONAR_PREVIEW_CACHE_LAST_UPDATE_KEY).setResourceId(project.getId()) + .setValue(String.valueOf(System.currentTimeMillis())); + } + + @Override + public String description() { + return "Invalidate preview cache"; + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java index 52de0619f01..cefc8430479 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java @@ -118,7 +118,10 @@ import org.sonar.server.issue.db.IssueDao; import org.sonar.server.issue.filter.IssueFilterService; import org.sonar.server.issue.filter.IssueFilterWriter; import org.sonar.server.issue.filter.IssueFilterWs; -import org.sonar.server.issue.index.*; +import org.sonar.server.issue.index.IssueAuthorizationIndex; +import org.sonar.server.issue.index.IssueAuthorizationNormalizer; +import org.sonar.server.issue.index.IssueIndex; +import org.sonar.server.issue.index.IssueNormalizer; import org.sonar.server.issue.ws.IssueActionsWriter; import org.sonar.server.issue.ws.IssueShowAction; import org.sonar.server.issue.ws.IssuesWs; @@ -268,7 +271,7 @@ class ServerComponents { ActivityNormalizer.class, ActivityIndex.class, ActivityDao.class - )); + )); components.addAll(CorePropertyDefinitions.all()); components.addAll(DatabaseMigrations.CLASSES); components.addAll(DaoUtils.getDaoClasses()); @@ -301,7 +304,7 @@ class ServerComponents { // ws RestartHandler.class, SystemWs.class - ); + ); } /** @@ -318,7 +321,7 @@ class ServerComponents { HttpDownloader.class, UriReader.class, ServerIdGenerator.class - ); + ); } void startLevel4Components(ComponentContainer pico) { @@ -590,11 +593,13 @@ class ServerComponents { // Compute engine pico.addSingleton(ComputationService.class); pico.addSingleton(ComputationStepRegistry.class); - pico.addSingleton(SynchronizeProjectPermissionsStep.class); - pico.addSingleton(IndexProjectIssuesStep.class); - pico.addSingleton(SwitchSnapshotStep.class); - pico.addSingleton(DataCleanerStep.class); - pico.add(AnalysisReportQueue.class); + pico.addSingletons(Lists.newArrayList( + SynchronizeProjectPermissionsStep.class, + IndexProjectIssuesStep.class, + SwitchSnapshotStep.class, + InvalidatePreviewCacheStep.class, + DataCleanerStep.class)); + pico.addSingleton(AnalysisReportQueue.class); pico.addSingleton(AnalysisReportTaskLauncher.class); pico.addSingleton(AnalysisReportWebService.class); pico.addSingleton(ActiveAnalysisReportsAction.class); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ComputationStepRegistryTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ComputationStepRegistryTest.java index 8bf1607c54f..b47cdcf6521 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/ComputationStepRegistryTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/ComputationStepRegistryTest.java @@ -33,6 +33,7 @@ public class ComputationStepRegistryTest { private IndexProjectIssuesStep indexProjectIssuesStep; private SwitchSnapshotStep switchSnapshotStep; private DataCleanerStep dataCleanerStep; + private InvalidatePreviewCacheStep invalidatePreviewCacheStep; @Before public void before() { @@ -40,12 +41,13 @@ public class ComputationStepRegistryTest { indexProjectIssuesStep = mock(IndexProjectIssuesStep.class); switchSnapshotStep = mock(SwitchSnapshotStep.class); dataCleanerStep = mock(DataCleanerStep.class); + invalidatePreviewCacheStep = mock(InvalidatePreviewCacheStep.class); - sut = new ComputationStepRegistry(synchronizeProjectPermissionsStep, indexProjectIssuesStep, switchSnapshotStep, dataCleanerStep); + sut = new ComputationStepRegistry(synchronizeProjectPermissionsStep, indexProjectIssuesStep, switchSnapshotStep, dataCleanerStep, invalidatePreviewCacheStep); } @Test public void steps_returned_in_the_right_order() throws Exception { - assertThat(sut.steps()).containsExactly(synchronizeProjectPermissionsStep, switchSnapshotStep, dataCleanerStep, indexProjectIssuesStep); + assertThat(sut.steps()).containsExactly(synchronizeProjectPermissionsStep, switchSnapshotStep, invalidatePreviewCacheStep, dataCleanerStep, indexProjectIssuesStep); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/InvalidatePreviewCacheStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/InvalidatePreviewCacheStepTest.java new file mode 100644 index 00000000000..996d56daf77 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/InvalidatePreviewCacheStepTest.java @@ -0,0 +1,51 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.computation; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.core.component.ComponentDto; +import org.sonar.core.computation.db.AnalysisReportDto; +import org.sonar.core.persistence.DbSession; +import org.sonar.core.properties.PropertiesDao; +import org.sonar.core.properties.PropertyDto; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class InvalidatePreviewCacheStepTest { + private InvalidatePreviewCacheStep sut; + private PropertiesDao propertiesDao; + + @Before + public void before() { + this.propertiesDao = mock(PropertiesDao.class); + this.sut = new InvalidatePreviewCacheStep(propertiesDao); + } + + @Test + public void update_property_calling_propertiesDao() { + sut.execute(mock(DbSession.class), mock(AnalysisReportDto.class), mock(ComponentDto.class)); + + verify(propertiesDao).setProperty(any(PropertyDto.class)); + } +} \ No newline at end of file diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/batch_bootstrap_controller.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/batch_bootstrap_controller.rb index 547d0bf7dcf..111f423168c 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/batch_bootstrap_controller.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/batch_bootstrap_controller.rb @@ -35,22 +35,6 @@ class BatchBootstrapController < Api::ApiController send_data String.from_java_bytes(db_content) end - # PUT /batch_bootstrap/evict?project= - def evict - has_scan_role = has_role?('scan') - return render_unauthorized("You're not authorized to execute any SonarQube analysis. Please contact your SonarQube administrator.") if !has_scan_role - - project = load_project() - return render_unauthorized("You're not authorized to access to project '" + project.name + "', please contact your SonarQube administrator") if project && !has_scan_role && !has_role?(:user, project) - - if project - Property.set(Java::OrgSonarCorePreview::PreviewCache::SONAR_PREVIEW_CACHE_LAST_UPDATE_KEY, java.lang.System.currentTimeMillis, project.root_project.id) - render_success('dryRun DB evicted') - else - render_bad_request('missing projectId') - end - end - private diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/UpdateStatusJob.java b/sonar-batch/src/main/java/org/sonar/batch/phases/UpdateStatusJob.java index 4031462e94a..9e495b6c8df 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/UpdateStatusJob.java +++ b/sonar-batch/src/main/java/org/sonar/batch/phases/UpdateStatusJob.java @@ -42,7 +42,7 @@ public class UpdateStatusJob implements BatchComponent { private AnalysisMode analysisMode; public UpdateStatusJob(Settings settings, ServerClient server, - Project project, Snapshot snapshot, AnalysisMode analysisMode) { + Project project, Snapshot snapshot, AnalysisMode analysisMode) { this.server = server; this.project = project; this.snapshot = snapshot; @@ -61,14 +61,7 @@ public class UpdateStatusJob implements BatchComponent { // If this is a preview analysis then we should not upload reports return; } - String url = "/batch_bootstrap/evict?project=" + project.getId(); - try { - LOG.debug("Upload report"); - server.request(url, "POST"); - } catch (Exception e) { - throw new IllegalStateException("Unable to evict preview database: " + url, e); - } - url = "/batch/upload_report?project=" + project.getEffectiveKey() + "&snapshot=" + snapshot.getId(); + String url = "/batch/upload_report?project=" + project.getEffectiveKey() + "&snapshot=" + snapshot.getId(); try { LOG.debug("Publish results"); server.request(url, "POST", true, 0); diff --git a/sonar-batch/src/test/java/org/sonar/batch/phases/UpdateStatusJobTest.java b/sonar-batch/src/test/java/org/sonar/batch/phases/UpdateStatusJobTest.java index 117bb07f8ca..685e7373269 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/phases/UpdateStatusJobTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/phases/UpdateStatusJobTest.java @@ -82,7 +82,6 @@ public class UpdateStatusJobTest extends AbstractDbUnitTestCase { UpdateStatusJob job = new UpdateStatusJob(settings, serverClient, project, mock(Snapshot.class), mode); job.uploadReport(); - verify(serverClient).request(contains("/batch_bootstrap/evict"), eq("POST")); verify(serverClient).request(contains("/batch/upload_report"), eq("POST"), eq(true), eq(0)); }