]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6723 Use SQ version in cache keys
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 30 Jul 2015 11:46:10 +0000 (13:46 +0200)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Fri, 31 Jul 2015 10:04:26 +0000 (12:04 +0200)
sonar-batch/src/main/java/org/sonar/batch/bootstrap/PersistentCacheProvider.java
sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java
sonar-batch/src/main/java/org/sonar/batch/platform/DefaultServer.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/PersistentCacheProviderTest.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/WSLoaderTestWithServer.java
sonar-batch/src/test/java/org/sonar/batch/platform/DefaultServerTest.java
sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java
sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java
sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java

index 323b5aa0a0ee2ca15a8cf27311128156bbec05f3..ea9f3a14fc4f0101a96033626ddafc6112a4a138 100644 (file)
  */
 package org.sonar.batch.bootstrap;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Paths;
+
 import org.picocontainer.injectors.ProviderAdapter;
 import org.sonar.home.cache.PersistentCache;
 import org.sonar.home.cache.PersistentCacheBuilder;
@@ -36,9 +42,22 @@ public class PersistentCacheProvider extends ProviderAdapter {
         builder.setSonarHome(Paths.get(home));
       }
 
+      builder.setVersion(getVersion());
       cache = builder.build();
-    } 
-    
+    }
+
     return cache;
   }
+
+  private String getVersion() {
+    InputStream is = this.getClass().getClassLoader().getResourceAsStream("sq-version.txt");
+    if (is == null) {
+      return null;
+    }
+    try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
+      return br.readLine();
+    } catch (IOException e) {
+      return null;
+    }
+  }
 }
index 5cbcfe3671d6c3adcb588bcba75a211decdf347c..94065d8a2d375636a2435a2a85f4ed56d2f56faa 100644 (file)
@@ -105,15 +105,6 @@ public final class Batch {
     return this;
   }
 
-  /**
-   * @since 5.2
-   */
-  public Batch executeTask(Map<String, String> analysisProperties) {
-    checkStarted();
-    bootstrapContainer.executeAnalysis(analysisProperties, components);
-    return this;
-  }
-
   /**
    * @since 5.2
    */
index 4ec4c85cb93edb74935102fcd1208837e316d8e2..0102366217de8cc172c93a06e2286acee2be290e 100644 (file)
  */
 package org.sonar.batch.platform;
 
+import org.apache.commons.lang.StringUtils;
+
+import org.sonar.batch.bootstrap.BootstrapProperties;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.batch.BatchSide;
 import org.sonar.api.CoreProperties;
 import org.sonar.api.config.Settings;
 import org.sonar.api.platform.Server;
-import org.sonar.batch.bootstrap.ServerClient;
 
 import javax.annotation.CheckForNull;
 
