]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18924 Remove node JVM setting from Global Setting of System Info if SQ is in...
authorZipeng WU <zipeng.wu@sonarsource.com>
Tue, 28 Mar 2023 12:46:14 +0000 (14:46 +0200)
committersonartech <sonartech@sonarsource.com>
Tue, 28 Mar 2023 20:04:04 +0000 (20:04 +0000)
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/SettingsSection.java
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/SettingsSectionTest.java

index 5fd07459159e817f1a0a6a1d031a20178b3f3f9e..5ba82ce31545e9c8556cf14cff5c349b1b1c31c5 100644 (file)
  */
 package org.sonar.server.platform.monitoring;
 
+import java.util.Collection;
 import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.TreeMap;
+import java.util.stream.Stream;
 import org.sonar.api.PropertyType;
 import org.sonar.api.config.PropertyDefinition;
 import org.sonar.api.config.PropertyDefinitions;
@@ -30,28 +32,48 @@ import org.sonar.api.server.ServerSide;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.newcodeperiod.NewCodePeriodDto;
+import org.sonar.process.ProcessProperties.Property;
 import org.sonar.process.systeminfo.Global;
 import org.sonar.process.systeminfo.SystemInfoSection;
 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo.Section.Builder;
+import org.sonar.server.platform.NodeInformation;
 
+import static java.util.stream.Collectors.toUnmodifiableSet;
 import static org.apache.commons.lang.StringUtils.abbreviate;
 import static org.apache.commons.lang.StringUtils.containsIgnoreCase;
 import static org.apache.commons.lang.StringUtils.endsWithIgnoreCase;
 import static org.sonar.process.ProcessProperties.Property.AUTH_JWT_SECRET;
