From 69d136d3a71e36521ddd56d0e5b037fb7472c95d Mon Sep 17 00:00:00 2001 From: Eric Hartmann Date: Sat, 21 Feb 2015 08:28:12 +0100 Subject: [PATCH] SONAR-6036 : Remove KnownJavaCommand and clean the memory at the startup of the ProcessCommand --- .../sonar/process/monitor/JavaCommand.java | 5 +- .../process/monitor/KnownJavaCommand.java | 64 ------------------- .../org/sonar/process/monitor/Monitor.java | 2 +- .../sonar/process/monitor/MonitorTest.java | 2 +- .../org/sonar/process/ProcessCommands.java | 7 ++ .../main/java/org/sonar/application/App.java | 3 +- 6 files changed, 11 insertions(+), 72 deletions(-) delete mode 100644 server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/KnownJavaCommand.java diff --git a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/JavaCommand.java b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/JavaCommand.java index f4593308b9b..d4b774cb851 100644 --- a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/JavaCommand.java +++ b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/JavaCommand.java @@ -58,10 +58,7 @@ public class JavaCommand { public JavaCommand(String key) { this.key = key; - processIndex = KnownJavaCommand.lookIndexFor(key); - if (processIndex == -1) { - processIndex = Monitor.getNextProcessId(); - } + processIndex = Monitor.getNextProcessId(); } public String getKey() { diff --git a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/KnownJavaCommand.java b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/KnownJavaCommand.java deleted file mode 100644 index ce9a49aed1a..00000000000 --- a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/KnownJavaCommand.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.process.monitor; - -/** - * Created by eric on 20/02/15. - */ -public enum KnownJavaCommand { - APP("app", 0), WEB("web", 1), ELASTIC_SEARCH("search", 2), UNKNOWN("unknown", -1); - - private String key; - private int index; - - KnownJavaCommand(String key, int index) { - this.key = key; - this.index = index; - } - - public String getKey() { - return key; - } - - public int getIndex() { - return index; - } - - public static KnownJavaCommand lookFor(String key) { - for (KnownJavaCommand knownJavaCommand : KnownJavaCommand.values()) { - if (knownJavaCommand.getKey().equals(key)) { - return knownJavaCommand; - } - } - return KnownJavaCommand.UNKNOWN; - } - - public static int lookIndexFor(String key) { - return lookFor(key).getIndex(); - } - - public static int getFirstIndexAvailable() { - int result = 0; - for (KnownJavaCommand knownJavaCommand : KnownJavaCommand.values()) { - result = knownJavaCommand.getIndex() >= result ? knownJavaCommand.getIndex() + 1 : result; - } - return result; - } -} diff --git a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/Monitor.java b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/Monitor.java index ccbeb558db0..fd546b9df5d 100644 --- a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/Monitor.java +++ b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/Monitor.java @@ -40,7 +40,7 @@ public class Monitor { // used by awaitStop() to block until all processes are shutdown private final List watcherThreads = new CopyOnWriteArrayList(); - static int nextProcessId = KnownJavaCommand.getFirstIndexAvailable(); + static int nextProcessId = 0; Monitor(JavaProcessLauncher launcher, SystemExit exit, TerminatorThread terminator) { this.launcher = launcher; diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/MonitorTest.java b/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/MonitorTest.java index 4a27d50ed62..e88b8d49379 100644 --- a/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/MonitorTest.java +++ b/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/MonitorTest.java @@ -217,7 +217,7 @@ public class MonitorTest { } catch (IllegalStateException e) { assertThat(e).hasMessageStartingWith("The maximum number of processes launched has been reached "); } finally { - Monitor.nextProcessId = KnownJavaCommand.getFirstIndexAvailable(); + Monitor.nextProcessId = 0; } } diff --git a/server/sonar-process/src/main/java/org/sonar/process/ProcessCommands.java b/server/sonar-process/src/main/java/org/sonar/process/ProcessCommands.java index 0edd51d3456..c9eafb30e54 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/ProcessCommands.java +++ b/server/sonar-process/src/main/java/org/sonar/process/ProcessCommands.java @@ -84,6 +84,7 @@ public class ProcessCommands { try { sharedMemory = new RandomAccessFile(new File(directory, "sharedmemory"), "rw"); mappedByteBuffer = sharedMemory.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, MAX_SHARED_MEMORY); + cleanData(); } catch (IOException e) { throw new IllegalArgumentException("Unable to create shared memory : ", e); } @@ -143,6 +144,12 @@ public class ProcessCommands { return result; } + private void cleanData() { + for (int i = offset(); i < BYTE_LENGTH_FOR_ONE_PROCESS; i++) { + mappedByteBuffer.put(i, EMPTY); + } + } + public static final int getMaxProcesses() { return MAX_PROCESSES; } diff --git a/sonar-application/src/main/java/org/sonar/application/App.java b/sonar-application/src/main/java/org/sonar/application/App.java index f5418d42ab8..68cb982a845 100644 --- a/sonar-application/src/main/java/org/sonar/application/App.java +++ b/sonar-application/src/main/java/org/sonar/application/App.java @@ -28,7 +28,6 @@ import org.sonar.process.Props; import org.sonar.process.StopWatcher; import org.sonar.process.Stoppable; import org.sonar.process.monitor.JavaCommand; -import org.sonar.process.monitor.KnownJavaCommand; import org.sonar.process.monitor.Monitor; import java.io.File; @@ -55,7 +54,7 @@ public class App implements Stoppable { public void start(Props props) { if (props.valueAsBoolean(ProcessConstants.ENABLE_STOP_COMMAND, false)) { File tempDir = props.nonNullValueAsFile(ProcessConstants.PATH_TEMP); - ProcessCommands commands = new ProcessCommands(tempDir, KnownJavaCommand.APP.getIndex()); + ProcessCommands commands = new ProcessCommands(tempDir, 0); stopWatcher = new StopWatcher(commands, this); stopWatcher.start(); } -- 2.39.5