ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Sentinel源码之ClusterBuilderSlot解析

2022-05-21 10:31:52  阅读:178  来源: 互联网

标签:node count prioritized args ClusterBuilderSlot resourceWrapper 源码 context Sentin


ClusterBuilderSlot解析

ClusterBuilderSlot:则用于存储资源的统计信息以及调用者信息,例如该资源的 RT, QPS, thread count 等等,这些信息将用作为多维度限流,降级的依据;

//NodeSelectorSlot.entry()
@Override
public void entry(Context context, ResourceWrapper resourceWrapper, Object obj, int count, boolean prioritized, Object... args)
 throws Throwable {
 // 从缓存中获取,创建DefaultNode
 DefaultNode node = map.get(context.getName());
 // 双重判断,如果判断为空
 if (node == null) {
     synchronized (this) {
         node = map.get(context.getName());
         if (node == null) {
             // 创建一个DefaultNode并且放入到缓存中
             node = new DefaultNode(resourceWrapper, null);
             HashMap<String, DefaultNode> cacheMap = new HashMap<String, DefaultNode>(map.size());
             cacheMap.putAll(map);
             cacheMap.put(context.getName(), node);
             map = cacheMap;
             // Build invocation tree
             // 将新建的Node添加到调用树中
             ((DefaultNode) context.getLastNode()).addChild(node);
         }

     }
 }

 context.setCurNode(node);
 // 触发下一个节点
 fireEntry(context, resourceWrapper, node, count, prioritized, args);
}

再触发下一个节点以后,调用的是父类AbstractLinkedProcessorSlot.fireEntry()方法,然后next调用transformEntry

@Override
public void fireEntry(Context context, ResourceWrapper resourceWrapper, Object obj, int count, boolean prioritized, Object... args)
 throws Throwable {
 if (next != null) {
     // 调用下一个节点
     next.transformEntry(context, resourceWrapper, obj, count, prioritized, args);
 }
}

//next就代表循环到下一个节点所以这里调用entry的就是ClusterBuilderSlot
@SuppressWarnings("unchecked")
void transformEntry(Context context, ResourceWrapper resourceWrapper, Object o, int count, boolean prioritized, Object... args)
     throws Throwable {
     T t = (T)o;
     entry(context, resourceWrapper, t, count, prioritized, args);
 }

ClusterBuilderSlot

// ClusterBuilderSlot.entry
@Override
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count,
               boolean prioritized, Object... args)
 throws Throwable {
 if (clusterNode == null) {
     synchronized (lock) {
         if (clusterNode == null) {
             // Create the cluster node.
             clusterNode = new ClusterNode(resourceWrapper.getName(), resourceWrapper.getResourceType());
             // key为资源 value为ClusterNode
             HashMap<ResourceWrapper, ClusterNode> newMap = new HashMap<>(Math.max(clusterNodeMap.size(), 16));
             newMap.putAll(clusterNodeMap);
             newMap.put(node.getId(), clusterNode);

             clusterNodeMap = newMap;
         }
     }
 }
	// 添加节点
 node.setClusterNode(clusterNode);

 /*
         * if context origin is set, we should get or create a new {@link Node} of
         * the specific origin.
         */
    // 确认资源的来源
    if (!"".equals(context.getOrigin())) {
        Node originNode = node.getClusterNode().getOrCreateOriginNode(context.getOrigin());
        context.getCurEntry().setOriginNode(originNode);
    }

    fireEntry(context, resourceWrapper, node, count, prioritized, args);
}

总结

ClusterNode作用就是与DefaultNode进行关联,即不同的DefaultNode都关联了一个ClusterNode,这样我们在不同上下文中都可以拿到当前资源一个总的流量统计情况。

标签:node,count,prioritized,args,ClusterBuilderSlot,resourceWrapper,源码,context,Sentin
来源: https://www.cnblogs.com/ZT-666/p/16294553.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有