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

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


The following commit(s) were added to refs/heads/master by this push:
     new 1ee5a4d1c [SCB-2843]detect known configuration problems and send 
warning message (#4137)
1ee5a4d1c is described below

commit 1ee5a4d1c7bde743f3f2844cbf73f990dbe6dc2c
Author: liubao68 <[email protected]>
AuthorDate: Fri Dec 15 17:09:06 2023 +0800

    [SCB-2843]detect known configuration problems and send warning message 
(#4137)
---
 .../core/ServiceCombCoreConfiguration.java         |   6 ++
 .../core/bootup/BootUpInformationCollector.java    |   5 +-
 ...r.java => ConfigurationProblemsAlarmEvent.java} |  18 ++--
 .../bootup/ConfigurationProblemsCollector.java     | 101 +++++++++++++++++++++
 .../demo/springmvc/SpringmvcServer.java            |  17 ++++
 .../server/ConfigurationProblemsCollectorTest.java |  47 ++++++++++
 .../src/main/resources/microservice.yaml           |   5 +-
 7 files changed, 187 insertions(+), 12 deletions(-)

diff --git 
a/core/src/main/java/org/apache/servicecomb/core/ServiceCombCoreConfiguration.java
 
b/core/src/main/java/org/apache/servicecomb/core/ServiceCombCoreConfiguration.java
index 99d2b9788..8ea9f726c 100644
--- 
a/core/src/main/java/org/apache/servicecomb/core/ServiceCombCoreConfiguration.java
+++ 
b/core/src/main/java/org/apache/servicecomb/core/ServiceCombCoreConfiguration.java
@@ -16,6 +16,7 @@
  */
 package org.apache.servicecomb.core;
 
+import org.apache.servicecomb.core.bootup.ConfigurationProblemsCollector;
 import org.apache.servicecomb.core.bootup.FilterChainCollector;
 import org.apache.servicecomb.core.bootup.ServiceInformationCollector;
 import org.apache.servicecomb.core.executor.ExecutorManager;
@@ -83,6 +84,11 @@ public class ServiceCombCoreConfiguration {
     return new FilterChainCollector();
   }
 
+  @Bean
+  public ConfigurationProblemsCollector configurationProblemsCollector() {
+    return new ConfigurationProblemsCollector();
+  }
+
   @Bean
   public ServiceInformationCollector serviceInformationCollector() {
     return new ServiceInformationCollector();
diff --git 
a/core/src/main/java/org/apache/servicecomb/core/bootup/BootUpInformationCollector.java
 
b/core/src/main/java/org/apache/servicecomb/core/bootup/BootUpInformationCollector.java
index 62a9ac5ce..4ebae5180 100644
--- 
a/core/src/main/java/org/apache/servicecomb/core/bootup/BootUpInformationCollector.java
+++ 
b/core/src/main/java/org/apache/servicecomb/core/bootup/BootUpInformationCollector.java
@@ -18,13 +18,12 @@
 package org.apache.servicecomb.core.bootup;
 
 import org.apache.servicecomb.core.SCBEngine;
+import org.springframework.core.Ordered;
 
-public interface BootUpInformationCollector {
+public interface BootUpInformationCollector extends Ordered {
   default String collect(SCBEngine engine) {
     return collect();
   }
 
   String collect();
-
-  int getOrder();
 }
diff --git 
a/core/src/main/java/org/apache/servicecomb/core/bootup/BootUpInformationCollector.java
 
b/core/src/main/java/org/apache/servicecomb/core/bootup/ConfigurationProblemsAlarmEvent.java
similarity index 71%
copy from 
core/src/main/java/org/apache/servicecomb/core/bootup/BootUpInformationCollector.java
copy to 
core/src/main/java/org/apache/servicecomb/core/bootup/ConfigurationProblemsAlarmEvent.java
index 62a9ac5ce..631cc3612 100644
--- 
a/core/src/main/java/org/apache/servicecomb/core/bootup/BootUpInformationCollector.java
+++ 
b/core/src/main/java/org/apache/servicecomb/core/bootup/ConfigurationProblemsAlarmEvent.java
@@ -14,17 +14,19 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.servicecomb.core.bootup;
 
-import org.apache.servicecomb.core.SCBEngine;
+import org.apache.servicecomb.foundation.common.event.AlarmEvent;
 
-public interface BootUpInformationCollector {
-  default String collect(SCBEngine engine) {
-    return collect();
-  }
+public class ConfigurationProblemsAlarmEvent extends AlarmEvent {
+  private final String problems;
 
-  String collect();
+  public ConfigurationProblemsAlarmEvent(Type type, String problems) {
+    super(type);
+    this.problems = problems;
+  }
 
-  int getOrder();
+  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..bcf85dbda
--- /dev/null
+++ 
b/core/src/main/java/org/apache/servicecomb/core/bootup/ConfigurationProblemsCollector.java
@@ -0,0 +1,101 @@
+/*
+ * 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) {
+    StringBuilder result = new StringBuilder();
+    collectCsePrefix(engine.getEnvironment(), result);
+    collectServiceDefinition(engine.getEnvironment(), result);
+    collectTimeoutConfiguration(engine.getEnvironment(), result);
+    if (result.isEmpty()) {
+      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/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 aa91890c3..52ede2792 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.springframework.boot.WebApplicationType;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.builder.SpringApplicationBuilder;
@@ -25,5 +27,20 @@ import 
org.springframework.boot.builder.SpringApplicationBuilder;
 public class SpringmvcServer {
   public static void main(String[] args) throws Exception {
     new 
SpringApplicationBuilder(SpringmvcServer.class).web(WebApplicationType.NONE).run(args);
+
+    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..3fcfca300
--- /dev/null
+++ 
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ConfigurationProblemsCollectorTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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 `service_description` is deprecated, "
+        + "use `servicecomb.service` instead. Find keys 
[service_description.initialStatus]\n");
+  }
+}
diff --git 
a/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml 
b/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
index 39302e155..7f6579af9 100644
--- a/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
+++ b/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
@@ -15,8 +15,11 @@
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
 
-instance_description:
+# test not supported configuration
+# service_description.initialStatus
+service_description:
   initialStatus: TESTING
+
 servicecomb:
   service:
     application: springmvctest

Reply via email to