]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7909 reset sharedMemory at startup 1227/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 8 Sep 2016 16:04:43 +0000 (18:04 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 9 Sep 2016 13:57:02 +0000 (15:57 +0200)
server/sonar-process/src/main/java/org/sonar/process/AllProcessesCommands.java
server/sonar-process/src/test/java/org/sonar/process/AllProcessesCommandsTest.java
sonar-application/src/main/java/org/sonar/application/AppFileSystem.java
sonar-application/src/test/java/org/sonar/application/AppFileSystemTest.java

index d9bb448c1d62f4f3c1e74dfb188c993b3a7de8fa..5d55ee1c1f7e7bf10d4c86309c5e2098cbe6aa2d 100644 (file)
@@ -101,6 +101,12 @@ public class AllProcessesCommands implements AutoCloseable {
     }
   }
 
+  public void clean() {
+    for (int i = 0; i < MAX_PROCESSES; i++) {
+      cleanData(i);
+    }
+  }
+
   public ProcessCommands create(int processNumber) {
     return createForProcess(processNumber, false);
   }
index 1c26836fc1e20ed34b0bde467af2f767e75319c3..3d915f0f52c947c3e16d29805d2417fbc39e607b 100644 (file)
@@ -60,129 +60,152 @@ public class AllProcessesCommandsTest {
 
   @Test
   public void write_and_read_up() throws IOException {
-    AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder());
-    int offset = 0;
+    try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
+      int offset = 0;
 
-    assertThat(commands.isUp(PROCESS_NUMBER)).isFalse();
-    assertThat(readByte(commands, offset)).isEqualTo(EMPTY);
+      assertThat(commands.isUp(PROCESS_NUMBER)).isFalse();
+      assertThat(readByte(commands, offset)).isEqualTo(EMPTY);
 
-    commands.setUp(PROCESS_NUMBER);
-    assertThat(commands.isUp(PROCESS_NUMBER)).isTrue();
-    assertThat(readByte(commands, offset)).isEqualTo(UP);
+      commands.setUp(PROCESS_NUMBER);
+      assertThat(commands.isUp(PROCESS_NUMBER)).isTrue();
+      assertThat(readByte(commands, offset)).isEqualTo(UP);
+    }
   }
 
   @Test
   public void write_and_read_operational() throws IOException {
-    AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder());
-    int offset = 3;
+    try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
+      int offset = 3;
 
-    assertThat(commands.isOperational(PROCESS_NUMBER)).isFalse();
-    assertThat(readByte(commands, offset)).isEqualTo(EMPTY);
+      assertThat(commands.isOperational(PROCESS_NUMBER)).isFalse();
+      assertThat(readByte(commands, offset)).isEqualTo(EMPTY);
 
-    commands.setOperational(PROCESS_NUMBER);
-    assertThat(commands.isOperational(PROCESS_NUMBER)).isTrue();
-    assertThat(readByte(commands, offset)).isEqualTo(OPERATIONAL);
+      commands.setOperational(PROCESS_NUMBER);
+      assertThat(commands.isOperational(PROCESS_NUMBER)).isTrue();
+      assertThat(readByte(commands, offset)).isEqualTo(OPERATIONAL);
+    }
   }
 
   @Test
   public void write_and_read_ping() throws IOException {
-    AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder());
+    try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
 
-    int offset = 4;
-    assertThat(readLong(commands, offset)).isEqualTo(0L);
+      int offset = 4;
+      assertThat(readLong(commands, offset)).isEqualTo(0L);
 
-    long currentTime = System.currentTimeMillis();
-    commands.ping(PROCESS_NUMBER);
-    assertThat(readLong(commands, offset)).isGreaterThanOrEqualTo(currentTime);
+      long currentTime = System.currentTimeMillis();
+      commands.ping(PROCESS_NUMBER);
+      assertThat(readLong(commands, offset)).isGreaterThanOrEqualTo(currentTime);
+    }
   }
 
   @Test
   public void write_and_read_jmx_url() throws IOException {
-    AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder());
+    try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
 
-    int offset = 12;
-    for (int i = 0; i < 500; i++) {
-      assertThat(readByte(commands, offset + i)).isEqualTo(EMPTY);
-    }
+      int offset = 12;
+      for (int i = 0; i < 500; i++) {
+        assertThat(readByte(commands, offset + i)).isEqualTo(EMPTY);
+      }
 
-    commands.setSystemInfoUrl(PROCESS_NUMBER, "jmx:foo");
-    assertThat(readByte(commands, offset)).isNotEqualTo(EMPTY);
-    assertThat(commands.getSystemInfoUrl(PROCESS_NUMBER)).isEqualTo("jmx:foo");
+      commands.setSystemInfoUrl(PROCESS_NUMBER, "jmx:foo");
+      assertThat(readByte(commands, offset)).isNotEqualTo(EMPTY);
+      assertThat(commands.getSystemInfoUrl(PROCESS_NUMBER)).isEqualTo("jmx:foo");
+    }
   }
 
   @Test
   public void ask_for_stop() throws Exception {
-    AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder());
-    int offset = 1;
+    try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
+      int offset = 1;
 