+import static org.sonar.process.ProcessProperties.Property.CE_JAVA_ADDITIONAL_OPTS;
+import static org.sonar.process.ProcessProperties.Property.CE_JAVA_OPTS;
+import static org.sonar.process.ProcessProperties.Property.SEARCH_JAVA_ADDITIONAL_OPTS;
+import static org.sonar.process.ProcessProperties.Property.SEARCH_JAVA_OPTS;
+import static org.sonar.process.ProcessProperties.Property.WEB_JAVA_ADDITIONAL_OPTS;
+import static org.sonar.process.ProcessProperties.Property.WEB_JAVA_OPTS;
 import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute;
 
 @ServerSide
 public class SettingsSection implements SystemInfoSection, Global {
   private static final int MAX_VALUE_LENGTH = 500;
   private static final String PASSWORD_VALUE = "xxxxxxxx";
+  private static final Collection<String> IGNORED_SETTINGS_IN_CLUSTER = Stream.of(
+      WEB_JAVA_OPTS,
+      WEB_JAVA_ADDITIONAL_OPTS,
+      CE_JAVA_OPTS,
+      CE_JAVA_ADDITIONAL_OPTS,
+      SEARCH_JAVA_OPTS,
+      SEARCH_JAVA_ADDITIONAL_OPTS)
+    .map(Property::getKey)
+    .collect(toUnmodifiableSet());
 
   private final DbClient dbClient;
   private final Settings settings;
+  private final NodeInformation nodeInformation;
 
-  public SettingsSection(DbClient dbClient, Settings settings) {
+  public SettingsSection(DbClient dbClient, Settings settings, NodeInformation nodeInformation) {
     this.dbClient = dbClient;
     this.settings = settings;
+    this.nodeInformation = nodeInformation;
   }
 
   @Override
@@ -61,14 +83,15 @@ public class SettingsSection implements SystemInfoSection, Global {
 
     PropertyDefinitions definitions = settings.getDefinitions();
     TreeMap<String, String> orderedProps = new TreeMap<>(settings.getProperties());
-    for (Entry<String, String> prop : orderedProps.entrySet()) {
-      includeSetting(protobuf, definitions, prop);
-    }
+    orderedProps.entrySet()
+      .stream()
+      .filter(prop -> nodeInformation.isStandalone() || !IGNORED_SETTINGS_IN_CLUSTER.contains(prop.getKey()))
+      .forEach(prop -> includeSetting(protobuf, definitions, prop));
     addDefaultNewCodeDefinition(protobuf);
     return protobuf.build();
   }
 
-  private static void includeSetting(Builder protobuf, PropertyDefinitions definitions, Entry<String, String> prop) {
+  private void includeSetting(Builder protobuf, PropertyDefinitions definitions, Entry<String, String> prop) {
     String key = prop.getKey();
     String value = obfuscateValue(definitions, key, prop.getValue());
     setAttribute(protobuf, key, value);
index eeed8ab3c796b10e08d517fb826e6fda1125dd09..ff4a919f6684fed59460fb9050b5df52a3d00438 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.platform.monitoring;
 
+import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.PropertyType;
@@ -30,10 +31,17 @@ import org.sonar.api.utils.System2;
 import org.sonar.db.DbTester;
 import org.sonar.db.newcodeperiod.NewCodePeriodType;
 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
+import org.sonar.server.platform.NodeInformation;
 
 import static org.apache.commons.lang.StringUtils.repeat;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.sonar.process.ProcessProperties.Property.CE_JAVA_OPTS;
+import static org.sonar.process.ProcessProperties.Property.SEARCH_JAVA_OPTS;
+import static org.sonar.process.ProcessProperties.Property.WEB_JAVA_OPTS;
 import static org.sonar.process.systeminfo.SystemInfoUtils.attribute;
+import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeDoesNotExist;
 import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs;
 
 public class SettingsSectionTest {
@@ -45,7 +53,39 @@ public class SettingsSectionTest {
 
   private PropertyDefinitions defs = new PropertyDefinitions(System2.INSTANCE, PropertyDefinition.builder(PASSWORD_PROPERTY).type(PropertyType.PASSWORD).build());
   private Settings settings = new MapSettings(defs);
-  private SettingsSection underTest = new SettingsSection(dbTester.getDbClient(), settings);
+  private NodeInformation nodeInformation = mock(NodeInformation.class);
+  private SettingsSection underTest= new SettingsSection(dbTester.getDbClient(), settings, nodeInformation);
+
+  @Before
+  public void setup(){
+    when(nodeInformation.isStandalone()).thenReturn(true);
+  }
+
+  @Test
+  public void should_show_java_settings_in_standalone(){
+    settings.setProperty(WEB_JAVA_OPTS.getKey(), WEB_JAVA_OPTS.getDefaultValue());
+    settings.setProperty(CE_JAVA_OPTS.getKey(), CE_JAVA_OPTS.getDefaultValue());
+    settings.setProperty(SEARCH_JAVA_OPTS.getKey(), SEARCH_JAVA_OPTS.getDefaultValue());
+
+    ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
+
+    assertThatAttributeIs(protobuf, WEB_JAVA_OPTS.getKey(), WEB_JAVA_OPTS.getDefaultValue());
+    assertThatAttributeIs(protobuf, CE_JAVA_OPTS.getKey(), CE_JAVA_OPTS.getDefaultValue());
+    assertThatAttributeIs(protobuf, SEARCH_JAVA_OPTS.getKey(), SEARCH_JAVA_OPTS.getDefaultValue());
+  }
+  @Test
+  public void should_not_show_java_settings_in_cluster(){
+    when(nodeInformation.isStandalone()).thenReturn(false);
+    settings.setProperty(WEB_JAVA_OPTS.getKey(), WEB_JAVA_OPTS.getDefaultValue());
+    settings.setProperty(CE_JAVA_OPTS.getKey(), CE_JAVA_OPTS.getDefaultValue());
+    settings.setProperty(SEARCH_JAVA_OPTS.getKey(), SEARCH_JAVA_OPTS.getDefaultValue());
+
+    ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
+
+    assertThatAttributeDoesNotExist(protobuf, WEB_JAVA_OPTS.getKey());
+    assertThatAttributeDoesNotExist(protobuf, CE_JAVA_OPTS.getKey());
+    assertThatAttributeDoesNotExist(protobuf, SEARCH_JAVA_OPTS.getKey());
+  }
 
   @Test
   public void return_properties_and_sort_by_key() {