From fbeb3ad73f472ccbff4b29fe02dca7874c7faa69 Mon Sep 17 00:00:00 2001 From: Pil0tXia Date: Sat, 13 Apr 2024 16:53:25 +0800 Subject: [PATCH 1/4] Extract commonConfiguration out of subclasses --- .../eventmesh/common/utils/JsonUtils.java | 2 +- .../admin/handler/AdminHandlerManager.java | 6 ++- .../handler/v2/ConfigurationHandler.java | 49 +++++++++++++++++-- .../response/v2/GetConfigurationResponse.java | 3 ++ 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/JsonUtils.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/JsonUtils.java index 71d42e3452..cf07bdfbe7 100644 --- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/JsonUtils.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/JsonUtils.java @@ -58,7 +58,7 @@ public static T mapToObject(Map map, Class beanClass) { return null; } Object obj = OBJECT_MAPPER.convertValue(map, beanClass); - return (T) obj; + return beanClass.cast(obj); } /** diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AdminHandlerManager.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AdminHandlerManager.java index 4f81584b9d..1da928dc10 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AdminHandlerManager.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AdminHandlerManager.java @@ -58,6 +58,8 @@ public class AdminHandlerManager { + private EventMeshServer eventMeshServer; + private EventMeshTCPServer eventMeshTCPServer; private EventMeshHTTPServer eventMeshHTTPServer; @@ -71,9 +73,10 @@ public class AdminHandlerManager { private final Map httpHandlerMap = new ConcurrentHashMap<>(); public AdminHandlerManager(EventMeshServer eventMeshServer) { + this.eventMeshServer = eventMeshServer; + this.eventMeshTCPServer = eventMeshServer.getEventMeshTCPServer(); this.eventMeshGrpcServer = eventMeshServer.getEventMeshGrpcServer(); this.eventMeshHTTPServer = eventMeshServer.getEventMeshHTTPServer(); - this.eventMeshTCPServer = eventMeshServer.getEventMeshTCPServer(); this.eventMeshMetaStorage = eventMeshServer.getMetaStorage(); this.adminWebHookConfigOperationManage = eventMeshTCPServer.getAdminWebHookConfigOperationManage(); } @@ -112,6 +115,7 @@ public void registerHttpHandler() { // v2 endpoints initHandler(new ConfigurationHandler( + eventMeshServer.getConfiguration(), eventMeshTCPServer.getEventMeshTCPConfiguration(), eventMeshHTTPServer.getEventMeshHttpConfiguration(), eventMeshGrpcServer.getEventMeshGrpcConfiguration())); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v2/ConfigurationHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v2/ConfigurationHandler.java index ce16b95749..7c94b36a61 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v2/ConfigurationHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v2/ConfigurationHandler.java @@ -39,6 +39,7 @@ import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.filter.Filter; import com.alibaba.fastjson2.filter.NameFilter; +import com.alibaba.fastjson2.filter.PropertyFilter; import com.alibaba.fastjson2.filter.ValueFilter; import lombok.extern.slf4j.Slf4j; @@ -56,6 +57,7 @@ @EventMeshHttpHandler(path = "/v2/configuration") public class ConfigurationHandler extends AbstractHttpHandler { + private final CommonConfiguration commonConfiguration; private final EventMeshTCPConfiguration eventMeshTCPConfiguration; private final EventMeshHTTPConfiguration eventMeshHTTPConfiguration; private final EventMeshGrpcConfiguration eventMeshGrpcConfiguration; @@ -68,10 +70,12 @@ public class ConfigurationHandler extends AbstractHttpHandler { * @param eventMeshGrpcConfiguration the gRPC configuration for EventMesh */ public ConfigurationHandler( + CommonConfiguration commonConfiguration, EventMeshTCPConfiguration eventMeshTCPConfiguration, EventMeshHTTPConfiguration eventMeshHTTPConfiguration, EventMeshGrpcConfiguration eventMeshGrpcConfiguration) { super(); + this.commonConfiguration = commonConfiguration; this.eventMeshTCPConfiguration = eventMeshTCPConfiguration; this.eventMeshHTTPConfiguration = eventMeshHTTPConfiguration; this.eventMeshGrpcConfiguration = eventMeshGrpcConfiguration; @@ -92,10 +96,10 @@ protected void get(HttpRequest httpRequest, ChannelHandlerContext ctx) { String format = HttpRequestUtil.getQueryParam(httpRequest, "format", "properties"); Filter[] filters; - if (format.equals("properties")) { - filters = new Filter[] {new ConfigFieldFilter(), new IPAddressToStringFilter()}; + if (format.equals("properties")) { // TODO add a param for SuperClassFieldFilter + filters = new Filter[] {new SuperClassFieldFilter(), new IPAddressToStringFilter(), new ConfigFieldFilter()}; } else if (format.equals("bean")) { - filters = new Filter[] {new IPAddressToStringFilter()}; + filters = new Filter[] {new SuperClassFieldFilter(), new IPAddressToStringFilter()}; } else { log.warn("Invalid format param: {}", format); writeBadRequest(ctx, "Invalid format param: " + format); @@ -103,16 +107,18 @@ protected void get(HttpRequest httpRequest, ChannelHandlerContext ctx) { } GetConfigurationResponse getConfigurationResponse = new GetConfigurationResponse( + commonConfiguration, eventMeshTCPConfiguration, eventMeshHTTPConfiguration, - eventMeshGrpcConfiguration + eventMeshGrpcConfiguration, + "v1.10.0-release" // TODO get version number after merging https://github.com/apache/eventmesh/pull/4055 ); String json = JSON.toJSONString(Result.success(getConfigurationResponse), filters); writeJson(ctx, json); } /** - * For each member of {@link EventMeshTCPConfiguration}, {@link EventMeshHTTPConfiguration}, and {@link EventMeshGrpcConfiguration}, + * For each member of configuration classes, * the value of the {@link ConfigField} annotation for each field is obtained through reflection, * and then concatenated with the configuration prefix in the {@link Config} annotation to serve as the JSON key for this field. *

@@ -155,6 +161,39 @@ private Field findFieldInClassHierarchy(Class clazz, String fieldName) throws } } + /** + * For each member of {@link EventMeshTCPConfiguration}, {@link EventMeshHTTPConfiguration}, and {@link EventMeshGrpcConfiguration}, + * if the {@code name} is a member that exists in {@link CommonConfiguration} class, it will be skipped. + */ + static class SuperClassFieldFilter implements PropertyFilter { + @Override + public boolean apply(Object object, String name, Object value) { + try { + Field field = findFieldInClassNonHierarchy(object.getClass(), name); + return field != null; + } catch (NoSuchFieldException e) { + log.error("Failed to get field {} from object {}", name, object, e); + return true; + } + } + + /** + * If a field of a subclass exists in the superclass, return null, causing FastJSON to skip this field. + */ + private Field findFieldInClassNonHierarchy(Class clazz, String fieldName) throws NoSuchFieldException { + try { + return clazz.getDeclaredField(fieldName); + } catch (NoSuchFieldException e) { + Class superclass = clazz.getSuperclass(); + if (superclass == null) { + throw e; + } else { + return null; + } + } + } + } + /** * {@link IPAddress} can't be serialized directly by FastJSON, * so this filter converts {@link IPAddress} objects to their string representation. diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/response/v2/GetConfigurationResponse.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/response/v2/GetConfigurationResponse.java index 40161cca73..5ae9eeb2d3 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/response/v2/GetConfigurationResponse.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/response/v2/GetConfigurationResponse.java @@ -17,6 +17,7 @@ package org.apache.eventmesh.runtime.admin.response.v2; +import org.apache.eventmesh.common.config.CommonConfiguration; import org.apache.eventmesh.runtime.configuration.EventMeshGrpcConfiguration; import org.apache.eventmesh.runtime.configuration.EventMeshHTTPConfiguration; import org.apache.eventmesh.runtime.configuration.EventMeshTCPConfiguration; @@ -30,7 +31,9 @@ @AllArgsConstructor public class GetConfigurationResponse { + private CommonConfiguration commonConfiguration; private EventMeshTCPConfiguration eventMeshTCPConfiguration; private EventMeshHTTPConfiguration eventMeshHTTPConfiguration; private EventMeshGrpcConfiguration eventMeshGrpcConfiguration; + private String eventMeshVersion; } From 2a81b5d399a1d01a7de16c5277557a7cefc1fbd8 Mon Sep 17 00:00:00 2001 From: Pil0tXia Date: Sat, 13 Apr 2024 18:28:34 +0800 Subject: [PATCH 2/4] Add configs param --- .../admin/handler/AbstractHttpHandler.java | 46 +++++++++++-------- .../handler/v2/ConfigurationHandler.java | 37 ++++++++++----- 2 files changed, 53 insertions(+), 30 deletions(-) diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java index 0b0a6afedf..43f9753dfd 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java @@ -35,7 +35,9 @@ import com.alibaba.fastjson2.JSONWriter; import lombok.Data; +import lombok.extern.slf4j.Slf4j; +@Slf4j @Data public abstract class AbstractHttpHandler implements HttpHandler { @@ -92,25 +94,31 @@ protected void write(ChannelHandlerContext ctx, HttpResponse response) { @Override public void handle(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception { - switch (HttpMethod.valueOf(httpRequest.method().name())) { - case OPTIONS: - preflight(ctx); - break; - case GET: - get(httpRequest, ctx); - break; - case POST: - post(httpRequest, ctx); - break; - case PUT: - put(httpRequest, ctx); - break; - case DELETE: - delete(httpRequest, ctx); - break; - default: - // do nothing - break; + try { + switch (HttpMethod.valueOf(httpRequest.method().name())) { + case OPTIONS: + preflight(ctx); + break; + case GET: + get(httpRequest, ctx); + break; + case POST: + post(httpRequest, ctx); + break; + case PUT: + put(httpRequest, ctx); + break; + case DELETE: + delete(httpRequest, ctx); + break; + default: // do nothing + } + } catch (IllegalArgumentException e) { + StackTraceElement element = e.getStackTrace()[0]; + String className = element.getClassName(); + String handlerName = className.substring(className.lastIndexOf(".") + 1); + log.warn("Admin handler {}:{} - {}", handlerName, element.getLineNumber(), e.getMessage()); + writeBadRequest(ctx, e.getMessage()); } } diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v2/ConfigurationHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v2/ConfigurationHandler.java index 7c94b36a61..7831078966 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v2/ConfigurationHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v2/ConfigurationHandler.java @@ -89,22 +89,37 @@ public ConfigurationHandler( *

When {@code properties}, the field names are returned in Properties format; *

When {@code bean}, the field names themselves are used as json keys. * + *

  • + * {@code configs}: String; Optional, DefaultValue: {@code exclusive}, SelectableValue: {@code all}. + *

    When {@code exclusive}, protocol-specific configurations will only contain protocol-exclusive fields + * and won't contain any {@link CommonConfiguration} fields; + *

    When {@code all}, protocol-specific configurations will contain all fields, including those in {@link CommonConfiguration}. + *

  • * */ @Override protected void get(HttpRequest httpRequest, ChannelHandlerContext ctx) { String format = HttpRequestUtil.getQueryParam(httpRequest, "format", "properties"); - - Filter[] filters; - if (format.equals("properties")) { // TODO add a param for SuperClassFieldFilter - filters = new Filter[] {new SuperClassFieldFilter(), new IPAddressToStringFilter(), new ConfigFieldFilter()}; - } else if (format.equals("bean")) { - filters = new Filter[] {new SuperClassFieldFilter(), new IPAddressToStringFilter()}; - } else { - log.warn("Invalid format param: {}", format); - writeBadRequest(ctx, "Invalid format param: " + format); - return; + String configs = HttpRequestUtil.getQueryParam(httpRequest, "configs", "exclusive"); + + List filters = new ArrayList<>(); + switch (configs) { + case "exclusive": + filters.add(new SuperClassFieldFilter()); + break; + case "all": break; + default: + throw new IllegalArgumentException("Invalid param 'configs': " + configs); + } + switch (format) { + case "properties": + filters.add(new ConfigFieldFilter()); + break; + case "bean": break; + default: + throw new IllegalArgumentException("Invalid param 'format': " + format); } + filters.add(new IPAddressToStringFilter()); GetConfigurationResponse getConfigurationResponse = new GetConfigurationResponse( commonConfiguration, @@ -113,7 +128,7 @@ protected void get(HttpRequest httpRequest, ChannelHandlerContext ctx) { eventMeshGrpcConfiguration, "v1.10.0-release" // TODO get version number after merging https://github.com/apache/eventmesh/pull/4055 ); - String json = JSON.toJSONString(Result.success(getConfigurationResponse), filters); + String json = JSON.toJSONString(Result.success(getConfigurationResponse), filters.toArray(new Filter[0])); writeJson(ctx, json); } From 9e9c96c1fd833867bf71c544dee49a552b784d0f Mon Sep 17 00:00:00 2001 From: Pil0tXia Date: Sun, 14 Apr 2024 15:53:01 +0800 Subject: [PATCH 3/4] Unify return position --- .../runtime/admin/handler/v2/ConfigurationHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v2/ConfigurationHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v2/ConfigurationHandler.java index 7831078966..1c149ca521 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v2/ConfigurationHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v2/ConfigurationHandler.java @@ -188,8 +188,8 @@ public boolean apply(Object object, String name, Object value) { return field != null; } catch (NoSuchFieldException e) { log.error("Failed to get field {} from object {}", name, object, e); - return true; } + return true; } /** From 6544b92de24bd9a66b3a53ff3e9e90abe887ee66 Mon Sep 17 00:00:00 2001 From: Pil0tXia Date: Sun, 14 Apr 2024 16:12:10 +0800 Subject: [PATCH 4/4] Catch more exceptions --- .../admin/handler/AbstractHttpHandler.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java index 43f9753dfd..cdfe4e163a 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java @@ -85,6 +85,12 @@ protected void writeUnauthorized(ChannelHandlerContext ctx, String message) { writeJson(ctx, json, HttpResponseStatus.UNAUTHORIZED); } + protected void writeInternalServerError(ChannelHandlerContext ctx, String message) { + Result result = new Result<>(message); + String json = JSON.toJSONString(result, JSONWriter.Feature.WriteNulls); + writeJson(ctx, json, HttpResponseStatus.INTERNAL_SERVER_ERROR); + } + /** * Use {@link HttpResponseUtils#buildHttpResponse} to build {@link HttpResponse} param. */ @@ -113,12 +119,17 @@ public void handle(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Ex break; default: // do nothing } - } catch (IllegalArgumentException e) { + } catch (RuntimeException e) { StackTraceElement element = e.getStackTrace()[0]; String className = element.getClassName(); String handlerName = className.substring(className.lastIndexOf(".") + 1); - log.warn("Admin handler {}:{} - {}", handlerName, element.getLineNumber(), e.getMessage()); - writeBadRequest(ctx, e.getMessage()); + if (e instanceof IllegalArgumentException) { + log.warn("Admin endpoint {}:{} - {}", handlerName, element.getLineNumber(), e.getMessage()); + writeBadRequest(ctx, e.getMessage()); + } else { + log.error("Admin endpoint {}:{} - {}", handlerName, element.getLineNumber(), e.getMessage(), e); + writeInternalServerError(ctx, e.getMessage()); + } } }