【sentinel】深入浅出之原理篇StatisticSlot&滑动窗口

StatisticSlot则用于记录,统计不同纬度的 runtime 信息,在这里记录线程数变化,请求数量,计算RT时间,代码比较简单:

public class StatisticSlot extends AbstractLinkedProcessorSlot<DefaultNode> {

    @Override
    public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count,
                      boolean prioritized, Object... args) throws Throwable {
        try {

            fireEntry(context, resourceWrapper, node, count, prioritized, args);
            //请求通过,增加线程数
            node.increaseThreadNum();
            //增加请求通过数
            node.addPassRequest(count);
            //如果原始节点存在,则新增线程数和通过的请求总数
            if (context.getCurEntry().getOriginNode() != null) {
                context.getCurEntry().getOriginNode().increaseThreadNum();
                context.getCurEntry().getOriginNode().addPassRequest(count);
            }
            //如果是IN,则在Cluster节点上新增线程数和通过请求数,这个是全局的ClusterNode,和ClusterBuilderSlot的ClusterNode不一样,此处所有请求共享同一个Cluster
            if (resourceWrapper.getType() == EntryType.IN) {
                // Add count for global inbound entry node for global statistics.
                Constants.ENTRY_NODE.increaseThreadNum();
                Constants.ENTRY_NODE.addPassRequest(count);
            }
            //钩子函数
            for (ProcessorSlotEntryCallback<DefaultNode> handler : StatisticSlotCallbackRegistry.getEntryCallbacks()) {
                handler.onPass(context, resourceWrapper, node, count, args);
            }
        } catch (PriorityWaitException ex) {
            //增加线程数
            node.increaseThreadNum();
            if (context.getCurEntry().getOriginNode() != null) {
                context.getCurEntry().getOriginNode().increaseThreadNum();
            }
            //增加线程数 共享全局Cluster
            if (resourceWrapper.getType() == EntryType.IN) {
                Constants.ENTRY_NODE.increaseThreadNum();
            }
             //钩子函数
            for (ProcessorSlotEntryCallback<DefaultNode> handler : StatisticSlotCallbackRegistry.getEntryCallbacks()) {
                handler.onPass(context, resourceWrapper, node, count, args);
            }
        } catch (BlockException e) {
            context.getCurEntry().setError(e);
            //节点Block数量加一
            node.increaseBlockQps(count);
            if (context.getCurEntry().getOriginNode() != null) {
                context.getCurEntry().getOriginNode().increaseBlockQps(count);
            }
            if (resourceWrapper.getType() == EntryType.IN) {
                Constants.ENTRY_NODE.increaseBlockQps(count);
            }
            //钩子,扩展
            for (ProcessorSlotEntryCallback<DefaultNode> handler : StatisticSlotCallbackRegistry.getEntryCallbacks()) {
                handler.onBlocked(e, context, resourceWrapper, node, count, args);
            }

            throw e;
        } catch (Throwable e) {
            context.getCurEntry().setError(e);
            node.increaseExceptionQps(count);
            if (context.getCurEntry().getOriginNode() != null) {
                context.getCurEntry().getOriginNode().increaseExceptionQps(count);
            }

            if (resourceWrapper.getType() == EntryType.IN) {
                Constants.ENTRY_NODE.increaseExceptionQps(count);
            }
            throw e;
        }
    }

    @Override
    public void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
        DefaultNode node = (DefaultNode)context.getCurNode();
        if (context.getCurEntry().getError() == null) {
            //计算响应时间,通过当前时间-CurEntry的创建时间取毫秒值
            long rt = TimeUtil.currentTimeMillis() - context.getCurEntry().getCreateTime();
            if (rt > Constants.TIME_DROP_VALVE) {
                rt = Constants.TIME_DROP_VALVE;
            }
            //新增响应时间和成功数
            node.addRtAndSuccess(rt, count);
            if (context.getCurEntry().getOriginNode() != null) {
                context.getCurEntry().getOriginNode().addRtAndSuccess(rt, count);
            }
            //线程数减1
            node.decreaseThreadNum();
            if (context.getCurEntry().getOriginNode() != null) {
                context.getCurEntry().getOriginNode().decreaseThreadNum();
            }
            //全局线程数-1
            if (resourceWrapper.getType() == EntryType.IN) {
                Constants.ENTRY_NODE.addRtAndSuccess(rt, count);
                Constants.ENTRY_NODE.decreaseThreadNum();
            }
        } else {
            // Error may happen.
        }
        //回调钩子
        Collection<ProcessorSlotExitCallback> exitCallbacks = StatisticSlotCallbackRegistry.getExitCallbacks();
        for (ProcessorSlotExitCallback handler : exitCallbacks) {
            handler.onExit(context, resourceWrapper, count, args);
        }
        fireExit(context, resourceWrapper, count);
    }
}

逻辑简单,但实现并不简单,先了解一下DefaultNode的Api:


public class DefaultNode extends StatisticNode {

   private ResourceWrapper id;
   private volatile Set<Node> childList = new HashSet<>();
   private ClusterNode clusterNode;

