一、Entry介绍
- Entry是sentinel中用来表示是资源是否可以继续执行一个凭证,可以理解为token。
- 使用SphU.entry()时,会返回一个entry,如果抛出了一个异常BlockException,说明资源被保护了。
- 使用 SphO.entry() 时,资源被保护了会返回false,反之true。
二、组成
先看代码
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
- createTime:当前entry的创建时间,毫秒值,用来计算响应时间rt。
- curNode:当前Entry所关联的node,会在NodeSelectorSlot插槽中设置,主要是记录了当前Context下的统计信息。
- originNode:context的请求源节点,通常是服务的消费端,如果存在的话,在ClusterBuilderSlot的entry方法中设置
- resourceWrapper:当前Entry所关联的资源包装器
CtEntry
CtEntry 是 Entry的子类,主要保存了实体之间的关系、调用链、上下文信息。
- parent:entry的父entry,用于在同一个context上下文中,多次调用entry方法,保存entry之间的关系。
- child:entry的子entry,与parent相反
- chain:entry中的插槽链,上一篇文章介绍过:见:https://www.jianshu.com/p/f2dd45f02fe8
- context: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的parent和child都是null,只是设置context.setCurEntry(this)了;
第二次调用时,context.getCurEntry()是有值的,这个时候设置parent和child;
Entry的关系如图:
接下来,如果还有entry进来,会继续设置parent,child关系;即在一个上下文context中保存entry之间的父子关系。
细心的读者可以发现在Entry中还有以下两个变量:
private Node curNode;
private Node originNode;
- 我们可以把Entry理解为Context这个树的树干,curNode理解为Entry树干的叶子,originNode为树的根。
- curNode保存了这一次调用统计信息,curNode节点在NodeSelectorSlot中设置的。
- originNode保存了在这个上下文中,所有的调用的统计信息和,originNode是一个StatisticNode节点,originNode设置的前提是origin不为空。
四、我的总结
1、Entry的含义,可以理解为一次调用的凭证。
2、介绍了Entry对象的组成部分及对应含义。
3、通过源码构建的分析,理解的parent和child的关系,在同一个上下文中初次调用parent和child都为空,后续在调用时parent即为上一调用enry。
4、Entry的curNode、originNode是用来保存统计信息Node。
以上内容,如有不当之处,请指正
网友评论