new TryableSemaphoreActual(HystrixProperty.Factory.asProperty(3)); new TryableSemaphoreActual(HystrixProperty.Factory.asProperty(1));
@Override public HystrixProperty<Integer> timerDelayInMilliseconds() { return HystrixProperty.Factory.asProperty(builder.timerDelayInMilliseconds); }
@Override public HystrixProperty<Integer> maxRequestsInBatch() { return HystrixProperty.Factory.asProperty(builder.maxRequestsInBatch); }
@Override public HystrixProperty<Boolean> requestCachingEnabled() { return HystrixProperty.Factory.asProperty(builder.requestCacheEnabled); }
@Override public HystrixProperty<ExecutionIsolationStrategy> executionIsolationStrategy() { // Don't allow change isolation strategy at runtime if (this.isolationStrategy != null) { return HystrixProperty.Factory.asProperty(this.isolationStrategy); } return HystrixProperty.Factory.asProperty(ExecutionIsolationStrategy.THREAD); }
@Override public HystrixProperty<Integer> fallbackIsolationSemaphoreMaxConcurrentRequests() { /* * Astrix does not use the fallback of HystrixCommand to do any unsafe operations, and * we always want to call the getFallback method to get proper handling of a failed * service invocation in Astrix, see HystrixCommandFacade/HystrixObservableCommandFacade. * */ return HystrixProperty.Factory.asProperty(Integer.MAX_VALUE); }
new TryableSemaphoreActual(HystrixProperty.Factory.asProperty(3)); new TryableSemaphoreActual(HystrixProperty.Factory.asProperty(1));
new TryableSemaphoreActual(HystrixProperty.Factory.asProperty(1));
new TryableSemaphoreActual(HystrixProperty.Factory.asProperty(1));
new TryableSemaphoreActual(HystrixProperty.Factory.asProperty(1));
final ArrayBlockingQueue<Boolean> results = new ArrayBlockingQueue<Boolean>(2); final TryableSemaphoreActual semaphore = new TryableSemaphoreActual(HystrixProperty.Factory.asProperty(1));
@Test public void testThreadSafety() { final MockedTime time = new MockedTime(); final HystrixRollingPercentile p = new HystrixRollingPercentile(time, 100, 25, 1000, HystrixProperty.Factory.asProperty(true));
@Test public void testSemaphoreThreadSafety() { final int NUM_PERMITS = 1; final TryableSemaphoreActual s = new TryableSemaphoreActual(HystrixProperty.Factory.asProperty(NUM_PERMITS));
@Test public void testWriteThreadSafety() { final MockedTime time = new MockedTime(); final HystrixRollingPercentile p = new HystrixRollingPercentile(time, 100, 25, 1000, HystrixProperty.Factory.asProperty(true)); final int NUM_THREADS = 10; final int NUM_ITERATIONS = 1000; final CountDownLatch latch = new CountDownLatch(NUM_THREADS); final Random r = new Random(); final AtomicInteger added = new AtomicInteger(0); for (int i = 0; i < NUM_THREADS; i++) { threadPool.submit(new Runnable() { @Override public void run() { for (int j = 1; j < NUM_ITERATIONS / NUM_THREADS + 1; j++) { int nextInt = r.nextInt(100); p.addValue(nextInt); added.getAndIncrement(); } latch.countDown(); } }); } try { latch.await(100, TimeUnit.SECONDS); assertEquals(added.get(), p.buckets.peekLast().data.length()); } catch (InterruptedException ex) { fail("Timeout on all threads writing percentiles"); } }
@Override public C call() { AbstractCommand.TryableSemaphore semaphore = new AbstractCommand.TryableSemaphoreActual(HystrixProperty.Factory.asProperty(2)); final C cmd1 = getLatentCommand(ExecutionIsolationStrategy.SEMAPHORE, ExecutionResult.SUCCESS, 1500, FallbackResult.SUCCESS, semaphore); final C cmd2 = getLatentCommand(ExecutionIsolationStrategy.SEMAPHORE, ExecutionResult.SUCCESS, 1500, FallbackResult.SUCCESS, semaphore); //saturate the semaphore new Thread() { @Override public void run() { cmd1.observe(); } }.start(); new Thread() { @Override public void run() { cmd2.observe(); } }.start(); try { //give the saturating threads a chance to run before we run the command we want to get rejected Thread.sleep(200); } catch (InterruptedException ie) { throw new RuntimeException(ie); } return getLatentCommand(ExecutionIsolationStrategy.SEMAPHORE, ExecutionResult.SUCCESS, 500, FallbackResult.SUCCESS, semaphore); } },
@Override public C call() { AbstractCommand.TryableSemaphore semaphore = new AbstractCommand.TryableSemaphoreActual(HystrixProperty.Factory.asProperty(2)); final C cmd1 = getLatentCommand(ExecutionIsolationStrategy.SEMAPHORE, ExecutionResult.SUCCESS, 500, FallbackResult.FAILURE, semaphore); final C cmd2 = getLatentCommand(ExecutionIsolationStrategy.SEMAPHORE, ExecutionResult.SUCCESS, 500, FallbackResult.FAILURE, semaphore); //saturate the semaphore new Thread() { @Override public void run() { cmd1.observe(); } }.start(); new Thread() { @Override public void run() { cmd2.observe(); } }.start(); try { //give the saturating threads a chance to run before we run the command we want to get rejected Thread.sleep(200); } catch (InterruptedException ie) { throw new RuntimeException(ie); } return getLatentCommand(ExecutionIsolationStrategy.SEMAPHORE, ExecutionResult.SUCCESS, 500, FallbackResult.FAILURE, semaphore); } },
@Override public C call() { AbstractCommand.TryableSemaphore semaphore = new AbstractCommand.TryableSemaphoreActual(HystrixProperty.Factory.asProperty(2)); final C cmd1 = getLatentCommand(ExecutionIsolationStrategy.SEMAPHORE, ExecutionResult.SUCCESS, 500, FallbackResult.UNIMPLEMENTED, semaphore); final C cmd2 = getLatentCommand(ExecutionIsolationStrategy.SEMAPHORE, ExecutionResult.SUCCESS, 500, FallbackResult.UNIMPLEMENTED, semaphore); //saturate the semaphore new Thread() { @Override public void run() { cmd1.observe(); } }.start(); new Thread() { @Override public void run() { cmd2.observe(); } }.start(); try { //give the saturating threads a chance to run before we run the command we want to get rejected Thread.sleep(200); } catch (InterruptedException ie) { throw new RuntimeException(ie); } return getLatentCommand(ExecutionIsolationStrategy.SEMAPHORE, ExecutionResult.SUCCESS, 500, FallbackResult.UNIMPLEMENTED, semaphore); } },
/** * This code should work without throwing exceptions but the data returned will all be -1 since the rolling percentile is disabled. */ @Test public void testDoesNothingWhenDisabled() { MockedTime time = new MockedTime(); int previousTime = 0; HystrixRollingPercentile p = new HystrixRollingPercentile(time, timeInMilliseconds, numberOfBuckets, bucketDataLength, HystrixProperty.Factory.asProperty(false)); for (int i = 0; i < SampleDataHolder2.data.length; i++) { int timeInMillisecondsSinceStart = SampleDataHolder2.data[i][0]; int latency = SampleDataHolder2.data[i][1]; time.increment(timeInMillisecondsSinceStart - previousTime); previousTime = timeInMillisecondsSinceStart; p.addValue(latency); } assertEquals(-1, p.getPercentile(50)); assertEquals(-1, p.getPercentile(75)); assertEquals(-1, p.getMean()); }
abstract C getCommand(HystrixCommandKey commandKey, ExecutionIsolationStrategy isolationStrategy, ExecutionResult executionResult, int executionLatency, FallbackResult fallbackResult, int fallbackLatency, HystrixCircuitBreakerTest.TestCircuitBreaker circuitBreaker, HystrixThreadPool threadPool, int timeout, CacheEnabled cacheEnabled, Object value, AbstractCommand.TryableSemaphore executionSemaphore, AbstractCommand.TryableSemaphore fallbackSemaphore, boolean circuitBreakerDisabled);
@Override public HystrixProperty<Boolean> requestLogEnabled() { return HystrixProperty.Factory.asProperty(builder.getRequestLogEnabled()); }