Skip to content

Commit

Permalink
SONAR-6723 Use SQ version in cache keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dbmeneses committed Jul 31, 2015
1 parent c003fa9 commit f0a1c30
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 32 deletions.
Expand Up @@ -19,7 +19,13 @@
*/ */
package org.sonar.batch.bootstrap; 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 java.nio.file.Paths;

import org.picocontainer.injectors.ProviderAdapter; import org.picocontainer.injectors.ProviderAdapter;
import org.sonar.home.cache.PersistentCache; import org.sonar.home.cache.PersistentCache;
import org.sonar.home.cache.PersistentCacheBuilder; import org.sonar.home.cache.PersistentCacheBuilder;
Expand All @@ -36,9 +42,22 @@ public PersistentCache provide(UserProperties props) {
builder.setSonarHome(Paths.get(home)); builder.setSonarHome(Paths.get(home));
} }


builder.setVersion(getVersion());
cache = builder.build(); cache = builder.build();
} }

return cache; 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;
}
}
} }
Expand Up @@ -105,15 +105,6 @@ public Batch executeTask(Map<String, String> analysisProperties, Object... compo
return this; return this;
} }


/**
* @since 5.2
*/
public Batch executeTask(Map<String, String> analysisProperties) {
checkStarted();
bootstrapContainer.executeAnalysis(analysisProperties, components);
return this;
}

/** /**
* @since 5.2 * @since 5.2
*/ */
Expand Down
Expand Up @@ -19,12 +19,14 @@
*/ */
package org.sonar.batch.platform; package org.sonar.batch.platform;


import org.apache.commons.lang.StringUtils;

import org.sonar.batch.bootstrap.BootstrapProperties;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.sonar.api.batch.BatchSide; import org.sonar.api.batch.BatchSide;
import org.sonar.api.CoreProperties; import org.sonar.api.CoreProperties;
import org.sonar.api.config.Settings; import org.sonar.api.config.Settings;
import org.sonar.api.platform.Server; import org.sonar.api.platform.Server;
import org.sonar.batch.bootstrap.ServerClient;


import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;


Expand All @@ -37,11 +39,11 @@
public class DefaultServer extends Server { public class DefaultServer extends Server {


private Settings settings; 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.settings = settings;
this.client = client; this.props = props;
} }