   @Override
   public void increaseBlockQps(int count) {
       super.increaseBlockQps(count);
       this.clusterNode.increaseBlockQps(count);
   }

   @Override
   public void increaseExceptionQps(int count) {
       super.increaseExceptionQps(count);
       this.clusterNode.increaseExceptionQps(count);
   }

   @Override
   public void addRtAndSuccess(long rt, int successCount) {
       super.addRtAndSuccess(rt, successCount);
       this.clusterNode.addRtAndSuccess(rt, successCount);
   }

   @Override
   public void increaseThreadNum() {
       super.increaseThreadNum();
       this.clusterNode.increaseThreadNum();
   }

   @Override
   public void decreaseThreadNum() {
       super.decreaseThreadNum();
       this.clusterNode.decreaseThreadNum();
   }

   @Override
   public void addPassRequest(int count) {
       super.addPassRequest(count);
       this.clusterNode.addPassRequest(count);
   }

   private void visitTree(int level, DefaultNode node) {
       for (int i = 0; i < level; ++i) {
           System.out.print("-");
       }
       if (!(node instanceof EntranceNode)) {
           System.out.println(
               String.format("%s(thread:%s pq:%s bq:%s tq:%s rt:%s 1mp:%s 1mb:%s 1mt:%s)", node.id.getShowName(),
                   node.curThreadNum(), node.passQps(), node.blockQps(), node.totalQps(), node.avgRt(),
                   node.totalRequest() - node.blockRequest(), node.blockRequest(), node.totalRequest()));
       } else {
           System.out.println(
               String.format("Entry-%s(t:%s pq:%s bq:%s tq:%s rt:%s 1mp:%s 1mb:%s 1mt:%s)", node.id.getShowName(),
                   node.curThreadNum(), node.passQps(), node.blockQps(), node.totalQps(), node.avgRt(),
                   node.totalRequest() - node.blockRequest(), node.blockRequest(), node.totalRequest()));
       }
       for (Node n : node.getChildList()) {
           DefaultNode dn = (DefaultNode)n;
           visitTree(level + 1, dn);
       }
   }

}

上文链接 ClusterBuilderSlot原理介绍已经提到过,一个ContextName对应的同一个Resource对应ClusterNode为同一个,所以这里同步新增,或减少记录数,都是基于当前节点和对应的ClusterNode一起统计的。
不管是ClusterNode,或者DefaultNode节点,对其添加,或记录Qps,rt都是基于父类去实现,这样来讲,所有Sentinel最核心的代码就在StatisticNode中。


StatisticNode中,是这样注释的:

Sentinel使用滑动窗口来记录和统计实时调用数据。

  • 当第一个请求到来,Sentinel会创建一个特殊的时间片(time-span)去保存运行时的数据,比如:响应时间(rt),QPS, block request,在这里叫做滑动窗口(window bucket),这个滑动窗口通过sample count定义。Sentinel通过滑动窗口有效的数据来决定当前请求是否通过,滑动窗口将记录所有的qps,将其与规则中定义的阈值进行比较。
  • 不同的请求进来,根据不同的时间存放在不同滑动窗口中。
  • 请求不断的进入系统,先前的滑动窗口将会过期无效。

理解StatisticNode节点之前,先了解几个数据结构:

  • LeapArray Sentinel中的metrics的基本数据结构
    • LeapArray使用滑动窗口算法统计数据,每一个桶覆盖windowLengthInMs的时间长数据,总的时间长度是intervalInMs,所以,sampleCount = intervalInMs / windowLengthInMs。
public abstract class LeapArray<T> {
    //单位时间窗口长度
    protected int windowLengthInMs;
    //总的桶个数
    protected int sampleCount;
    //总的时间长度
    protected int intervalInMs;
    //记录的窗口数,长度与sampleCount一样
    protected final AtomicReferenceArray<WindowWrap<T>> array;
}

构造方法如下:

public LeapArray(int sampleCount, int intervalInMs) {
    //每ms的窗口长度为总的时间长度/桶的总数
    this.windowLengthInMs = intervalInMs / sampleCount;
    this.intervalInMs = intervalInMs;
    this.sampleCount = sampleCount;
    //记录每个windowLengthInMs的滑动窗口信息
    this.array = new AtomicReferenceArray<>(sampleCount);
}

而在WindowWrap中,则记录了该窗口的开始时间,和时长,和该时间窗口的数据信息。

public class WindowWrap<T> {
    //窗口长度
    private final long windowLengthInMs;
    //窗口开始时间 long类型,
    private long windowStart;
    //data数据
    private T value;
    //复位该时间窗口
    public WindowWrap<T> resetTo(long startTime) {
        this.windowStart = startTime;
        return this;
    }
    //判断是否该时间在该窗口内
    public boolean isTimeInWindow(long timeMillis) {
        return windowStart <= timeMillis && timeMillis < windowStart + windowLengthInMs;
    }
}

