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

xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 8ff9fd161 [type:BUG] hystrix plugin can't circuitBreaker. (#3828)
8ff9fd161 is described below

commit 8ff9fd16142a37f45fa583996d8d3696ca5aa934
Author: yunlongn <[email protected]>
AuthorDate: Wed Aug 10 17:40:36 2022 +0800

    [type:BUG] hystrix plugin can't circuitBreaker. (#3828)
    
    * [type:BUG] hystrix plugin can't circuitBreaker.
    
    * [type:BUG] hystrix plugin can't circuitBreaker.
    
    * [type:BUG] hystrix plugin can't circuitBreaker.
    
    * [type:BUG] hystrix plugin can't circuitBreaker.
    
    * [type:BUG] hystrix plugin can't circuitBreaker.
---
 .../shenyu/plugin/hystrix/HystrixPlugin.java       | 14 ++++----
 .../plugin/hystrix/builder/HystrixBuilder.java     | 42 +++++++++++++++++-----
 .../shenyu/plugin/hystrix/command/Command.java     |  5 +++
 .../plugin/hystrix/command/HystrixCommand.java     |  5 +++
 .../hystrix/command/HystrixCommandOnThread.java    |  5 +++
 .../hystrix/handler/HystrixPluginDataHandler.java  | 11 ++++++
 6 files changed, 67 insertions(+), 15 deletions(-)

diff --git 
a/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/HystrixPlugin.java
 
