From: Sébastien Lesaint Date: Fri, 19 Aug 2016 16:57:53 +0000 (+0200) Subject: SONAR-7825 rename ProcessSystemInfoClient CeHttpClient X-Git-Tag: 6.1-RC1~327 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2b5236702c9c655d20633cfb737eaa86b955fbee;p=sonarqube.git SONAR-7825 rename ProcessSystemInfoClient CeHttpClient --- diff --git a/server/sonar-server/src/main/java/org/sonar/ce/CeModule.java b/server/sonar-server/src/main/java/org/sonar/ce/CeModule.java index 09b13d7434c..ad632590e50 100644 --- a/server/sonar-server/src/main/java/org/sonar/ce/CeModule.java +++ b/server/sonar-server/src/main/java/org/sonar/ce/CeModule.java @@ -19,6 +19,7 @@ */ package org.sonar.ce; +import org.sonar.ce.http.CeHttpClient; import org.sonar.ce.log.CeLogging; import org.sonar.ce.queue.CeQueueImpl; import org.sonar.ce.taskprocessor.ReportTaskProcessorDeclaration; @@ -29,11 +30,12 @@ public class CeModule extends Module { @Override protected void configureModule() { add(CeLogging.class, + CeHttpClient.class, // Queue CeQueueImpl.class, ReportSubmitter.class, - + // Core tasks processors ReportTaskProcessorDeclaration.class); } diff --git a/server/sonar-server/src/main/java/org/sonar/ce/http/CeHttpClient.java b/server/sonar-server/src/main/java/org/sonar/ce/http/CeHttpClient.java new file mode 100644 index 00000000000..026dd8ef511 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/ce/http/CeHttpClient.java @@ -0,0 +1,61 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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. + */ +package org.sonar.ce.http; + +import java.io.File; +import java.net.URI; +import java.util.Optional; +import org.apache.commons.io.IOUtils; +import org.sonar.api.config.Settings; +import org.sonar.process.DefaultProcessCommands; +import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; + +import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH; +import static org.sonar.process.ProcessId.COMPUTE_ENGINE; + +/** + * Client for the HTTP server of the Compute Engine. + */ +public class CeHttpClient { + + private final File ipcSharedDir; + + public CeHttpClient(Settings props) { + this.ipcSharedDir = new File(props.getString(PROPERTY_SHARED_PATH)); + } + + /** + * Connects to the specified JVM process and requests system information. + * @return the system info, or absent if the process is not up or if its HTTP URL + * is not registered into IPC. + */ + public Optional retrieveSystemInfo() { + try (DefaultProcessCommands commands = DefaultProcessCommands.secondary(ipcSharedDir, COMPUTE_ENGINE.getIpcIndex())) { + if (commands.isUp()) { + String url = commands.getHttpUrl() + "/systemInfo"; + byte[] protobuf = IOUtils.toByteArray(new URI(url)); + return Optional.of(ProtobufSystemInfo.SystemInfo.parseFrom(protobuf)); + } + return Optional.empty(); + } catch (Exception e) { + throw new IllegalStateException("Can not get system info of process " + COMPUTE_ENGINE, e); + } + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/ce/http/package-info.java b/server/sonar-server/src/main/java/org/sonar/ce/http/package-info.java new file mode 100644 index 00000000000..0392bfeccc9 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/ce/http/package-info.java @@ -0,0 +1,23 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.ce.http; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/ProcessSystemInfoClient.java b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/ProcessSystemInfoClient.java deleted file mode 100644 index ce919b9b40c..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/ProcessSystemInfoClient.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact 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. - */ -package org.sonar.server.platform.monitoring; - -import com.google.common.base.Optional; -import java.io.File; -import java.net.URI; -import org.apache.commons.io.IOUtils; -import org.sonar.api.config.Settings; -import org.sonar.process.DefaultProcessCommands; -import org.sonar.process.ProcessId; -import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; - -import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH; - -/** - * Connects to the System Info HTTP server of another JVM process. - */ -public class ProcessSystemInfoClient { - - private final File ipcSharedDir; - - public ProcessSystemInfoClient(Settings props) { - this.ipcSharedDir = new File(props.getString(PROPERTY_SHARED_PATH)); - } - - /** - * Connects to the specified JVM process and requests system information. - * @return the system info, or absent if the process is not up or if its HTTP URL - * is not registered into IPC. - */ - public Optional connect(ProcessId processId) { - try (DefaultProcessCommands commands = DefaultProcessCommands.secondary(ipcSharedDir, processId.getIpcIndex())) { - if (commands.isUp()) { - String url = commands.getHttpUrl() + "/systemInfo"; - byte[] protobuf = IOUtils.toByteArray(new URI(url)); - return Optional.of(ProtobufSystemInfo.SystemInfo.parseFrom(protobuf)); - } - return Optional.absent(); - } catch (Exception e) { - throw new IllegalStateException("Can not get system info of process " + processId, e); - } - } -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index e92fc98886d..829b1bb9fab 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -145,7 +145,6 @@ import org.sonar.server.platform.monitoring.DatabaseMonitor; import org.sonar.server.platform.monitoring.EsMonitor; import org.sonar.server.platform.monitoring.JvmPropsMonitor; import org.sonar.server.platform.monitoring.PluginsMonitor; -import org.sonar.server.platform.monitoring.ProcessSystemInfoClient; import org.sonar.server.platform.monitoring.SettingsMonitor; import org.sonar.server.platform.monitoring.SonarQubeMonitor; import org.sonar.server.platform.monitoring.SystemMonitor; @@ -591,7 +590,6 @@ public class PlatformLevel4 extends PlatformLevel { ProjectLinksModule.class, // System - ProcessSystemInfoClient.class, ServerLogging.class, RestartAction.class, InfoAction.class, diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/InfoAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/InfoAction.java index 51902513182..8006c680429 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/InfoAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/InfoAction.java @@ -19,17 +19,16 @@ */ package org.sonar.server.platform.ws; -import com.google.common.base.Optional; import java.util.Map; +import java.util.Optional; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.text.JsonWriter; +import org.sonar.ce.http.CeHttpClient; import org.sonar.core.permission.GlobalPermissions; -import org.sonar.process.ProcessId; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; import org.sonar.server.platform.monitoring.Monitor; -import org.sonar.server.platform.monitoring.ProcessSystemInfoClient; import org.sonar.server.user.UserSession; /** @@ -38,12 +37,12 @@ import org.sonar.server.user.UserSession; public class InfoAction implements SystemWsAction { private final UserSession userSession; - private final ProcessSystemInfoClient processSystemInfoClient; + private final CeHttpClient ceHttpClient; private final Monitor[] monitors; - public InfoAction(UserSession userSession, ProcessSystemInfoClient processSystemInfoClient, Monitor... monitors) { + public InfoAction(UserSession userSession, CeHttpClient ceHttpClient, Monitor... monitors) { this.userSession = userSession; - this.processSystemInfoClient = processSystemInfoClient; + this.ceHttpClient = ceHttpClient; this.monitors = monitors; } @@ -78,7 +77,7 @@ public class InfoAction implements SystemWsAction { } json.endObject(); } - Optional ceSysInfo = processSystemInfoClient.connect(ProcessId.COMPUTE_ENGINE); + Optional ceSysInfo = ceHttpClient.retrieveSystemInfo(); if (ceSysInfo.isPresent()) { for (ProtobufSystemInfo.Section section : ceSysInfo.get().getSectionsList()) { json.name(section.getName()); diff --git a/server/sonar-server/src/test/java/org/sonar/ce/http/CeHttpClientTest.java b/server/sonar-server/src/test/java/org/sonar/ce/http/CeHttpClientTest.java new file mode 100644 index 00000000000..8a2759128ce --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/ce/http/CeHttpClientTest.java @@ -0,0 +1,97 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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. + */ +package org.sonar.ce.http; + +import java.io.File; +import java.util.Optional; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okio.Buffer; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.config.Settings; +import org.sonar.process.DefaultProcessCommands; +import org.sonar.process.ProcessEntryPoint; +import org.sonar.process.ProcessId; +import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; + +import static java.lang.String.format; +import static org.assertj.core.api.Assertions.assertThat; + +public class CeHttpClientTest { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Rule + public MockWebServer server = new MockWebServer(); + + private File ipcSharedDir; + private CeHttpClient underTest; + + @Before + public void setUp() throws Exception { + ipcSharedDir = temp.newFolder(); + Settings settings = new Settings(); + settings.setProperty(ProcessEntryPoint.PROPERTY_SHARED_PATH, ipcSharedDir.getAbsolutePath()); + underTest = new CeHttpClient(settings); + } + + @Test + public void retrieveSystemInfo_returns_absent_if_process_is_down() throws Exception { + Optional info = underTest.retrieveSystemInfo(); + + assertThat(info.isPresent()).isFalse(); + } + + @Test + public void retrieveSystemInfo_get_information_if_process_is_up() throws Exception { + Buffer response = new Buffer(); + response.read(ProtobufSystemInfo.Section.newBuilder().build().toByteArray()); + server.enqueue(new MockResponse().setBody(response)); + + // initialize registration of process + try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(ipcSharedDir, ProcessId.COMPUTE_ENGINE.getIpcIndex())) { + processCommands.setUp(); + processCommands.setHttpUrl(format("http://%s:%d", server.getHostName(), server.getPort())); + } + + Optional info = underTest.retrieveSystemInfo(); + assertThat(info.get().getSectionsCount()).isEqualTo(0); + } + + @Test + public void retrieveSystemInfo_throws_ISE_if_http_error() throws Exception { + server.enqueue(new MockResponse().setResponseCode(500)); + + // initialize registration of process + try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(ipcSharedDir, ProcessId.COMPUTE_ENGINE.getIpcIndex())) { + processCommands.setUp(); + processCommands.setHttpUrl(format("http://%s:%d", server.getHostName(), server.getPort())); + } + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Can not get system info of process " + ProcessId.COMPUTE_ENGINE); + underTest.retrieveSystemInfo(); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/ProcessSystemInfoClientTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/ProcessSystemInfoClientTest.java deleted file mode 100644 index 0facb75502f..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/ProcessSystemInfoClientTest.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact 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. - */ -package org.sonar.server.platform.monitoring; - -import com.google.common.base.Optional; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.MockWebServer; -import java.io.File; -import okio.Buffer; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; -import org.sonar.api.config.Settings; -import org.sonar.process.DefaultProcessCommands; -import org.sonar.process.ProcessEntryPoint; -import org.sonar.process.ProcessId; -import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; - -import static java.lang.String.format; -import static org.assertj.core.api.Assertions.assertThat; - -public class ProcessSystemInfoClientTest { - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Rule - public MockWebServer server = new MockWebServer(); - - File ipcSharedDir; - ProcessSystemInfoClient underTest; - - @Before - public void setUp() throws Exception { - ipcSharedDir = temp.newFolder(); - Settings settings = new Settings(); - settings.setProperty(ProcessEntryPoint.PROPERTY_SHARED_PATH, ipcSharedDir.getAbsolutePath()); - underTest = new ProcessSystemInfoClient(settings); - } - - @Test - public void connect_returns_absent_if_process_is_down() throws Exception { - Optional info = underTest.connect(ProcessId.COMPUTE_ENGINE); - - assertThat(info.isPresent()).isFalse(); - } - - @Test - public void get_information_if_process_is_up() throws Exception { - Buffer response = new Buffer(); - response.read(ProtobufSystemInfo.Section.newBuilder().build().toByteArray()); - server.enqueue(new MockResponse().setBody(response)); - - // initialize registration of process - try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(ipcSharedDir, ProcessId.COMPUTE_ENGINE.getIpcIndex())) { - processCommands.setUp(); - processCommands.setHttpUrl(format("http://%s:%d", server.getHostName(), server.getPort())); - } - - Optional info = underTest.connect(ProcessId.COMPUTE_ENGINE); - assertThat(info.get().getSectionsCount()).isEqualTo(0); - } - - @Test - public void throws_ISE_if_http_error() throws Exception { - server.enqueue(new MockResponse().setResponseCode(500)); - - // initialize registration of process - try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(ipcSharedDir, ProcessId.COMPUTE_ENGINE.getIpcIndex())) { - processCommands.setUp(); - processCommands.setHttpUrl(format("http://%s:%d", server.getHostName(), server.getPort())); - } - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Can not get system info of process " + ProcessId.COMPUTE_ENGINE); - underTest.connect(ProcessId.COMPUTE_ENGINE); - } -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java index 6744c4b7568..af70d848614 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java @@ -21,13 +21,14 @@ package org.sonar.server.platform.ws; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Optional; import org.junit.Rule; import org.junit.Test; import org.mockito.Mockito; import org.sonar.core.permission.GlobalPermissions; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.platform.monitoring.Monitor; -import org.sonar.server.platform.monitoring.ProcessSystemInfoClient; +import org.sonar.ce.http.CeHttpClient; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestResponse; import org.sonar.server.ws.WsActionTester; @@ -43,9 +44,9 @@ public class InfoActionTest { Monitor monitor1 = mock(Monitor.class); Monitor monitor2 = mock(Monitor.class); - ProcessSystemInfoClient processSystemInfoClient = mock(ProcessSystemInfoClient.class, Mockito.RETURNS_MOCKS); + CeHttpClient ceHttpClient = mock(CeHttpClient.class, Mockito.RETURNS_MOCKS); - WsActionTester ws = new WsActionTester(new InfoAction(userSessionRule, processSystemInfoClient, monitor1, monitor2)); + WsActionTester ws = new WsActionTester(new InfoAction(userSessionRule, ceHttpClient, monitor1, monitor2)); @Test public void test_definition() throws Exception { @@ -75,6 +76,7 @@ public class InfoActionTest { when(monitor1.attributes()).thenReturn(attributes1); when(monitor2.name()).thenReturn("Monitor Two"); when(monitor2.attributes()).thenReturn(attributes2); + when(ceHttpClient.retrieveSystemInfo()).thenReturn(Optional.empty()); TestResponse response = ws.newRequest().execute(); // response does not contain empty "Monitor Three" diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemWsTest.java index 112df0694a3..6b754958e72 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemWsTest.java @@ -25,7 +25,7 @@ import org.sonar.api.server.ws.WebService; import org.sonar.server.app.ProcessCommandWrapper; import org.sonar.server.app.RestartFlagHolder; import org.sonar.server.platform.Platform; -import org.sonar.server.platform.monitoring.ProcessSystemInfoClient; +import org.sonar.ce.http.CeHttpClient; import org.sonar.server.tester.AnonymousMockUserSession; import org.sonar.server.user.UserSession; @@ -34,12 +34,12 @@ import static org.mockito.Mockito.mock; public class SystemWsTest { - ProcessSystemInfoClient processSystemInfoClient = mock(ProcessSystemInfoClient.class); + CeHttpClient ceHttpClient = mock(CeHttpClient.class); @Test public void define() { RestartAction action1 = new RestartAction(mock(UserSession.class), mock(Settings.class), mock(Platform.class), mock(ProcessCommandWrapper.class), mock(RestartFlagHolder.class)); - InfoAction action2 = new InfoAction(new AnonymousMockUserSession(), processSystemInfoClient); + InfoAction action2 = new InfoAction(new AnonymousMockUserSession(), ceHttpClient); SystemWs ws = new SystemWs(action1, action2); WebService.Context context = new WebService.Context();