This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push:
new 3432d744c [SCB-2628] remove invocation context for governance (#3167)
3432d744c is described below
commit 3432d744ce4398501bb5b96ff6db57d23203ab40
Author: liubao68 <[email protected]>
AuthorDate: Fri Jul 1 09:15:18 2022 +0800
[SCB-2628] remove invocation context for governance (#3167)
---
.../governance/ServiceCombInvocationContext.java | 60 ----------------------
.../core/provider/consumer/InvokerUtils.java | 22 ++------
.../governance/GovernanceConfiguration.java | 4 +-
.../servicecomb/governance/InvocationContext.java | 26 ----------
.../servicecomb/governance/MatchersManager.java | 15 +-----
.../governance/InstanceIsolationTest.java | 1 +
.../servicecomb/governance/MockConfiguration.java | 5 --
.../governance/MockInvocationContext.java | 45 ----------------
.../governance/ProviderGovernanceHandler.java | 12 ++---
9 files changed, 11 insertions(+), 179 deletions(-)
diff --git
a/core/src/main/java/org/apache/servicecomb/core/governance/ServiceCombInvocationContext.java
b/core/src/main/java/org/apache/servicecomb/core/governance/ServiceCombInvocationContext.java
deleted file mode 100644
index 38c1243e1..000000000
---
a/core/src/main/java/org/apache/servicecomb/core/governance/ServiceCombInvocationContext.java
+++ /dev/null
@@ -1,60 +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.governance;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.servicecomb.governance.InvocationContext;
-import org.springframework.stereotype.Component;
-
-@Component
-public class ServiceCombInvocationContext implements InvocationContext {
- private static final String CONTEXT_KEY = "x-servicecomb-governance-match";
-
- private static final
ThreadLocal<org.apache.servicecomb.swagger.invocation.context.InvocationContext>
contextMgr = new ThreadLocal<>();
-
- public static void setInvocationContext(
- org.apache.servicecomb.swagger.invocation.context.InvocationContext
invocationContext) {
- contextMgr.set(invocationContext);
- }
-
- public static void removeInvocationContext() {
- contextMgr.remove();
- }
-
- @Override
- public Map<String, Boolean> getCalculatedMatches() {
- Map<String, Boolean> result =
contextMgr.get().getLocalContext(CONTEXT_KEY);
- if (result == null) {
- return Collections.emptyMap();
- }
- return result;
- }
-
- @Override
- public void addMatch(String key, Boolean value) {
- Map<String, Boolean> result =
contextMgr.get().getLocalContext(CONTEXT_KEY);
- if (result == null) {
- result = new HashMap<>();
- contextMgr.get().addLocalContext(CONTEXT_KEY, result);
- }
- result.put(key, value);
- }
-}
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 ac5fd41af..a1df058d0 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
@@ -44,7 +44,6 @@ import org.apache.servicecomb.core.definition.SchemaMeta;
import org.apache.servicecomb.core.governance.GovernanceConfiguration;
import org.apache.servicecomb.core.governance.MatchType;
import org.apache.servicecomb.core.governance.RetryContext;
-import org.apache.servicecomb.core.governance.ServiceCombInvocationContext;
import org.apache.servicecomb.core.invocation.InvocationFactory;
import org.apache.servicecomb.foundation.common.utils.AsyncUtils;
import org.apache.servicecomb.foundation.common.utils.BeanUtils;
@@ -193,12 +192,7 @@ public final class InvokerUtils {
public static Response innerSyncInvoke(Invocation invocation) {
GovernanceRequest request = MatchType.createGovHttpRequest(invocation);
- try {
- ServiceCombInvocationContext.setInvocationContext(invocation);
- return decorateSyncRetry(invocation, request);
- } finally {
- ServiceCombInvocationContext.removeInvocationContext();
- }
+ return decorateSyncRetry(invocation, request);
}
private static Response innerSyncInvokeImpl(Invocation invocation) throws
Throwable {
@@ -314,12 +308,7 @@ public final class InvokerUtils {
DecorateCompletionStage<Response> dcs = Decorators.ofCompletionStage(next);
GovernanceRequest request = MatchType.createGovHttpRequest(invocation);
- try {
- ServiceCombInvocationContext.setInvocationContext(invocation);
- decorateReactiveRetry(invocation, dcs, request);
- } finally {
- ServiceCombInvocationContext.removeInvocationContext();
- }
+ decorateReactiveRetry(invocation, dcs, request);
dcs.get().whenComplete((r, e) -> {
if (e == null) {
@@ -415,12 +404,7 @@ public final class InvokerUtils {
DecorateCompletionStage<Response> dcs = Decorators.ofCompletionStage(next);
GovernanceRequest request = MatchType.createGovHttpRequest(invocation);
- try {
- ServiceCombInvocationContext.setInvocationContext(invocation);
- decorateReactiveRetry(invocation, dcs, request);
- } finally {
- ServiceCombInvocationContext.removeInvocationContext();
- }
+ decorateReactiveRetry(invocation, dcs, request);
CompletableFuture<Response> result = new CompletableFuture<>();
dcs.get().whenComplete((r, e) -> {
diff --git
a/governance/src/main/java/org/apache/servicecomb/governance/GovernanceConfiguration.java
b/governance/src/main/java/org/apache/servicecomb/governance/GovernanceConfiguration.java
index f1b0f1efb..9679999de 100644
---
a/governance/src/main/java/org/apache/servicecomb/governance/GovernanceConfiguration.java
+++
b/governance/src/main/java/org/apache/servicecomb/governance/GovernanceConfiguration.java
@@ -124,8 +124,8 @@ public class GovernanceConfiguration {
}
@Bean
- public MatchersManager matchersManager(MatchersService matchersService,
InvocationContext invocationContext) {
- return new MatchersManager(matchersService, invocationContext);
+ public MatchersManager matchersManager(MatchersService matchersService) {
+ return new MatchersManager(matchersService);
}
// operators
diff --git
a/governance/src/main/java/org/apache/servicecomb/governance/InvocationContext.java
b/governance/src/main/java/org/apache/servicecomb/governance/InvocationContext.java
deleted file mode 100644
index 07d03ceec..000000000
---
a/governance/src/main/java/org/apache/servicecomb/governance/InvocationContext.java
+++ /dev/null
@@ -1,26 +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.governance;
-
-import java.util.Map;
-
-public interface InvocationContext {
- Map<String, Boolean> getCalculatedMatches();
-
- void addMatch(String key, Boolean value);
-}
diff --git
a/governance/src/main/java/org/apache/servicecomb/governance/MatchersManager.java
b/governance/src/main/java/org/apache/servicecomb/governance/MatchersManager.java
index 4e9685aaa..1c84a54c9 100644
---
a/governance/src/main/java/org/apache/servicecomb/governance/MatchersManager.java
+++
b/governance/src/main/java/org/apache/servicecomb/governance/MatchersManager.java
@@ -27,28 +27,17 @@ import
org.apache.servicecomb.governance.service.MatchersService;
public class MatchersManager {
private MatchersService matchersService;
- private InvocationContext invocationContext;
-
- public MatchersManager(MatchersService matchersService, InvocationContext
invocationContext) {
+ public MatchersManager(MatchersService matchersService) {
this.matchersService = matchersService;
- this.invocationContext = invocationContext;
}
public <T extends AbstractPolicy> T match(GovernanceRequest request,
Map<String, T> policies) {
- Map<String, Boolean> calculatedMatches =
invocationContext.getCalculatedMatches();
-
List<T> sortPolicies = new ArrayList<>(policies.size());
sortPolicies.addAll(policies.values());
sortPolicies.sort(T::compareTo);
for (T policy : sortPolicies) {
- if (calculatedMatches.containsKey(policy.getName())) {
- return policy;
- }
-
- boolean keyMatch = matchersService.checkMatch(request, policy.getName());
- invocationContext.addMatch(policy.getName(), keyMatch);
- if (keyMatch) {
+ if (matchersService.checkMatch(request, policy.getName())) {
return policy;
}
}
diff --git
a/governance/src/test/java/org/apache/servicecomb/governance/InstanceIsolationTest.java
b/governance/src/test/java/org/apache/servicecomb/governance/InstanceIsolationTest.java
index cd8d317ec..34847c301 100644
---
a/governance/src/test/java/org/apache/servicecomb/governance/InstanceIsolationTest.java
+++
b/governance/src/test/java/org/apache/servicecomb/governance/InstanceIsolationTest.java
@@ -89,6 +89,7 @@ public class InstanceIsolationTest {
GovernanceRequest request2 = new GovernanceRequest();
request2.setInstanceId("instance02");
request2.setServiceName("service01");
+ request2.setUri("/test");
CircuitBreaker circuitBreaker2 =
instanceIsolationHandler.getActuator(request2);
ds2.withCircuitBreaker(circuitBreaker2);
diff --git
a/governance/src/test/java/org/apache/servicecomb/governance/MockConfiguration.java
b/governance/src/test/java/org/apache/servicecomb/governance/MockConfiguration.java
index b9a62d81a..7b0281682 100644
---
a/governance/src/test/java/org/apache/servicecomb/governance/MockConfiguration.java
+++
b/governance/src/test/java/org/apache/servicecomb/governance/MockConfiguration.java
@@ -25,11 +25,6 @@ import io.micrometer.prometheus.PrometheusMeterRegistry;
@Configuration
public class MockConfiguration {
- @Bean
- public MockInvocationContext mockInvocationContext() {
- return new MockInvocationContext();
- }
-
@Bean
public MockMicroserviceMeta mockMicroserviceMeta() {
return new MockMicroserviceMeta();
diff --git
a/governance/src/test/java/org/apache/servicecomb/governance/MockInvocationContext.java
b/governance/src/test/java/org/apache/servicecomb/governance/MockInvocationContext.java
deleted file mode 100644
index 7c3e92705..000000000
---
a/governance/src/test/java/org/apache/servicecomb/governance/MockInvocationContext.java
+++ /dev/null
@@ -1,45 +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.governance;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-public class MockInvocationContext implements InvocationContext {
- private final ThreadLocal<Map<String, Boolean>> context = new
ThreadLocal<>();
-
- @Override
- public Map<String, Boolean> getCalculatedMatches() {
- Map<String, Boolean> result = context.get();
- if (result == null) {
- return Collections.emptyMap();
- }
- return result;
- }
-
- @Override
- public void addMatch(String key, Boolean value) {
- Map<String, Boolean> result = context.get();
- if (result == null) {
- result = new HashMap<>();
- context.set(result);
- }
- result.put(key, value);
- }
-}
diff --git
a/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ProviderGovernanceHandler.java
b/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ProviderGovernanceHandler.java
index 3ae26434d..8ae3df80a 100644
---
a/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ProviderGovernanceHandler.java
+++
b/handlers/handler-governance/src/main/java/org/apache/servicecomb/handler/governance/ProviderGovernanceHandler.java
@@ -24,7 +24,6 @@ import java.util.function.Supplier;
import org.apache.servicecomb.core.Handler;
import org.apache.servicecomb.core.Invocation;
import org.apache.servicecomb.core.governance.MatchType;
-import org.apache.servicecomb.core.governance.ServiceCombInvocationContext;
import org.apache.servicecomb.foundation.common.utils.BeanUtils;
import org.apache.servicecomb.governance.handler.BulkheadHandler;
import org.apache.servicecomb.governance.handler.CircuitBreakerHandler;
@@ -62,14 +61,9 @@ public class ProviderGovernanceHandler implements Handler {
DecorateCompletionStage<Response> dcs = Decorators.ofCompletionStage(next);
GovernanceRequest request = MatchType.createGovHttpRequest(invocation);
- try {
- ServiceCombInvocationContext.setInvocationContext(invocation);
- addRateLimiting(dcs, request);
- addCircuitBreaker(dcs, request);
- addBulkhead(dcs, request);
- } finally {
- ServiceCombInvocationContext.removeInvocationContext();
- }
+ addRateLimiting(dcs, request);
+ addCircuitBreaker(dcs, request);
+ addBulkhead(dcs, request);
dcs.get().whenComplete((r, e) -> {
if (e == null) {