一宗雇、Entry介紹
- Entry是sentinel中用來表示是資源是否可以繼續(xù)執(zhí)行一個憑證鸽照,可以理解為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的創(chuàng)建時間,毫秒值趾撵,用來計算響應(yīng)時間rt侄柔。
- curNode:當前Entry所關(guān)聯(lián)的node,會在NodeSelectorSlot插槽中設(shè)置占调,主要是記錄了當前Context下的統(tǒng)計信息暂题。
- originNode:context的請求源節(jié)點,通常是服務(wù)的消費端究珊,如果存在的話薪者,在ClusterBuilderSlot的entry方法中設(shè)置
- resourceWrapper:當前Entry所關(guān)聯(lián)的資源包裝器
CtEntry
CtEntry 是 Entry的子類,主要保存了實體之間的關(guān)系剿涮、調(diào)用鏈言津、上下文信息。
- parent:entry的父entry取试,用于在同一個context上下文中悬槽,多次調(diào)用entry方法,保存entry之間的關(guān)系瞬浓。
- child:entry的子entry初婆,與parent相反
- chain:entry中的插槽鏈,上一篇文章介紹過:見:http://www.reibang.com/p/f2dd45f02fe8
- context:entry的上下文
三瑟蜈、Entry源碼分析
Entry是在CtShp類調(diào)用創(chuàng)建的烟逊;
Entry e = new CtEntry(resourceWrapper, chain, context);
進入到CtEntry中:
CtEntry(ResourceWrapper resourceWrapper, ProcessorSlot<Object> chain, Context context) {
//調(diào)用父類構(gòu)造器
super(resourceWrapper);
//設(shè)置插槽鏈,資源的保護進行
this.chain = chain;
//設(shè)置上下文
this.context = context;
setUpEntryFor(context);
}
//更新entry父子關(guān)系
private void setUpEntryFor(Context context) {
// The entry should not be associated to NullContext.
if (context instanceof NullContext) {
return;
}
//獲取context的當前的entry铺根,并設(shè)為parent
this.parent = context.getCurEntry();
if (parent != null) {
//如果parent存在宪躯,并把parent的entry的child設(shè)為前的entry
((CtEntry)parent).child = this;
}
//設(shè)置context的當前entry
context.setCurEntry(this);
}
可以發(fā)現(xiàn)
1、在CtEntry的父類Entry中設(shè)置的entry的createTime為當前時間
2位迂、在CtEntry中访雪,主要對chain详瑞、context賦值以及parent、child的關(guān)系確定臣缀;
具體分析
當在一個context上下文中首次調(diào)用時坝橡,context.getCurEntry()為null;
Entry的關(guān)系如圖:
這個時候entry的parent和child都是null精置,只是設(shè)置context.setCurEntry(this)了计寇;
第二次調(diào)用時,context.getCurEntry()是有值的脂倦,這個時候設(shè)置parent和child番宁;
Entry的關(guān)系如圖:
接下來,如果還有entry進來赖阻,會繼續(xù)設(shè)置parent蝶押,child關(guān)系;即在一個上下文context中保存entry之間的父子關(guān)系火欧。
細心的讀者可以發(fā)現(xiàn)在Entry中還有以下兩個變量:
private Node curNode;
private Node originNode;
- 我們可以把Entry理解為Context這個樹的樹干棋电,curNode理解為Entry樹干的葉子,originNode為樹的根苇侵。
- curNode保存了這一次調(diào)用統(tǒng)計信息赶盔,curNode節(jié)點在NodeSelectorSlot中設(shè)置的。
- originNode保存了在這個上下文中衅檀,所有的調(diào)用的統(tǒng)計信息和招刨,originNode是一個StatisticNode節(jié)點霎俩,originNode設(shè)置的前提是origin不為空哀军。
四、我的總結(jié)
1打却、Entry的含義杉适,可以理解為一次調(diào)用的憑證。
2柳击、介紹了Entry對象的組成部分及對應(yīng)含義猿推。
3、通過源碼構(gòu)建的分析捌肴,理解的parent和child的關(guān)系蹬叭,在同一個上下文中初次調(diào)用parent和child都為空,后續(xù)在調(diào)用時parent即為上一調(diào)用enry状知。
4秽五、Entry的curNode、originNode是用來保存統(tǒng)計信息Node饥悴。
以上內(nèi)容坦喘,如有不當之處盲再,請指正