This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch 2.8.x
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/2.8.x by this push:
     new 8a3a2a169 [SCB-2843]detect known configuration problems and send 
warning message (#4138)
8a3a2a169 is described below

commit 8a3a2a16946629b6c07cc3fe6931f88267e2e341
Author: liubao68 <[email protected]>
AuthorDate: Fri Dec 15 17:08:52 2023 +0800

    [SCB-2843]detect known configuration problems and send warning message 
(#4138)
---
 .../org/apache/servicecomb/core/SCBEngine.java     |   9 ++
 .../bootup/ConfigurationProblemsAlarmEvent.java    |  18 ++--
 .../bootup/ConfigurationProblemsCollector.java     | 105 +++++++++++++++++++++
 ...vicecomb.core.bootup.BootUpInformationCollector |   3 +-
 .../demo/springmvc/SpringmvcServer.java            |  17 ++++
 .../server/ConfigurationProblemsCollectorTest.java |  50 ++++++++++
 .../org/apache/servicecomb/config/ConfigUtil.java  |  22 ++++-
 7 files changed, 215 insertions(+), 9 deletions(-)

diff --git a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java 
b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
index df58e1599..0b0b24076 100644
--- a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
+++ b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
@@ -68,6 +68,7 @@ import 
org.apache.servicecomb.swagger.invocation.exception.InvocationException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.context.ApplicationContext;
+import org.springframework.core.env.Environment;
 
 import com.google.common.eventbus.AllowConcurrentEvents;
 import com.google.common.eventbus.EventBus;
@@ -146,6 +147,14 @@ public class SCBEngine {
     this.applicationContext = applicationContext;
   }
 
+  public Environment getEnvironment() {
+    if (this.applicationContext == null) {
+      // some test cases
+      return null;
+    }
+    return this.applicationContext.getEnvironment();
+  }
+
   public VendorExtensions getVendorExtensions() {
     return vendorExtensions;
   }
diff --git 
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/SpringmvcServer.java
 
b/core/src/main/java/org/apache/servicecomb/core/bootup/ConfigurationProblemsAlarmEvent.java
similarity index 68%
copy from 
demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/SpringmvcServer.java
copy to 
core/src/main/java/org/apache/servicecomb/core/bootup/ConfigurationProblemsAlarmEvent.java
index 38841a982..631cc3612 100644
--- 
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/SpringmvcServer.java
+++ 
b/core/src/main/java/org/apache/servicecomb/core/bootup/ConfigurationProblemsAlarmEvent.java
@@ -14,15 +14,19 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+package org.apache.servicecomb.core.bootup;
 
-package org.apache.servicecomb.demo.springmvc;
+import org.apache.servicecomb.foundation.common.event.AlarmEvent;
 
-import org.apache.servicecomb.foundation.common.utils.BeanUtils;
-import org.apache.servicecomb.foundation.common.utils.Log4jUtils;
+public class ConfigurationProblemsAlarmEvent extends AlarmEvent {
+  private final String problems;
 
-public class SpringmvcServer {
-  public static void main(String[] args) throws Exception {
-    Log4jUtils.init();
-    BeanUtils.init();
+  public ConfigurationProblemsAlarmEvent(Type type, String problems) {
+    super(type);
+    this.problems = problems;
+  }
+
+  public String getProblems() {
+    return problems;
   }
 }
diff --git 
a/core/src/main/java/org/apache/servicecomb/core/bootup/ConfigurationProblemsCollector.java
 
b/core/src/main/java/org/apache/servicecomb/core/bootup/ConfigurationProblemsCollector.java
new file mode 100644
index 000000000..8fc4979aa
--- /dev/null
+++ 
b/core/src/main/java/org/apache/servicecomb/core/bootup/ConfigurationProblemsCollector.java
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.core.bootup;
+
+import java.util.Arrays;
+import java.util.Set;
+
+import org.apache.servicecomb.config.ConfigUtil;
+import org.apache.servicecomb.core.SCBEngine;
+import org.apache.servicecomb.foundation.common.event.AlarmEvent.Type;
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.springframework.core.env.Environment;
+
+/**
+ * Detect deprecated and wrong usages of configurations
+ * and print warning messages
+ * and sending ConfigurationProblemsAlarmEvent.
+ */
+public class ConfigurationProblemsCollector implements 
BootUpInformationCollector {
+  @Override
+  public String collect(SCBEngine engine) {
+    if (engine.getEnvironment() == null) {
+      // some test cases
+      return null;
+    }
+    StringBuilder result = new StringBuilder();
+    collectCsePrefix(engine.getEnvironment(), result);
+    collectServiceDefinition(engine.getEnvironment(), result);
+    collectTimeoutConfiguration(engine.getEnvironment(), result);
+    if (result.length() <= 0) {
+      return null;
+    }
+    String warnings = "Configurations warnings:\n" + result;
+    EventManager.post(new ConfigurationProblemsAlarmEvent(Type.OPEN, 
warnings));
+    return warnings;
+  }
+
+  private void collectTimeoutConfiguration(Environment environment, 
StringBuilder result) {
+    int keepAliveTimeoutInSeconds = environment.getProperty(
+        "servicecomb.rest.client.connection.keepAliveTimeoutInSeconds", 
int.class, 60);
+    int idleTimeoutInSeconds = environment.getProperty(
+        "servicecomb.rest.client.connection.idleTimeoutInSeconds", int.class, 
150);
+    if (keepAliveTimeoutInSeconds >= idleTimeoutInSeconds) {
+      result.append("Configuration 
`servicecomb.rest.client.connection.keepAliveTimeoutInSeconds` is longer than "
+          + "servicecomb.rest.client.connection.idleTimeoutInSeconds.");
+      
result.append("[").append(keepAliveTimeoutInSeconds).append(",").append(idleTimeoutInSeconds).append("]\n");
+    }
+    keepAliveTimeoutInSeconds = environment.getProperty(
+        "servicecomb.rest.client.http2.connection.keepAliveTimeoutInSeconds", 
int.class, 60);
+    idleTimeoutInSeconds = environment.getProperty(
+        "servicecomb.rest.client.http2.connection.idleTimeoutInSeconds", 
int.class, 150);
+    if (keepAliveTimeoutInSeconds >= idleTimeoutInSeconds) {
+      result.append("Configuration 
`servicecomb.rest.client.http2.connection.keepAliveTimeoutInSeconds` is longer 
than "
+          + "servicecomb.rest.client.http2.connection.idleTimeoutInSeconds.");
+      
result.append("[").append(keepAliveTimeoutInSeconds).append(",").append(idleTimeoutInSeconds).append("]\n");
+    }
+  }
+
+  private void collectServiceDefinition(Environment environment, StringBuilder 
result) {
+    if (environment.getProperty("APPLICATION_ID") != null) {
+      result.append("Configurations `APPLICATION_ID` is deprecated, "
+          + "use `servicecomb.service.application` instead.\n");
+    }
+    Set<String> names = ConfigUtil.propertiesWithPrefix(environment, 
"service_description.");
+    if (!names.isEmpty()) {
+      result.append("Configurations with prefix `service_description` is 
deprecated, "
+          + "use `servicecomb.service` instead. Find keys ");
+      result.append(Arrays.toString(names.toArray()));
+      result.append("\n");
+    }
+  }
+
+  private void collectCsePrefix(Environment environment, StringBuilder result) 
{
+    Set<String> names = ConfigUtil.propertiesWithPrefix(environment, "cse.");
+    if (!names.isEmpty()) {
+      result.append("Configurations with prefix `cse` is deprecated, use 
`servicecomb` instead. Find keys ");
+      result.append(Arrays.toString(names.toArray()));
+      result.append("\n");
+    }
+  }
+
+  @Override
+  public String collect() {
+    return null;
+  }
+
+  @Override
+  public int getOrder() {
+    return 1000;
+  }
+}
diff --git 
a/core/src/main/resources/META-INF/services/org.apache.servicecomb.core.bootup.BootUpInformationCollector
 
b/core/src/main/resources/META-INF/services/org.apache.servicecomb.core.bootup.BootUpInformationCollector
index c33b0de60..36ee3dc51 100644
--- 
a/core/src/main/resources/META-INF/services/org.apache.servicecomb.core.bootup.BootUpInformationCollector
+++ 
b/core/src/main/resources/META-INF/services/org.apache.servicecomb.core.bootup.BootUpInformationCollector
@@ -16,4 +16,5 @@
 #
 
 org.apache.servicecomb.core.bootup.ServiceInformationCollector
-org.apache.servicecomb.core.bootup.FilterChainCollector
\ No newline at end of file
+org.apache.servicecomb.core.bootup.FilterChainCollector
+org.apache.servicecomb.core.bootup.ConfigurationProblemsCollector
diff --git 
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/SpringmvcServer.java
 
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/SpringmvcServer.java
index 38841a982..768930e71 100644
--- 
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/SpringmvcServer.java
+++ 
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/SpringmvcServer.java
@@ -17,6 +17,8 @@
 
 package org.apache.servicecomb.demo.springmvc;
 
+import org.apache.servicecomb.demo.CategorizedTestCaseRunner;
+import org.apache.servicecomb.demo.TestMgr;
 import org.apache.servicecomb.foundation.common.utils.BeanUtils;
 import org.apache.servicecomb.foundation.common.utils.Log4jUtils;
 
@@ -24,5 +26,20 @@ public class SpringmvcServer {
   public static void main(String[] args) throws Exception {
     Log4jUtils.init();
     BeanUtils.init();
+
+    runTests();
+
+    TestMgr.summary();
+    if (!TestMgr.isSuccess()) {
+      System.exit(1);
+    }
+  }
+
+  private static void runTests() {
+    try {
+      CategorizedTestCaseRunner.runCategorizedTestCase("springmvc");
+    } catch (Exception e) {
+      TestMgr.failed("runCategorizedTestCase failed", e);
+    }
   }
 }
diff --git 
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ConfigurationProblemsCollectorTest.java
 
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ConfigurationProblemsCollectorTest.java
new file mode 100644
index 000000000..4852176bb
--- /dev/null
+++ 
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ConfigurationProblemsCollectorTest.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.demo.springmvc.server;
+
+import org.apache.servicecomb.core.bootup.ConfigurationProblemsAlarmEvent;
+import org.apache.servicecomb.demo.CategorizedTestCase;
+import org.apache.servicecomb.demo.TestMgr;
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.springframework.stereotype.Component;
+
+import com.google.common.eventbus.Subscribe;
+
+@Component
+public class ConfigurationProblemsCollectorTest implements CategorizedTestCase 
{
+  private ConfigurationProblemsAlarmEvent event;
+
+  public ConfigurationProblemsCollectorTest() {
+    EventManager.register(this);
+  }
+
+  @Subscribe
+  public void 
onConfigurationProblemsAlarmEvent(ConfigurationProblemsAlarmEvent event) {
+    this.event = event;
+  }
+
+  @Override
+  public void testRestTransport() throws Exception {
+    TestMgr.check(event != null, true);
+    TestMgr.check(event.getProblems(), "Configurations warnings:\n"
+        + "Configurations with prefix `cse` is deprecated, use `servicecomb` 
instead. Find keys [cse.test.duplicate1]\n"
+        + "Configurations `APPLICATION_ID` is deprecated, use 
`servicecomb.service.application` instead.\n"
+        + "Configurations with prefix `service_description` is deprecated, "
+        + "use `servicecomb.service` instead. Find keys 
[service_description.name, service_description.paths, "
+        + "service_description.version]\n");
+  }
+}
diff --git 
a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigUtil.java
 
b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigUtil.java
index d0e7730ff..10d9bc7b3 100644
--- 
a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigUtil.java
+++ 
b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigUtil.java
@@ -22,18 +22,19 @@ import static 
org.apache.servicecomb.foundation.common.base.ServiceCombConstants
 import static 
org.apache.servicecomb.foundation.common.base.ServiceCombConstants.CONFIG_SERVICECOMB_PREFIX;
 
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.stream.Collectors;
 
 import javax.annotation.Nonnull;
 
-import com.google.common.annotations.VisibleForTesting;
 import org.apache.commons.configuration.AbstractConfiguration;
 import org.apache.commons.configuration.Configuration;
 import org.apache.commons.configuration.EnvironmentConfiguration;
@@ -52,7 +53,12 @@ import 
org.apache.servicecomb.foundation.common.event.EventManager;
 import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.EnumerablePropertySource;
+import org.springframework.core.env.Environment;
+import org.springframework.core.env.PropertySource;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.netflix.config.ConcurrentCompositeConfiguration;
 import com.netflix.config.ConcurrentMapConfiguration;
 import com.netflix.config.ConfigurationManager;
@@ -342,4 +348,18 @@ public final class ConfigUtil {
       throw new IllegalStateException(e);
     }
   }
+
+  public static Set<String> propertiesWithPrefix(Environment environment, 
String prefix) {
+    Set<String> result = new HashSet<>();
+    for (PropertySource<?> propertySource : ((ConfigurableEnvironment) 
environment).getPropertySources()) {
+      if (propertySource instanceof EnumerablePropertySource) {
+        for (String key : ((EnumerablePropertySource<?>) 
propertySource).getPropertyNames()) {
+          if (key.startsWith(prefix)) {
+            result.add(key);
+          }
+        }
+      }
+    }
+    return result;
+  }
 }

Reply via email to