b/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/HystrixPlugin.java
index 57d81ca91..4ceaff4d9 100644
--- 
a/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/HystrixPlugin.java
+++ 
b/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/HystrixPlugin.java
@@ -54,13 +54,15 @@ public class HystrixPlugin extends AbstractShenyuPlugin {
         final ShenyuContext shenyuContext = 
exchange.getAttribute(Constants.CONTEXT);
         assert shenyuContext != null;
         final HystrixHandle hystrixHandle = 
HystrixPluginDataHandler.CACHED_HANDLE.get().obtainHandle(CacheKeyUtils.INST.getKey(rule));
+        String groupKey = hystrixHandle.getGroupKey();
         if (StringUtils.isBlank(hystrixHandle.getGroupKey())) {
-            
hystrixHandle.setGroupKey(Objects.requireNonNull(shenyuContext).getModule());
+            groupKey = Objects.requireNonNull(shenyuContext).getModule();
         }
+        String commandKey = hystrixHandle.getCommandKey();
         if (StringUtils.isBlank(hystrixHandle.getCommandKey())) {
-            
hystrixHandle.setCommandKey(Objects.requireNonNull(shenyuContext).getMethod());
+            commandKey = Objects.requireNonNull(shenyuContext).getMethod();
         }
-        Command command = fetchCommand(hystrixHandle, exchange, chain);
+        Command command = fetchCommand(hystrixHandle, exchange, chain, 
commandKey, groupKey);
         return Mono.create(s -> {
             Subscription sub = command.fetchObservable().subscribe(s::success,
                     s::error, s::success);
@@ -75,12 +77,12 @@ public class HystrixPlugin extends AbstractShenyuPlugin {
         }).then();
     }
 
-    private Command fetchCommand(final HystrixHandle hystrixHandle, final 
ServerWebExchange exchange, final ShenyuPluginChain chain) {
+    private Command fetchCommand(final HystrixHandle hystrixHandle, final 
ServerWebExchange exchange, final ShenyuPluginChain chain, final String 
commandKey, final String groupKey) {
         if (hystrixHandle.getExecutionIsolationStrategy() == 
HystrixIsolationModeEnum.SEMAPHORE.getCode()) {
-            return new HystrixCommand(HystrixBuilder.build(hystrixHandle),
+            return new HystrixCommand(HystrixBuilder.build(hystrixHandle, 
commandKey, groupKey),
                     exchange, chain, hystrixHandle.getCallBackUri());
         }
-        return new 
HystrixCommandOnThread(HystrixBuilder.buildForHystrixCommand(hystrixHandle),
+        return new 
HystrixCommandOnThread(HystrixBuilder.buildForHystrixCommand(hystrixHandle, 
commandKey, groupKey),
                 exchange, chain, hystrixHandle.getCallBackUri());
     }
 
diff --git 
a/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/builder/HystrixBuilder.java
 
b/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/builder/HystrixBuilder.java
index 65b3430f2..decfa42f0 100644
--- 
a/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/builder/HystrixBuilder.java
+++ 
b/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/builder/HystrixBuilder.java
@@ -41,12 +41,14 @@ public final class HystrixBuilder {
      * this is build HystrixObservableCommand.Setter.
      *
      * @param hystrixHandle {@linkplain HystrixHandle}
+     * @param commandKey commandKey
+     * @param groupKey groupKey
      * @return {@linkplain HystrixObservableCommand.Setter}
      */
-    public static HystrixObservableCommand.Setter build(final HystrixHandle 
hystrixHandle) {
+    public static HystrixObservableCommand.Setter build(final HystrixHandle 
hystrixHandle, final String commandKey, final String groupKey) {
         initHystrixHandleOnRequire(hystrixHandle);
-        HystrixCommandGroupKey groupKey = 
HystrixCommandGroupKey.Factory.asKey(hystrixHandle.getGroupKey());
-        HystrixCommandKey commandKey = 
HystrixCommandKey.Factory.asKey(hystrixHandle.getCommandKey());
+        HystrixCommandGroupKey hystrixCommandGroupKey = 
HystrixCommandGroupKey.Factory.asKey(groupKey);
+        HystrixCommandKey hystrixCommandKey = 
HystrixCommandKey.Factory.asKey(commandKey);
         HystrixCommandProperties.Setter propertiesSetter =
                 HystrixCommandProperties.Setter()
                         .withExecutionTimeoutInMilliseconds((int) 
hystrixHandle.getTimeout())
@@ -57,11 +59,21 @@ public final class HystrixBuilder {
                         
.withCircuitBreakerRequestVolumeThreshold(hystrixHandle.getRequestVolumeThreshold())
                         
.withCircuitBreakerSleepWindowInMilliseconds(hystrixHandle.getSleepWindowInMilliseconds());
         return HystrixObservableCommand.Setter
-                .withGroupKey(groupKey)
-                .andCommandKey(commandKey)
+                .withGroupKey(hystrixCommandGroupKey)
+                .andCommandKey(hystrixCommandKey)
                 .andCommandPropertiesDefaults(propertiesSetter);
     }
 
+    /**
+     * this is build HystrixObservableCommand.Setter.
+     *
+     * @param hystrixHandle {@linkplain HystrixHandle}
+     * @return {@linkplain HystrixObservableCommand.Setter}
+     */
+    public static HystrixObservableCommand.Setter build(final HystrixHandle 
hystrixHandle) {
+        return build(hystrixHandle, hystrixHandle.getCommandKey(), 
hystrixHandle.getGroupKey());
+    }
+
     /**
      * this is build HystrixCommand.Setter.
      *
@@ -69,9 +81,21 @@ public final class HystrixBuilder {
      * @return {@linkplain HystrixCommand.Setter}
      */
     public static HystrixCommand.Setter buildForHystrixCommand(final 
HystrixHandle hystrixHandle) {
+        return buildForHystrixCommand(hystrixHandle, 
hystrixHandle.getCommandKey(), hystrixHandle.getGroupKey());
+    }
+
+    /**
+     * this is build HystrixCommand.Setter.
+     *
+     * @param hystrixHandle {@linkplain HystrixHandle}
+     * @param commandKey commandKey
+     * @param groupKey groupKey
+     * @return {@linkplain HystrixCommand.Setter}
+     */
+    public static HystrixCommand.Setter buildForHystrixCommand(final 
HystrixHandle hystrixHandle, final String commandKey, final String groupKey) {
         initHystrixHandleOnRequire(hystrixHandle);
-        HystrixCommandGroupKey groupKey = 
HystrixCommandGroupKey.Factory.asKey(hystrixHandle.getGroupKey());
-        HystrixCommandKey commandKey = 
HystrixCommandKey.Factory.asKey(hystrixHandle.getCommandKey());
+        HystrixCommandGroupKey hystrixCommandGroupKey = 
HystrixCommandGroupKey.Factory.asKey(groupKey);
+        HystrixCommandKey hystrixCommandKey = 
HystrixCommandKey.Factory.asKey(commandKey);
         HystrixCommandProperties.Setter propertiesSetter =
                 HystrixCommandProperties.Setter()
                         .withExecutionTimeoutInMilliseconds((int) 
hystrixHandle.getTimeout())
@@ -88,8 +112,8 @@ public final class HystrixBuilder {
                         
.withKeepAliveTimeMinutes(hystrixThreadPoolConfig.getKeepAliveTimeMinutes())
                         .withAllowMaximumSizeToDivergeFromCoreSize(true);
         return HystrixCommand.Setter
-                .withGroupKey(groupKey)
-                .andCommandKey(commandKey)
+                .withGroupKey(hystrixCommandGroupKey)
+                .andCommandKey(hystrixCommandKey)
                 .andCommandPropertiesDefaults(propertiesSetter)
                 .andThreadPoolPropertiesDefaults(threadPoolPropertiesSetter);
     }
diff --git 
a/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/command/Command.java
 
b/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/command/Command.java
index 3dc531a25..27e9e7072 100644
--- 
a/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/command/Command.java
+++ 
b/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/command/Command.java
@@ -111,4 +111,9 @@ public interface Command {
      */
     void removeCommandKey(String commandKey);
 
+    /**
+     * clean all command.
+     */
+    void cleanCommand();
+
 }
diff --git 
a/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/command/HystrixCommand.java
 
b/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/command/HystrixCommand.java
index b1c76f76a..0631b2a83 100644
--- 
a/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/command/HystrixCommand.java
+++ 
b/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/command/HystrixCommand.java
@@ -92,4 +92,9 @@ public class HystrixCommand extends 
HystrixObservableCommand<Void> implements Co
     public void removeCommandKey(final String commandKey) {
         executionSemaphorePerCircuit.remove(commandKey);
     }
+
+    @Override
+    public void cleanCommand() {
+        executionSemaphorePerCircuit.clear();
+    }
 }
diff --git 
a/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/command/HystrixCommandOnThread.java
 
b/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/command/HystrixCommandOnThread.java
index acde21107..b6b36bdac 100644
--- 
a/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/command/HystrixCommandOnThread.java
+++ 
b/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/command/HystrixCommandOnThread.java
@@ -89,4 +89,9 @@ public class HystrixCommandOnThread extends 
HystrixCommand<Mono<Void>> implement
     public void removeCommandKey(final String commandKey) {
         executionSemaphorePerCircuit.remove(commandKey);
     }
+
+    @Override
+    public void cleanCommand() {
+        executionSemaphorePerCircuit.clear();
+    }
 }
diff --git 
a/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/handler/HystrixPluginDataHandler.java
 
b/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/handler/HystrixPluginDataHandler.java
index 959a81e9f..2e4c2f2bb 100644
--- 
a/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/handler/HystrixPluginDataHandler.java
+++ 
b/shenyu-plugin/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/handler/HystrixPluginDataHandler.java
@@ -29,6 +29,7 @@ import org.apache.shenyu.plugin.base.utils.CacheKeyUtils;
 import org.apache.shenyu.plugin.hystrix.builder.HystrixBuilder;
 import org.apache.shenyu.plugin.hystrix.command.Command;
 import org.apache.shenyu.plugin.hystrix.command.HystrixCommand;
+import org.springframework.util.StringUtils;
 
 import java.util.Optional;
 import java.util.function.Supplier;
@@ -52,6 +53,16 @@ public class HystrixPluginDataHandler implements 
PluginDataHandler {
                     Command command = new 
HystrixCommand(HystrixBuilder.build(hystrixHandle), null, null, null);
                     command.removeCommandKey(commandKey);
                 }
+                // fix ISSUE #3820, in same rule, change isolation strategy, 
can't circuit breaker
+                if (hystrixHandleCache.getExecutionIsolationStrategy() != 
hystrixHandle.getExecutionIsolationStrategy()) {
+                    Command command = new 
HystrixCommand(HystrixBuilder.build(hystrixHandleCache), null, null, null);
+                    if (StringUtils.hasText(hystrixHandle.getCommandKey())) {
+                        
command.removeCommandKey(hystrixHandle.getCommandKey());
+                    } else {
+                        // delete all old Commands of the specified group
+                        command.cleanCommand();
+                    }
+                }
             });
             CACHED_HANDLE.get().cachedHandle(key, hystrixHandle);
         });

Reply via email to