/** * Get the rolling window specified by metric and user. * * @param metric the updated metric * @param user the user that updated the metric * @return the rolling window */ private RollingWindow getRollingWindow(String metric, String user) { RollingWindowMap rwMap = metricMap.get(metric); if (rwMap == null) { rwMap = new RollingWindowMap(); RollingWindowMap prevRwMap = metricMap.putIfAbsent(metric, rwMap); if (prevRwMap != null) { rwMap = prevRwMap; } } RollingWindow window = rwMap.get(user); if (window != null) { return window; } window = new RollingWindow(windowLenMs, bucketsPerWindow); RollingWindow prevWindow = rwMap.putIfAbsent(user, window); if (prevWindow != null) { window = prevWindow; } return window; } }
String userName = entry.getKey(); RollingWindow aWindow = entry.getValue(); long windowSum = aWindow.getSum(time);
/** * Called when the metric command is changed by "delta" units at time "time" * via user "user" * * @param time the time of the event * @param command the metric that is updated, e.g., the operation name * @param user the user that updated the metric * @param delta the amount of change in the metric, e.g., +1 */ public void recordMetric(long time, String command, String user, long delta) { RollingWindow window = getRollingWindow(command, user); window.incAt(time, delta); }
@Test public void testReorderedAccess() { RollingWindow window = new RollingWindow(WINDOW_LEN, BUCKET_CNT); long time = 2 * WINDOW_LEN + BUCKET_LEN * 3 / 2; window.incAt(time, 5); time++; Assert.assertEquals( "The sum of rolling window does not reflect the recent update", 5, window.getSum(time)); long reorderedTime = time - 2 * BUCKET_LEN; window.incAt(reorderedTime, 6); Assert.assertEquals( "The sum of rolling window does not reflect the reordered update", 11, window.getSum(time)); time = reorderedTime + WINDOW_LEN; Assert.assertEquals( "The sum of rolling window does not reflect rolling effect", 5, window.getSum(time)); }
/** * When an event occurs at the specified time, this method reflects that in * the rolling window. * <p/> * * @param time the time at which the event occurred * @param delta the delta that will be added to the window */ public void incAt(long time, long delta) { int bi = computeBucketIndex(time); Bucket bucket = buckets[bi]; // If the last time the bucket was updated is out of the scope of the // rolling window, reset the bucket. if (bucket.isStaleNow(time)) { bucket.safeReset(time); } bucket.inc(delta); }
@Test public void testBasics() { RollingWindow window = new RollingWindow(WINDOW_LEN, BUCKET_CNT); long time = 1; Assert.assertEquals("The initial sum of rolling window must be 0", 0, window.getSum(time)); time = WINDOW_LEN + BUCKET_LEN * 3 / 2; Assert.assertEquals("The initial sum of rolling window must be 0", 0, window.getSum(time)); window.incAt(time, 5); Assert.assertEquals( "The sum of rolling window does not reflect the recent update", 5, window.getSum(time)); time += BUCKET_LEN; window.incAt(time, 6); Assert.assertEquals( "The sum of rolling window does not reflect the recent update", 11, window.getSum(time)); time += WINDOW_LEN - BUCKET_LEN; Assert.assertEquals( "The sum of rolling window does not reflect rolling effect", 6, window.getSum(time)); time += BUCKET_LEN; Assert.assertEquals( "The sum of rolling window does not reflect rolling effect", 0, window.getSum(time)); }
/** * When an event occurs at the specified time, this method reflects that in * the rolling window. * <p/> * * @param time the time at which the event occurred * @param delta the delta that will be added to the window */ public void incAt(long time, long delta) { int bi = computeBucketIndex(time); Bucket bucket = buckets[bi]; // If the last time the bucket was updated is out of the scope of the // rolling window, reset the bucket. if (bucket.isStaleNow(time)) { bucket.safeReset(time); } bucket.inc(delta); }
/** * Called when the metric command is changed by "delta" units at time "time" * via user "user" * * @param time the time of the event * @param command the metric that is updated, e.g., the operation name * @param user the user that updated the metric * @param delta the amount of change in the metric, e.g., +1 */ public void recordMetric(long time, String command, String user, long delta) { RollingWindow window = getRollingWindow(command, user); window.incAt(time, delta); }
String userName = entry.getKey(); RollingWindow aWindow = entry.getValue(); long windowSum = aWindow.getSum(time);
/** * Get the rolling window specified by metric and user. * * @param metric the updated metric * @param user the user that updated the metric * @return the rolling window */ private RollingWindow getRollingWindow(String metric, String user) { RollingWindowMap rwMap = metricMap.get(metric); if (rwMap == null) { rwMap = new RollingWindowMap(); RollingWindowMap prevRwMap = metricMap.putIfAbsent(metric, rwMap); if (prevRwMap != null) { rwMap = prevRwMap; } } RollingWindow window = rwMap.get(user); if (window != null) { return window; } window = new RollingWindow(windowLenMs, bucketsPerWindow); RollingWindow prevWindow = rwMap.putIfAbsent(user, window); if (prevWindow != null) { window = prevWindow; } return window; }
/** * When an event occurs at the specified time, this method reflects that in * the rolling window. * <p/> * * @param time the time at which the event occurred * @param delta the delta that will be added to the window */ public void incAt(long time, long delta) { int bi = computeBucketIndex(time); Bucket bucket = buckets[bi]; // If the last time the bucket was updated is out of the scope of the // rolling window, reset the bucket. if (bucket.isStaleNow(time)) { bucket.safeReset(time); } bucket.inc(delta); }
/** * Called when the metric command is changed by "delta" units at time "time" * via user "user" * * @param time the time of the event * @param command the metric that is updated, e.g., the operation name * @param user the user that updated the metric * @param delta the amount of change in the metric, e.g., +1 */ public void recordMetric(long time, String command, String user, long delta) { RollingWindow window = getRollingWindow(command, user); window.incAt(time, delta); }
String userName = entry.getKey(); RollingWindow aWindow = entry.getValue(); long windowSum = aWindow.getSum(time);
/** * Get the rolling window specified by metric and user. * * @param metric the updated metric * @param user the user that updated the metric * @return the rolling window */ private RollingWindow getRollingWindow(String metric, String user) { RollingWindowMap rwMap = metricMap.get(metric); if (rwMap == null) { rwMap = new RollingWindowMap(); RollingWindowMap prevRwMap = metricMap.putIfAbsent(metric, rwMap); if (prevRwMap != null) { rwMap = prevRwMap; } } RollingWindow window = rwMap.get(user); if (window != null) { return window; } window = new RollingWindow(windowLenMs, bucketsPerWindow); RollingWindow prevWindow = rwMap.putIfAbsent(user, window); if (prevWindow != null) { window = prevWindow; } return window; }