diff options
author | Aurelien Poscia <aurelien.poscia@sonarsource.com> | 2022-03-15 08:40:06 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-03-18 20:02:58 +0000 |
commit | 87d3bfccdbac9d330e9ad5a78da0ebbcac4d8284 (patch) | |
tree | f1b15343d98283eda2be5ac6ef5c707eb98d1f37 /server | |
parent | 3cd52ef6dab2fe5b7369141819fdf8f30b484f90 (diff) | |
download | sonarqube-87d3bfccdbac9d330e9ad5a78da0ebbcac4d8284.tar.gz sonarqube-87d3bfccdbac9d330e9ad5a78da0ebbcac4d8284.zip |
SONAR-10179 improve consistency of start/stop log messages and identifiers of processes
Author: Aurelien Poscia <aurelien.poscia@sonarsource.com>
Diffstat (limited to 'server')
14 files changed, 54 insertions, 48 deletions
diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/app/CeServer.java b/server/sonar-ce/src/main/java/org/sonar/ce/app/CeServer.java index 9aea84a0e15..4a100845946 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/app/CeServer.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/app/CeServer.java @@ -36,6 +36,7 @@ import org.sonar.process.ProcessEntryPoint; import org.sonar.process.Props; import static com.google.common.base.Preconditions.checkState; +import static org.sonar.process.ProcessId.COMPUTE_ENGINE; /** * The Compute Engine server which starts a daemon thread to run the {@link ComputeEngineImpl} when it's {@link #start()} @@ -159,22 +160,22 @@ public class CeServer implements Monitored { private boolean attemptStartup() { try { - LOG.info("Compute Engine starting up..."); + LOG.info("{} starting up...", COMPUTE_ENGINE.getHumanReadableName()); computeEngine.startup(); - LOG.info("Compute Engine is operational"); + LOG.info("{} is started", COMPUTE_ENGINE.getHumanReadableName()); return true; } catch (org.sonar.api.utils.MessageException | org.sonar.process.MessageException e) { - LOG.error("Compute Engine startup failed: " + e.getMessage()); + LOG.error("{} startup failed: {}", COMPUTE_ENGINE.getHumanReadableName(), e.getMessage()); return false; } catch (Throwable e) { - LOG.error("Compute Engine startup failed", e); + LOG.error("{} startup failed", COMPUTE_ENGINE.getHumanReadableName(), e); return false; } } private void attemptShutdown() { try { - LOG.info("Compute Engine is stopping..."); + LOG.info("{} is stopping...", COMPUTE_ENGINE.getHumanReadableName()); if (!hardStop) { computeEngine.stopProcessing(); } @@ -182,9 +183,9 @@ public class CeServer implements Monitored { // make sure that interrupt flag is unset because we don't want to interrupt shutdown of pico container interrupted(); computeEngine.shutdown(); - LOG.info("Compute Engine is stopped"); + LOG.info("{} is stopped", COMPUTE_ENGINE.getHumanReadableName()); } catch (Throwable e) { - LOG.error("Compute Engine failed to stop", e); + LOG.error("{} failed to stop", COMPUTE_ENGINE.getHumanReadableName(), e); } } diff --git a/server/sonar-main/src/main/java/org/sonar/application/ProcessLauncherImpl.java b/server/sonar-main/src/main/java/org/sonar/application/ProcessLauncherImpl.java index b81cd0f4f79..f63f0e9307f 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/ProcessLauncherImpl.java +++ b/server/sonar-main/src/main/java/org/sonar/application/ProcessLauncherImpl.java @@ -117,7 +117,7 @@ public class ProcessLauncherImpl implements ProcessLauncher { if (process != null) { process.destroyForcibly(); } - throw new IllegalStateException(format("Fail to launch monitor of process [%s]", processId.getKey()), e); + throw new IllegalStateException(format("Fail to launch monitor of process [%s]", processId.getHumanReadableName()), e); } } @@ -127,7 +127,7 @@ public class ProcessLauncherImpl implements ProcessLauncher { logLaunchedCommand(esScriptCommand, processBuilder); return processBuilder.start(); } catch (Exception e) { - throw new IllegalStateException(format("Fail to launch process [%s]", esScriptCommand.getProcessId().getKey()), e); + throw new IllegalStateException(format("Fail to launch process [%s]", esScriptCommand.getProcessId().getHumanReadableName()), e); } } @@ -223,7 +223,7 @@ public class ProcessLauncherImpl implements ProcessLauncher { logLaunchedCommand(javaCommand, processBuilder); return processBuilder.start(); } catch (Exception e) { - throw new IllegalStateException(format("Fail to launch process [%s]", processId.getKey()), e); + throw new IllegalStateException(format("Fail to launch process [%s]", processId.getHumanReadableName()), e); } } diff --git a/server/sonar-main/src/main/java/org/sonar/application/SchedulerImpl.java b/server/sonar-main/src/main/java/org/sonar/application/SchedulerImpl.java index 3e4b2cdb72e..391a8bc7f63 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/SchedulerImpl.java +++ b/server/sonar-main/src/main/java/org/sonar/application/SchedulerImpl.java @@ -202,7 +202,7 @@ public class SchedulerImpl implements Scheduler, ManagedProcessEventListener, Pr // this early stop of the process will be picked up by onProcessStop (which calls hardStopAsync) // through interface ProcessLifecycleListener#onProcessState implemented by SchedulerImpl LOG.trace("Failed to start process [{}] (currentThread={})", - processHandler.getProcessId().getKey(), Thread.currentThread().getName(), e); + processHandler.getProcessId().getHumanReadableName(), Thread.currentThread().getName(), e); } } @@ -240,7 +240,7 @@ public class SchedulerImpl implements Scheduler, ManagedProcessEventListener, Pr private void stopProcess(ProcessId processId) throws InterruptedException { ManagedProcessHandler process = processesById.get(processId); if (process != null) { - LOG.debug("Stopping [{}]...", process.getProcessId().getKey()); + LOG.debug("Stopping [{}]...", process.getProcessId().getHumanReadableName()); process.stop(); } } @@ -331,7 +331,7 @@ public class SchedulerImpl implements Scheduler, ManagedProcessEventListener, Pr if (type == Type.OPERATIONAL) { onProcessOperational(processId); } else if (type == Type.ASK_FOR_RESTART && nodeLifecycle.tryToMoveTo(RESTARTING)) { - LOG.info("SQ restart requested by Process[{}]", processId.getKey()); + LOG.info("SQ restart requested by Process[{}]", processId.getHumanReadableName()); stopAsyncForRestart(); } } @@ -341,7 +341,7 @@ public class SchedulerImpl implements Scheduler, ManagedProcessEventListener, Pr appState.setOperational(processId); boolean lastProcessStarted = operationalCountDown.decrementAndGet() == 0; if (lastProcessStarted && nodeLifecycle.tryToMoveTo(NodeLifecycle.State.OPERATIONAL)) { - LOG.info("SonarQube is up"); + LOG.info("SonarQube is operational"); } } @@ -352,7 +352,7 @@ public class SchedulerImpl implements Scheduler, ManagedProcessEventListener, Pr tryToStartAll(); } catch (InterruptedException e) { // startup process was interrupted, let's assume it means shutdown was requested - LOG.debug("Startup process was interrupted on notification that process [{}] was operation", processId.getKey(), e); + LOG.debug("Startup process was interrupted on notification that process [{}] was operational", processId.getHumanReadableName(), e); hardStopAsync(); Thread.currentThread().interrupt(); } @@ -375,7 +375,7 @@ public class SchedulerImpl implements Scheduler, ManagedProcessEventListener, Pr } private void onProcessStop(ProcessId processId) { - LOG.info("Process[{}] is stopped", processId.getKey()); + LOG.info("Process[{}] is stopped", processId.getHumanReadableName()); boolean lastProcessStopped = stopCountDown.decrementAndGet() == 0; switch (nodeLifecycle.getState()) { case RESTARTING: diff --git a/server/sonar-main/src/main/java/org/sonar/application/process/AbstractManagedProcess.java b/server/sonar-main/src/main/java/org/sonar/application/process/AbstractManagedProcess.java index 73c9942a32b..9584c3eaa26 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/process/AbstractManagedProcess.java +++ b/server/sonar-main/src/main/java/org/sonar/application/process/AbstractManagedProcess.java @@ -78,9 +78,9 @@ abstract class AbstractManagedProcess implements ManagedProcess { int exitValue = process.waitFor(); if (exitValueLogged.compareAndSet(false, true)) { if (exitValue != EXPECTED_EXIT_VALUE) { - LOG.warn("Process exited with exit value [{}]: {}", processId.getKey(), exitValue); + LOG.warn("Process exited with exit value [{}]: {}", processId.getHumanReadableName(), exitValue); } else { - LOG.debug("Process exited with exit value [{}]: {}", processId.getKey(), exitValue); + LOG.debug("Process exited with exit value [{}]: {}", processId.getHumanReadableName(), exitValue); } } } diff --git a/server/sonar-main/src/main/java/org/sonar/application/process/ManagedProcessHandler.java b/server/sonar-main/src/main/java/org/sonar/application/process/ManagedProcessHandler.java index ef57c88b61c..272232292bf 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/process/ManagedProcessHandler.java +++ b/server/sonar-main/src/main/java/org/sonar/application/process/ManagedProcessHandler.java @@ -75,7 +75,7 @@ public class ManagedProcessHandler { try { this.process = commandLauncher.get(); } catch (RuntimeException e) { - LOG.error("Fail to launch process [{}]", processId.getKey(), e); + LOG.error("Failed to launch process [{}]", processId.getHumanReadableName(), e); lifecycle.tryToMoveTo(ManagedProcessLifecycle.State.STOPPING); finalizeStop(); throw e; @@ -104,7 +104,7 @@ public class ManagedProcessHandler { if (lifecycle.tryToMoveTo(ManagedProcessLifecycle.State.STOPPING)) { stopImpl(); if (process != null && process.isAlive()) { - LOG.info("{} failed to stop in a graceful fashion. Hard stopping it.", processId.getKey()); + LOG.info("{} failed to stop in a graceful fashion. Hard stopping it.", processId.getHumanReadableName()); hardStop(); } else { // enforce stop and clean-up even if process has been quickly stopped @@ -124,7 +124,7 @@ public class ManagedProcessHandler { if (lifecycle.tryToMoveTo(ManagedProcessLifecycle.State.HARD_STOPPING)) { hardStopImpl(); if (process != null && process.isAlive()) { - LOG.info("{} failed to stop in a quick fashion. Killing it.", processId.getKey()); + LOG.info("{} failed to stop in a quick fashion. Killing it.", processId.getHumanReadableName()); } // enforce stop and clean-up even if process has been quickly stopped finalizeStop(); @@ -233,7 +233,7 @@ public class ManagedProcessHandler { @Override public String toString() { - return format("Process[%s]", processId.getKey()); + return format("Process[%s]", processId.getHumanReadableName()); } /** @@ -250,7 +250,7 @@ public class ManagedProcessHandler { // this name is different than Thread#toString(), which includes name, priority // and thread group // -> do not override toString() - super(format("StopWatcher[%s]", processId.getKey())); + super(format("StopWatcher[%s]", processId.getHumanReadableName())); } @Override @@ -267,7 +267,7 @@ public class ManagedProcessHandler { try { hardStop(); } catch (InterruptedException e) { - LOG.debug("Interrupted while stopping [{}] after process ended", processId.getKey(), e); + LOG.debug("Interrupted while stopping [{}] after process ended", processId.getHumanReadableName(), e); Thread.currentThread().interrupt(); } } @@ -278,7 +278,7 @@ public class ManagedProcessHandler { // this name is different than Thread#toString(), which includes name, priority // and thread group // -> do not override toString() - super(format("EventWatcher[%s]", processId.getKey())); + super(format("EventWatcher[%s]", processId.getHumanReadableName())); } @Override diff --git a/server/sonar-main/src/main/java/org/sonar/application/process/ManagedProcessLifecycle.java b/server/sonar-main/src/main/java/org/sonar/application/process/ManagedProcessLifecycle.java index 2bb67237683..f0d25cf2ed0 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/process/ManagedProcessLifecycle.java +++ b/server/sonar-main/src/main/java/org/sonar/application/process/ManagedProcessLifecycle.java @@ -91,7 +91,7 @@ public class ManagedProcessLifecycle { res = true; listeners.forEach(listener -> listener.onProcessState(processId, to)); } - LOG.debug("{} tryToMoveTo {} from {} to {} => {}", Thread.currentThread().getName(), processId.getKey(), currentState, to, res); + LOG.debug("{} tryToMoveTo {} from {} to {} => {}", Thread.currentThread().getName(), processId.getHumanReadableName(), currentState, to, res); return res; } } diff --git a/server/sonar-main/src/test/java/org/sonar/application/ProcessLauncherImplTest.java b/server/sonar-main/src/test/java/org/sonar/application/ProcessLauncherImplTest.java index ab86b4ab35b..14a2a7d888e 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/ProcessLauncherImplTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/ProcessLauncherImplTest.java @@ -277,7 +277,7 @@ public class ProcessLauncherImplTest { JavaCommand<?> javaCommand = new JavaCommand<>(ProcessId.ELASTICSEARCH, temp.newFolder()); assertThatThrownBy(() -> underTest.launch(javaCommand)) .isInstanceOf(IllegalStateException.class) - .hasMessage("Fail to launch process [es]"); + .hasMessage("Fail to launch process [%s]", ProcessId.ELASTICSEARCH.getHumanReadableName()); } private EsScriptCommand createEsScriptCommand(File tempDir, File homeDir, File dataDir, File logDir) throws IOException { diff --git a/server/sonar-main/src/test/java/org/sonar/application/process/ManagedProcessHandlerTest.java b/server/sonar-main/src/test/java/org/sonar/application/process/ManagedProcessHandlerTest.java index 1db0edaba82..bd2dcf78445 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/process/ManagedProcessHandlerTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/process/ManagedProcessHandlerTest.java @@ -279,7 +279,7 @@ public class ManagedProcessHandlerTest { @Test public void test_toString() { ManagedProcessHandler underTest = newHanderBuilder(A_PROCESS_ID).build(); - assertThat(underTest.toString()).isEqualTo("Process[" + A_PROCESS_ID.getKey() + "]"); + assertThat(underTest).hasToString("Process[" + A_PROCESS_ID.getHumanReadableName() + "]"); } private static class TestManagedProcess implements ManagedProcess, AutoCloseable { diff --git a/server/sonar-process/src/main/java/org/sonar/process/ProcessEntryPoint.java b/server/sonar-process/src/main/java/org/sonar/process/ProcessEntryPoint.java index 51afdcf82e1..700f840f7bf 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/ProcessEntryPoint.java +++ b/server/sonar-process/src/main/java/org/sonar/process/ProcessEntryPoint.java @@ -38,7 +38,7 @@ public class ProcessEntryPoint { private static final long HARD_STOP_TIMEOUT_MS = 1_000L; private final Props props; - private final String processKey; + private final ProcessId processId; private final Lifecycle lifecycle = new Lifecycle(); private final ProcessCommands commands; private final SystemExit exit; @@ -51,7 +51,7 @@ public class ProcessEntryPoint { public ProcessEntryPoint(Props props, SystemExit exit, ProcessCommands commands, Runtime runtime) { this.props = props; - this.processKey = props.nonNullValue(PROPERTY_PROCESS_KEY); + this.processId = ProcessId.fromKey(props.nonNullValue(PROPERTY_PROCESS_KEY)); this.exit = exit; this.commands = commands; this.stopWatcher = createStopWatcher(commands, this); @@ -80,13 +80,13 @@ public class ProcessEntryPoint { try { launch(logger); } catch (Exception e) { - logger.warn("Fail to start {}", processKey, e); + logger.warn("Fail to start {}", processId.getHumanReadableName(), e); hardStop(); } } private void launch(Logger logger) throws InterruptedException { - logger.info("Starting {}", processKey); + logger.info("Starting {}", processId.getHumanReadableName()); runtime.addShutdownHook(new Thread(() -> { exit.setInShutdownHook(); stop(); diff --git a/server/sonar-process/src/main/java/org/sonar/process/ProcessId.java b/server/sonar-process/src/main/java/org/sonar/process/ProcessId.java index 7ec0f477ace..380ca6aea9d 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/ProcessId.java +++ b/server/sonar-process/src/main/java/org/sonar/process/ProcessId.java @@ -23,18 +23,21 @@ import static java.lang.String.format; public enum ProcessId { - APP("app", 0, "sonar"), - ELASTICSEARCH("es", 1, "es"), - WEB_SERVER("web", 2, "web"), - COMPUTE_ENGINE("ce", 3, "ce"); + APP("app", 0, "sonar", "SonarQube"), + ELASTICSEARCH("es", 1, "es", "ElasticSearch"), + WEB_SERVER("web", 2, "web", "Web Server"), + COMPUTE_ENGINE("ce", 3, "ce", "Compute Engine"); private final String key; private final int ipcIndex; private final String logFilenamePrefix; - ProcessId(String key, int ipcIndex, String logFilenamePrefix) { + private final String humanReadableName; + + ProcessId(String key, int ipcIndex, String logFilenamePrefix, String humanReadableName) { this.key = key; this.ipcIndex = ipcIndex; this.logFilenamePrefix = logFilenamePrefix; + this.humanReadableName = humanReadableName; } public String getKey() { @@ -55,14 +58,8 @@ public enum ProcessId { return logFilenamePrefix; } - @Override - public String toString() { - StringBuilder sb = new StringBuilder("["); - sb.append("key='").append(key).append('\''); - sb.append(", ipcIndex=").append(ipcIndex); - sb.append(", logFilenamePrefix=").append(logFilenamePrefix); - sb.append(']'); - return sb.toString(); + public String getHumanReadableName() { + return humanReadableName; } public static ProcessId fromKey(String key) { diff --git a/server/sonar-process/src/test/java/org/sonar/process/ProcessEntryPointTest.java b/server/sonar-process/src/test/java/org/sonar/process/ProcessEntryPointTest.java index ce89c599c80..69b501707c0 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/ProcessEntryPointTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/ProcessEntryPointTest.java @@ -179,7 +179,7 @@ public class ProcessEntryPointTest { Props props = new Props(new Properties()); props.set(PROPERTY_SHARED_PATH, temp.newFolder().getAbsolutePath()); props.set(PROPERTY_PROCESS_INDEX, "1"); - props.set(PROPERTY_PROCESS_KEY, "test"); + props.set(PROPERTY_PROCESS_KEY, ProcessId.COMPUTE_ENGINE.getKey()); props.set(PROPERTY_GRACEFUL_STOP_TIMEOUT_MS, "30000"); return props; } diff --git a/server/sonar-process/src/test/java/org/sonar/process/ProcessIdTest.java b/server/sonar-process/src/test/java/org/sonar/process/ProcessIdTest.java index 828c022a6a7..ca171b8bceb 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/ProcessIdTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/ProcessIdTest.java @@ -39,12 +39,15 @@ public class ProcessIdTest { public void all_values_are_unique() { Set<Integer> ipcIndices = new HashSet<>(); Set<String> keys = new HashSet<>(); + Set<String> humanReadableNames = new HashSet<>(); for (ProcessId processId : ProcessId.values()) { ipcIndices.add(processId.getIpcIndex()); keys.add(processId.getKey()); + humanReadableNames.add(processId.getHumanReadableName()); } assertThat(ipcIndices).hasSize(ProcessId.values().length); assertThat(keys).hasSize(ProcessId.values().length); + assertThat(humanReadableNames).hasSize(ProcessId.values().length); } @Test @@ -68,4 +71,5 @@ public class ProcessIdTest { .isInstanceOf(IllegalArgumentException.class) .hasMessage("Process [foo] does not exist"); } + } diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/app/WebServer.java b/server/sonar-webserver/src/main/java/org/sonar/server/app/WebServer.java index 841be2b21b6..be02a3969df 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/app/WebServer.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/app/WebServer.java @@ -30,6 +30,8 @@ import org.sonar.process.ProcessId; import org.sonar.process.Props; import org.sonar.process.sharedmemoryfile.DefaultProcessCommands; +import static org.sonar.process.ProcessId.WEB_SERVER; + public class WebServer implements Monitored { public static final String PROPERTY_SHARED_PATH = "process.sharedDir"; @@ -76,7 +78,7 @@ public class WebServer implements Monitored { public void stop() { // hard stop is as graceful as stop for the WebServer hardStop(); - LoggerFactory.getLogger(WebServer.class).info("WebServer stopped"); + LoggerFactory.getLogger(WebServer.class).info("{} stopped", WEB_SERVER.getHumanReadableName()); } @Override diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/PlatformImpl.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/PlatformImpl.java index f691b8e942b..0d422108d49 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/PlatformImpl.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/PlatformImpl.java @@ -40,6 +40,8 @@ import org.sonar.server.platform.platformlevel.PlatformLevel4; import org.sonar.server.platform.platformlevel.PlatformLevelSafeMode; import org.sonar.server.platform.platformlevel.PlatformLevelStartup; +import static org.sonar.process.ProcessId.WEB_SERVER; + /** * @since 2.2 */ @@ -106,7 +108,7 @@ public class PlatformImpl implements Platform { // switch current container last to avoid giving access to a partially initialized container runIfNotAborted(() -> { currentLevel = level4; - LOGGER.info("WebServer is operational"); + LOGGER.info("{} is operational", WEB_SERVER.getHumanReadableName()); }); // stop safemode container if it existed |