From 609bb9ad43bf3c19da6dff5b66f39ded9d6b2265 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Wed, 4 Sep 2013 12:24:25 +0200 Subject: [PATCH] SONAR-4602 Evict dryRun cache when a new snapshot is available --- .../sonar/batch/phases/UpdateStatusJob.java | 19 +++++++++++++++++-- .../batch/phases/UpdateStatusJobTest.java | 9 ++++++--- .../org/sonar/core/dryrun/DryRunCache.java | 4 +++- 3 files changed, 26 insertions(+), 6 deletions(-) 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 f25f92de517..3bd4a77946a 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 @@ -31,6 +31,9 @@ import org.sonar.api.resources.Project; import org.sonar.api.resources.Scopes; import org.sonar.batch.bootstrap.ServerClient; import org.sonar.batch.index.ResourcePersister; +import org.sonar.core.dryrun.DryRunCache; +import org.sonar.core.properties.PropertiesDao; +import org.sonar.core.properties.PropertyDto; import javax.persistence.Query; @@ -45,19 +48,31 @@ public class UpdateStatusJob implements BatchComponent { private ResourcePersister resourcePersister; private Settings settings; private Project project; + private PropertiesDao propertiesDao; - public UpdateStatusJob(Settings settings, ServerClient server, DatabaseSession session, ResourcePersister resourcePersister, Project project, Snapshot snapshot) { + public UpdateStatusJob(Settings settings, ServerClient server, DatabaseSession session, + ResourcePersister resourcePersister, Project project, Snapshot snapshot, PropertiesDao propertiesDao) { this.session = session; this.server = server; this.resourcePersister = resourcePersister; this.project = project; this.snapshot = snapshot; this.settings = settings; + this.propertiesDao = propertiesDao; } public void execute() { disablePreviousSnapshot(); enableCurrentSnapshot(); + updateDryRunLastModificationTimestamp(); + } + + private void updateDryRunLastModificationTimestamp() { + propertiesDao.setProperty( + new PropertyDto() + .setKey(DryRunCache.SONAR_DRY_RUN_CACHE_LAST_UPDATE_KEY) + .setResourceId(Long.valueOf(project.getId())) + .setValue(String.valueOf(System.nanoTime()))); } private void disablePreviousSnapshot() { @@ -115,7 +130,7 @@ public class UpdateStatusJob implements BatchComponent { query.setParameter("last", last); query.setParameter("rootId", snapshot.getId()); query.setParameter("path", snapshot.getPath() + snapshot.getId() + ".%"); - query.setParameter("pathRootId", snapshot.getRootId()==null ? snapshot.getId() : snapshot.getRootId()); + query.setParameter("pathRootId", snapshot.getRootId() == null ? snapshot.getId() : snapshot.getRootId()); query.executeUpdate(); session.commit(); 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 b38e777583e..6b4040d15b5 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 @@ -32,6 +32,7 @@ import org.sonar.batch.index.DefaultResourcePersister; import org.sonar.batch.index.ResourceCache; import org.sonar.batch.index.ResourcePersister; import org.sonar.batch.index.SnapshotCache; +import org.sonar.core.properties.PropertiesDao; import org.sonar.jpa.test.AbstractDbUnitTestCase; import javax.persistence.Query; @@ -60,9 +61,11 @@ public class UpdateStatusJobTest extends AbstractDbUnitTestCase { setupData("sharedFixture", fixture); DatabaseSession session = getSession(); + Project project = new Project("foo"); + project.setId(1); UpdateStatusJob job = new UpdateStatusJob(new Settings().appendProperty(CoreProperties.SERVER_BASE_URL, "http://myserver/"), mock(ServerClient.class), session, new DefaultResourcePersister(session, mock(ResourcePermissions.class), mock(SnapshotCache.class), mock(ResourceCache.class)), - mock(Project.class), loadSnapshot(snapshotId)); + project, loadSnapshot(snapshotId), mock(PropertiesDao.class)); job.execute(); checkTables(fixture, "snapshots"); @@ -80,7 +83,7 @@ public class UpdateStatusJobTest extends AbstractDbUnitTestCase { settings.setProperty(CoreProperties.SERVER_BASE_URL, "http://myserver/"); Project project = new Project("struts"); UpdateStatusJob job = new UpdateStatusJob(settings, mock(ServerClient.class), mock(DatabaseSession.class), - mock(ResourcePersister.class), project, mock(Snapshot.class)); + mock(ResourcePersister.class), project, mock(Snapshot.class), mock(PropertiesDao.class)); Logger logger = mock(Logger.class); job.logSuccess(logger); @@ -94,7 +97,7 @@ public class UpdateStatusJobTest extends AbstractDbUnitTestCase { settings.setProperty("sonar.dryRun", true); Project project = new Project("struts"); UpdateStatusJob job = new UpdateStatusJob(settings, mock(ServerClient.class), mock(DatabaseSession.class), - mock(ResourcePersister.class), project, mock(Snapshot.class)); + mock(ResourcePersister.class), project, mock(Snapshot.class), mock(PropertiesDao.class)); Logger logger = mock(Logger.class); job.logSuccess(logger); diff --git a/sonar-core/src/main/java/org/sonar/core/dryrun/DryRunCache.java b/sonar-core/src/main/java/org/sonar/core/dryrun/DryRunCache.java index 6c8d86d2bf9..277e73ba8f7 100644 --- a/sonar-core/src/main/java/org/sonar/core/dryrun/DryRunCache.java +++ b/sonar-core/src/main/java/org/sonar/core/dryrun/DryRunCache.java @@ -19,6 +19,8 @@ */ package org.sonar.core.dryrun; +import org.sonar.api.ServerExtension; + import org.apache.commons.io.FileUtils; import org.sonar.api.platform.ServerFileSystem; import org.sonar.core.properties.PropertiesDao; @@ -33,7 +35,7 @@ import java.io.File; /** * @since 4.0 */ -public class DryRunCache { +public class DryRunCache implements ServerExtension { public static final String SONAR_DRY_RUN_CACHE_LAST_UPDATE_KEY = "sonar.dryRun.cache.lastUpdate"; -- 2.39.5