]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6083 Server OutOfMemory error with preview analysis mode 53/head
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 22 Jan 2015 20:53:13 +0000 (21:53 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 22 Jan 2015 20:53:13 +0000 (21:53 +0100)
server/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java
server/sonar-web/src/main/webapp/WEB-INF/app/controllers/batch_bootstrap_controller.rb
sonar-core/src/main/java/org/sonar/core/preview/PreviewCache.java
sonar-core/src/test/java/org/sonar/core/preview/PreviewCacheTest.java

index 2f75323511bcdd4e013eec5465920e55a66151cf..89ad70b2c9c80e2fd042cf53857f2bba423860dc 100644 (file)
@@ -360,8 +360,8 @@ public final class JRubyFacade {
     }
   }
 
-  public byte[] createDatabaseForPreview(@Nullable Long projectId) {
-    return get(PreviewCache.class).getDatabaseForPreview(projectId);
+  public String pathToPreviewDbFile(@Nullable Long projectId) {
+     return get(PreviewCache.class).getPathToDatabaseFile(projectId);
   }
 
   public String getPeriodLabel(int periodIndex) {
index 111f423168c68a3ae8566edc5274c991ad6147c1..3b96ee03de6ff1ce53dc24c9f247e1ed7afdad11 100644 (file)
@@ -30,9 +30,9 @@ class BatchBootstrapController < Api::ApiController
     return render_unauthorized("You're not authorized to execute a dry run analysis. Please contact your SonarQube administrator.") if !has_dryrun_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_role?(:user, project)
-    db_content = java_facade.createDatabaseForPreview(project && project.id)
+    db_file = java_facade.pathToPreviewDbFile(project && project.id)
 
-    send_data String.from_java_bytes(db_content)
+    send_file db_file
   end
 
 
index 9cfeceec6617cd2cb31fd548dca6415e9ebe6008..1e02ad829fefc2a956c79fbc8c2bb4b58ed5736b 100644 (file)
@@ -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));
index 6ef5ae0974e812c8af20cc48737cd958c4490da1..5440062cb3afd3daf0ec82a8e10faf447827d9c9 100644 (file)
@@ -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());
   }