diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-09-19 17:39:54 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-09-26 23:49:38 +0200 |
commit | b5f3b88aac1f507586983dd6b2ebbdd46b97810e (patch) | |
tree | c203d8f0592dc9f44a5653815c84db9cb747e634 /server/sonar-process/src | |
parent | 8fa40905cc8afa06f384ed455e06871126804751 (diff) | |
download | sonarqube-b5f3b88aac1f507586983dd6b2ebbdd46b97810e.tar.gz sonarqube-b5f3b88aac1f507586983dd6b2ebbdd46b97810e.zip |
SONAR-9802 request all nodes from api/system/cluster_info
Diffstat (limited to 'server/sonar-process/src')
7 files changed, 124 insertions, 14 deletions
diff --git a/server/sonar-process/src/main/java/org/sonar/process/systeminfo/Global.java b/server/sonar-process/src/main/java/org/sonar/process/systeminfo/Global.java new file mode 100644 index 00000000000..83e9568b832 --- /dev/null +++ b/server/sonar-process/src/main/java/org/sonar/process/systeminfo/Global.java @@ -0,0 +1,29 @@ +/* + * 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. + */ +package org.sonar.process.systeminfo; + +/** + * Interface to mark {@link SystemInfoSection} of web server as global. + * In case of cluster mode, all the processes and nodes would return + * the same values, so it's loaded once on the web server that receives + * the user request. + */ +public interface Global { +} diff --git a/server/sonar-process/src/main/java/org/sonar/process/systeminfo/JvmPropertiesSection.java b/server/sonar-process/src/main/java/org/sonar/process/systeminfo/JvmPropertiesSection.java index 8b0a31b0065..1cb014bde4f 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/systeminfo/JvmPropertiesSection.java +++ b/server/sonar-process/src/main/java/org/sonar/process/systeminfo/JvmPropertiesSection.java @@ -21,6 +21,7 @@ package org.sonar.process.systeminfo; import java.util.Map; import java.util.Objects; +import java.util.TreeMap; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute; @@ -41,7 +42,8 @@ public class JvmPropertiesSection implements SystemInfoSection { ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); protobuf.setName(name); - for (Map.Entry<Object, Object> systemProp : System.getProperties().entrySet()) { + Map<Object, Object> sortedProperties = new TreeMap<>(System.getProperties()); + for (Map.Entry<Object, Object> systemProp : sortedProperties.entrySet()) { if (systemProp.getValue() != null) { setAttribute(protobuf, Objects.toString(systemProp.getKey()), Objects.toString(systemProp.getValue())); } diff --git a/server/sonar-process/src/main/java/org/sonar/process/systeminfo/JvmStateSection.java b/server/sonar-process/src/main/java/org/sonar/process/systeminfo/JvmStateSection.java index 656fba7e75b..804e2f0de5e 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/systeminfo/JvmStateSection.java +++ b/server/sonar-process/src/main/java/org/sonar/process/systeminfo/JvmStateSection.java @@ -61,8 +61,6 @@ public class JvmStateSection implements SystemInfoSection { ThreadMXBean thread = ManagementFactory.getThreadMXBean(); setAttribute(protobuf, "Threads", thread.getThreadCount()); - setAttribute(protobuf,"Processors", Runtime.getRuntime().availableProcessors()); - return protobuf.build(); } diff --git a/server/sonar-process/src/main/java/org/sonar/process/systeminfo/SystemInfoUtils.java b/server/sonar-process/src/main/java/org/sonar/process/systeminfo/SystemInfoUtils.java index 63dbdec055d..0126bbe2ae7 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/systeminfo/SystemInfoUtils.java +++ b/server/sonar-process/src/main/java/org/sonar/process/systeminfo/SystemInfoUtils.java @@ -19,10 +19,17 @@ */ package org.sonar.process.systeminfo; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; +import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo.Section; + +import static java.util.Arrays.stream; public class SystemInfoUtils { @@ -30,7 +37,7 @@ public class SystemInfoUtils { // prevent instantiation } - public static void setAttribute(ProtobufSystemInfo.Section.Builder section, String key, @Nullable String value) { + public static void setAttribute(Section.Builder section, String key, @Nullable String value) { if (value != null) { section.addAttributesBuilder() .setKey(key) @@ -39,7 +46,7 @@ public class SystemInfoUtils { } } - public static void setAttribute(ProtobufSystemInfo.Section.Builder section, String key, @Nullable Collection<String> values) { + public static void setAttribute(Section.Builder section, String key, @Nullable Collection<String> values) { if (values != null) { section.addAttributesBuilder() .setKey(key) @@ -48,14 +55,14 @@ public class SystemInfoUtils { } } - public static void setAttribute(ProtobufSystemInfo.Section.Builder section, String key, boolean value) { + public static void setAttribute(Section.Builder section, String key, boolean value) { section.addAttributesBuilder() .setKey(key) .setBooleanValue(value) .build(); } - public static void setAttribute(ProtobufSystemInfo.Section.Builder section, String key, long value) { + public static void setAttribute(Section.Builder section, String key, long value) { section.addAttributesBuilder() .setKey(key) .setLongValue(value) @@ -63,7 +70,7 @@ public class SystemInfoUtils { } @CheckForNull - public static ProtobufSystemInfo.Attribute attribute(ProtobufSystemInfo.Section section, String key) { + public static ProtobufSystemInfo.Attribute attribute(Section section, String key) { for (ProtobufSystemInfo.Attribute attribute : section.getAttributesList()) { if (attribute.getKey().equals(key)) { return attribute; @@ -71,4 +78,19 @@ public class SystemInfoUtils { } return null; } + + public static List<Section> order(Collection<Section> sections, String... orderedNames) { + Map<String, Section> alphabeticalOrderedMap = new TreeMap<>(); + sections.forEach(section -> alphabeticalOrderedMap.put(section.getName(), section)); + + List<Section> result = new ArrayList<>(sections.size()); + stream(orderedNames).forEach(name -> { + Section section = alphabeticalOrderedMap.remove(name); + if (section != null) { + result.add(section); + } + }); + result.addAll(alphabeticalOrderedMap.values()); + return result; + } } diff --git a/server/sonar-process/src/test/java/org/sonar/process/systeminfo/JvmPropertiesSectionTest.java b/server/sonar-process/src/test/java/org/sonar/process/systeminfo/JvmPropertiesSectionTest.java index dcf4cb9893c..2050e0a3f73 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/systeminfo/JvmPropertiesSectionTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/systeminfo/JvmPropertiesSectionTest.java @@ -19,12 +19,14 @@ */ package org.sonar.process.systeminfo; -import org.assertj.core.api.Assertions; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; import org.junit.Test; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.process.systeminfo.SystemInfoUtils.attribute; public class JvmPropertiesSectionTest { @@ -36,10 +38,17 @@ public class JvmPropertiesSectionTest { } @Test - public void test_toProtobuf() { + public void system_properties_are_returned_in_alphabetical_order() { ProtobufSystemInfo.Section section = underTest.toProtobuf(); - Assertions.assertThat(attribute(section, "java.vm.vendor").getStringValue()).isNotEmpty(); - Assertions.assertThat(attribute(section, "os.name").getStringValue()).isNotEmpty(); + List<String> keys = section.getAttributesList() + .stream() + .map(ProtobufSystemInfo.Attribute::getKey) + .collect(Collectors.toList()); + assertThat(keys).contains("java.vm.vendor", "os.name"); + + List<String> sortedKeys = new ArrayList<>(keys); + Collections.sort(sortedKeys); + assertThat(sortedKeys).isEqualTo(keys); } } diff --git a/server/sonar-process/src/test/java/org/sonar/process/systeminfo/JvmStateSectionTest.java b/server/sonar-process/src/test/java/org/sonar/process/systeminfo/JvmStateSectionTest.java index 3057e9933e9..77da5df5719 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/systeminfo/JvmStateSectionTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/systeminfo/JvmStateSectionTest.java @@ -39,7 +39,7 @@ public class JvmStateSectionTest { assertThat(section.getName()).isEqualTo(PROCESS_NAME); assertThat(section.getAttributesCount()).isGreaterThan(0); - assertThat(section.getAttributesList()).extracting("key").contains("Threads", "Processors"); + assertThat(section.getAttributesList()).extracting("key").contains("Threads", "Heap Max (MB)"); } @Test diff --git a/server/sonar-process/src/test/java/org/sonar/process/systeminfo/SystemInfoUtilsTest.java b/server/sonar-process/src/test/java/org/sonar/process/systeminfo/SystemInfoUtilsTest.java new file mode 100644 index 00000000000..03410f34ea9 --- /dev/null +++ b/server/sonar-process/src/test/java/org/sonar/process/systeminfo/SystemInfoUtilsTest.java @@ -0,0 +1,50 @@ +/* + * 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. + */ +package org.sonar.process.systeminfo; + +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; +import org.junit.Test; +import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo.Section; + +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; + +public class SystemInfoUtilsTest { + + @Test + public void test_order() { + Collection<Section> sections = asList( + newSection("end2"), + newSection("bar"), + newSection("end1"), + newSection("foo")); + + List<String> ordered = SystemInfoUtils.order(sections, "foo", "bar").stream() + .map(Section::getName) + .collect(Collectors.toList()); + assertThat(ordered).isEqualTo(asList("foo", "bar", "end1", "end2")); + } + + private static Section newSection(String name) { + return Section.newBuilder().setName(name).build(); + } +} |