@@ -37,11 +39,11 @@ import java.util.Date;
 public class DefaultServer extends Server {
 
   private Settings settings;
-  private ServerClient client;
+  private BootstrapProperties props;
 
-  public DefaultServer(Settings settings, ServerClient client) {
+  public DefaultServer(Settings settings, BootstrapProperties props) {
     this.settings = settings;
-    this.client = client;
+    this.props = props;
   }
 
   @Override
@@ -86,7 +88,7 @@ public class DefaultServer extends Server {
 
   @Override
   public String getURL() {
-    return client.getURL();
+    return StringUtils.removeEnd(StringUtils.defaultIfBlank(props.property("sonar.host.url"), "http://localhost:9000"), "/");
   }
 
   @Override
index 4857621a9102b647e420d3030ec5116928b17e9c..4f975659a896b3db459a4af568c724402267caa5 100644 (file)
 package org.sonar.batch.bootstrap;
 
 import java.util.Collections;
-
 import org.junit.Before;
 import static org.assertj.core.api.Assertions.assertThat;
 import org.junit.Test;
 
 public class PersistentCacheProviderTest {
   private PersistentCacheProvider provider = null;
-
   private BootstrapProperties props = null;
 
   @Before
index 5b5f2c7efee23d758d1ab53f63269080546b8921..b99d851d321361a8939b3ce83d35bb03aa974e46 100644 (file)
@@ -51,7 +51,7 @@ public class WSLoaderTestWithServer {
     when(bootstrapProps.property("sonar.host.url")).thenReturn("http://localhost:" + server.getPort());
 
     client = new ServerClient(bootstrapProps, new EnvironmentInformation("Junit", "4"));
-    cache = new PersistentCache(temp.getRoot().toPath(), 1000 * 60, new Slf4jLogger());
+    cache = new PersistentCache(temp.getRoot().toPath(), 1000 * 60, new Slf4jLogger(), null);
     loader = new WSLoader(cache, client);
   }
 
index adb2b134b1fcdef2d8fd9228466127ab05f29b6a..47bc2f9c012bbff5a3686608c160a0c793c68d08 100644 (file)
  */
 package org.sonar.batch.platform;
 
+import org.sonar.batch.bootstrap.BootstrapProperties;
+
 import org.junit.Test;
 import org.sonar.api.CoreProperties;
 import org.sonar.api.config.Settings;
-import org.sonar.batch.bootstrap.ServerClient;
-
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -37,10 +37,10 @@ public class DefaultServerTest {
     settings.setProperty(CoreProperties.SERVER_VERSION, "2.2");
     settings.setProperty(CoreProperties.SERVER_STARTTIME, "2010-05-18T17:59:00+0000");
     settings.setProperty(CoreProperties.PERMANENT_SERVER_ID, "abcde");
-    ServerClient client = mock(ServerClient.class);
-    when(client.getURL()).thenReturn("http://foo.com");
+    BootstrapProperties props = mock(BootstrapProperties.class);
+    when(props.property("sonar.host.url")).thenReturn("http://foo.com");
 
-    DefaultServer metadata = new DefaultServer(settings, client);
+    DefaultServer metadata = new DefaultServer(settings, props);
 
     assertThat(metadata.getId()).isEqualTo("123");
     assertThat(metadata.getVersion()).isEqualTo("2.2");
index 058075f83ed5b0837e250b4d457ac5207df3e3c7..640d5c29401aefce79f2e650b5a350f53104bdc8 100644 (file)
@@ -31,6 +31,7 @@ import java.nio.file.Path;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
+
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
@@ -50,11 +51,13 @@ public class PersistentCache {
   // eviction strategy is to expire entries after modification once a time duration has elapsed
   private final long defaultDurationToExpireMs;
   private final Logger logger;
+  private final String version;
 
-  public PersistentCache(Path baseDir, long defaultDurationToExpireMs, Logger logger) {
+  public PersistentCache(Path baseDir, long defaultDurationToExpireMs, Logger logger, String version) {
     this.baseDir = baseDir;
     this.defaultDurationToExpireMs = defaultDurationToExpireMs;
     this.logger = logger;
+    this.version = version;
 
     reconfigure();
     logger.debug("cache: " + baseDir + ", default expiration time (ms): " + defaultDurationToExpireMs);
@@ -200,10 +203,14 @@ public class PersistentCache {
     lockChannel = null;
   }
 
-  private static String getKey(String uri) {
+  private String getKey(String uri) {
     try {
+      String key = uri;
+      if (version != null) {
+        key += version;
+      }
       MessageDigest digest = MessageDigest.getInstance(DIGEST_ALGO);
-      digest.update(uri.getBytes(StandardCharsets.UTF_8));
+      digest.update(key.getBytes(StandardCharsets.UTF_8));
       return byteArrayToHex(digest.digest());
     } catch (NoSuchAlgorithmException e) {
       throw new IllegalStateException("Couldn't create hash", e);
index f0fa159691737740f2a48ad74bfc464e70bb9d35..055d2615ed6960077d80a8365e6396926645c60b 100644 (file)
@@ -30,6 +30,7 @@ public class PersistentCacheBuilder {
 
   private Path cachePath;
   private final Logger logger;
+  private String version;
 
   public PersistentCacheBuilder(Logger logger) {
     this.logger = logger;
@@ -40,7 +41,12 @@ public class PersistentCacheBuilder {
       setSonarHome(findHome());
     }
 
-    return new PersistentCache(cachePath, DEFAULT_EXPIRE_DURATION, logger);
+    return new PersistentCache(cachePath, DEFAULT_EXPIRE_DURATION, logger, version);
+  }
+
+  public PersistentCacheBuilder setVersion(String version) {
+    this.version = version;
+    return this;
   }
 
   public PersistentCacheBuilder setSonarHome(@Nullable Path p) {
index 62e105bf861a9f637ffd3ac951f778477e94902e..c327ff7c5eb150dc1453a3ad18c9f8c63b3ef999 100644 (file)
@@ -41,7 +41,7 @@ public class PersistentCacheTest {
 
   @Before
   public void setUp() {
-    cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class));
+    cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), null);
   }
 
   @Test
@@ -79,7 +79,7 @@ public class PersistentCacheTest {
 
   @Test
   public void testReconfigure() throws Exception {
-    cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class));
+    cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), null);
     assertCacheHit(false);
     assertCacheHit(true);
 
@@ -97,14 +97,29 @@ public class PersistentCacheTest {
   @Test
   public void testExpiration() throws Exception {
     // negative time to make sure it is expired on the second call
-    cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class));
+    cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class), null);
     assertCacheHit(false);
     assertCacheHit(false);
   }
+  
+  @Test
+  public void testDifferentServerVersions() throws Exception {
+    assertCacheHit(false);
+    assertCacheHit(true);
+    
+    PersistentCache cache2 = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), "5.2");
+    assertCacheHit(cache2, false);
+    assertCacheHit(cache2, true);
+    
+  }
 
   private void assertCacheHit(boolean hit) throws Exception {
+    assertCacheHit(cache, hit);
+  }
+  
+  private void assertCacheHit(PersistentCache pCache, boolean hit) throws Exception {
     CacheFillerString c = new CacheFillerString();
-    assertThat(cache.getString(URI, c)).isEqualTo(VALUE);
+    assertThat(pCache.getString(URI, c)).isEqualTo(VALUE);
     assertThat(c.wasCalled).isEqualTo(!hit);
   }