From 4f877fb1d54b1c4127d9063ca48c523cb51daccf Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 8 Sep 2016 18:04:43 +0200 Subject: [PATCH] SONAR-7909 reset sharedMemory at startup --- .../sonar/process/AllProcessesCommands.java | 6 + .../process/AllProcessesCommandsTest.java | 157 ++++++++++-------- .../org/sonar/application/AppFileSystem.java | 9 +- .../sonar/application/AppFileSystemTest.java | 24 ++- 4 files changed, 126 insertions(+), 70 deletions(-) diff --git a/server/sonar-process/src/main/java/org/sonar/process/AllProcessesCommands.java b/server/sonar-process/src/main/java/org/sonar/process/AllProcessesCommands.java index d9bb448c1d6..5d55ee1c1f7 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/AllProcessesCommands.java +++ b/server/sonar-process/src/main/java/org/sonar/process/AllProcessesCommands.java @@ -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); } diff --git a/server/sonar-process/src/test/java/org/sonar/process/AllProcessesCommandsTest.java b/server/sonar-process/src/test/java/org/sonar/process/AllProcessesCommandsTest.java index 1c26836fc1e..3d915f0f52c 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/AllProcessesCommandsTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/AllProcessesCommandsTest.java @@ -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) { diff --git a/sonar-application/src/main/java/org/sonar/application/AppFileSystem.java b/sonar-application/src/main/java/org/sonar/application/AppFileSystem.java index cea9d9a0e3d..01c19276757 100644 --- a/sonar-application/src/main/java/org/sonar/application/AppFileSystem.java +++ b/sonar-application/src/main/java/org/sonar/application/AppFileSystem.java @@ -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 { diff --git a/sonar-application/src/test/java/org/sonar/application/AppFileSystemTest.java b/sonar-application/src/test/java/org/sonar/application/AppFileSystemTest.java index aaed934963c..3b44a40f91b 100644 --- a/sonar-application/src/test/java/org/sonar/application/AppFileSystemTest.java +++ b/sonar-application/src/test/java/org/sonar/application/AppFileSystemTest.java @@ -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 -- 2.39.5