use of com.netflix.hystrix.HystrixCommandKey in project Hystrix by Netflix.
the class RollingCommandLatencyDistributionStreamTest method testResponseFromCacheDoesNotGetLatencyTracked.
@Test
public void testResponseFromCacheDoesNotGetLatencyTracked() {
HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Latency-G");
stream = RollingCommandLatencyDistributionStream.getInstance(key, 10, 100);
stream.startCachingStreamValuesIfUnstarted();
//should get 1 SUCCESS and 1 RESPONSE_FROM_CACHE
List<Command> commands = Command.getCommandsWithResponseFromCache(groupKey, key);
final CountDownLatch latch = new CountDownLatch(1);
stream.observe().take(10).subscribe(new Subscriber<CachedValuesHistogram>() {
@Override
public void onCompleted() {
latch.countDown();
}
@Override
public void onError(Throwable e) {
fail(e.getMessage());
}
@Override
public void onNext(CachedValuesHistogram distribution) {
System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " Received distribution with count : " + distribution.getTotalCount() + " and mean : " + distribution.getMean());
assertTrue(distribution.getTotalCount() <= 1);
}
});
for (Command cmd : commands) {
cmd.observe();
}
try {
assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
} catch (InterruptedException ex) {
fail("Interrupted ex");
}
assertEquals(1, stream.getLatest().getTotalCount());
assertBetween(0, 30, stream.getLatestMean());
System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
}
use of com.netflix.hystrix.HystrixCommandKey in project Hystrix by Netflix.
the class RollingCommandLatencyDistributionStreamTest method testSemaphoreRejectedCommandDoesNotGetLatencyTracked.
@Test
public void testSemaphoreRejectedCommandDoesNotGetLatencyTracked() {
HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Latency-F");
stream = RollingCommandLatencyDistributionStream.getInstance(key, 10, 100);
stream.startCachingStreamValuesIfUnstarted();
//10 commands with latency should occupy all semaphores. execute those, then wait for bucket to roll
//next command should be a semaphore rejection
List<Command> commands = new ArrayList<Command>();
for (int i = 0; i < 10; i++) {
commands.add(Command.from(groupKey, key, HystrixEventType.SUCCESS, 200, HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE));
}
final CountDownLatch latch = new CountDownLatch(1);
stream.observe().take(10).subscribe(new Subscriber<CachedValuesHistogram>() {
@Override
public void onCompleted() {
latch.countDown();
}
@Override
public void onError(Throwable e) {
fail(e.getMessage());
}
@Override
public void onNext(CachedValuesHistogram distribution) {
System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " Received distribution with count : " + distribution.getTotalCount() + " and mean : " + distribution.getMean());
if (distribution.getTotalCount() > 0) {
assertBetween(200, 250, (int) distribution.getMean());
}
}
});
for (final Command cmd : commands) {
//since these are blocking calls on the caller thread, we need a new caller thread for each command to actually get the desired concurrency
new Thread(new HystrixContextRunnable(new Runnable() {
@Override
public void run() {
cmd.observe();
}
})).start();
}
Command semaphoreRejected = Command.from(groupKey, key, HystrixEventType.SUCCESS);
try {
Thread.sleep(40);
semaphoreRejected.observe();
} catch (InterruptedException ie) {
fail(ie.getMessage());
}
try {
assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
} catch (InterruptedException ex) {
fail("Interrupted ex");
}
assertEquals(10, stream.getLatest().getTotalCount());
assertBetween(200, 250, stream.getLatestMean());
System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
assertTrue(semaphoreRejected.isResponseSemaphoreRejected());
}
use of com.netflix.hystrix.HystrixCommandKey in project Hystrix by Netflix.
the class RollingCommandLatencyDistributionStreamTest method testMultipleBucketsBothGetStored.
@Test
public void testMultipleBucketsBothGetStored() {
HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Latency-H");
stream = RollingCommandLatencyDistributionStream.getInstance(key, 10, 100);
stream.startCachingStreamValuesIfUnstarted();
final CountDownLatch latch = new CountDownLatch(1);
stream.observe().take(10).subscribe(new Subscriber<CachedValuesHistogram>() {
@Override
public void onCompleted() {
latch.countDown();
}
@Override
public void onError(Throwable e) {
fail(e.getMessage());
}
@Override
public void onNext(CachedValuesHistogram distribution) {
System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " Received distribution with count : " + distribution.getTotalCount() + " and mean : " + distribution.getMean());
if (distribution.getTotalCount() == 2) {
assertBetween(55, 90, (int) distribution.getMean());
}
if (distribution.getTotalCount() == 5) {
assertEquals(60, 90, (long) distribution.getMean());
}
}
});
Command cmd1 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 10);
Command cmd2 = Command.from(groupKey, key, HystrixEventType.FAILURE, 100);
cmd1.observe();
cmd2.observe();
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
fail("Interrupted ex");
}
Command cmd3 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 60);
Command cmd4 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 60);
Command cmd5 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 70);
cmd3.observe();
cmd4.observe();
cmd5.observe();
try {
assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
} catch (InterruptedException ex) {
fail("Interrupted ex");
}
assertBetween(55, 90, stream.getLatestMean());
assertBetween(10, 50, stream.getLatestPercentile(0.0));
assertBetween(100, 150, stream.getLatestPercentile(100.0));
}
use of com.netflix.hystrix.HystrixCommandKey in project Hystrix by Netflix.
the class RollingCommandMaxConcurrencyStreamTest method testMultipleCommandsCarryOverMultipleBucketsAndThenAgeOut.
/**
* BUCKETS
* A | B | C | D | E |
* 1: [-------------------------------]
* 2: [-------------------------------]
* 3: [--]
* 4: [--]
*
* Max concurrency should be 3, but by waiting for 30 bucket rolls, final max concurrency should be 0
*/
@Test
public void testMultipleCommandsCarryOverMultipleBucketsAndThenAgeOut() throws InterruptedException {
HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Concurrency-E");
stream = RollingCommandMaxConcurrencyStream.getInstance(key, 10, 100);
stream.startCachingStreamValuesIfUnstarted();
final CountDownLatch latch = new CountDownLatch(1);
stream.observe().take(30).subscribe(getSubscriber(latch));
Command cmd1 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 300);
Command cmd2 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 300);
Command cmd3 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 10);
Command cmd4 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 10);
cmd1.observe();
//bucket roll
Thread.sleep(100);
cmd2.observe();
Thread.sleep(100);
cmd3.observe();
Thread.sleep(100);
cmd4.observe();
assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
assertEquals(0, stream.getLatestRollingMax());
}
use of com.netflix.hystrix.HystrixCommandKey in project Hystrix by Netflix.
the class RollingCommandMaxConcurrencyStreamTest method testConcurrencyStreamProperlyFiltersOutShortCircuits.
@Test
public void testConcurrencyStreamProperlyFiltersOutShortCircuits() throws InterruptedException {
HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Concurrency-G");
stream = RollingCommandMaxConcurrencyStream.getInstance(key, 10, 100);
stream.startCachingStreamValuesIfUnstarted();
final CountDownLatch latch = new CountDownLatch(1);
stream.observe().take(10).subscribe(getSubscriber(latch));
//after 3 failures, next command should short-circuit.
//to prove short-circuited commands don't contribute to concurrency, execute 3 FAILURES in the first bucket sequentially
//then when circuit is open, execute 20 concurrent commands. they should all get short-circuited, and max concurrency should be 1
Command failure1 = Command.from(groupKey, key, HystrixEventType.FAILURE);
Command failure2 = Command.from(groupKey, key, HystrixEventType.FAILURE);
Command failure3 = Command.from(groupKey, key, HystrixEventType.FAILURE);
List<Command> shortCircuited = new ArrayList<Command>();
for (int i = 0; i < 20; i++) {
shortCircuited.add(Command.from(groupKey, key, HystrixEventType.SUCCESS, 100));
}
failure1.execute();
failure2.execute();
failure3.execute();
Thread.sleep(150);
for (Command cmd : shortCircuited) {
cmd.observe();
}
assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
assertEquals(1, stream.getLatestRollingMax());
}
Aggregations