hystrixHandler.setStatefully(this.statefully); Setter setter = Setter.withGroupKey(groupKey).andCommandKey(commandKey);
@Override public HystrixCommand.Setter createSetter(FilterInvoker invoker, SofaRequest request) { Method clientMethod = request.getMethod(); if (!SETTER_CACHE.containsKey(clientMethod)) { synchronized (DefaultSetterFactory.class) { if (!SETTER_CACHE.containsKey(clientMethod)) { String groupKey = invoker.getConfig().getInterfaceId(); String commandKey = request.getMethodName(); HystrixCommand.Setter setter = HystrixCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)) .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)); SETTER_CACHE.put(clientMethod, setter); } } } return SETTER_CACHE.get(clientMethod); }
@Override public HystrixCommand.Setter createSetter(FilterInvoker invoker, SofaRequest request) { Method clientMethod = request.getMethod(); if (!SETTER_CACHE.containsKey(clientMethod)) { synchronized (DefaultSetterFactory.class) { if (!SETTER_CACHE.containsKey(clientMethod)) { String groupKey = invoker.getConfig().getInterfaceId(); String commandKey = request.getMethodName(); HystrixCommand.Setter setter = HystrixCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)) .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)); SETTER_CACHE.put(clientMethod, setter); } } } return SETTER_CACHE.get(clientMethod); }
.andCommandKey(HystrixCommandKey.Factory.asKey("CommandName"))) {
HystrixEventType desiredFallbackEventType, int fallbackLatency) { Setter setter = Setter.withGroupKey(groupKey) .andCommandKey(key) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(600)
.andCommandKey(HystrixCommandKey.Factory.asKey("OuterCommand"))) {
.andCommandKey(HystrixCommandKey.Factory.asKey("OuterSemaphoreCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE))) {
@Override protected Boolean run() { assertEquals("OuterSemaphoreCommand", Hystrix.getCurrentThreadExecutingCommand().name()); System.out.println("Outer Semaphore Thread : " + Thread.currentThread().getName()); //should be a single execution on the caller thread assertEquals(1, Hystrix.getCommandCount()); if (Hystrix.getCurrentThreadExecutingCommand() == null) { throw new RuntimeException("BEFORE expected it to run inside a semaphore"); } HystrixCommand<Boolean> command2 = new HystrixCommand<Boolean>(Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestUtil")) .andCommandKey(HystrixCommandKey.Factory.asKey("InnerCommand"))) { @Override protected Boolean run() { assertEquals("InnerCommand", Hystrix.getCurrentThreadExecutingCommand().name()); System.out.println("Inner Thread : " + Thread.currentThread().getName()); //should be a single execution on the thread isolating the second command assertEquals(1, Hystrix.getCommandCount()); return Hystrix.getCurrentThreadExecutingCommand() != null; } }; if (Hystrix.getCurrentThreadExecutingCommand() == null) { throw new RuntimeException("AFTER expected it to run inside a semaphore"); } return command2.execute(); }
@Override protected Boolean run() { assertEquals("OuterCommand", Hystrix.getCurrentThreadExecutingCommand().name()); System.out.println("Outer Thread : " + Thread.currentThread().getName()); //should be a single execution on this thread assertEquals(1, Hystrix.getCommandCount()); if (Hystrix.getCurrentThreadExecutingCommand() == null) { throw new RuntimeException("BEFORE expected it to run inside a thread"); } HystrixCommand<Boolean> command2 = new HystrixCommand<Boolean>(Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestUtil")) .andCommandKey(HystrixCommandKey.Factory.asKey("InnerCommand"))) { @Override protected Boolean run() { assertEquals("InnerCommand", Hystrix.getCurrentThreadExecutingCommand().name()); System.out.println("Inner Thread : " + Thread.currentThread().getName()); //should be a single execution on this thread, since outer/inner are on different threads assertEquals(1, Hystrix.getCommandCount()); return Hystrix.getCurrentThreadExecutingCommand() != null; } }; if (Hystrix.getCurrentThreadExecutingCommand() == null) { throw new RuntimeException("AFTER expected it to run inside a thread"); } return command2.execute(); }
/** * Creates instance of {@link HystrixCommand.Setter}. * * @return the instance of {@link HystrixCommand.Setter} */ public HystrixCommand.Setter build() throws HystrixPropertyException { HystrixCommand.Setter setter = HystrixCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)) .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)); if (StringUtils.isNotBlank(threadPoolKey)) { setter.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(threadPoolKey)); } try { setter.andThreadPoolPropertiesDefaults(HystrixPropertiesManager.initializeThreadPoolProperties(threadPoolProperties)); } catch (IllegalArgumentException e) { throw new HystrixPropertyException("Failed to set Thread Pool properties. " + getInfo(), e); } try { setter.andCommandPropertiesDefaults(HystrixPropertiesManager.initializeCommandProperties(commandProperties)); } catch (IllegalArgumentException e) { throw new HystrixPropertyException("Failed to set Command properties. " + getInfo(), e); } return setter; }
@Test public void testInsideHystrixSemaphoreObserve() throws Exception { HystrixCommand<Boolean> command = new HystrixCommand<Boolean>(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestUtil")) .andCommandKey(HystrixCommandKey.Factory.asKey("SemaphoreIsolatedCommandName")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE))) { @Override protected Boolean run() { assertEquals("SemaphoreIsolatedCommandName", Hystrix.getCurrentThreadExecutingCommand().name()); System.out.println("Semaphore Thread : " + Thread.currentThread().getName()); //should be a single execution on the caller thread (since it's busy here) assertEquals(1, Hystrix.getCommandCount()); return Hystrix.getCurrentThreadExecutingCommand() != null; } }; // it should be true for semaphore isolation as well assertTrue(command.toObservable().toBlocking().single()); // and then be null again once done assertNull(Hystrix.getCurrentThreadExecutingCommand()); }
@Test public void testInsideHystrixSemaphoreQueue() throws Exception { HystrixCommand<Boolean> command = new HystrixCommand<Boolean>(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestUtil")) .andCommandKey(HystrixCommandKey.Factory.asKey("SemaphoreIsolatedCommandName")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE))) { @Override protected Boolean run() { assertEquals("SemaphoreIsolatedCommandName", Hystrix.getCurrentThreadExecutingCommand().name()); System.out.println("Semaphore Thread : " + Thread.currentThread().getName()); //should be a single execution on the caller thread (since it's busy here) assertEquals(1, Hystrix.getCommandCount()); return Hystrix.getCurrentThreadExecutingCommand() != null; } }; // it should be true for semaphore isolation as well assertTrue(command.queue().get()); // and then be null again once done assertNull(Hystrix.getCurrentThreadExecutingCommand()); }
@Test public void testInsideHystrixSemaphoreExecute() { HystrixCommand<Boolean> command = new HystrixCommand<Boolean>(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestUtil")) .andCommandKey(HystrixCommandKey.Factory.asKey("SemaphoreIsolatedCommandName")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE))) { @Override protected Boolean run() { assertEquals("SemaphoreIsolatedCommandName", Hystrix.getCurrentThreadExecutingCommand().name()); System.out.println("Semaphore Thread : " + Thread.currentThread().getName()); //should be a single execution on the caller thread (since it's busy here) assertEquals(1, Hystrix.getCommandCount()); return Hystrix.getCurrentThreadExecutingCommand() != null; } }; // it should be true for semaphore isolation as well assertTrue(command.execute()); // and then be null again once done assertNull(Hystrix.getCurrentThreadExecutingCommand()); }
RunnableCommand(String commandKey, Runnable runnable) { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(HELIDON_MICROPROFILE_FAULTTOLERANCE)) .andCommandKey( HystrixCommandKey.Factory.asKey(commandKey)) .andCommandPropertiesDefaults( HystrixCommandProperties.Setter().withFallbackEnabled(false))); this.runnable = runnable; }
@Test public void testInsideHystrixThreadViaExecute() { assertNull(Hystrix.getCurrentThreadExecutingCommand()); HystrixCommand<Boolean> command = new HystrixCommand<Boolean>(Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestUtil")) .andCommandKey(HystrixCommandKey.Factory.asKey("CommandName"))) { @Override protected Boolean run() { assertEquals("CommandName", Hystrix.getCurrentThreadExecutingCommand().name()); assertEquals(1, Hystrix.getCommandCount()); return Hystrix.getCurrentThreadExecutingCommand() != null; } }; assertTrue(command.execute()); assertNull(Hystrix.getCurrentThreadExecutingCommand()); assertEquals(0, Hystrix.getCommandCount()); }
Command() { super(Setter.withGroupKey(hystrixCommandGroupKey).andCommandKey(hystrixCommandKey).andThreadPoolKey(hystrixThreadPool)); }
public Command(String commandKey, boolean shouldFail, boolean shouldFailWithBadRequest, long latencyToAdd) { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Command")) .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)) .andCommandPropertiesDefaults(HystrixCommandPropertiesTest.getUnitTestPropertiesSetter() .withExecutionTimeoutInMilliseconds(1000) .withCircuitBreakerRequestVolumeThreshold(20))); this.shouldFail = shouldFail; this.shouldFailWithBadRequest = shouldFailWithBadRequest; this.latencyToAdd = latencyToAdd; }
protected TestCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("testTimeoutConcurrency")) .andCommandKey(HystrixCommandKey.Factory.asKey("testTimeoutConcurrencyCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(3) .withCircuitBreakerEnabled(false) .withFallbackIsolationSemaphoreMaxConcurrentRequests(NUM_CONCURRENT_COMMANDS)) .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() .withCoreSize(NUM_CONCURRENT_COMMANDS) .withMaxQueueSize(NUM_CONCURRENT_COMMANDS) .withQueueSizeRejectionThreshold(NUM_CONCURRENT_COMMANDS))); }
public Command(String commandKey, boolean shouldFail, boolean shouldFailWithBadRequest, long latencyToAdd, int sleepWindow, int requestVolumeThreshold) { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Command")).andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)). andCommandPropertiesDefaults(HystrixCommandPropertiesTest.getUnitTestPropertiesSetter(). withExecutionTimeoutInMilliseconds(500). withCircuitBreakerRequestVolumeThreshold(requestVolumeThreshold). withCircuitBreakerSleepWindowInMilliseconds(sleepWindow))); this.shouldFail = shouldFail; this.shouldFailWithBadRequest = shouldFailWithBadRequest; this.latencyToAdd = latencyToAdd; }
public TestCommand(String commandName, String value, boolean fail, boolean failOnFallback) { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RequestLogTestCommand")).andCommandKey(HystrixCommandKey.Factory.asKey(commandName))); this.value = value; this.fail = fail; this.failOnFallback = failOnFallback; this.timeout = false; this.useFallback = true; this.useCache = true; }