SONAR-9590 move Es/JavaCommand to module sonar-process-monitor

This commit is contained in:
Sébastien Lesaint 2017-08-18 10:23:26 +02:00 committed by Simon Brandhof
parent dd056cf31f
commit 13e83f1cbb
21 changed files with 111 additions and 54 deletions

View File

@ -30,9 +30,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.application.config.AppSettings;
import org.sonar.application.config.ClusterSettings;
import org.sonar.application.process.CommandFactory;
import org.sonar.application.process.EsCommand;
import org.sonar.application.process.JavaCommand;
import org.sonar.process.command.CommandFactory;
import org.sonar.process.command.EsCommand;
import org.sonar.process.command.JavaCommand;
import org.sonar.application.process.ProcessLauncher;
import org.sonar.application.process.Lifecycle;
import org.sonar.application.process.ProcessEventListener;
@ -107,7 +107,7 @@ public class SchedulerImpl implements Scheduler, ProcessEventListener, ProcessLi
private void tryToStartEs() {
SQProcess process = processesById.get(ProcessId.ELASTICSEARCH);
if (process != null) {
tryToStartEsProcess(process, () -> commandFactory.createEsCommand(settings));
tryToStartEsProcess(process, commandFactory::createEsCommand);
}
}

View File

@ -41,6 +41,7 @@ import org.elasticsearch.transport.Netty4Plugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.process.ProcessId;
import org.sonar.process.command.EsCommand;
import static java.util.Collections.singletonList;
import static java.util.Collections.unmodifiableList;

View File

@ -19,8 +19,8 @@
*/
package org.sonar.application.process;
import org.sonar.process.sharedmemoryfile.ProcessCommands;
import org.sonar.process.ProcessId;
import org.sonar.process.sharedmemoryfile.ProcessCommands;
import static java.util.Objects.requireNonNull;

View File

