资讯专栏INFORMATION COLUMN

SlidingTimeWindowReservoir的大小控制

txgcwm / 2418人阅读

SlidingTimeWindowReservoir
/**
 * A {@link Reservoir} implementation backed by a sliding window that stores only the measurements made
 * in the last {@code N} seconds (or other time unit).
 */
public class SlidingTimeWindowReservoir implements Reservoir {
    // allow for this many duplicate ticks before overwriting measurements
    private static final int COLLISION_BUFFER = 256;
    // only trim on updating once every N
    private static final int TRIM_THRESHOLD = 256;

    private final Clock clock;
    private final ConcurrentSkipListMap measurements;
    private final long window;
    private final AtomicLong lastTick;
    private final AtomicLong count;

    /**
     * Creates a new {@link SlidingTimeWindowReservoir} with the given window of time.
     *
     * @param window     the window of time
     * @param windowUnit the unit of {@code window}
     */
    public SlidingTimeWindowReservoir(long window, TimeUnit windowUnit) {
        this(window, windowUnit, Clock.defaultClock());
    }

    /**
     * Creates a new {@link SlidingTimeWindowReservoir} with the given clock and window of time.
     *
     * @param window     the window of time
     * @param windowUnit the unit of {@code window}
     * @param clock      the {@link Clock} to use
     */
    public SlidingTimeWindowReservoir(long window, TimeUnit windowUnit, Clock clock) {
        this.clock = clock;
        this.measurements = new ConcurrentSkipListMap();
        this.window = windowUnit.toNanos(window) * COLLISION_BUFFER;
        this.lastTick = new AtomicLong(clock.getTick() * COLLISION_BUFFER);
        this.count = new AtomicLong();
    }

    @Override
    public int size() {
        trim();
        return measurements.size();
    }

    @Override
    public void update(long value) {
        if (count.incrementAndGet() % TRIM_THRESHOLD == 0) {
            trim();
        }
        measurements.put(getTick(), value);
    }

    @Override
    public Snapshot getSnapshot() {
        trim();
        return new UniformSnapshot(measurements.values());
    }

    private long getTick() {
        for (; ; ) {
            final long oldTick = lastTick.get();
            final long tick = clock.getTick() * COLLISION_BUFFER;
            // ensure the tick is strictly incrementing even if there are duplicate ticks
            final long newTick = tick - oldTick > 0 ? tick : oldTick + 1;
            if (lastTick.compareAndSet(oldTick, newTick)) {
                return newTick;
            }
        }
    }

    private void trim() {
        measurements.headMap(getTick() - window).clear();
    }
}
ConcurrentSkipListMap.headMap
    public void update(long value) {
        if (count.incrementAndGet() % TRIM_THRESHOLD == 0) {
            trim();
        }
        measurements.put(getTick(), value);
    }
    private void trim() {
        measurements.headMap(getTick() - window).clear();
    }

返回key小于/等于指定值的部分,然后清除掉

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66892.html

相关文章

  • 关于codahaleHistogramMetric

    摘要:百分位数第百分位数是这样一个值,它使得至少有的数据项小于或等于这个值,且至少有的数据项大于或等于这个值。即使极值变动大,相比其他几个,还是比较接近实际数据,曲线会有明显变动,不像其他的一段时间可能都是平滑的。 基本概念 mean(平均值) 均值是就全部数据计算的,它具有优良的数学性质,是实际中应用最广泛的集中趋势测度值.其主要缺点是易受数据极端值的影响,对于偏态分布的数据,均值的代表性...

    JiaXinYi 评论0 收藏0
  • rem 单位实现页面自适应布局

    摘要:单位介绍既然扯到了这个单位,那就有必要先解释下这个单位具体含义,上的解释注意图中的画线部分,这个单位需要高版本的浏览器支持,不过,一般用于移动端布局,所以,基本上无需考虑浏览器版本问题,放心用吧。相对于根元素的大小,自动计算出其具体值。 A. 先看一个函数: /* * 设置根元素字体大小 * @param Number minSW 最小缩放的设备屏幕宽度 * @param Nu...

    xavier 评论0 收藏0
  • summernote.js实际运用中遇到上传图片大小控制及bootstrap模态框嵌套关闭这两个问

    摘要:需求控制编辑框上传的图片不超过。从文件中找到如下图代码位置,选取需要上传的图片,获取图片的大小,将其当做新增参数,给标签新增属性。我的解决方法就是将在中加载出来的模态框中,关闭按钮的属性替换成代码的事件,用来隐藏模态框,如下图所示。 这里先看一下summernote.js模样,如下图,可以添加图片、视频、链接等。showImg(https://segmentfault.com/img/...

    madthumb 评论0 收藏0
  • viewport 理解

    摘要:首先屏幕是由一个一个显示单元组成的每一个显示单元都是物理世界真实存在的把一个显示单元的大小称为一个物理像素通常我们所说的分辨率就是指一块屏幕显示单元的个数比如叉表示这块屏幕由叉个显示单元组成其次通常情况下个显示单元对应计算机系统中的也就是说 首先 屏幕是由一个一个显示单元组成的.1 每一个显示单元都是物理世界真实存在的;2 把一个显示单元的大小称为一个物理像素;3 通常我们所说的 分辨...

    makeFoxPlay 评论0 收藏0

发表评论

0条评论

txgcwm

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<