]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5806 Invalidate database preview cache after snapshot switch on the server...
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 30 Oct 2014 19:58:43 +0000 (20:58 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 30 Oct 2014 20:28:59 +0000 (21:28 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/ComputationStepRegistry.java
server/sonar-server/src/main/java/org/sonar/server/computation/InvalidatePreviewCacheStep.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
server/sonar-server/src/test/java/org/sonar/server/computation/ComputationStepRegistryTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/InvalidatePreviewCacheStepTest.java [new file with mode: 0644]
server/sonar-web/src/main/webapp/WEB-INF/app/controllers/batch_bootstrap_controller.rb
sonar-batch/src/main/java/org/sonar/batch/phases/UpdateStatusJob.java
sonar-batch/src/test/java/org/sonar/batch/phases/UpdateStatusJobTest.java

index e4a2d573bc4d97d92a3922e18b3854a89099e81e..0e392ca2aadf0f318c03b33bee3beceed4854316 100644 (file)
@@ -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<ComputationStep> 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 (file)
index 0000000..dcdf9c8
--- /dev/null
@@ -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";
+  }
+}
index 52de0619f018948e0ce58d4d6708417a53903263..cefc8430479820bcbc6a0fc1ded1e6043cf8ea83 100644 (file)
@@ -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);
index 8bf1607c54fae11f7caf337347617c45164ee752..b47cdcf65211c4c36b4f11ff09f53820624c3b92 100644 (file)
@@ -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 (file)
index 0000000..996d56d
--- /dev/null
@@ -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
index 547d0bf7dcfdadb852d408731afa4bcf9a007f4f..111f423168c68a3ae8566edc5274c991ad6147c1 100644 (file)
@@ -35,22 +35,6 @@ class BatchBootstrapController < Api::ApiController
     send_data String.from_java_bytes(db_content)
   end
 
-  # PUT /batch_bootstrap/evict?project=<key or id>
-  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
 
index 4031462e94a9d7d93b581f5e8d936d4a3c6723a1..9e495b6c8df436a62876b4fcaae72e23c286f873 100644 (file)
@@ -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);
index 117bb07f8ca84cf8d8e75fd0b93837cf648a4abb..685e73732693972395dfdd9732c7922a71ccec4d 100644 (file)
@@ -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));
   }