Sentinel之Entry构建源码解析

2018-12-19  本文已影响0人  橘子_好多灰

一、Entry介绍

二、组成

先看代码

public abstract class Entry {

    private static final Object[] OBJECTS0 = new Object[0];

    private long createTime;
    private Node curNode;
    /**
     * {@link Node} of the specific origin, Usually the origin is the Service Consumer.
     */
    private Node originNode;
    private Throwable error;
    protected ResourceWrapper resourceWrapper;
   
    //以下代码省略
}

class CtEntry extends Entry {

    protected Entry parent = null;
    protected Entry child = null;

    protected ProcessorSlot<Object> chain;
    protected Context context;
  
    //以下代码省略
}

Entry

CtEntry

CtEntry 是 Entry的子类,主要保存了实体之间的关系、调用链、上下文信息。

三、Entry源码分析

Entry是在CtShp类调用创建的;

    Entry e = new CtEntry(resourceWrapper, chain, context);

进入到CtEntry中:

  CtEntry(ResourceWrapper resourceWrapper, ProcessorSlot<Object> chain, Context context) {
        //调用父类构造器
        super(resourceWrapper);
        //设置插槽链,资源的保护进行
        this.chain = chain;
        //设置上下文
        this.context = context;

        setUpEntryFor(context);
    }
      
    //更新entry父子关系
    private void setUpEntryFor(Context context) {
        // The entry should not be associated to NullContext.
        if (context instanceof NullContext) {
            return;
        }
       //获取context的当前的entry,并设为parent
        this.parent = context.getCurEntry();
        if (parent != null) {
            //如果parent存在,并把parent的entry的child设为前的entry
            ((CtEntry)parent).child = this;
        }
        //设置context的当前entry
        context.setCurEntry(this);
    }

可以发现
1、在CtEntry的父类Entry中设置的entry的createTime为当前时间
2、在CtEntry中,主要对chain、context赋值以及parent、child的关系确定;

具体分析
当在一个context上下文中首次调用时,context.getCurEntry()为null;
Entry的关系如图:

Entry

这个时候entry的parent和child都是null,只是设置context.setCurEntry(this)了;

第二次调用时,context.getCurEntry()是有值的,这个时候设置parent和child;
Entry的关系如图:

entry

接下来,如果还有entry进来,会继续设置parent,child关系;即在一个上下文context中保存entry之间的父子关系。

细心的读者可以发现在Entry中还有以下两个变量:

    private Node curNode;

    private Node originNode;

四、我的总结

1、Entry的含义,可以理解为一次调用的凭证。
2、介绍了Entry对象的组成部分及对应含义。
3、通过源码构建的分析,理解的parent和child的关系,在同一个上下文中初次调用parent和child都为空,后续在调用时parent即为上一调用enry。
4、Entry的curNode、originNode是用来保存统计信息Node。


以上内容,如有不当之处,请指正

上一篇 下一篇

猜你喜欢

热点阅读