继续回到 LeapArray,看看如何根据时间找到该窗口:

  • 根据当前时间,算出该时间的timeId,并根据timeId算出当前窗口在采样窗口数组中的索引idx
  • 根据当前时间算出当前窗口的应该对应的开始时间time,以毫秒为单位,时间窗口开始时间为 windowLengthInMs的整数倍(取该时间单位整数开始时间,比如1000501,则从1000500开始)
  • 获取idx位置的窗口
public WindowWrap<T> currentWindow(long timeMillis) {
    if (timeMillis < 0) {
        return null;
    }
    //计算当前时间的时间窗口的位置
    int idx = calculateTimeIdx(timeMillis);
    //计算当前时间窗口的开始时间
    long windowStart = calculateWindowStart(timeMillis);
    while (true) {
        //取该下表对应的时间窗口
        WindowWrap<T> old = array.get(idx);
        if (old == null) {
            //不存在,则创建一个新的
            WindowWrap<T> window = new WindowWrap<T>(windowLengthInMs, windowStart, newEmptyBucket());
            if (array.compareAndSet(idx, null, window)) {
                return window;
            } else {
                //如果失败,则代表有其他的线程再创建,放弃时间片
                Thread.yield();
            }
        } else if (windowStart == old.windowStart()) {
            如果是这个窗口的开始时间,则直接返回
            return old;
        } else if (windowStart > old.windowStart()) {
            //如果当前时间的窗口开始时间>老的时间窗口,则重置该时间窗口时间
            // 防止并发,加重入锁
            if (updateLock.tryLock()) {
                try {
                    return resetWindowTo(old, windowStart);
                } finally {
                    updateLock.unlock();
                }
            } else {
                //失败则代表锁已经被其他线程占用
                Thread.yield();
            }
        } else if (windowStart < old.windowStart()) {
            return new WindowWrap<T>(windowLengthInMs, windowStart, newEmptyBucket());
        }
    }
}

而在StatisticNode节点中,实质也是使用LeapArray来存储,从LeapArray中获取MetricBucket,对QPS,请求线程数,rt时间等坐记录。
再来看一下StatisticNode的定义:

public class StatisticNode implements Node {
    //每秒的滚动计数器 SAMPLE_COUNT为2对应LeapArray中的sample count,IntervalProperty.INTERVAL为1000代表1s,1s分为两个桶,保存数据。
    private transient volatile Metric rollingCounterInSecond = new ArrayMetric(SampleCountProperty.SAMPLE_COUNT,
        IntervalProperty.INTERVAL);
    //每分钟的滚动计数器1分钟分为60个记录,1分钟一个。
    private transient Metric rollingCounterInMinute = new ArrayMetric(60, 60 * 1000, false);
    //当前线程数
    private AtomicInteger curThreadNum = new AtomicInteger(0);
    //最后一次metrics被获取的时间
    private long lastFetchTime = -1;
}

所以,在添加rt时间,qps,BlockQps等实质都是使用LeapArray的当前窗口去做添加

//StatisticNode.java
@Override
public void addPassRequest(int count) {
    rollingCounterInSecond.addPass(count);
    rollingCounterInMinute.addPass(count);
}

@Override
public void addRtAndSuccess(long rt, int successCount) {
    rollingCounterInSecond.addSuccess(successCount);
    rollingCounterInSecond.addRT(rt);
    rollingCounterInMinute.addSuccess(successCount);
    rollingCounterInMinute.addRT(rt);
}
@Override
public void increaseBlockQps(int count) {
    rollingCounterInSecond.addBlock(count);
    rollingCounterInMinute.addBlock(count);
}
@Override
public void increaseExceptionQps(int count) {
    rollingCounterInSecond.addException(count);
    rollingCounterInMinute.addException(count);
}   
@Override
public void addBlock(int count) {
    WindowWrap<MetricBucket> wrap = data.currentWindow();
    wrap.value().addBlock(count);
}

@Override
public void addSuccess(int count) {
    //当前窗口
    WindowWrap<MetricBucket> wrap = data.currentWindow();
    wrap.value().addSuccess(count);
}

@Override
public void addPass(int count) {
    WindowWrap<MetricBucket> wrap = data.currentWindow();
    wrap.value().addPass(count);
}

@Override
public void addRT(long rt) {
    WindowWrap<MetricBucket> wrap = data.currentWindow();
    wrap.value().addRT(rt);
}

https://www.jianshu.com/p/6ee4b7bdb844 这篇博客对滑动窗口讲的比较细,可以看看。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • 在一个方法内部定义的变量都存储在栈中,当这个函数运行结束后,其对应的栈就会被回收,此时,在其方法体中定义的变量将不...
    Y了个J阅读 4,390评论 1 14
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,036评论 1 32
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,612评论 4 59
  • 问题:文件名含中文时,pd.read_excel读取该文件报错,ascii码无法解码bytes 代码示例: raw...
    一木之夏阅读 19,365评论 1 4
  • 麦子又黄了,一穗穗金灿灿的,阳光下舞动,微风中摇曳,上演着一幕满城尽带黄金甲般如诗的画面。麦芒尖尖笔指向天空...
    木子_62b2阅读 517评论 0 0