@Override @Override
Expand Down Expand Up @@ -86,7 +88,7 @@ public String getContextPath() {


@Override @Override
public String getURL() { public String getURL() {
return client.getURL(); return StringUtils.removeEnd(StringUtils.defaultIfBlank(props.property("sonar.host.url"), "http://localhost:9000"), "/");
} }


@Override @Override
Expand Down
Expand Up @@ -20,14 +20,12 @@
package org.sonar.batch.bootstrap; package org.sonar.batch.bootstrap;


import java.util.Collections; import java.util.Collections;

import org.junit.Before; import org.junit.Before;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test; import org.junit.Test;


public class PersistentCacheProviderTest { public class PersistentCacheProviderTest {
private PersistentCacheProvider provider = null; private PersistentCacheProvider provider = null;

private BootstrapProperties props = null; private BootstrapProperties props = null;


@Before @Before
Expand Down
Expand Up @@ -51,7 +51,7 @@ public void setUp() throws Exception {
when(bootstrapProps.property("sonar.host.url")).thenReturn("http://localhost:" + server.getPort()); when(bootstrapProps.property("sonar.host.url")).thenReturn("http://localhost:" + server.getPort());


client = new ServerClient(bootstrapProps, new EnvironmentInformation("Junit", "4")); 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); loader = new WSLoader(cache, client);
} }


Expand Down
Expand Up @@ -19,11 +19,11 @@
*/ */
package org.sonar.batch.platform; package org.sonar.batch.platform;


import org.sonar.batch.bootstrap.BootstrapProperties;

import org.junit.Test; import org.junit.Test;
import org.sonar.api.CoreProperties; import org.sonar.api.CoreProperties;
import org.sonar.api.config.Settings; import org.sonar.api.config.Settings;
import org.sonar.batch.bootstrap.ServerClient;

import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
Expand All @@ -37,10 +37,10 @@ public void shouldLoadServerProperties() {
settings.setProperty(CoreProperties.SERVER_VERSION, "2.2"); settings.setProperty(CoreProperties.SERVER_VERSION, "2.2");
settings.setProperty(CoreProperties.SERVER_STARTTIME, "2010-05-18T17:59:00+0000"); settings.setProperty(CoreProperties.SERVER_STARTTIME, "2010-05-18T17:59:00+0000");
settings.setProperty(CoreProperties.PERMANENT_SERVER_ID, "abcde"); settings.setProperty(CoreProperties.PERMANENT_SERVER_ID, "abcde");
ServerClient client = mock(ServerClient.class); BootstrapProperties props = mock(BootstrapProperties.class);
when(client.getURL()).thenReturn("http://foo.com"); 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.getId()).isEqualTo("123");
assertThat(metadata.getVersion()).isEqualTo("2.2"); assertThat(metadata.getVersion()).isEqualTo("2.2");
Expand Down
13 changes: 10 additions & 3 deletions sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java
Expand Up @@ -31,6 +31,7 @@
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;

import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
Expand All @@ -50,11 +51,13 @@ public class PersistentCache {
// eviction strategy is to expire entries after modification once a time duration has elapsed // eviction strategy is to expire entries after modification once a time duration has elapsed
private final long defaultDurationToExpireMs; private final long defaultDurationToExpireMs;
private final Logger logger; 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.baseDir = baseDir;
this.defaultDurationToExpireMs = defaultDurationToExpireMs; this.defaultDurationToExpireMs = defaultDurationToExpireMs;
this.logger = logger; this.logger = logger;
this.version = version;


reconfigure(); reconfigure();
logger.debug("cache: " + baseDir + ", default expiration time (ms): " + defaultDurationToExpireMs); logger.debug("cache: " + baseDir + ", default expiration time (ms): " + defaultDurationToExpireMs);
Expand Down Expand Up @@ -200,10 +203,14 @@ private void unlock() {
lockChannel = null; lockChannel = null;
} }


private static String getKey(String uri) { private String getKey(String uri) {
try { try {
String key = uri;
if (version != null) {
key += version;
}
MessageDigest digest = MessageDigest.getInstance(DIGEST_ALGO); MessageDigest digest = MessageDigest.getInstance(DIGEST_ALGO);
digest.update(uri.getBytes(StandardCharsets.UTF_8)); digest.update(key.getBytes(StandardCharsets.UTF_8));
return byteArrayToHex(digest.digest()); return byteArrayToHex(digest.digest());
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("Couldn't create hash", e); throw new IllegalStateException("Couldn't create hash", e);
Expand Down
Expand Up @@ -30,6 +30,7 @@ public class PersistentCacheBuilder {


private Path cachePath; private Path cachePath;
private final Logger logger; private final Logger logger;
private String version;


public PersistentCacheBuilder(Logger logger) { public PersistentCacheBuilder(Logger logger) {
this.logger = logger; this.logger = logger;
Expand All @@ -40,7 +41,12 @@ public PersistentCache build() {
setSonarHome(findHome()); 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) { public PersistentCacheBuilder setSonarHome(@Nullable Path p) {
Expand Down
Expand Up @@ -41,7 +41,7 @@ public class PersistentCacheTest {


@Before @Before
public void setUp() { 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 @Test
Expand Down Expand Up @@ -79,7 +79,7 @@ public void testCacheHit() throws Exception {


@Test @Test
public void testReconfigure() throws Exception { 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(false);
assertCacheHit(true); assertCacheHit(true);


Expand All @@ -97,14 +97,29 @@ public void testReconfigure() throws Exception {
@Test @Test
public void testExpiration() throws Exception { public void testExpiration() throws Exception {
// negative time to make sure it is expired on the second call // 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);
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 { private void assertCacheHit(boolean hit) throws Exception {
assertCacheHit(cache, hit);
}

private void assertCacheHit(PersistentCache pCache, boolean hit) throws Exception {
CacheFillerString c = new CacheFillerString(); CacheFillerString c = new CacheFillerString();
assertThat(cache.getString(URI, c)).isEqualTo(VALUE); assertThat(pCache.getString(URI, c)).isEqualTo(VALUE);
assertThat(c.wasCalled).isEqualTo(!hit); assertThat(c.wasCalled).isEqualTo(!hit);
} }


Expand Down

0 comments on commit f0a1c30

Please sign in to comment.