/** * Get value represented by this window at the specified time * <p/> * * If time lags behind the latest update time, the new updates are still * included in the sum * * @param time * @return number of events occurred in the past period */ public long getSum(long time) { long sum = 0; for (Bucket bucket : buckets) { boolean stale = bucket.isStaleNow(time); if (!stale) { sum += bucket.value.get(); } if (LOG.isDebugEnabled()) { long bucketTime = bucket.updateTime.get(); String timeStr = new Date(bucketTime).toString(); LOG.debug("Sum: + " + sum + " Bucket: updateTime: " + timeStr + " (" + bucketTime + ") isStale " + stale + " at " + time); } } return sum; }
/** * Safely reset the bucket state considering concurrent updates (inc) and * resets. * * @param time the current time */ void safeReset(long time) { // At any point in time, only one thread is allowed to reset the // bucket synchronized (this) { if (isStaleNow(time)) { // reset the value before setting the time, it allows other // threads to safely assume that the value is updated if the // time is not stale value.set(0); updateTime.set(time); } // else a concurrent thread has already reset it: do nothing } }
/** * Get value represented by this window at the specified time * <p/> * * If time lags behind the latest update time, the new updates are still * included in the sum * * @param time * @return number of events occurred in the past period */ public long getSum(long time) { long sum = 0; for (Bucket bucket : buckets) { boolean stale = bucket.isStaleNow(time); if (!stale) { sum += bucket.value.get(); } if (LOG.isDebugEnabled()) { long bucketTime = bucket.updateTime.get(); String timeStr = new Date(bucketTime).toString(); LOG.debug("Sum: + " + sum + " Bucket: updateTime: " + timeStr + " (" + bucketTime + ") isStale " + stale + " at " + time); } } return sum; }
/** * Get value represented by this window at the specified time * <p/> * * If time lags behind the latest update time, the new updates are still * included in the sum * * @param time * @return number of events occurred in the past period */ public long getSum(long time) { long sum = 0; for (Bucket bucket : buckets) { boolean stale = bucket.isStaleNow(time); if (!stale) { sum += bucket.value.get(); } if (LOG.isDebugEnabled()) { long bucketTime = bucket.updateTime.get(); String timeStr = new Date(bucketTime).toString(); LOG.debug("Sum: + " + sum + " Bucket: updateTime: " + timeStr + " (" + bucketTime + ") isStale " + stale + " at " + time); } } return sum; }
/** * 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); }
/** * 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); }
/** * Safely reset the bucket state considering concurrent updates (inc) and * resets. * * @param time the current time */ void safeReset(long time) { // At any point in time, only one thread is allowed to reset the // bucket synchronized (this) { if (isStaleNow(time)) { // reset the value before setting the time, it allows other // threads to safely assume that the value is updated if the // time is not stale value.set(0); updateTime.set(time); } // else a concurrent thread has already reset it: do nothing } }
/** * Safely reset the bucket state considering concurrent updates (inc) and * resets. * * @param time the current time */ void safeReset(long time) { // At any point in time, only one thread is allowed to reset the // bucket synchronized (this) { if (isStaleNow(time)) { // reset the value before setting the time, it allows other // threads to safely assume that the value is updated if the // time is not stale value.set(0); updateTime.set(time); } // else a concurrent thread has already reset it: do nothing } }
/** * 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); }