@Test public void testSetNeitherCoreNorMaximumSizeWithDivergenceAllowed() { HystrixThreadPoolProperties properties = new HystrixThreadPoolProperties(TestThreadPoolKey.TEST, HystrixThreadPoolProperties.Setter() .withAllowMaximumSizeToDivergeFromCoreSize(true)) { }; assertEquals(HystrixThreadPoolProperties.default_coreSize, properties.coreSize().get().intValue()); assertEquals(HystrixThreadPoolProperties.default_maximumSize, properties.maximumSize().get().intValue()); assertEquals(HystrixThreadPoolProperties.default_maximumSize, (int) properties.actualMaximumSize()); }
@Test public void testSetNeitherCoreNorMaximumSizeWithDivergenceDisallowed() { HystrixThreadPoolProperties properties = new HystrixThreadPoolProperties(TestThreadPoolKey.TEST, HystrixThreadPoolProperties.Setter() .withAllowMaximumSizeToDivergeFromCoreSize(false)) { }; assertEquals(HystrixThreadPoolProperties.default_coreSize, properties.coreSize().get().intValue()); assertEquals(HystrixThreadPoolProperties.default_maximumSize, properties.maximumSize().get().intValue()); assertEquals(HystrixThreadPoolProperties.default_coreSize, (int) properties.actualMaximumSize()); }
@Test public void testSetMaximumSizeOnlyLowerThanDefaultCoreSizeWithDivergenceAllowed() { HystrixThreadPoolProperties properties = new HystrixThreadPoolProperties(TestThreadPoolKey.TEST, HystrixThreadPoolProperties.Setter() .withMaximumSize(3) .withAllowMaximumSizeToDivergeFromCoreSize(true)) { }; assertEquals(HystrixThreadPoolProperties.default_coreSize, properties.coreSize().get().intValue()); assertEquals(3, properties.maximumSize().get().intValue()); assertEquals(HystrixThreadPoolProperties.default_coreSize, (int) properties.actualMaximumSize()); }
if (operation.isAsync()) { HystrixThreadPoolProperties.Setter threadPoolSetter = HystrixThreadPoolProperties.Setter(); threadPoolSetter.withAllowMaximumSizeToDivergeFromCoreSize(false).withCoreSize(bulkhead.get(BulkheadConfig.VALUE)) .withMaximumSize(bulkhead.get(BulkheadConfig.VALUE)).withMaxQueueSize(bulkhead.get(BulkheadConfig.WAITING_TASK_QUEUE)) .withQueueSizeRejectionThreshold(bulkhead.get(BulkheadConfig.WAITING_TASK_QUEUE));
/** * Basic constructor for {@link TaskHandler}. The Hystrix command name is commandName. The group name is the Handler Name * (HystrixTaskHandler#getName) * * @param taskHandler The taskHandler to be wrapped * @param taskContext The context (Unique context required by Handlers to communicate with the container.) * @param commandName name of the command * @param timeout the timeout for the Hystrix thread * @param threadPoolName Name of the thread pool * @param coreThreadPoolSize core size of the thread pool * @param maxThreadPoolSize max size of the thread pool * @param taskRequestWrapper requestWrapper containing the data and the parameters * @param decoder Decoder sent by the Client */ protected TaskHandlerExecutor(TaskHandler taskHandler, TaskContext taskContext, String commandName, int timeout, String threadPoolName, int coreThreadPoolSize, int maxThreadPoolSize, TaskRequestWrapper<S> taskRequestWrapper , Decoder decoder ) { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(taskHandler.getName())) .andCommandKey(HystrixCommandKey.Factory.asKey(commandName)) .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(taskHandler.getVersionedThreadPoolName(threadPoolName))) .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withAllowMaximumSizeToDivergeFromCoreSize(true).withCoreSize(coreThreadPoolSize >= 0 ? coreThreadPoolSize : maxThreadPoolSize).withMaximumSize(maxThreadPoolSize)) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(timeout))); this.taskHandler = taskHandler; this.taskContext = taskContext; this.command = commandName; this.data = taskRequestWrapper.getData(); this.params = taskRequestWrapper.getParams(); this.taskRequestWrapper = taskRequestWrapper; this.eventBuilder = new ServiceProxyEvent.Builder(commandName, COMMAND_HANDLER); this.decoder = decoder; }
public GrpcHystrixCommand(String serviceName, String methodName, Boolean isEnabledFallBack) { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(serviceName))// .andCommandKey(HystrixCommandKey.Factory.asKey(serviceName + ":" + methodName))// .andCommandPropertiesDefaults( HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(20)// 10秒钟内至少19此请求失败,熔断器才发挥起作用 .withCircuitBreakerSleepWindowInMilliseconds(30000)// 熔断器中断请求30秒后会进入半打开状态,放部分流量过去重试 .withCircuitBreakerErrorThresholdPercentage(50)// 错误率达到50开启熔断保护 .withExecutionTimeoutEnabled(false)// 禁用这里的超时 .withFallbackEnabled(isEnabledFallBack))// .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(100) .withAllowMaximumSizeToDivergeFromCoreSize(true).withMaximumSize(Integer.MAX_VALUE))); this.serviceName = serviceName; this.methodName = methodName; this.start = System.currentTimeMillis(); this.rpcContext = new ImmutableTriple<Map<String, String>, Map<String, Object>, Set<Class>>( RpcContext.getContext().getAttachments(), RpcContext.getContext().get(), RpcContext.getContext().getHoldenGroups()); RpcContext.removeContext(); }
private static Setter initSetter(FaultToleranceOperation operation) { HystrixCommandProperties.Setter properties = HystrixCommandProperties.Setter(); HystrixCommandKey commandKey = HystrixCommandKey.Factory .asKey(CompositeCommand.class.getSimpleName() + "#" + SimpleCommand.getCommandKey(operation.getMethod())); properties.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD); properties.withFallbackEnabled(false); properties.withCircuitBreakerEnabled(false); Setter setter = Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CompositeCommandGroup")).andCommandKey(commandKey) .andCommandPropertiesDefaults(properties); // We use a dedicated thread pool for each async operation setter.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(commandKey.name())); HystrixThreadPoolProperties.Setter threadPoolSetter = HystrixThreadPoolProperties.Setter(); threadPoolSetter.withAllowMaximumSizeToDivergeFromCoreSize(true); setter.andThreadPoolPropertiesDefaults(threadPoolSetter); return setter; }
public static HystrixThreadPoolProperties.Setter create(URL url) { return HystrixThreadPoolProperties.Setter().withCoreSize(url.getParameter("coreSize", 10)) .withAllowMaximumSizeToDivergeFromCoreSize(true) .withMaximumSize(url.getParameter("maximumSize", 20)) .withMaxQueueSize(-1) .withKeepAliveTimeMinutes(url.getParameter("keepAliveTimeMinutes", 1)); } }
@Test public void testSetCoreSizeEqualToMaximumSizeDivergenceDisallowed() { HystrixThreadPoolProperties properties = new HystrixThreadPoolProperties(TestThreadPoolKey.TEST, HystrixThreadPoolProperties.Setter() .withCoreSize(7) .withMaximumSize(7) .withAllowMaximumSizeToDivergeFromCoreSize(false)) { }; assertEquals(7, properties.coreSize().get().intValue()); assertEquals(7, properties.maximumSize().get().intValue()); assertEquals(7, (int) properties.actualMaximumSize()); }
@Test public void testSetMaximumSizeOnlyGreaterThanDefaultCoreSizeWithDivergenceAllowed() { HystrixThreadPoolProperties properties = new HystrixThreadPoolProperties(TestThreadPoolKey.TEST, HystrixThreadPoolProperties.Setter() .withMaximumSize(21) .withAllowMaximumSizeToDivergeFromCoreSize(true)) { }; assertEquals(HystrixThreadPoolProperties.default_coreSize, properties.coreSize().get().intValue()); assertEquals(21, properties.maximumSize().get().intValue()); assertEquals(21, (int) properties.actualMaximumSize()); }
@Test public void testSetMaximumSizeOnlyGreaterThanDefaultCoreSizeWithDivergenceDisallowed() { HystrixThreadPoolProperties properties = new HystrixThreadPoolProperties(TestThreadPoolKey.TEST, HystrixThreadPoolProperties.Setter() .withMaximumSize(21) .withAllowMaximumSizeToDivergeFromCoreSize(false)) { }; assertEquals(HystrixThreadPoolProperties.default_coreSize, properties.coreSize().get().intValue()); assertEquals(21, properties.maximumSize().get().intValue()); assertEquals(HystrixThreadPoolProperties.default_coreSize, (int) properties.actualMaximumSize()); }
@Test public void testSetMaximumSizeOnlyLowerThanDefaultCoreSizeWithDivergenceDisallowed() { HystrixThreadPoolProperties properties = new HystrixThreadPoolProperties(TestThreadPoolKey.TEST, HystrixThreadPoolProperties.Setter() .withMaximumSize(3) .withAllowMaximumSizeToDivergeFromCoreSize(false)) { }; assertEquals(HystrixThreadPoolProperties.default_coreSize, properties.coreSize().get().intValue()); assertEquals(3, properties.maximumSize().get().intValue()); assertEquals(HystrixThreadPoolProperties.default_coreSize, (int) properties.actualMaximumSize()); }
@Test public void testSetCoreSizeOnlyWithDivergenceDisallowed() { HystrixThreadPoolProperties properties = new HystrixThreadPoolProperties(TestThreadPoolKey.TEST, HystrixThreadPoolProperties.Setter() .withCoreSize(14) .withAllowMaximumSizeToDivergeFromCoreSize(false)) { }; assertEquals(14, properties.coreSize().get().intValue()); assertEquals(HystrixThreadPoolProperties.default_maximumSize, properties.maximumSize().get().intValue()); assertEquals(14, (int) properties.actualMaximumSize()); }
@Test public void testSetCoreSizeGreaterThanMaximumSizeWithDivergenceAllowed() { HystrixThreadPoolProperties properties = new HystrixThreadPoolProperties(TestThreadPoolKey.TEST, HystrixThreadPoolProperties.Setter() .withCoreSize(12) .withMaximumSize(8) .withAllowMaximumSizeToDivergeFromCoreSize(true)) { }; assertEquals(12, properties.coreSize().get().intValue()); assertEquals(8, properties.maximumSize().get().intValue()); assertEquals(12, (int) properties.actualMaximumSize()); } }
@Test public void testSetCoreSizeLessThanMaximumSizeWithDivergenceAllowed() { HystrixThreadPoolProperties properties = new HystrixThreadPoolProperties(TestThreadPoolKey.TEST, HystrixThreadPoolProperties.Setter() .withCoreSize(2) .withMaximumSize(8) .withAllowMaximumSizeToDivergeFromCoreSize(true)) { }; assertEquals(2, properties.coreSize().get().intValue()); assertEquals(8, properties.maximumSize().get().intValue()); assertEquals(8, (int) properties.actualMaximumSize()); }
@Test public void testSetCoreSizeLessThanMaximumSizeWithDivergenceDisallowed() { HystrixThreadPoolProperties properties = new HystrixThreadPoolProperties(TestThreadPoolKey.TEST, HystrixThreadPoolProperties.Setter() .withCoreSize(2) .withMaximumSize(8) .withAllowMaximumSizeToDivergeFromCoreSize(false)) { }; assertEquals(2, properties.coreSize().get().intValue()); assertEquals(8, properties.maximumSize().get().intValue()); assertEquals(2, (int) properties.actualMaximumSize()); }
@Test public void testSetCoreSizeOnlyWithDivergenceAllowed() { HystrixThreadPoolProperties properties = new HystrixThreadPoolProperties(TestThreadPoolKey.TEST, HystrixThreadPoolProperties.Setter() .withCoreSize(14) .withAllowMaximumSizeToDivergeFromCoreSize(true)) { }; assertEquals(14, properties.coreSize().get().intValue()); assertEquals(HystrixThreadPoolProperties.default_maximumSize, properties.maximumSize().get().intValue()); assertEquals(14, (int) properties.actualMaximumSize()); }
@Test public void testSetCoreSizeGreaterThanMaximumSizeWithDivergenceDisallowed() { HystrixThreadPoolProperties properties = new HystrixThreadPoolProperties(TestThreadPoolKey.TEST, HystrixThreadPoolProperties.Setter() .withCoreSize(12) .withMaximumSize(8) .withAllowMaximumSizeToDivergeFromCoreSize(false)) { }; assertEquals(12, properties.coreSize().get().intValue()); assertEquals(8, properties.maximumSize().get().intValue()); assertEquals(12, (int) properties.actualMaximumSize()); }
@Test public void testSetCoreSizeEqualToMaximumSizeDivergenceAllowed() { HystrixThreadPoolProperties properties = new HystrixThreadPoolProperties(TestThreadPoolKey.TEST, HystrixThreadPoolProperties.Setter() .withCoreSize(7) .withMaximumSize(7) .withAllowMaximumSizeToDivergeFromCoreSize(true)) { }; assertEquals(7, properties.coreSize().get().intValue()); assertEquals(7, properties.maximumSize().get().intValue()); assertEquals(7, (int) properties.actualMaximumSize()); }