@ -20,6 +20,8 @@
package org.sonar.application.process;
import java.io.Closeable;
import org.sonar.process.command.EsCommand;
import org.sonar.process.command.JavaCommand;
public interface ProcessLauncher extends Closeable {

View File

@ -35,6 +35,9 @@ import java.util.stream.Collectors;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.process.command.AbstractCommand;
import org.sonar.process.command.EsCommand;
import org.sonar.process.command.JavaCommand;
import org.sonar.process.sharedmemoryfile.AllProcessesCommands;
import org.sonar.process.sharedmemoryfile.ProcessCommands;
import org.sonar.process.ProcessId;

View File

@ -34,16 +34,15 @@ import org.junit.rules.ExpectedException;
import org.junit.rules.TestRule;
import org.junit.rules.Timeout;
import org.mockito.Mockito;
import org.sonar.application.config.AppSettings;
import org.sonar.application.config.TestAppSettings;
import org.sonar.application.process.AbstractCommand;
import org.sonar.application.process.CommandFactory;
import org.sonar.application.process.EsCommand;
import org.sonar.application.process.JavaCommand;
import org.sonar.application.process.ProcessLauncher;
import org.sonar.application.process.ProcessMonitor;
import org.sonar.process.ProcessId;
import org.sonar.process.ProcessProperties;
import org.sonar.process.command.AbstractCommand;
import org.sonar.process.command.CommandFactory;
import org.sonar.process.command.EsCommand;
import org.sonar.process.command.JavaCommand;
import static java.util.Collections.synchronizedList;
import static org.assertj.core.api.Assertions.assertThat;
@ -310,7 +309,7 @@ public class SchedulerImplTest {
private static class TestCommandFactory implements CommandFactory {
@Override
public EsCommand createEsCommand(AppSettings settings) {
public EsCommand createEsCommand() {
return ES_COMMAND;
}

View File

@ -30,6 +30,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.process.command.JavaCommand;
import org.sonar.process.sharedmemoryfile.AllProcessesCommands;
import org.sonar.process.ProcessId;

View File

@ -17,7 +17,7 @@
* 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.application.process;
package org.sonar.process.command;
import java.io.File;
import java.util.HashMap;

View File

@ -17,13 +17,11 @@
* 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.application.process;
import org.sonar.application.config.AppSettings;
package org.sonar.process.command;
public interface CommandFactory {
EsCommand createEsCommand(AppSettings settings);
EsCommand createEsCommand();
JavaCommand createWebCommand(boolean leader);

View File

@ -17,14 +17,16 @@
* 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.application.process;
package org.sonar.process.command;
import java.io.File;
import java.util.Map;
import java.util.Optional;
import org.sonar.application.config.AppSettings;
import org.sonar.process.ProcessId;
import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
import org.sonar.process.es.EsLogging;
import org.sonar.process.es.EsSettings;
import static org.sonar.process.ProcessProperties.HTTPS_PROXY_HOST;
import static org.sonar.process.ProcessProperties.HTTPS_PROXY_PORT;
@ -45,21 +47,21 @@ public class CommandFactoryImpl implements CommandFactory {
"socksProxyHost",
"socksProxyPort"};
private final AppSettings settings;
private final Props props;
public CommandFactoryImpl(AppSettings settings) {
this.settings = settings;
public CommandFactoryImpl(Props props) {
this.props = props;
}
@Override
public EsCommand createEsCommand(AppSettings settings) {
File homeDir = this.settings.getProps().nonNullValueAsFile(ProcessProperties.PATH_HOME);
public EsCommand createEsCommand() {
File homeDir = props.nonNullValueAsFile(ProcessProperties.PATH_HOME);
File executable = new File(homeDir, getExecutable());
if (!executable.exists()) {
throw new IllegalStateException("Cannot find elasticsearch binary");
}
Map<String, String> settingsMap = new EsSettings(this.settings.getProps()).build();
Map<String, String> settingsMap = new EsSettings(props).build();
File logDir = new File(settingsMap.get("path.logs"));
File confDir = new File(settingsMap.get("path.conf"));
@ -67,13 +69,13 @@ public class CommandFactoryImpl implements CommandFactory {
.setWorkDir(executable.getParentFile().getParentFile())
.setExecutable(executable)
.setConfDir(confDir)
.setLog4j2Properties(new EsLogging().createProperties(settings.getProps(), logDir))
.setArguments(this.settings.getProps().rawProperties())
.setLog4j2Properties(new EsLogging().createProperties(props, logDir))
.setArguments(props.rawProperties())
.setClusterName(settingsMap.get("cluster.name"))
.setHost(settingsMap.get("network.host"))
.setPort(Integer.valueOf(settingsMap.get("transport.tcp.port")))
.addJvmOption(settings.getProps().nonNullValue(ProcessProperties.SEARCH_JAVA_OPTS))
.addJvmOption(settings.getProps().nonNullValue(ProcessProperties.SEARCH_JAVA_ADDITIONAL_OPTS))
.addJvmOption(props.nonNullValue(ProcessProperties.SEARCH_JAVA_OPTS))
.addJvmOption(props.nonNullValue(ProcessProperties.SEARCH_JAVA_ADDITIONAL_OPTS))
.setEnvVariable("JAVA_HOME", System.getProperties().getProperty("java.home"));
settingsMap.forEach((key, value) -> res.addEsOption("-E" + key + "=" + value));
@ -90,18 +92,18 @@ public class CommandFactoryImpl implements CommandFactory {
@Override
public JavaCommand createWebCommand(boolean leader) {
File homeDir = settings.getProps().nonNullValueAsFile(ProcessProperties.PATH_HOME);
File homeDir = props.nonNullValueAsFile(ProcessProperties.PATH_HOME);
JavaCommand command = newJavaCommand(ProcessId.WEB_SERVER, homeDir)
.addJavaOptions(ProcessProperties.WEB_ENFORCED_JVM_ARGS)
.addJavaOptions(settings.getProps().nonNullValue(ProcessProperties.WEB_JAVA_OPTS))
.addJavaOptions(settings.getProps().nonNullValue(ProcessProperties.WEB_JAVA_ADDITIONAL_OPTS))
.addJavaOptions(props.nonNullValue(ProcessProperties.WEB_JAVA_OPTS))
.addJavaOptions(props.nonNullValue(ProcessProperties.WEB_JAVA_ADDITIONAL_OPTS))
// required for logback tomcat valve
.setEnvVariable(ProcessProperties.PATH_LOGS, settings.getProps().nonNullValue(ProcessProperties.PATH_LOGS))
.setEnvVariable(ProcessProperties.PATH_LOGS, props.nonNullValue(ProcessProperties.PATH_LOGS))
.setArgument("sonar.cluster.web.startupLeader", Boolean.toString(leader))
.setClassName("org.sonar.server.app.WebServer")
.addClasspath("./lib/common/*")
.addClasspath("./lib/server/*");
String driverPath = settings.getProps().value(ProcessProperties.JDBC_DRIVER_PATH);
String driverPath = props.value(ProcessProperties.JDBC_DRIVER_PATH);
if (driverPath != null) {
command.addClasspath(driverPath);
}
@ -110,16 +112,16 @@ public class CommandFactoryImpl implements CommandFactory {
@Override
public JavaCommand createCeCommand() {
File homeDir = settings.getProps().nonNullValueAsFile(ProcessProperties.PATH_HOME);
File homeDir = props.nonNullValueAsFile(ProcessProperties.PATH_HOME);
JavaCommand command = newJavaCommand(ProcessId.COMPUTE_ENGINE, homeDir)
.addJavaOptions(ProcessProperties.CE_ENFORCED_JVM_ARGS)
.addJavaOptions(settings.getProps().nonNullValue(ProcessProperties.CE_JAVA_OPTS))
.addJavaOptions(settings.getProps().nonNullValue(ProcessProperties.CE_JAVA_ADDITIONAL_OPTS))
.addJavaOptions(props.nonNullValue(ProcessProperties.CE_JAVA_OPTS))
.addJavaOptions(props.nonNullValue(ProcessProperties.CE_JAVA_ADDITIONAL_OPTS))
.setClassName("org.sonar.ce.app.CeServer")
.addClasspath("./lib/common/*")
.addClasspath("./lib/server/*")
.addClasspath("./lib/ce/*");
String driverPath = settings.getProps().value(ProcessProperties.JDBC_DRIVER_PATH);
String driverPath = props.value(ProcessProperties.JDBC_DRIVER_PATH);
if (driverPath != null) {
command.addClasspath(driverPath);
}
@ -129,10 +131,10 @@ public class CommandFactoryImpl implements CommandFactory {
private JavaCommand newJavaCommand(ProcessId id, File homeDir) {
JavaCommand command = new JavaCommand(id)
.setWorkDir(homeDir)
.setArguments(settings.getProps().rawProperties());
.setArguments(props.rawProperties());
for (String key : PROXY_PROPERTY_KEYS) {
settings.getValue(key).ifPresent(val -> command.addJavaOption("-D" + key + "=" + val));
getPropsValue(key).ifPresent(val -> command.addJavaOption("-D" + key + "=" + val));
}
// defaults of HTTPS are the same than HTTP defaults
@ -143,10 +145,14 @@ public class CommandFactoryImpl implements CommandFactory {
private void setSystemPropertyToDefaultIfNotSet(JavaCommand command,
String httpsProperty, String httpProperty) {
Optional<String> httpValue = settings.getValue(httpProperty);
Optional<String> httpsValue = settings.getValue(httpsProperty);
Optional<String> httpValue = getPropsValue(httpProperty);
Optional<String> httpsValue = getPropsValue(httpsProperty);
if (!httpsValue.isPresent() && httpValue.isPresent()) {
command.addJavaOption("-D" + httpsProperty + "=" + httpValue.get());
}
}
private Optional<String> getPropsValue(String key) {
return Optional.ofNullable(props.value(key));
}
}

View File

@ -17,7 +17,7 @@
* 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.application.process;
package org.sonar.process.command;
import java.io.File;
import java.util.ArrayList;

View File

@ -17,7 +17,7 @@
* 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.application.process;
package org.sonar.process.command;
import java.util.ArrayList;
import java.util.List;

View File

@ -0,0 +1,23 @@
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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.
*/
@ParametersAreNonnullByDefault
package org.sonar.process.command;
import javax.annotation.ParametersAreNonnullByDefault;

View File

@ -17,7 +17,7 @@
* 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.application.process;
package org.sonar.process.es;
import ch.qos.logback.classic.Level;
import java.io.File;

View File

@ -17,7 +17,7 @@
* 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.application.process;
package org.sonar.process.es;
import java.io.File;
import java.net.InetAddress;
@ -49,7 +49,7 @@ public class EsSettings {
private final String clusterName;
private final String nodeName;
EsSettings(Props props) {
public EsSettings(Props props) {
this.props = props;
this.clusterName = props.nonNullValue(ProcessProperties.CLUSTER_NAME);
@ -61,7 +61,7 @@ public class EsSettings {
}
}
Map<String, String> build() {
public Map<String, String> build() {
Map<String, String> builder = new HashMap<>();
configureFileSystem(builder);
configureNetwork(builder);

View File

@ -0,0 +1,23 @@
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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.
*/
@ParametersAreNonnullByDefault
package org.sonar.process.es;
import javax.annotation.ParametersAreNonnullByDefault;

View File

@ -17,7 +17,7 @@
* 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.application.process;
package org.sonar.process.command;
import java.io.File;
import java.util.Properties;

View File

@ -17,7 +17,7 @@
* 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.application.process;
package org.sonar.process.command;
import java.io.File;
import java.util.Properties;

View File

@ -17,7 +17,7 @@
* 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.application.process;
package org.sonar.process.es;
import java.io.File;
import java.io.IOException;
@ -28,6 +28,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.process.Props;
import org.sonar.process.es.EsLogging;
import static org.assertj.core.api.Assertions.assertThat;

View File

@ -17,7 +17,7 @@
* 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.application.process;
package org.sonar.process.es;
import java.io.File;
import java.io.IOException;

View File

@ -23,8 +23,8 @@ import java.io.IOException;
import org.sonar.application.config.AppSettings;
import org.sonar.application.config.AppSettingsLoader;
import org.sonar.application.config.AppSettingsLoaderImpl;
import org.sonar.application.process.CommandFactory;
import org.sonar.application.process.CommandFactoryImpl;
import org.sonar.process.command.CommandFactory;
import org.sonar.process.command.CommandFactoryImpl;
import org.sonar.application.process.ProcessLauncher;
import org.sonar.application.process.ProcessLauncherImpl;
import org.sonar.application.process.StopRequestWatcher;
@ -49,7 +49,7 @@ public class App {
try (AppState appState = new AppStateFactory(settings).create()) {
appState.registerSonarQubeVersion(getSonarqubeVersion());
AppReloader appReloader = new AppReloaderImpl(settingsLoader, fileSystem, appState, logging);
CommandFactory commandFactory = new CommandFactoryImpl(settings);
CommandFactory commandFactory = new CommandFactoryImpl(settings.getProps());
fileSystem.reset();
try (ProcessLauncher processLauncher = new ProcessLauncherImpl(fileSystem.getTempDir())) {