diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-01-22 21:53:13 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-01-22 21:53:13 +0100 |
commit | ea467d04b90d716d2960cc155c71533e05da1bfc (patch) | |
tree | 2a53dc71422f37ff7609fef5f64177e15e43af5e /sonar-core | |
parent | cbe291b6aff0884c2dfe1ce2ac38ef0a8222092c (diff) | |
download | sonarqube-ea467d04b90d716d2960cc155c71533e05da1bfc.tar.gz sonarqube-ea467d04b90d716d2960cc155c71533e05da1bfc.zip |
SONAR-6083 Server OutOfMemory error with preview analysis mode
Diffstat (limited to 'sonar-core')
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/preview/PreviewCache.java | 14 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/preview/PreviewCacheTest.java | 34 |
2 files changed, 19 insertions, 29 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/preview/PreviewCache.java b/sonar-core/src/main/java/org/sonar/core/preview/PreviewCache.java index 9cfeceec661..1e02ad829fe 100644 --- a/sonar-core/src/main/java/org/sonar/core/preview/PreviewCache.java +++ b/sonar-core/src/main/java/org/sonar/core/preview/PreviewCache.java @@ -19,7 +19,6 @@ */ package org.sonar.core.preview; -import com.google.common.io.Files; import org.apache.commons.io.FileUtils; import org.apache.ibatis.session.SqlSession; import org.slf4j.Logger; @@ -38,7 +37,6 @@ import org.sonar.core.resource.ResourceDto; import javax.annotation.Nullable; import java.io.File; -import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.concurrent.locks.ReadWriteLock; @@ -72,7 +70,7 @@ public class PreviewCache implements ServerExtension { this.previewDatabaseFactory = previewDatabaseFactory; } - public byte[] getDatabaseForPreview(@Nullable Long projectId) { + public String getPathToDatabaseFile(@Nullable Long projectId) { long notNullProjectId = projectId != null ? projectId.longValue() : 0L; ReadWriteLock rwl = getLock(notNullProjectId); try { @@ -93,7 +91,7 @@ public class PreviewCache implements ServerExtension { rwl.writeLock().unlock(); } File dbFile = new File(getCacheLocation(projectId), lastTimestampPerProject.get(notNullProjectId) + PreviewDatabaseFactory.H2_FILE_SUFFIX); - return fileToByte(dbFile); + return dbFile.getAbsolutePath(); } finally { rwl.readLock().unlock(); } @@ -129,14 +127,6 @@ public class PreviewCache implements ServerExtension { lastTimestampPerProject.put(notNullProjectId, newTimestamp); } - private byte[] fileToByte(File dbFile) { - try { - return Files.toByteArray(dbFile); - } catch (IOException e) { - throw new SonarException("Unable to create h2 database file", e); - } - } - private synchronized ReadWriteLock getLock(long notNullProjectId) { if (!lockPerProject.containsKey(notNullProjectId)) { lockPerProject.put(notNullProjectId, new ReentrantReadWriteLock(true)); diff --git a/sonar-core/src/test/java/org/sonar/core/preview/PreviewCacheTest.java b/sonar-core/src/test/java/org/sonar/core/preview/PreviewCacheTest.java index 6ef5ae0974e..5440062cb3a 100644 --- a/sonar-core/src/test/java/org/sonar/core/preview/PreviewCacheTest.java +++ b/sonar-core/src/test/java/org/sonar/core/preview/PreviewCacheTest.java @@ -87,16 +87,16 @@ public class PreviewCacheTest { when(dryRunDatabaseFactory.createNewDatabaseForDryRun(isNull(Long.class), any(File.class), anyString())).thenAnswer(new Answer<File>() { public File answer(InvocationOnMock invocation) throws IOException { Object[] args = invocation.getArguments(); - File dbFile = new File(new File(dryRunCacheLocation, "default"), (String) args[2] + ".h2.db"); + File dbFile = new File(new File(dryRunCacheLocation, "default"), args[2] + ".h2.db"); FileUtils.write(dbFile, "fake db content"); return dbFile; } }); - byte[] dbContent = dryRunCache.getDatabaseForPreview(null); - assertThat(new String(dbContent)).isEqualTo("fake db content"); + String path = dryRunCache.getPathToDatabaseFile(null); + assertThat(FileUtils.readFileToString(new File(path))).isEqualTo("fake db content"); - dbContent = dryRunCache.getDatabaseForPreview(null); - assertThat(new String(dbContent)).isEqualTo("fake db content"); + path = dryRunCache.getPathToDatabaseFile(null); + assertThat(FileUtils.readFileToString(new File(path))).isEqualTo("fake db content"); verify(dryRunDatabaseFactory, times(1)).createNewDatabaseForDryRun(anyLong(), any(File.class), anyString()); } @@ -112,11 +112,11 @@ public class PreviewCacheTest { } }); when(resourceDao.getRootProjectByComponentId(123L)).thenReturn(new ResourceDto().setId(123L)); - byte[] dbContent = dryRunCache.getDatabaseForPreview(123L); - assertThat(new String(dbContent)).isEqualTo("fake db content"); + String path = dryRunCache.getPathToDatabaseFile(123L); + assertThat(FileUtils.readFileToString(new File(path))).isEqualTo("fake db content"); - dbContent = dryRunCache.getDatabaseForPreview(123L); - assertThat(new String(dbContent)).isEqualTo("fake db content"); + path = dryRunCache.getPathToDatabaseFile(123L); + assertThat(FileUtils.readFileToString(new File(path))).isEqualTo("fake db content"); verify(dryRunDatabaseFactory, times(1)).createNewDatabaseForDryRun(anyLong(), any(File.class), anyString()); } @@ -140,15 +140,15 @@ public class PreviewCacheTest { return dbFile; } }); - byte[] dbContent = dryRunCache.getDatabaseForPreview(null); - assertThat(new String(dbContent)).isEqualTo("fake db content 1"); + String path = dryRunCache.getPathToDatabaseFile(null); + assertThat(FileUtils.readFileToString(new File(path))).isEqualTo("fake db content 1"); // Emulate invalidation of cache Thread.sleep(100); when(propertiesDao.selectGlobalProperty(PreviewCache.SONAR_PREVIEW_CACHE_LAST_UPDATE_KEY)).thenReturn(new PropertyDto().setValue("" + System.currentTimeMillis())); - dbContent = dryRunCache.getDatabaseForPreview(null); - assertThat(new String(dbContent)).isEqualTo("fake db content 2"); + path = dryRunCache.getPathToDatabaseFile(null); + assertThat(FileUtils.readFileToString(new File(path))).isEqualTo("fake db content 2"); verify(dryRunDatabaseFactory, times(2)).createNewDatabaseForDryRun(anyLong(), any(File.class), anyString()); } @@ -174,15 +174,15 @@ public class PreviewCacheTest { }); when(resourceDao.getRootProjectByComponentId(123L)).thenReturn(new ResourceDto().setId(123L)); - byte[] dbContent = dryRunCache.getDatabaseForPreview(123L); - assertThat(new String(dbContent)).isEqualTo("fake db content 1"); + String path = dryRunCache.getPathToDatabaseFile(123L); + assertThat(FileUtils.readFileToString(new File(path))).isEqualTo("fake db content 1"); // Emulate invalidation of cache Thread.sleep(100); when(propertiesDao.selectProjectProperty(123L, PreviewCache.SONAR_PREVIEW_CACHE_LAST_UPDATE_KEY)).thenReturn(new PropertyDto().setValue("" + System.currentTimeMillis())); - dbContent = dryRunCache.getDatabaseForPreview(123L); - assertThat(new String(dbContent)).isEqualTo("fake db content 2"); + path = dryRunCache.getPathToDatabaseFile(123L); + assertThat(FileUtils.readFileToString(new File(path))).isEqualTo("fake db content 2"); verify(dryRunDatabaseFactory, times(2)).createNewDatabaseForDryRun(anyLong(), any(File.class), anyString()); } |