-    assertThat(readByte(commands, offset)).isNotEqualTo(STOP);
-    assertThat(commands.askedForStop(PROCESS_NUMBER)).isFalse();
+      assertThat(readByte(commands, offset)).isNotEqualTo(STOP);
+      assertThat(commands.askedForStop(PROCESS_NUMBER)).isFalse();
 
-    commands.askForStop(PROCESS_NUMBER);
-    assertThat(commands.askedForStop(PROCESS_NUMBER)).isTrue();
-    assertThat(readByte(commands, offset)).isEqualTo(STOP);
+      commands.askForStop(PROCESS_NUMBER);
+      assertThat(commands.askedForStop(PROCESS_NUMBER)).isTrue();
+      assertThat(readByte(commands, offset)).isEqualTo(STOP);
+    }
   }
 
   @Test
   public void ask_for_restart() throws Exception {
-    AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder());
-    int offset = 2;
+    try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
+      int offset = 2;
 
-    assertThat(readByte(commands, offset)).isNotEqualTo(RESTART);
-    assertThat(commands.askedForRestart(PROCESS_NUMBER)).isFalse();
+      assertThat(readByte(commands, offset)).isNotEqualTo(RESTART);
+      assertThat(commands.askedForRestart(PROCESS_NUMBER)).isFalse();
 
-    commands.askForRestart(PROCESS_NUMBER);
-    assertThat(commands.askedForRestart(PROCESS_NUMBER)).isTrue();
-    assertThat(readByte(commands, offset)).isEqualTo(RESTART);
+      commands.askForRestart(PROCESS_NUMBER);
+      assertThat(commands.askedForRestart(PROCESS_NUMBER)).isTrue();
+      assertThat(readByte(commands, offset)).isEqualTo(RESTART);
+    }
   }
 
   @Test
   public void acknowledgeAskForRestart_has_no_effect_when_no_restart_asked() throws Exception {
-    AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder());
-    int offset = 2;
+    try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
+      int offset = 2;
 
-    assertThat(readByte(commands, offset)).isNotEqualTo(RESTART);
-    assertThat(commands.askedForRestart(PROCESS_NUMBER)).isFalse();
+      assertThat(readByte(commands, offset)).isNotEqualTo(RESTART);
+      assertThat(commands.askedForRestart(PROCESS_NUMBER)).isFalse();
 
-    commands.acknowledgeAskForRestart(PROCESS_NUMBER);
-    assertThat(readByte(commands, offset)).isNotEqualTo(RESTART);
-    assertThat(commands.askedForRestart(PROCESS_NUMBER)).isFalse();
+      commands.acknowledgeAskForRestart(PROCESS_NUMBER);
+      assertThat(readByte(commands, offset)).isNotEqualTo(RESTART);
+      assertThat(commands.askedForRestart(PROCESS_NUMBER)).isFalse();
+    }
   }
 
   @Test
   public void acknowledgeAskForRestart_resets_askForRestart_has_no_effect_when_no_restart_asked() throws Exception {
-    AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder());
-    int offset = 2;
+    try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
+      int offset = 2;
 
-    commands.askForRestart(PROCESS_NUMBER);
-    assertThat(commands.askedForRestart(PROCESS_NUMBER)).isTrue();
-    assertThat(readByte(commands, offset)).isEqualTo(RESTART);
+      commands.askForRestart(PROCESS_NUMBER);
+      assertThat(commands.askedForRestart(PROCESS_NUMBER)).isTrue();
+      assertThat(readByte(commands, offset)).isEqualTo(RESTART);
 
