diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/RestServerCodecFilter.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/RestServerCodecFilter.java index 2d74f4c8e05..07795d678db 100644 --- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/RestServerCodecFilter.java +++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/RestServerCodecFilter.java @@ -34,6 +34,8 @@ import org.apache.servicecomb.core.CoreConst; import org.apache.servicecomb.core.Invocation; import org.apache.servicecomb.core.definition.OperationMeta; +import org.apache.servicecomb.core.filter.AbstractFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.core.filter.ProviderFilter; @@ -41,8 +43,8 @@ import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx; import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx; import org.apache.servicecomb.foundation.vertx.stream.BufferOutputStream; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; +import org.apache.servicecomb.swagger.invocation.context.TransportContext; import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,7 +54,7 @@ import jakarta.servlet.http.Part; import jakarta.ws.rs.core.HttpHeaders; -public class RestServerCodecFilter implements ProviderFilter { +public class RestServerCodecFilter extends AbstractFilter implements ProviderFilter, EdgeFilter { private static final Logger LOGGER = LoggerFactory.getLogger(RestServerCodecFilter.class); public static final String NAME = "rest-server-codec"; @@ -63,7 +65,7 @@ public String getName() { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { // almost time, should be the first filter. return Filter.PROVIDER_SCHEDULE_FILTER_ORDER - 2000; } @@ -83,6 +85,10 @@ public CompletableFuture onFilter(Invocation invocation, FilterNode ne } protected CompletableFuture invokeNext(Invocation invocation, FilterNode nextNode) { + if (invocation.isEdge()) { + TransportContext transportContext = invocation.getTransportContext(); + return nextNode.onFilter(invocation).whenComplete((r, e) -> invocation.setTransportContext(transportContext)); + } return nextNode.onFilter(invocation); } 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 081f479b724..d89a46cf9e8 100644 --- a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java +++ b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java @@ -383,7 +383,7 @@ private void createProducerMicroserviceMeta() { String microserviceName = BootStrapProperties.readServiceName(environment); producerMicroserviceMeta = new MicroserviceMeta(this, BootStrapProperties.readApplication(environment), microserviceName, false); - producerMicroserviceMeta.setFilterChain(filterChainsManager.findProducerChain( + producerMicroserviceMeta.setProviderFilterChain(filterChainsManager.findProducerChain( BootStrapProperties.readApplication(environment), microserviceName)); producerMicroserviceMeta.setMicroserviceVersionsMeta(new MicroserviceVersionsMeta(this)); } diff --git a/core/src/main/java/org/apache/servicecomb/core/bootstrap/SCBEngineForTest.java b/core/src/main/java/org/apache/servicecomb/core/bootstrap/SCBEngineForTest.java index 48d3fe01ddf..46ccd954155 100644 --- a/core/src/main/java/org/apache/servicecomb/core/bootstrap/SCBEngineForTest.java +++ b/core/src/main/java/org/apache/servicecomb/core/bootstrap/SCBEngineForTest.java @@ -17,14 +17,12 @@ package org.apache.servicecomb.core.bootstrap; -import java.util.Arrays; import java.util.List; import org.apache.servicecomb.config.priority.ConfigObjectFactory; import org.apache.servicecomb.config.priority.PriorityPropertyFactory; import org.apache.servicecomb.config.priority.PriorityPropertyManager; import org.apache.servicecomb.core.SCBEngine; -import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterChainsManager; import org.apache.servicecomb.core.filter.impl.EmptyFilter; import org.apache.servicecomb.foundation.common.event.EventManager; @@ -37,11 +35,12 @@ */ public class SCBEngineForTest extends SCBEngine { public SCBEngineForTest(Environment environment) { - List filters = Arrays.asList( - new EmptyFilter() - ); + EmptyFilter emptyFilter = new EmptyFilter(); + emptyFilter.setEnvironment(environment); setFilterChainsManager(new FilterChainsManager() - .addFilters(filters)); + .setProviderFilters(List.of(emptyFilter)) + .setConsumerFilters(List.of(emptyFilter)) + .setEdgeFilters(List.of(emptyFilter))); PriorityPropertyFactory propertyFactory = new PriorityPropertyFactory(environment); ConfigObjectFactory configObjectFactory = new ConfigObjectFactory(propertyFactory); diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/MicroserviceMeta.java b/core/src/main/java/org/apache/servicecomb/core/definition/MicroserviceMeta.java index a9b29d5dbaa..9476a34229b 100644 --- a/core/src/main/java/org/apache/servicecomb/core/definition/MicroserviceMeta.java +++ b/core/src/main/java/org/apache/servicecomb/core/definition/MicroserviceMeta.java @@ -26,10 +26,6 @@ import io.swagger.v3.oas.models.OpenAPI; -/** - * should named MicroserviceVersionMeta
- * but for compatible reason, keep the old name - */ public class MicroserviceMeta { private final SCBEngine scbEngine; @@ -45,9 +41,14 @@ public class MicroserviceMeta { // key is OperationMeta.getMicroserviceQualifiedName() private final Map operationMetas = new HashMap<>(); + // Used to indicate configuration items type. EDGE & CONSUMER chain are all consumer. private final boolean consumer; - private FilterNode filterChain = FilterNode.EMPTY; + private FilterNode consumerFilterChain = FilterNode.EMPTY; + + private FilterNode providerFilterChain = FilterNode.EMPTY; + + private FilterNode edgeFilterChain = FilterNode.EMPTY; private final VendorExtensions vendorExtensions = new VendorExtensions(); @@ -132,11 +133,27 @@ public T getExtData(String key) { return vendorExtensions.get(key); } - public FilterNode getFilterChain() { - return filterChain; + public FilterNode getConsumerFilterChain() { + return consumerFilterChain; + } + + public void setConsumerFilterChain(FilterNode consumerFilterChain) { + this.consumerFilterChain = consumerFilterChain; + } + + public FilterNode getProviderFilterChain() { + return providerFilterChain; + } + + public void setProviderFilterChain(FilterNode providerFilterChain) { + this.providerFilterChain = providerFilterChain; + } + + public FilterNode getEdgeFilterChain() { + return edgeFilterChain; } - public void setFilterChain(FilterNode filterChain) { - this.filterChain = filterChain; + public void setEdgeFilterChain(FilterNode edgeFilterChain) { + this.edgeFilterChain = edgeFilterChain; } } diff --git a/core/src/main/java/org/apache/servicecomb/core/filter/AbstractFilter.java b/core/src/main/java/org/apache/servicecomb/core/filter/AbstractFilter.java new file mode 100644 index 00000000000..6f68024f22e --- /dev/null +++ b/core/src/main/java/org/apache/servicecomb/core/filter/AbstractFilter.java @@ -0,0 +1,53 @@ +/* + * 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.filter; + +import org.springframework.context.EnvironmentAware; +import org.springframework.core.env.Environment; + +public abstract class AbstractFilter implements Filter, EnvironmentAware { + private static final String ORDER_KEY = "servicecomb.filter.%s.%s.%s.order"; + + private static final String ENABLE_KEY = "servicecomb.filter.%s.%s.%s.enabled"; + + protected Environment environment; + + @Override + public void setEnvironment(Environment environment) { + this.environment = environment; + } + + @Override + public int getOrder(String application, String serviceName) { + Integer custom = environment.getProperty(String.format(ORDER_KEY, getName(), application, serviceName), + Integer.class); + if (custom != null) { + return custom; + } + return getOrder(); + } + + @Override + public boolean enabledForMicroservice(String application, String serviceName) { + Boolean custom = environment.getProperty(String.format(ENABLE_KEY, getName(), application, serviceName), + Boolean.class); + if (custom != null) { + return custom; + } + return true; + } +} diff --git a/core/src/main/java/org/apache/servicecomb/core/filter/ConsumerFilter.java b/core/src/main/java/org/apache/servicecomb/core/filter/ConsumerFilter.java index bd658be71cf..de13aa0ac2e 100644 --- a/core/src/main/java/org/apache/servicecomb/core/filter/ConsumerFilter.java +++ b/core/src/main/java/org/apache/servicecomb/core/filter/ConsumerFilter.java @@ -16,11 +16,6 @@ */ package org.apache.servicecomb.core.filter; -import org.apache.servicecomb.swagger.invocation.InvocationType; - public interface ConsumerFilter extends Filter { - @Override - default boolean enabledForInvocationType(InvocationType invocationType) { - return invocationType == InvocationType.CONSUMER; - } + } diff --git a/core/src/main/java/org/apache/servicecomb/core/filter/CoreFilterConfiguration.java b/core/src/main/java/org/apache/servicecomb/core/filter/CoreFilterConfiguration.java index 23bc14eac53..7359fdc558c 100644 --- a/core/src/main/java/org/apache/servicecomb/core/filter/CoreFilterConfiguration.java +++ b/core/src/main/java/org/apache/servicecomb/core/filter/CoreFilterConfiguration.java @@ -25,12 +25,12 @@ @Configuration public class CoreFilterConfiguration { @Bean - public ProviderFilter producerOperationFilter() { + public ProviderOperationFilter producerOperationFilter() { return new ProviderOperationFilter(); } @Bean - public ProviderFilter scheduleFilter() { + public ScheduleFilter scheduleFilter() { return new ScheduleFilter(); } @@ -40,7 +40,7 @@ public FilterChainsManager filterChainsManager() { } @Bean - public ProviderFilter parameterValidatorFilter() { + public ParameterValidatorFilter parameterValidatorFilter() { return new ParameterValidatorFilter(); } } diff --git a/core/src/main/java/org/apache/servicecomb/core/filter/EdgeFilter.java b/core/src/main/java/org/apache/servicecomb/core/filter/EdgeFilter.java new file mode 100644 index 00000000000..70d8bef4ef5 --- /dev/null +++ b/core/src/main/java/org/apache/servicecomb/core/filter/EdgeFilter.java @@ -0,0 +1,21 @@ +/* + * 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.filter; + +public interface EdgeFilter extends Filter { + +} diff --git a/core/src/main/java/org/apache/servicecomb/core/filter/Filter.java b/core/src/main/java/org/apache/servicecomb/core/filter/Filter.java index 48e4e8ffc32..38213b12f84 100644 --- a/core/src/main/java/org/apache/servicecomb/core/filter/Filter.java +++ b/core/src/main/java/org/apache/servicecomb/core/filter/Filter.java @@ -19,15 +19,15 @@ import java.util.concurrent.CompletableFuture; import org.apache.servicecomb.core.Invocation; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; +import org.springframework.core.Ordered; /** *
  *  Filters are the basics of how an invocation is executed.
  *
  * thread rule:
- *   assume a producer filter chains is: f1, f2, schedule, f3, f4
+ *   assume a provider filter chains is: f1, f2, schedule, f3, f4
  *
  *   schedule is a builtIn filter, which will dispatch invocations to operation related threadPool
  *
@@ -47,15 +47,11 @@
  *        (reactive golden rule)
  * 
*/ -public interface Filter { +public interface Filter extends Ordered { int PROVIDER_SCHEDULE_FILTER_ORDER = 0; int CONSUMER_LOAD_BALANCE_ORDER = 0; - default boolean enabledForInvocationType(InvocationType invocationType) { - return true; - } - default boolean enabledForTransport(String transport) { return true; } @@ -64,7 +60,11 @@ default boolean enabledForMicroservice(String application, String serviceName) { return true; } - default int getOrder(InvocationType invocationType, String application, String serviceName) { + default int getOrder(String application, String serviceName) { + return 0; + } + + default int getOrder() { return 0; } diff --git a/core/src/main/java/org/apache/servicecomb/core/filter/FilterChainsManager.java b/core/src/main/java/org/apache/servicecomb/core/filter/FilterChainsManager.java index 4f7392f861a..2cc6281d018 100644 --- a/core/src/main/java/org/apache/servicecomb/core/filter/FilterChainsManager.java +++ b/core/src/main/java/org/apache/servicecomb/core/filter/FilterChainsManager.java @@ -22,26 +22,30 @@ import java.util.List; import java.util.stream.Collectors; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.springframework.beans.factory.annotation.Autowired; public class FilterChainsManager { - private final InvocationFilterChains consumerChains = new InvocationFilterChains(InvocationType.CONSUMER); + private InvocationFilterChains consumerChains; - private final InvocationFilterChains producerChains = new InvocationFilterChains(InvocationType.PROVIDER); + private InvocationFilterChains providerChains; + + private InvocationFilterChains edgeChains; @Autowired - public FilterChainsManager addFilters(List filters) { - for (Filter filter : filters) { - if (filter.enabledForInvocationType(InvocationType.CONSUMER)) { - consumerChains.addFilter(filter); - } + public FilterChainsManager setEdgeFilters(List filters) { + edgeChains = new InvocationFilterChains(filters); + return this; + } - if (filter.enabledForInvocationType(InvocationType.PROVIDER)) { - producerChains.addFilter(filter); - } - } + @Autowired + public FilterChainsManager setConsumerFilters(List filters) { + consumerChains = new InvocationFilterChains(filters); + return this; + } + @Autowired + public FilterChainsManager setProviderFilters(List filters) { + providerChains = new InvocationFilterChains(filters); return this; } @@ -54,24 +58,31 @@ public FilterNode findConsumerChain(String application, String serviceName) { } public FilterNode findProducerChain(String application, String serviceName) { - return producerChains.findChain(application, serviceName); + return providerChains.findChain(application, serviceName); + } + + public FilterNode findEdgeChain(String application, String serviceName) { + return edgeChains.findChain(application, serviceName); } public String collectResolvedChains() { StringBuilder sb = new StringBuilder(); appendLine(sb, "consumer: "); - appendLine(sb, " filters: %s", collectFilterNames(consumerChains, InvocationType.CONSUMER)); + appendLine(sb, " filters: %s", collectFilterNames(consumerChains)); appendLine(sb, "producer: "); - appendLine(sb, " filters: %s", collectFilterNames(producerChains, InvocationType.PROVIDER)); + appendLine(sb, " filters: %s", collectFilterNames(providerChains)); + + appendLine(sb, "edge: "); + appendLine(sb, " filters: %s", collectFilterNames(edgeChains)); return deleteLast(sb, 1).toString(); } - private List collectFilterNames(InvocationFilterChains chains, InvocationType invocationType) { + private List collectFilterNames(InvocationFilterChains chains) { return chains.getFilters().stream() - .map(filter -> filter.getName() + "(" + filter.getOrder(invocationType, null, null) + ")") + .map(filter -> filter.getName() + "(" + filter.getOrder() + ")") .collect(Collectors.toList()); } } diff --git a/core/src/main/java/org/apache/servicecomb/core/filter/FilterNode.java b/core/src/main/java/org/apache/servicecomb/core/filter/FilterNode.java index b5cb63c0195..acfdbb3151d 100644 --- a/core/src/main/java/org/apache/servicecomb/core/filter/FilterNode.java +++ b/core/src/main/java/org/apache/servicecomb/core/filter/FilterNode.java @@ -62,7 +62,9 @@ private void setNextNode(FilterNode nextNode) { } public CompletableFuture onFilter(Invocation invocation) { - if (!filter.enabledForTransport(invocation.getTransportName())) { + // When transport name is empty, maybe edge and transport filters need to be executed. + // And we can't set Endpoint before load balance in edge. + if (invocation.getTransportName() != null && !filter.enabledForTransport(invocation.getTransportName())) { return nextNode.onFilter(invocation); } diff --git a/core/src/main/java/org/apache/servicecomb/core/filter/InvocationFilterChains.java b/core/src/main/java/org/apache/servicecomb/core/filter/InvocationFilterChains.java index 064e4471b8a..9739f0b2dd7 100644 --- a/core/src/main/java/org/apache/servicecomb/core/filter/InvocationFilterChains.java +++ b/core/src/main/java/org/apache/servicecomb/core/filter/InvocationFilterChains.java @@ -16,44 +16,35 @@ */ package org.apache.servicecomb.core.filter; -import java.util.Collection; import java.util.Comparator; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import org.apache.servicecomb.swagger.invocation.InvocationType; +import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx; public class InvocationFilterChains { - private final Map filters = new HashMap<>(); + private final List filters; - private final Map microserviceChains = new HashMap<>(); + private final Map> microserviceChains = new ConcurrentHashMapEx<>(); - private final InvocationType invocationType; - - public InvocationFilterChains(InvocationType invocationType) { - this.invocationType = invocationType; - } - - public Collection getFilters() { - return filters.values(); + public InvocationFilterChains(List filters) { + this.filters = filters; } - public void addFilter(Filter filter) { - filters.put(filter.getName(), filter); + public List getFilters() { + return filters; } public FilterNode findChain(String application, String serviceName) { - FilterNode filterNode = microserviceChains.get(serviceName); + FilterNode filterNode = microserviceChains.computeIfAbsent(application, key -> new ConcurrentHashMapEx<>()) + .get(serviceName); if (filterNode == null) { - List serviceFilters = filters.entrySet().stream() - .filter(e -> e.getValue().enabledForMicroservice(application, serviceName)) - .map(e -> e.getValue()) - .collect(Collectors.toList()); - serviceFilters.sort(Comparator.comparingInt(a -> a.getOrder(invocationType, application, serviceName))); + List serviceFilters = filters.stream() + .filter(e -> e.enabledForMicroservice(application, serviceName)) + .sorted(Comparator.comparingInt(a -> a.getOrder(application, serviceName))).collect(Collectors.toList()); filterNode = FilterNode.buildChain(serviceFilters); - microserviceChains.put(serviceName, filterNode); + microserviceChains.get(application).put(serviceName, filterNode); } return filterNode; } diff --git a/core/src/main/java/org/apache/servicecomb/core/filter/ProviderFilter.java b/core/src/main/java/org/apache/servicecomb/core/filter/ProviderFilter.java index 7e1dbc318cb..6d5edd7814e 100644 --- a/core/src/main/java/org/apache/servicecomb/core/filter/ProviderFilter.java +++ b/core/src/main/java/org/apache/servicecomb/core/filter/ProviderFilter.java @@ -16,11 +16,6 @@ */ package org.apache.servicecomb.core.filter; -import org.apache.servicecomb.swagger.invocation.InvocationType; - public interface ProviderFilter extends Filter { - @Override - default boolean enabledForInvocationType(InvocationType invocationType) { - return invocationType == InvocationType.PROVIDER; - } + } diff --git a/core/src/main/java/org/apache/servicecomb/core/filter/impl/EmptyFilter.java b/core/src/main/java/org/apache/servicecomb/core/filter/impl/EmptyFilter.java index f611616b4e3..d2b8eb82319 100644 --- a/core/src/main/java/org/apache/servicecomb/core/filter/impl/EmptyFilter.java +++ b/core/src/main/java/org/apache/servicecomb/core/filter/impl/EmptyFilter.java @@ -19,12 +19,15 @@ import java.util.concurrent.CompletableFuture; import org.apache.servicecomb.core.Invocation; -import org.apache.servicecomb.core.filter.Filter; +import org.apache.servicecomb.core.filter.AbstractFilter; +import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.FilterNode; +import org.apache.servicecomb.core.filter.ProviderFilter; import org.apache.servicecomb.swagger.invocation.Response; // just for test -public class EmptyFilter implements Filter { +public class EmptyFilter extends AbstractFilter implements ProviderFilter, ConsumerFilter, EdgeFilter { @Override public String getName() { return "empty"; @@ -34,4 +37,9 @@ public String getName() { public CompletableFuture onFilter(Invocation invocation, FilterNode nextNode) { return CompletableFuture.completedFuture(Response.ok(null)); } + + @Override + public int getOrder() { + return 0; + } } diff --git a/core/src/main/java/org/apache/servicecomb/core/filter/impl/ParameterValidatorFilter.java b/core/src/main/java/org/apache/servicecomb/core/filter/impl/ParameterValidatorFilter.java index bce1b014de9..ccc5f043f56 100644 --- a/core/src/main/java/org/apache/servicecomb/core/filter/impl/ParameterValidatorFilter.java +++ b/core/src/main/java/org/apache/servicecomb/core/filter/impl/ParameterValidatorFilter.java @@ -21,12 +21,12 @@ import java.util.concurrent.CompletableFuture; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.core.filter.ProviderFilter; import org.apache.servicecomb.foundation.common.utils.AsyncUtils; import org.apache.servicecomb.swagger.engine.SwaggerProducerOperation; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.hibernate.validator.HibernateValidator; import org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator; @@ -35,8 +35,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.env.Environment; import jakarta.validation.ConstraintViolation; import jakarta.validation.ConstraintViolationException; @@ -45,7 +43,7 @@ import jakarta.validation.executable.ExecutableValidator; import jakarta.validation.groups.Default; -public class ParameterValidatorFilter implements ProviderFilter, InitializingBean { +public class ParameterValidatorFilter extends AbstractFilter implements ProviderFilter, InitializingBean { private static final Logger LOGGER = LoggerFactory.getLogger(ParameterValidatorFilter.class); public static final String NAME = "validator"; @@ -54,20 +52,13 @@ public class ParameterValidatorFilter implements ProviderFilter, InitializingBea protected ExecutableValidator validator; - private Environment environment; - - @Autowired - public void setEnvironment(Environment environment) { - this.environment = environment; - } - @Override public String getName() { return NAME; } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.PROVIDER_SCHEDULE_FILTER_ORDER + 1000; } diff --git a/core/src/main/java/org/apache/servicecomb/core/filter/impl/ProviderOperationFilter.java b/core/src/main/java/org/apache/servicecomb/core/filter/impl/ProviderOperationFilter.java index bfd06f80eea..a45e3d40204 100644 --- a/core/src/main/java/org/apache/servicecomb/core/filter/impl/ProviderOperationFilter.java +++ b/core/src/main/java/org/apache/servicecomb/core/filter/impl/ProviderOperationFilter.java @@ -21,16 +21,16 @@ import org.apache.servicecomb.core.Invocation; import org.apache.servicecomb.core.exception.Exceptions; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.core.filter.ProviderFilter; import org.apache.servicecomb.foundation.common.utils.AsyncUtils; import org.apache.servicecomb.swagger.engine.SwaggerProducerOperation; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.context.ContextUtils; -public class ProviderOperationFilter implements ProviderFilter { +public class ProviderOperationFilter extends AbstractFilter implements ProviderFilter { public static final String NAME = "producer-operation"; @Override @@ -39,7 +39,7 @@ public String getName() { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { // almost time, should be the last filter. return Filter.PROVIDER_SCHEDULE_FILTER_ORDER + 2000; } diff --git a/core/src/main/java/org/apache/servicecomb/core/filter/impl/ScheduleFilter.java b/core/src/main/java/org/apache/servicecomb/core/filter/impl/ScheduleFilter.java index eaec70975d1..d8069fe0ace 100644 --- a/core/src/main/java/org/apache/servicecomb/core/filter/impl/ScheduleFilter.java +++ b/core/src/main/java/org/apache/servicecomb/core/filter/impl/ScheduleFilter.java @@ -21,14 +21,14 @@ import org.apache.servicecomb.core.Invocation; import org.apache.servicecomb.core.exception.Exceptions; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.core.filter.ProviderFilter; import org.apache.servicecomb.core.invocation.InvocationStageTrace; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; -public class ScheduleFilter implements ProviderFilter { +public class ScheduleFilter extends AbstractFilter implements ProviderFilter { public static final String NAME = "schedule"; @Override @@ -37,7 +37,7 @@ public String getName() { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.PROVIDER_SCHEDULE_FILTER_ORDER; } diff --git a/core/src/main/java/org/apache/servicecomb/core/invocation/ProducerInvocationFlow.java b/core/src/main/java/org/apache/servicecomb/core/invocation/ProducerInvocationFlow.java index 6f4f8e2ff2d..ba82fb515f1 100644 --- a/core/src/main/java/org/apache/servicecomb/core/invocation/ProducerInvocationFlow.java +++ b/core/src/main/java/org/apache/servicecomb/core/invocation/ProducerInvocationFlow.java @@ -62,7 +62,14 @@ private void tryRunInvocation(Invocation invocation) { } invocation.onStart(requestEx, startTime); - invocation.getMicroserviceMeta().getFilterChain() + if (invocation.isEdge()) { + invocation.getMicroserviceMeta().getEdgeFilterChain() + .onFilter(invocation) + .whenComplete((response, Throwable) -> sendResponse(invocation, response)) + .whenComplete((response, Throwable) -> finishInvocation(invocation, response, Throwable)); + return; + } + invocation.getMicroserviceMeta().getProviderFilterChain() .onFilter(invocation) .whenComplete((response, Throwable) -> sendResponse(invocation, response)) .whenComplete((response, Throwable) -> finishInvocation(invocation, response, Throwable)); diff --git a/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java b/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java index 65d78c49012..49be3bd4f20 100644 --- a/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java +++ b/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java @@ -314,7 +314,13 @@ private static Supplier> invokeImpl(Invocation invocat invocation.onStart(null, System.nanoTime()); updateRetryStatus(invocation); invocation.onStartHandlersRequest(); - return invocation.getMicroserviceMeta().getFilterChain() + if (invocation.isEdge()) { + return invocation.getMicroserviceMeta().getEdgeFilterChain() + .onFilter(invocation) + .exceptionally(throwable -> toConsumerResponse(invocation, throwable)) + .whenComplete((response, throwable) -> finishInvocation(invocation, response)); + } + return invocation.getMicroserviceMeta().getConsumerFilterChain() .onFilter(invocation) .exceptionally(throwable -> toConsumerResponse(invocation, throwable)) .whenComplete((response, throwable) -> finishInvocation(invocation, response)); diff --git a/core/src/main/java/org/apache/servicecomb/core/provider/consumer/ReferenceConfigManager.java b/core/src/main/java/org/apache/servicecomb/core/provider/consumer/ReferenceConfigManager.java index 5a8d2775be6..577a16e2082 100644 --- a/core/src/main/java/org/apache/servicecomb/core/provider/consumer/ReferenceConfigManager.java +++ b/core/src/main/java/org/apache/servicecomb/core/provider/consumer/ReferenceConfigManager.java @@ -131,7 +131,10 @@ private MicroserviceReferenceConfig buildMicroserviceReferenceConfig(SCBEngine e String application, String microserviceName) { ConsumerMicroserviceVersionsMeta microserviceVersionsMeta = new ConsumerMicroserviceVersionsMeta(engine); MicroserviceMeta microserviceMeta = new MicroserviceMeta(engine, application, microserviceName, true); - microserviceMeta.setFilterChain(engine.getFilterChainsManager().findConsumerChain(application, microserviceName)); + microserviceMeta.setConsumerFilterChain(engine.getFilterChainsManager() + .findConsumerChain(application, microserviceName)); + microserviceMeta.setEdgeFilterChain(engine.getFilterChainsManager() + .findEdgeChain(application, microserviceName)); microserviceMeta.setMicroserviceVersionsMeta(microserviceVersionsMeta); Set schemaIds = this.openAPIRegistryManager.getSchemaIds(application, microserviceName); diff --git a/core/src/test/java/org/apache/servicecomb/core/filter/FilterChainTest.java b/core/src/test/java/org/apache/servicecomb/core/filter/FilterChainTest.java index 0903cc03c53..db4d2b15c32 100644 --- a/core/src/test/java/org/apache/servicecomb/core/filter/FilterChainTest.java +++ b/core/src/test/java/org/apache/servicecomb/core/filter/FilterChainTest.java @@ -21,12 +21,9 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; -import java.io.IOException; -import java.time.Duration; import java.util.List; import java.util.Vector; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -133,20 +130,4 @@ public void should_stop_chain_when_middle_filter_throw_exception() { .isInstanceOf(IllegalStateException.class) .hasMessage("e1"); } - - @Test - public void should_support_retry_logic() { - Filter exceptionFilter = (invocation, nextNode) -> { - throw new CompletionException(new IOException("net error")); - }; - SimpleRetryFilter retryFilter = new SimpleRetryFilter().setMaxRetry(3); - - CompletableFuture future = buildChain(retryFilter, recordThreadFilter, exceptionFilter) - .onFilter(invocation); - assertThat(future) - .failsWithin(Duration.ofSeconds(1)) - .withThrowableOfType(ExecutionException.class) - .withCauseExactlyInstanceOf(IOException.class) - .withMessage("java.io.IOException: net error"); - } } diff --git a/core/src/test/java/org/apache/servicecomb/core/filter/SimpleRetryFilter.java b/core/src/test/java/org/apache/servicecomb/core/filter/SimpleRetryFilter.java deleted file mode 100644 index 4eeb730260f..00000000000 --- a/core/src/test/java/org/apache/servicecomb/core/filter/SimpleRetryFilter.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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.filter; - -import java.io.IOException; -import java.util.concurrent.CompletableFuture; - -import org.apache.servicecomb.core.Invocation; -import org.apache.servicecomb.core.exception.Exceptions; -import org.apache.servicecomb.swagger.invocation.InvocationType; -import org.apache.servicecomb.swagger.invocation.Response; - -public class SimpleRetryFilter implements ConsumerFilter { - protected int maxRetry = 3; - - @Override - public String getName() { - return "simple-retry"; - } - - @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { - return Filter.CONSUMER_LOAD_BALANCE_ORDER - 900; - } - - public SimpleRetryFilter setMaxRetry(int maxRetry) { - this.maxRetry = maxRetry; - return this; - } - - @Override - public CompletableFuture onFilter(Invocation invocation, FilterNode nextNode) { - return new RetrySession(invocation, nextNode).run(); - } - - protected Throwable unwrapException(Throwable throwable) { - return Exceptions.unwrapIncludeInvocationException(throwable); - } - - protected boolean isRetryException(Throwable throwable) { - return !(throwable instanceof IOException); - } - - class RetrySession { - Invocation invocation; - - FilterNode nextNode; - - int retryCount; - - CompletableFuture future = new CompletableFuture<>(); - - RetrySession(Invocation invocation, FilterNode nextNode) { - this.invocation = invocation; - this.nextNode = nextNode; - } - - CompletableFuture run() { - nextNode.onFilter(invocation) - .whenComplete(this::whenNextComplete); - return future; - } - - private void whenNextComplete(Response response, Throwable throwable) { - if (throwable == null) { - future.complete(response); - return; - } - - Throwable unwrapped = unwrapException(throwable); - if (isRetryException(unwrapped)) { - future.completeExceptionally(throwable); - return; - } - - retryCount++; - if (retryCount >= maxRetry) { - future.completeExceptionally(throwable); - return; - } - - run(); - } - } -} diff --git a/core/src/test/java/org/apache/servicecomb/core/invocation/ProducerInvocationFlowTest.java b/core/src/test/java/org/apache/servicecomb/core/invocation/ProducerInvocationFlowTest.java index 1cc4b4db84a..559bedb7b40 100644 --- a/core/src/test/java/org/apache/servicecomb/core/invocation/ProducerInvocationFlowTest.java +++ b/core/src/test/java/org/apache/servicecomb/core/invocation/ProducerInvocationFlowTest.java @@ -79,7 +79,7 @@ public void should_send_exception_response_when_failed_to_create_invocation() { private void mockFilterChain() { new Expectations() { { - microserviceMeta.getFilterChain(); + microserviceMeta.getProviderFilterChain(); result = filterNode; } }; diff --git a/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/EncryptEdgeDispatcher.java b/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/EncryptEdgeDispatcher.java index 0cacd364f0e..76ea1840402 100644 --- a/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/EncryptEdgeDispatcher.java +++ b/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/EncryptEdgeDispatcher.java @@ -24,6 +24,7 @@ import org.apache.servicecomb.core.Invocation; import org.apache.servicecomb.core.invocation.InvocationCreator; import org.apache.servicecomb.demo.edge.authentication.encrypt.Hcr; +import org.apache.servicecomb.demo.edge.service.EdgeConst; import org.apache.servicecomb.edge.core.AbstractEdgeDispatcher; import org.apache.servicecomb.edge.core.EdgeInvocationCreator; import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx; @@ -122,7 +123,7 @@ public CompletableFuture createAsync() { return result.whenComplete((invocation, throwable) -> { if (throwable == null) { - invocation.getHandlerContext().put("encryptContext", encryptContext); + invocation.getHandlerContext().put(EdgeConst.ENCRYPT_CONTEXT, encryptContext); } }); } diff --git a/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/filter/DecodeBodyFilter.java b/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/filter/DecodeBodyFilter.java index 997ed53e686..d9e0f332318 100644 --- a/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/filter/DecodeBodyFilter.java +++ b/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/filter/DecodeBodyFilter.java @@ -21,13 +21,13 @@ import org.apache.servicecomb.common.rest.codec.RestObjectMapperFactory; import org.apache.servicecomb.core.Invocation; -import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.AbstractFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.demo.edge.authentication.encrypt.Hcr; import org.apache.servicecomb.demo.edge.service.EdgeConst; import org.apache.servicecomb.demo.edge.service.encrypt.EncryptContext; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.springframework.stereotype.Component; @@ -35,11 +35,11 @@ import com.fasterxml.jackson.databind.type.TypeFactory; @Component -public class DecodeBodyFilter implements ConsumerFilter { +public class DecodeBodyFilter extends AbstractFilter implements EdgeFilter { private JavaType bodyType = TypeFactory.defaultInstance().constructMapType(Map.class, String.class, String[].class); @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.CONSUMER_LOAD_BALANCE_ORDER - 1790; } diff --git a/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/filter/EdgeSignatureRequestFilter.java b/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/filter/EdgeSignatureRequestFilter.java index 4e33f979afd..42443b555b4 100644 --- a/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/filter/EdgeSignatureRequestFilter.java +++ b/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/filter/EdgeSignatureRequestFilter.java @@ -23,13 +23,13 @@ import java.util.concurrent.CompletableFuture; import org.apache.servicecomb.core.Invocation; -import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.AbstractFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.demo.edge.authentication.encrypt.Hcr; import org.apache.servicecomb.demo.edge.service.EdgeConst; import org.apache.servicecomb.demo.edge.service.encrypt.EncryptContext; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,11 +39,11 @@ import com.google.common.hash.Hashing; @Component -public class EdgeSignatureRequestFilter implements ConsumerFilter { +public class EdgeSignatureRequestFilter extends AbstractFilter implements EdgeFilter { private static final Logger LOGGER = LoggerFactory.getLogger(EdgeSignatureRequestFilter.class); @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.CONSUMER_LOAD_BALANCE_ORDER - 1800; } diff --git a/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/filter/EdgeSignatureResponseFilter.java b/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/filter/EdgeSignatureResponseFilter.java index 0b0ae444a34..a3909d75540 100644 --- a/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/filter/EdgeSignatureResponseFilter.java +++ b/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/filter/EdgeSignatureResponseFilter.java @@ -20,13 +20,13 @@ import java.util.concurrent.CompletableFuture; import org.apache.servicecomb.core.Invocation; -import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.AbstractFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.demo.edge.authentication.encrypt.Hcr; import org.apache.servicecomb.demo.edge.service.EdgeConst; import org.apache.servicecomb.demo.edge.service.encrypt.EncryptContext; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,17 +38,17 @@ import io.vertx.core.buffer.Buffer; @Component -public class EdgeSignatureResponseFilter implements ConsumerFilter { +public class EdgeSignatureResponseFilter extends AbstractFilter implements EdgeFilter { private static final Logger LOGGER = LoggerFactory.getLogger(EdgeSignatureResponseFilter.class); @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.CONSUMER_LOAD_BALANCE_ORDER + 1991; } @Override public String getName() { - return "test-edge-signature-request"; + return "test-edge-signature-response"; } @Override diff --git a/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/filter/UserIdFilter.java b/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/filter/UserIdFilter.java index f6a54cacbc0..fd20c0e6d7e 100644 --- a/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/filter/UserIdFilter.java +++ b/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/encrypt/filter/UserIdFilter.java @@ -19,19 +19,19 @@ import java.util.concurrent.CompletableFuture; import org.apache.servicecomb.core.Invocation; -import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.AbstractFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.demo.edge.service.EdgeConst; import org.apache.servicecomb.demo.edge.service.encrypt.EncryptContext; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.springframework.stereotype.Component; @Component -public class UserIdFilter implements ConsumerFilter { +public class UserIdFilter extends AbstractFilter implements EdgeFilter { @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.CONSUMER_LOAD_BALANCE_ORDER - 1790; } diff --git a/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/handler/AuthHandler.java b/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/handler/AuthHandler.java index ebe97f3bf85..4aeb44be08a 100644 --- a/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/handler/AuthHandler.java +++ b/demo/demo-edge/edge-service/src/main/java/org/apache/servicecomb/demo/edge/service/handler/AuthHandler.java @@ -20,12 +20,12 @@ import java.util.concurrent.CompletableFuture; import org.apache.servicecomb.core.Invocation; -import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.AbstractFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.demo.edge.service.EdgeConst; import org.apache.servicecomb.provider.pojo.Invoker; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; import org.slf4j.Logger; @@ -35,7 +35,7 @@ import jakarta.ws.rs.core.Response.Status; @Component -public class AuthHandler implements ConsumerFilter { +public class AuthHandler extends AbstractFilter implements EdgeFilter { private static Logger LOGGER = LoggerFactory.getLogger(AuthHandler.class); private static Auth auth; @@ -45,7 +45,7 @@ public class AuthHandler implements ConsumerFilter { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.CONSUMER_LOAD_BALANCE_ORDER - 1980; } diff --git a/demo/demo-spring-boot-transport/demo-spring-boot-pojo-server/src/main/java/org/apache/servicecomb/demo/springboot/pojo/server/handler/MyHandler.java b/demo/demo-spring-boot-transport/demo-spring-boot-pojo-server/src/main/java/org/apache/servicecomb/demo/springboot/pojo/server/handler/MyHandler.java index 1ec59f40b5a..09d2e2eb06d 100644 --- a/demo/demo-spring-boot-transport/demo-spring-boot-pojo-server/src/main/java/org/apache/servicecomb/demo/springboot/pojo/server/handler/MyHandler.java +++ b/demo/demo-spring-boot-transport/demo-spring-boot-pojo-server/src/main/java/org/apache/servicecomb/demo/springboot/pojo/server/handler/MyHandler.java @@ -20,25 +20,25 @@ import java.util.concurrent.CompletableFuture; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.core.filter.ProviderFilter; import org.apache.servicecomb.demo.springboot.pojo.server.schema.server.User; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Component -public class MyHandler implements ProviderFilter { +public class MyHandler extends AbstractFilter implements ProviderFilter { private static final Logger LOGGER = LoggerFactory.getLogger(MyHandler.class); public static final String SPLITPARAM_RESPONSE_USER_SUFFIX = "(modified by MyHandler)"; @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.PROVIDER_SCHEDULE_FILTER_ORDER - 100; } diff --git a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/filter/ProviderTestFilter.java b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/filter/ProviderTestFilter.java index 8c0e1f3f4ac..310f68e0ca0 100644 --- a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/filter/ProviderTestFilter.java +++ b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/filter/ProviderTestFilter.java @@ -20,17 +20,17 @@ import java.util.concurrent.CompletableFuture; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.core.filter.ProviderFilter; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.springframework.stereotype.Component; @Component -public class ProviderTestFilter implements ProviderFilter { +public class ProviderTestFilter extends AbstractFilter implements ProviderFilter { @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.PROVIDER_SCHEDULE_FILTER_ORDER - 1800; } diff --git a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeAddHeaderFilter.java b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeAddHeaderFilter.java index 5ba63b04a53..a0897b87661 100644 --- a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeAddHeaderFilter.java +++ b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeAddHeaderFilter.java @@ -25,23 +25,19 @@ import org.apache.commons.lang3.StringUtils; import org.apache.servicecomb.config.ConfigurationChangedEvent; import org.apache.servicecomb.core.Invocation; -import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.AbstractFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.foundation.common.event.EventManager; import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.transport.rest.client.RestClientTransportContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.core.env.Environment; import com.google.common.eventbus.Subscribe; -public class EdgeAddHeaderFilter implements ConsumerFilter { - private static final Logger LOGGER = LoggerFactory.getLogger(EdgeAddHeaderFilter.class); - +public class EdgeAddHeaderFilter extends AbstractFilter implements EdgeFilter { public static final String NAME = "edge-add-headers"; private static final String PREFIX = "servicecomb.edge.filter.addHeader"; @@ -63,6 +59,7 @@ public EdgeAddHeaderFilter(Environment environment) { } @Subscribe + @SuppressWarnings("unused") public void onConfigurationChangedEvent(ConfigurationChangedEvent event) { for (String changed : event.getChanged()) { if (changed.startsWith(PREFIX)) { @@ -92,27 +89,19 @@ public boolean enabledForTransport(String transport) { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.CONSUMER_LOAD_BALANCE_ORDER + 1991; } @Override public CompletableFuture onFilter(Invocation invocation, FilterNode nextNode) { RestClientTransportContext transportContext = invocation.getTransportContext(); - if (transportContext != null) { - return CompletableFuture.completedFuture(null) - .thenAccept(v -> addHeaders(invocation, transportContext.getHttpClientRequest()::putHeader)) - .thenCompose(v -> nextNode.onFilter(invocation)); - } - // normal consumer in edge process - return nextNode.onFilter(invocation); + return CompletableFuture.completedFuture(null) + .thenAccept(v -> addHeaders(invocation, transportContext.getHttpClientRequest()::putHeader)) + .thenCompose(v -> nextNode.onFilter(invocation)); } public void addHeaders(Invocation invocation, BiConsumer headerAdder) { - if (!invocation.isEdge()) { - return; - } - HttpServletRequestEx oldRequest = invocation.getRequestEx(); publicHeaders.forEach(key -> { String value = oldRequest.getHeader(key); diff --git a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeCoreConfiguration.java b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeCoreConfiguration.java index 45333b7d04f..5f6adc097d8 100644 --- a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeCoreConfiguration.java +++ b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeCoreConfiguration.java @@ -27,11 +27,6 @@ public EdgeBootListener edgeBootListener() { return new EdgeBootListener(); } - @Bean - public EdgeServerCodecFilter edgeServerCodecFilter() { - return new EdgeServerCodecFilter(); - } - @Bean public EdgeAddHeaderFilter edgeAddHeaderFilter(Environment environment) { return new EdgeAddHeaderFilter(environment); diff --git a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeInvocationCreator.java b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeInvocationCreator.java index 82e47283be1..587811f54c6 100644 --- a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeInvocationCreator.java +++ b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeInvocationCreator.java @@ -21,6 +21,7 @@ import org.apache.servicecomb.common.rest.RestVertxProducerInvocationCreator; import org.apache.servicecomb.common.rest.locator.OperationLocator; import org.apache.servicecomb.common.rest.locator.ServicePathManager; +import org.apache.servicecomb.core.CoreConst; import org.apache.servicecomb.core.Invocation; import org.apache.servicecomb.core.SCBEngine; import org.apache.servicecomb.core.invocation.InvocationFactory; @@ -44,7 +45,10 @@ public class EdgeInvocationCreator extends RestVertxProducerInvocationCreator { public EdgeInvocationCreator(RoutingContext routingContext, HttpServletRequestEx requestEx, HttpServletResponseEx responseEx, String microserviceName, String path) { - super(routingContext, null, null, requestEx, responseEx); + // Set endpoint before load balance because edge service will use RESTFUL transport filters. + super(routingContext, null, + SCBEngine.getInstance().getTransportManager().findTransport(CoreConst.RESTFUL).getEndpoint(), + requestEx, responseEx); this.microserviceName = microserviceName; this.path = path; diff --git a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeServerCodecFilter.java b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeServerCodecFilter.java deleted file mode 100644 index cef9257b3cb..00000000000 --- a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeServerCodecFilter.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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.edge.core; - -import java.util.concurrent.CompletableFuture; - -import org.apache.servicecomb.common.rest.filter.inner.RestServerCodecFilter; -import org.apache.servicecomb.core.Invocation; -import org.apache.servicecomb.core.filter.Filter; -import org.apache.servicecomb.core.filter.FilterNode; -import org.apache.servicecomb.swagger.invocation.InvocationType; -import org.apache.servicecomb.swagger.invocation.Response; -import org.apache.servicecomb.swagger.invocation.context.TransportContext; - -public class EdgeServerCodecFilter extends RestServerCodecFilter { - public static final String NAME = "edge-server-codec"; - - @Override - public String getName() { - return NAME; - } - - @Override - public boolean enabledForInvocationType(InvocationType invocationType) { - return invocationType == InvocationType.CONSUMER; - } - - @Override - public boolean enabledForTransport(String transport) { - // For edge service, this filter executed before load balancer and transport is always null. - return true; - } - - @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { - return Filter.CONSUMER_LOAD_BALANCE_ORDER - 2000; - } - - @Override - public CompletableFuture onFilter(Invocation invocation, FilterNode nextNode) { - if (invocation.getRequestEx() == null) { - // to support normal consumer invocation in edge process - return nextNode.onFilter(invocation); - } - - return super.onFilter(invocation, nextNode); - } - - // save and restore transportContext to support edge invocation - @Override - protected CompletableFuture invokeNext(Invocation invocation, FilterNode nextNode) { - TransportContext transportContext = invocation.getTransportContext(); - return nextNode.onFilter(invocation) - .whenComplete((r, e) -> invocation.setTransportContext(transportContext)); - } -} diff --git a/handlers/handler-fault-injection/src/main/java/org/apache/servicecomb/faultinjection/ConsumerAbortFaultFilter.java b/handlers/handler-fault-injection/src/main/java/org/apache/servicecomb/faultinjection/ConsumerAbortFaultFilter.java index c13f1b8c2c0..e4eef91ef2f 100644 --- a/handlers/handler-fault-injection/src/main/java/org/apache/servicecomb/faultinjection/ConsumerAbortFaultFilter.java +++ b/handlers/handler-fault-injection/src/main/java/org/apache/servicecomb/faultinjection/ConsumerAbortFaultFilter.java @@ -19,17 +19,18 @@ import java.util.concurrent.CompletableFuture; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class ConsumerAbortFaultFilter implements ConsumerFilter { +public class ConsumerAbortFaultFilter extends AbstractFilter implements ConsumerFilter, EdgeFilter { private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerAbortFaultFilter.class); private static final String SUCCESS_RESPONSE = "success"; @@ -37,7 +38,7 @@ public class ConsumerAbortFaultFilter implements ConsumerFilter { public static final String ABORTED_ERROR_MSG = "aborted by fault inject"; @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.CONSUMER_LOAD_BALANCE_ORDER + 1020; } diff --git a/handlers/handler-fault-injection/src/main/java/org/apache/servicecomb/faultinjection/ConsumerDelayFaultFilter.java b/handlers/handler-fault-injection/src/main/java/org/apache/servicecomb/faultinjection/ConsumerDelayFaultFilter.java index 248ed748cfb..e8c0f6ffd67 100644 --- a/handlers/handler-fault-injection/src/main/java/org/apache/servicecomb/faultinjection/ConsumerDelayFaultFilter.java +++ b/handlers/handler-fault-injection/src/main/java/org/apache/servicecomb/faultinjection/ConsumerDelayFaultFilter.java @@ -19,10 +19,11 @@ import java.util.concurrent.CompletableFuture; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,11 +31,11 @@ import io.vertx.core.Context; import io.vertx.core.Vertx; -public class ConsumerDelayFaultFilter implements ConsumerFilter { +public class ConsumerDelayFaultFilter extends AbstractFilter implements ConsumerFilter, EdgeFilter { private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerDelayFaultFilter.class); @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.CONSUMER_LOAD_BALANCE_ORDER + 1030; } diff --git a/handlers/handler-flowcontrol-qps/src/main/java/org/apache/servicecomb/qps/ConsumerFlowControlFilter.java b/handlers/handler-flowcontrol-qps/src/main/java/org/apache/servicecomb/qps/ConsumerFlowControlFilter.java index b5fe884d93a..72828b35ccf 100644 --- a/handlers/handler-flowcontrol-qps/src/main/java/org/apache/servicecomb/qps/ConsumerFlowControlFilter.java +++ b/handlers/handler-flowcontrol-qps/src/main/java/org/apache/servicecomb/qps/ConsumerFlowControlFilter.java @@ -20,10 +20,11 @@ import java.util.concurrent.CompletableFuture; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; @@ -31,7 +32,7 @@ import com.google.common.annotations.VisibleForTesting; -public class ConsumerFlowControlFilter implements ConsumerFilter { +public class ConsumerFlowControlFilter extends AbstractFilter implements ConsumerFilter, EdgeFilter { private final QpsControllerManager qpsControllerMgr; public ConsumerFlowControlFilter(Environment environment) { @@ -44,7 +45,7 @@ public QpsControllerManager getQpsControllerMgr() { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.CONSUMER_LOAD_BALANCE_ORDER - 1990; } diff --git a/handlers/handler-flowcontrol-qps/src/main/java/org/apache/servicecomb/qps/ProviderFlowControlFilter.java b/handlers/handler-flowcontrol-qps/src/main/java/org/apache/servicecomb/qps/ProviderFlowControlFilter.java index ef9610d30ce..00f78f126f1 100644 --- a/handlers/handler-flowcontrol-qps/src/main/java/org/apache/servicecomb/qps/ProviderFlowControlFilter.java +++ b/handlers/handler-flowcontrol-qps/src/main/java/org/apache/servicecomb/qps/ProviderFlowControlFilter.java @@ -21,10 +21,10 @@ import org.apache.servicecomb.core.CoreConst; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.core.filter.ProviderFilter; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; @@ -32,7 +32,7 @@ import com.google.common.annotations.VisibleForTesting; -public class ProviderFlowControlFilter implements ProviderFilter { +public class ProviderFlowControlFilter extends AbstractFilter implements ProviderFilter { private final QpsControllerManager qpsControllerMgr; public ProviderFlowControlFilter(Environment environment) { @@ -40,7 +40,7 @@ public ProviderFlowControlFilter(Environment environment) { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.PROVIDER_SCHEDULE_FILTER_ORDER - 1990; } diff --git a/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ConsumerInstanceBulkheadFilter.java b/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ConsumerInstanceBulkheadFilter.java index 8d8fd4c94c7..1b007e111ad 100644 --- a/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ConsumerInstanceBulkheadFilter.java +++ b/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ConsumerInstanceBulkheadFilter.java @@ -22,13 +22,14 @@ import java.util.function.Supplier; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.core.governance.MatchType; import org.apache.servicecomb.governance.handler.InstanceBulkheadHandler; import org.apache.servicecomb.governance.marker.GovernanceRequestExtractor; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; @@ -42,7 +43,7 @@ import io.github.resilience4j.decorators.Decorators.DecorateCompletionStage; import jakarta.ws.rs.core.Response.Status; -public class ConsumerInstanceBulkheadFilter implements ConsumerFilter { +public class ConsumerInstanceBulkheadFilter extends AbstractFilter implements ConsumerFilter, EdgeFilter { private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerInstanceBulkheadFilter.class); private final InstanceBulkheadHandler instanceBulkheadHandler; @@ -53,7 +54,7 @@ public ConsumerInstanceBulkheadFilter(InstanceBulkheadHandler instanceBulkheadHa } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.CONSUMER_LOAD_BALANCE_ORDER + 1060; } diff --git a/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ConsumerInstanceIsolationFilter.java b/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ConsumerInstanceIsolationFilter.java index 875e53b75d4..e60d52fab18 100644 --- a/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ConsumerInstanceIsolationFilter.java +++ b/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ConsumerInstanceIsolationFilter.java @@ -23,7 +23,9 @@ import java.util.function.Supplier; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.core.governance.MatchType; @@ -31,7 +33,6 @@ import org.apache.servicecomb.governance.marker.GovernanceRequestExtractor; import org.apache.servicecomb.governance.policy.CircuitBreakerPolicy; import org.apache.servicecomb.registry.DiscoveryManager; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; @@ -45,7 +46,7 @@ import io.github.resilience4j.decorators.Decorators.DecorateCompletionStage; import jakarta.ws.rs.core.Response.Status; -public class ConsumerInstanceIsolationFilter implements ConsumerFilter { +public class ConsumerInstanceIsolationFilter extends AbstractFilter implements ConsumerFilter, EdgeFilter { private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerInstanceIsolationFilter.class); private final InstanceIsolationHandler instanceIsolationHandler; @@ -63,7 +64,7 @@ public void setDiscoveryManager(DiscoveryManager discoveryManager) { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.CONSUMER_LOAD_BALANCE_ORDER + 1050; } diff --git a/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ProviderBulkheadFilter.java b/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ProviderBulkheadFilter.java index c526916b0aa..a68ebf08fb1 100644 --- a/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ProviderBulkheadFilter.java +++ b/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ProviderBulkheadFilter.java @@ -22,13 +22,13 @@ import java.util.function.Supplier; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.core.filter.ProviderFilter; import org.apache.servicecomb.core.governance.MatchType; import org.apache.servicecomb.governance.handler.BulkheadHandler; import org.apache.servicecomb.governance.marker.GovernanceRequestExtractor; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; @@ -41,7 +41,7 @@ import io.github.resilience4j.decorators.Decorators; import io.github.resilience4j.decorators.Decorators.DecorateCompletionStage; -public class ProviderBulkheadFilter implements ProviderFilter { +public class ProviderBulkheadFilter extends AbstractFilter implements ProviderFilter { private static final Logger LOGGER = LoggerFactory.getLogger(ProviderBulkheadFilter.class); private final BulkheadHandler bulkheadHandler; @@ -52,7 +52,7 @@ public ProviderBulkheadFilter(BulkheadHandler bulkheadHandler) { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.PROVIDER_SCHEDULE_FILTER_ORDER - 1880; } diff --git a/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ProviderCircuitBreakerFilter.java b/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ProviderCircuitBreakerFilter.java index 0c5e2ed33de..dbc0b8978c6 100644 --- a/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ProviderCircuitBreakerFilter.java +++ b/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ProviderCircuitBreakerFilter.java @@ -22,13 +22,13 @@ import java.util.function.Supplier; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.core.filter.ProviderFilter; import org.apache.servicecomb.core.governance.MatchType; import org.apache.servicecomb.governance.handler.CircuitBreakerHandler; import org.apache.servicecomb.governance.marker.GovernanceRequestExtractor; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; @@ -41,7 +41,7 @@ import io.github.resilience4j.decorators.Decorators; import io.github.resilience4j.decorators.Decorators.DecorateCompletionStage; -public class ProviderCircuitBreakerFilter implements ProviderFilter { +public class ProviderCircuitBreakerFilter extends AbstractFilter implements ProviderFilter { private static final Logger LOGGER = LoggerFactory.getLogger(ProviderCircuitBreakerFilter.class); private final CircuitBreakerHandler circuitBreakerHandler; @@ -52,7 +52,7 @@ public ProviderCircuitBreakerFilter(CircuitBreakerHandler circuitBreakerHandler) } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.PROVIDER_SCHEDULE_FILTER_ORDER - 1890; } diff --git a/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ProviderRateLimitingFilter.java b/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ProviderRateLimitingFilter.java index 63db4cd7d93..ee435f3fb91 100644 --- a/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ProviderRateLimitingFilter.java +++ b/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ProviderRateLimitingFilter.java @@ -22,13 +22,13 @@ import java.util.function.Supplier; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.core.filter.ProviderFilter; import org.apache.servicecomb.core.governance.MatchType; import org.apache.servicecomb.governance.handler.RateLimitingHandler; import org.apache.servicecomb.governance.marker.GovernanceRequestExtractor; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; @@ -41,7 +41,7 @@ import io.github.resilience4j.ratelimiter.RateLimiter; import io.github.resilience4j.ratelimiter.RequestNotPermitted; -public class ProviderRateLimitingFilter implements ProviderFilter { +public class ProviderRateLimitingFilter extends AbstractFilter implements ProviderFilter { private static final Logger LOGGER = LoggerFactory.getLogger(ProviderRateLimitingFilter.class); private final RateLimitingHandler rateLimitingHandler; @@ -52,7 +52,7 @@ public ProviderRateLimitingFilter(RateLimitingHandler rateLimitingHandler) { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.PROVIDER_SCHEDULE_FILTER_ORDER - 1900; } diff --git a/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadBalanceConfiguration.java b/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadBalanceConfiguration.java index d0b2f087b5d..f7042ef7474 100644 --- a/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadBalanceConfiguration.java +++ b/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadBalanceConfiguration.java @@ -19,7 +19,6 @@ import java.util.List; import org.apache.servicecomb.core.SCBEngine; -import org.apache.servicecomb.core.filter.ConsumerFilter; import org.apache.servicecomb.loadbalance.filter.InstancePropertyDiscoveryFilter; import org.apache.servicecomb.loadbalance.filter.PriorityInstancePropertyDiscoveryFilter; import org.apache.servicecomb.loadbalance.filter.ServerDiscoveryFilter; @@ -38,7 +37,7 @@ public class LoadBalanceConfiguration { public static final String LOAD_BALANCE_ENABLED = LOAD_BALANCE_PREFIX + ".enabled"; @Bean - public ConsumerFilter loadBalanceFilter(ExtensionsManager extensionsManager, DiscoveryTree discoveryTree, + public LoadBalanceFilter loadBalanceFilter(ExtensionsManager extensionsManager, DiscoveryTree discoveryTree, SCBEngine scbEngine) { return new LoadBalanceFilter(extensionsManager, discoveryTree, scbEngine); } diff --git a/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadBalanceFilter.java b/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadBalanceFilter.java index e7d32844cc3..88e58e283be 100644 --- a/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadBalanceFilter.java +++ b/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadBalanceFilter.java @@ -27,7 +27,9 @@ import org.apache.servicecomb.core.Invocation; import org.apache.servicecomb.core.SCBEngine; import org.apache.servicecomb.core.Transport; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.core.governance.RetryContext; @@ -35,7 +37,6 @@ import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx; import org.apache.servicecomb.registry.discovery.DiscoveryContext; import org.apache.servicecomb.registry.discovery.DiscoveryTree; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.exception.ExceptionFactory; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; @@ -48,7 +49,7 @@ import io.github.resilience4j.core.metrics.Metrics.Outcome; import jakarta.ws.rs.core.Response.Status; -public class LoadBalanceFilter implements ConsumerFilter { +public class LoadBalanceFilter extends AbstractFilter implements ConsumerFilter, EdgeFilter { public static final String CONTEXT_KEY_LAST_SERVER = "x-context-last-server"; // Enough times to make sure to choose a different server in high volume. @@ -109,7 +110,7 @@ private void preCheck(SCBEngine scbEngine) { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.CONSUMER_LOAD_BALANCE_ORDER; } diff --git a/handlers/handler-publickey-auth/src/main/java/org/apache/servicecomb/authentication/consumer/ConsumerAuthFilter.java b/handlers/handler-publickey-auth/src/main/java/org/apache/servicecomb/authentication/consumer/ConsumerAuthFilter.java index 6957dc1b786..e26f24d936a 100644 --- a/handlers/handler-publickey-auth/src/main/java/org/apache/servicecomb/authentication/consumer/ConsumerAuthFilter.java +++ b/handlers/handler-publickey-auth/src/main/java/org/apache/servicecomb/authentication/consumer/ConsumerAuthFilter.java @@ -21,17 +21,18 @@ import org.apache.servicecomb.core.CoreConst; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; import org.springframework.beans.factory.annotation.Autowired; import jakarta.ws.rs.core.Response.Status; -public class ConsumerAuthFilter implements ConsumerFilter { +public class ConsumerAuthFilter extends AbstractFilter implements ConsumerFilter, EdgeFilter { private ConsumerTokenManager authenticationTokenManager; @Autowired @@ -40,7 +41,7 @@ public void setConsumerTokenManager(ConsumerTokenManager consumerTokenManager) { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.CONSUMER_LOAD_BALANCE_ORDER + 1010; } diff --git a/handlers/handler-publickey-auth/src/main/java/org/apache/servicecomb/authentication/provider/ProviderAuthFilter.java b/handlers/handler-publickey-auth/src/main/java/org/apache/servicecomb/authentication/provider/ProviderAuthFilter.java index 05374501653..93563f40317 100644 --- a/handlers/handler-publickey-auth/src/main/java/org/apache/servicecomb/authentication/provider/ProviderAuthFilter.java +++ b/handlers/handler-publickey-auth/src/main/java/org/apache/servicecomb/authentication/provider/ProviderAuthFilter.java @@ -20,17 +20,17 @@ import org.apache.servicecomb.core.CoreConst; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.core.filter.ProviderFilter; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; import org.springframework.beans.factory.annotation.Autowired; import jakarta.ws.rs.core.Response.Status; -public class ProviderAuthFilter implements ProviderFilter { +public class ProviderAuthFilter extends AbstractFilter implements ProviderFilter { private ProviderTokenManager authenticationTokenManager; @Autowired @@ -39,7 +39,7 @@ public void setProviderTokenManager(ProviderTokenManager providerTokenManager) { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.PROVIDER_SCHEDULE_FILTER_ORDER + 1010; } diff --git a/handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/RouterAddHeaderFilter.java b/handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/RouterAddHeaderFilter.java index bf7dbeb7eda..f99556dfb1a 100644 --- a/handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/RouterAddHeaderFilter.java +++ b/handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/RouterAddHeaderFilter.java @@ -25,20 +25,20 @@ import org.apache.commons.lang3.StringUtils; import org.apache.servicecomb.core.Invocation; -import org.apache.servicecomb.core.filter.Filter; +import org.apache.servicecomb.core.filter.AbstractFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.core.filter.ProviderFilter; import org.apache.servicecomb.foundation.common.LegacyPropertyFactory; import org.apache.servicecomb.foundation.common.utils.JsonUtils; import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.fasterxml.jackson.core.JsonProcessingException; -public class RouterAddHeaderFilter implements Filter { +public class RouterAddHeaderFilter extends AbstractFilter implements ProviderFilter, EdgeFilter { private static final Logger LOGGER = LoggerFactory.getLogger(RouterAddHeaderFilter.class); @@ -80,16 +80,10 @@ private Map getHeaderMap(HttpServletRequestEx httpServletRequest } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return ProviderFilter.PROVIDER_SCHEDULE_FILTER_ORDER - 1970; } - @Override - public boolean enabledForInvocationType(InvocationType invocationType) { - // enable for both edge and producer - return true; - } - @Override public String getName() { return "router-add-header"; @@ -97,10 +91,6 @@ public String getName() { @Override public CompletableFuture onFilter(Invocation invocation, FilterNode nextNode) { - if (!invocation.isEdge() && !invocation.isProducer()) { - return nextNode.onFilter(invocation); - } - if (!StringUtils.isEmpty(invocation.getContext(RouterServerListFilter.ROUTER_HEADER))) { return nextNode.onFilter(invocation); } diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/InvocationType.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/InvocationType.java index e519c28f007..351c876de71 100644 --- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/InvocationType.java +++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/InvocationType.java @@ -19,5 +19,6 @@ public enum InvocationType { CONSUMER, - PROVIDER + PROVIDER, + EDGE } diff --git a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayClientFilter.java b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayClientFilter.java index 82d0348c440..8fe679d8e67 100644 --- a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayClientFilter.java +++ b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayClientFilter.java @@ -23,18 +23,19 @@ import org.apache.servicecomb.core.CoreConst; import org.apache.servicecomb.core.Invocation; import org.apache.servicecomb.core.exception.Exceptions; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.foundation.common.utils.AsyncUtils; import org.apache.servicecomb.foundation.vertx.client.tcp.TcpData; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class HighwayClientFilter implements ConsumerFilter { +public class HighwayClientFilter extends AbstractFilter implements ConsumerFilter, EdgeFilter { private static final Logger LOGGER = LoggerFactory.getLogger(HighwayClientFilter.class); public static final String NAME = "highway-client"; @@ -50,7 +51,7 @@ public boolean enabledForTransport(String transport) { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.CONSUMER_LOAD_BALANCE_ORDER + 2000; } diff --git a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayServerCodecFilter.java b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayServerCodecFilter.java index 87d620156d6..aee833925a8 100644 --- a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayServerCodecFilter.java +++ b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayServerCodecFilter.java @@ -24,17 +24,17 @@ import org.apache.servicecomb.codec.protobuf.definition.ResponseRootSerializer; import org.apache.servicecomb.core.CoreConst; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; import org.apache.servicecomb.core.filter.ProviderFilter; import org.apache.servicecomb.foundation.common.utils.AsyncUtils; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.transport.highway.message.ResponseHeader; import io.vertx.core.buffer.Buffer; -public class HighwayServerCodecFilter implements ProviderFilter { +public class HighwayServerCodecFilter extends AbstractFilter implements ProviderFilter { public static final String NAME = "highway-server-codec"; @Override @@ -43,7 +43,7 @@ public String getName() { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { // almost time, should be the first filter. return Filter.PROVIDER_SCHEDULE_FILTER_ORDER - 2000; } diff --git a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientCodecFilter.java b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientCodecFilter.java index e77f58e99df..20a2d6c8dbf 100644 --- a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientCodecFilter.java +++ b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientCodecFilter.java @@ -23,16 +23,17 @@ import org.apache.servicecomb.common.rest.RestConst; import org.apache.servicecomb.core.CoreConst; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; import org.springframework.beans.factory.annotation.Autowired; import io.vertx.core.http.HttpClientRequest; -public class RestClientCodecFilter implements ConsumerFilter { +public class RestClientCodecFilter extends AbstractFilter implements ConsumerFilter, EdgeFilter { public static final String NAME = "rest-client-codec"; protected RestClientTransportContextFactory transportContextFactory; @@ -52,7 +53,7 @@ public boolean enabledForTransport(String transport) { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.CONSUMER_LOAD_BALANCE_ORDER + 1990; } diff --git a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientSenderFilter.java b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientSenderFilter.java index 8ee02c36039..cf088c764eb 100644 --- a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientSenderFilter.java +++ b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientSenderFilter.java @@ -20,13 +20,14 @@ import org.apache.servicecomb.core.CoreConst; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.filter.AbstractFilter; import org.apache.servicecomb.core.filter.ConsumerFilter; +import org.apache.servicecomb.core.filter.EdgeFilter; import org.apache.servicecomb.core.filter.Filter; import org.apache.servicecomb.core.filter.FilterNode; -import org.apache.servicecomb.swagger.invocation.InvocationType; import org.apache.servicecomb.swagger.invocation.Response; -public class RestClientSenderFilter implements ConsumerFilter { +public class RestClientSenderFilter extends AbstractFilter implements ConsumerFilter, EdgeFilter { public static final String NAME = "rest-client-sender"; @Override @@ -40,7 +41,7 @@ public boolean enabledForTransport(String transport) { } @Override - public int getOrder(InvocationType invocationType, String application, String serviceName) { + public int getOrder() { return Filter.CONSUMER_LOAD_BALANCE_ORDER + 2000; } diff --git a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/TransportRestClientConfiguration.java b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/TransportRestClientConfiguration.java index f510cf2e7d2..2b35eb4ca5b 100644 --- a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/TransportRestClientConfiguration.java +++ b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/TransportRestClientConfiguration.java @@ -16,14 +16,13 @@ */ package org.apache.servicecomb.transport.rest.client; -import org.apache.servicecomb.core.filter.ConsumerFilter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class TransportRestClientConfiguration { @Bean - public ConsumerFilter restClientSenderFilter() { + public RestClientSenderFilter restClientSenderFilter() { return new RestClientSenderFilter(); }