@Test public void testEmptySum() { MockedTime time = new MockedTime(); HystrixRollingNumberEvent type = HystrixRollingNumberEvent.COLLAPSED; HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); assertEquals(0, counter.getRollingSum(type)); }
@Test public void testCumulativeCounterAfterRollingAndReset() { MockedTime time = new MockedTime(); HystrixRollingNumberEvent type = HystrixRollingNumberEvent.SUCCESS; HystrixRollingNumber counter = new HystrixRollingNumber(time, 20, 2); assertEquals(0, counter.getCumulativeSum(type)); // iterate over 20 buckets on a queue sized for 2 for (int i = 0; i < 20; i++) { // first bucket counter.increment(type); try { time.increment(counter.bucketSizeInMillseconds); } catch (Exception e) { // ignore } assertEquals(2, counter.getValues(type).length); counter.getValueOfLatestBucket(type); if (i == 5 || i == 15) { // simulate a reset occurring every once in a while // so we ensure the absolute sum is handling it okay counter.reset(); } } // cumulative count should be 20 (for the number of loops above) regardless of buckets rolling assertEquals(20, counter.getCumulativeSum(type)); }
@Test public void testRolling() { MockedTime time = new MockedTime(); HystrixRollingNumberEvent type = HystrixRollingNumberEvent.THREAD_MAX_ACTIVE; HystrixRollingNumber counter = new HystrixRollingNumber(time, 20, 2); // iterate over 20 buckets on a queue sized for 2 for (int i = 0; i < 20; i++) { // first bucket counter.getCurrentBucket(); try { time.increment(counter.bucketSizeInMillseconds); } catch (Exception e) { // ignore } assertEquals(2, counter.getValues(type).length); counter.getValueOfLatestBucket(type); // System.out.println("Head: " + counter.buckets.state.get().head); // System.out.println("Tail: " + counter.buckets.state.get().tail); } }
@Test public void testCumulativeCounterAfterRollingAndReset3() { MockedTime time = new MockedTime(); HystrixRollingNumberEvent type = HystrixRollingNumberEvent.SUCCESS; HystrixRollingNumber counter = new HystrixRollingNumber(time, 20, 2); assertEquals(0, counter.getCumulativeSum(type)); counter.increment(type); counter.increment(type); counter.increment(type); // iterate over 20 buckets on a queue sized for 2 for (int i = 0; i < 20; i++) { try { time.increment(counter.bucketSizeInMillseconds); } catch (Exception e) { // ignore } } // since we are rolling over the buckets it should reset naturally // no increments during the loop, just some before and after counter.increment(type); counter.increment(type); // cumulative count should be 5 regardless of buckets rolling assertEquals(5, counter.getCumulativeSum(type)); }
@Test public void testCumulativeCounterAfterRollingAndReset2() { MockedTime time = new MockedTime(); HystrixRollingNumberEvent type = HystrixRollingNumberEvent.SUCCESS; HystrixRollingNumber counter = new HystrixRollingNumber(time, 20, 2); assertEquals(0, counter.getCumulativeSum(type)); counter.increment(type); counter.increment(type); counter.increment(type); // iterate over 20 buckets on a queue sized for 2 for (int i = 0; i < 20; i++) { try { time.increment(counter.bucketSizeInMillseconds); } catch (Exception e) { // ignore } if (i == 5 || i == 15) { // simulate a reset occurring every once in a while // so we ensure the absolute sum is handling it okay counter.reset(); } } // no increments during the loop, just some before and after counter.increment(type); counter.increment(type); // cumulative count should be 5 regardless of buckets rolling assertEquals(5, counter.getCumulativeSum(type)); }
@Test public void testCumulativeCounterAfterRolling() { MockedTime time = new MockedTime(); HystrixRollingNumberEvent type = HystrixRollingNumberEvent.SUCCESS; HystrixRollingNumber counter = new HystrixRollingNumber(time, 20, 2); assertEquals(0, counter.getCumulativeSum(type)); // iterate over 20 buckets on a queue sized for 2 for (int i = 0; i < 20; i++) { // first bucket counter.increment(type); try { time.increment(counter.bucketSizeInMillseconds); } catch (Exception e) { // ignore } assertEquals(2, counter.getValues(type).length); counter.getValueOfLatestBucket(type); } // cumulative count should be 20 (for the number of loops above) regardless of buckets rolling assertEquals(20, counter.getCumulativeSum(type)); }
MockedTime time = new MockedTime(); try { HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); counter.increment(HystrixRollingNumberEvent.SUCCESS); counter.increment(HystrixRollingNumberEvent.SUCCESS); counter.increment(HystrixRollingNumberEvent.SUCCESS); counter.increment(HystrixRollingNumberEvent.SUCCESS); counter.increment(HystrixRollingNumberEvent.FAILURE); counter.increment(HystrixRollingNumberEvent.FAILURE); assertEquals(4, counter.getRollingSum(HystrixRollingNumberEvent.SUCCESS)); assertEquals(2, counter.getRollingSum(HystrixRollingNumberEvent.FAILURE)); assertEquals(0, counter.getRollingSum(HystrixRollingNumberEvent.SUCCESS)); assertEquals(0, counter.getRollingSum(HystrixRollingNumberEvent.FAILURE)); counter.increment(HystrixRollingNumberEvent.SUCCESS); assertEquals(1, counter.getRollingSum(HystrixRollingNumberEvent.SUCCESS)); assertEquals(0, counter.getRollingSum(HystrixRollingNumberEvent.FAILURE));
MockedTime time = new MockedTime(); try { HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); counter.updateRollingMax(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE, 10); counter.updateRollingMax(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE, 30); counter.updateRollingMax(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE, 20); assertEquals(30, counter.getRollingMaxValue(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE)); counter.updateRollingMax(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE, 30); counter.updateRollingMax(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE, 30); counter.updateRollingMax(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE, 50); assertEquals(50, counter.getValueOfLatestBucket(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE)); long values[] = counter.getValues(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE); assertEquals(30, values[0]); // oldest bucket assertEquals(0, values[1]);
/** * Get the value of the latest (current) bucket in the rolling counter for the given {@link HystrixRollingNumberEvent} type. * <p> * The {@link HystrixRollingNumberEvent} must be a "counter" type <code>HystrixRollingNumberEvent.isCounter() == true</code>. * * @param type * HystrixRollingNumberEvent defining which counter to retrieve value from * @return * value from latest bucket for given {@link HystrixRollingNumberEvent} counter type */ public long getValueOfLatestBucket(HystrixRollingNumberEvent type) { Bucket lastBucket = getCurrentBucket(); if (lastBucket == null) return 0; // we have bucket data so we'll return the lastBucket return lastBucket.get(type); }
} else if (currentTime - (lastBucket.windowStart + this.bucketSizeInMillseconds) > timeInMilliseconds) { reset(); return getCurrentBucket(); } else { // we're past the window so we need to create a new bucket return getCurrentBucket();
@Test public void testIncrementInSingleBucket() { MockedTime time = new MockedTime(); try { HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); // increment counter.increment(HystrixRollingNumberEvent.SUCCESS); counter.increment(HystrixRollingNumberEvent.SUCCESS); counter.increment(HystrixRollingNumberEvent.SUCCESS); counter.increment(HystrixRollingNumberEvent.SUCCESS); counter.increment(HystrixRollingNumberEvent.FAILURE); counter.increment(HystrixRollingNumberEvent.FAILURE); counter.increment(HystrixRollingNumberEvent.TIMEOUT); // we should have 1 bucket assertEquals(1, counter.buckets.size()); // the count should be 4 assertEquals(4, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.SUCCESS).sum()); assertEquals(2, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.FAILURE).sum()); assertEquals(1, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.TIMEOUT).sum()); } catch (Exception e) { e.printStackTrace(); fail("Exception: " + e.getMessage()); } }
/** * Get the rolling count for the given {@link HystrixRollingNumberEvent}. * <p> * The rolling window is defined by {@link HystrixCommandProperties#metricsRollingStatisticalWindowInMilliseconds()}. * * @param event * {@link HystrixRollingNumberEvent} of the event to retrieve a sum for * @return long rolling count */ public long getRollingCount(HystrixRollingNumberEvent event) { return counter.getRollingSum(event); }
@Test public void testEmptyLatestValue() { MockedTime time = new MockedTime(); HystrixRollingNumberEvent type = HystrixRollingNumberEvent.THREAD_MAX_ACTIVE; HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); assertEquals(0, counter.getValueOfLatestBucket(type)); }
@Test public void testMaxValue() { MockedTime time = new MockedTime(); try { HystrixRollingNumberEvent type = HystrixRollingNumberEvent.THREAD_MAX_ACTIVE; HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); counter.updateRollingMax(type, 10); // sleep to get to a new bucket time.increment(counter.bucketSizeInMillseconds); counter.updateRollingMax(type, 30); // sleep to get to a new bucket time.increment(counter.bucketSizeInMillseconds); counter.updateRollingMax(type, 40); // sleep to get to a new bucket time.increment(counter.bucketSizeInMillseconds); counter.updateRollingMax(type, 15); assertEquals(40, counter.getRollingMaxValue(type)); } catch (Exception e) { e.printStackTrace(); fail("Exception: " + e.getMessage()); } }
/** * Get the cumulative count since the start of the application for the given {@link HystrixRollingNumberEvent}. * * @param event * {@link HystrixRollingNumberEvent} of the event to retrieve a sum for * @return long cumulative count */ public long getCumulativeCount(HystrixRollingNumberEvent event) { return counter.getCumulativeSum(event); }
/** * Get the cumulative sum of all buckets ever since the JVM started without rolling for the given {@link HystrixRollingNumberEvent} type. * <p> * See {@link #getRollingSum(HystrixRollingNumberEvent)} for the rolling sum. * <p> * The {@link HystrixRollingNumberEvent} must be a "counter" type <code>HystrixRollingNumberEvent.isCounter() == true</code>. * * @param type HystrixRollingNumberEvent defining which counter to retrieve values from * @return cumulative sum of all increments and adds for the given {@link HystrixRollingNumberEvent} counter type */ public long getCumulativeSum(HystrixRollingNumberEvent type) { // this isn't 100% atomic since multiple threads can be affecting latestBucket & cumulativeSum independently // but that's okay since the count is always a moving target and we're accepting a "point in time" best attempt // we are however putting 'getValueOfLatestBucket' first since it can have side-affects on cumulativeSum whereas the inverse is not true return getValueOfLatestBucket(type) + cumulativeSum.get(type); }
/** * Get the max value of values in all buckets for the given {@link HystrixRollingNumberEvent} type. * <p> * The {@link HystrixRollingNumberEvent} must be a "max updater" type <code>HystrixRollingNumberEvent.isMaxUpdater() == true</code>. * * @param type * HystrixRollingNumberEvent defining which "max updater" to retrieve values from * @return max value for given {@link HystrixRollingNumberEvent} type during rolling window */ public long getRollingMaxValue(HystrixRollingNumberEvent type) { long values[] = getValues(type); if (values.length == 0) { return 0; } else { Arrays.sort(values); return values[values.length - 1]; } }
@Test public void testEmptyMax() { MockedTime time = new MockedTime(); HystrixRollingNumberEvent type = HystrixRollingNumberEvent.THREAD_MAX_ACTIVE; HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); assertEquals(0, counter.getRollingMaxValue(type)); }
public void resetCounter() { // TODO can we do without this somehow? counter.reset(); lastHealthCountsSnapshot.set(System.currentTimeMillis()); healthCountsSnapshot = new HealthCounts(0, 0, 0); }
public void markFailure() { counter.increment(HystrixRollingNumberEvent.FAILURE); }