-    commands.acknowledgeAskForRestart(PROCESS_NUMBER);
-    assertThat(readByte(commands, offset)).isNotEqualTo(RESTART);
-    assertThat(commands.askedForRestart(PROCESS_NUMBER)).isFalse();
+      commands.acknowledgeAskForRestart(PROCESS_NUMBER);
+      assertThat(readByte(commands, offset)).isNotEqualTo(RESTART);
+      assertThat(commands.askedForRestart(PROCESS_NUMBER)).isFalse();
+    }
   }
 
   @Test
   public void getProcessCommands_fails_if_processNumber_is_less_than_0() throws Exception {
-    AllProcessesCommands allProcessesCommands = new AllProcessesCommands(temp.newFolder());
-    int processNumber = -2;
+    try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
+      int processNumber = -2;
 
-    expectedException.expect(IllegalArgumentException.class);
-    expectedException.expectMessage("Process number " + processNumber + " is not valid");
+      expectedException.expect(IllegalArgumentException.class);
+      expectedException.expectMessage("Process number " + processNumber + " is not valid");
 
-    allProcessesCommands.createAfterClean(processNumber);
+      commands.createAfterClean(processNumber);
+    }
   }
 
   @Test
   public void getProcessCommands_fails_if_processNumber_is_higher_than_MAX_PROCESSES() throws Exception {
-    AllProcessesCommands allProcessesCommands = new AllProcessesCommands(temp.newFolder());
-    int processNumber = MAX_PROCESSES + 1;
+    try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
+      int processNumber = MAX_PROCESSES + 1;
 
-    expectedException.expect(IllegalArgumentException.class);
-    expectedException.expectMessage("Process number " + processNumber + " is not valid");
+      expectedException.expect(IllegalArgumentException.class);
+      expectedException.expectMessage("Process number " + processNumber + " is not valid");
 
-    allProcessesCommands.createAfterClean(processNumber);
+      commands.createAfterClean(processNumber);
+    }
+  }
+
+  @Test
+  public void clean_cleans_sharedMemory_of_any_process_less_than_MAX_PROCESSES() throws IOException {
+    try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
+      for (int i = 0; i < MAX_PROCESSES; i++) {
+        commands.create(i).setUp();
+      }
+      commands.clean();
+      for (int i = 0; i < MAX_PROCESSES; i++) {
+        assertThat(commands.create(i).isUp()).isFalse();
+      }
+    }
   }
 
   private byte readByte(AllProcessesCommands commands, int offset) {
index cea9d9a0e3d63655d6d8521356bab2238e31871f..01c19276757d9df9e218577f90b7d1238fa5a26f 100644 (file)
@@ -31,6 +31,7 @@ import java.nio.file.attribute.BasicFileAttributes;
 import java.util.EnumSet;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.sonar.process.AllProcessesCommands;
 import org.sonar.process.Props;
 import org.sonar.process.monitor.FileSystem;
 
@@ -80,7 +81,10 @@ public class AppFileSystem implements FileSystem {
     createDirectory(props, PATH_DATA);
     createDirectory(props, PATH_WEB);
     createDirectory(props, PATH_LOGS);
-    createOrCleanTempDirectory(props, PATH_TEMP);
+    File tempDir = createOrCleanTempDirectory(props, PATH_TEMP);
+    try (AllProcessesCommands allProcessesCommands = new AllProcessesCommands(tempDir)) {
+      allProcessesCommands.clean();
+    }
   }
 
   @Override
@@ -119,12 +123,13 @@ public class AppFileSystem implements FileSystem {
     }
   }
 
-  private static void createOrCleanTempDirectory(Props props, String propKey) throws IOException {
+  private static File createOrCleanTempDirectory(Props props, String propKey) throws IOException {
     File dir = props.nonNullValueAsFile(propKey);
     LOG.info("Cleaning or creating temp directory {}", dir.getAbsolutePath());
     if (!createDirectory(props, propKey)) {
       Files.walkFileTree(dir.toPath(), FOLLOW_LINKS, CleanTempDirFileVisitor.VISIT_MAX_DEPTH, new CleanTempDirFileVisitor(dir.toPath()));
     }
+    return dir;
   }
 
   private static class CleanTempDirFileVisitor extends SimpleFileVisitor<Path> {
index aaed934963ca364fa9ef43ec4c0a35d053763bec..3b44a40f91bd69e8369622c058939bffe70d0020 100644 (file)
@@ -33,9 +33,11 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
+import org.sonar.process.AllProcessesCommands;
 import org.sonar.process.Props;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.process.ProcessCommands.MAX_PROCESSES;
 
 public class AppFileSystemTest {
 
@@ -178,7 +180,27 @@ public class AppFileSystemTest {
     assertThat(fileInTempDir).doesNotExist();
     assertThat(getFileKey(tempDir)).isEqualTo(tempDirKey);
     assertThat(getFileKey(sharedmemory)).isEqualTo(fileKey);
-    assertThat(FileUtils.readFileToString(sharedmemory)).isEqualTo("toto");
+    // content of sharedMemory file is reset
+    assertThat(FileUtils.readFileToString(sharedmemory)).isNotEqualTo("toto");
+  }
+
+  @Test
+  public void reset_cleans_the_sharedmemory_file() throws IOException {
+    File tempDir = new File(homeDir, DEFAULT_TEMP_DIR_NAME);
+    assertThat(tempDir.mkdir()).isTrue();
+    try (AllProcessesCommands commands = new AllProcessesCommands(tempDir)) {
+      for (int i = 0; i < MAX_PROCESSES; i++) {
+        commands.create(i).setUp();
+      }
+
+      AppFileSystem underTest = new AppFileSystem(new Props(properties));
+      underTest.verifyProps();
+      underTest.reset();
+
+      for (int i = 0; i < MAX_PROCESSES; i++) {
+        assertThat(commands.create(i).isUp()).isFalse();
+      }
+    }
   }
 
   @CheckForNull