《一起学sentinel》三、Slot的子类及实现之NodeSelectorSlot和ClusterBuilderSlot

简介: 随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。

一、概述

在 Sentinel 里面,所有的资源都对应一个资源名称(resourceName),每次资源调用都会创建一个 Entry 对象。Entry 可以通过对主流框架的适配自动创建,也可以通过注解的方式或调用 SphU API 显式创建。Entry 创建候,同时也会创建一系列功能插槽(slot chain),这些插槽有不同的职责,例如:

  • NodeSelectorSlot 负责收集资源的路径,并将这些资源的调用路径,以树状结构存储起来,用于根据调用路径来限流降级;
  • ClusterBuilderSlot 则用于存储资源的统计信息以及调用者信息,例如该资源的 RT, QPS, thread count 等等,这些信息将用作为多维度限流,降级的依据;
  • StatisticSlot 则用于记录、统计不同纬度的 runtime 指标监控信息;
  • FlowSlot 则用于根据预设的限流规则以及前面 slot 统计的状态,来进行流量控制;
  • AuthoritySlot 则根据配置的黑白名单和调用来源信息,来做黑白名单控制;
  • DegradeSlot 则通过统计信息以及预设的规则,来做熔断降级;
  • SystemSlot 则通过系统的状态,例如 load1 等,来控制总的入口流量;

下面是关系结构图

image.png


二、NodeSelectorSlot分析

1.NodeSelectorSlot介绍

官方文档是这样描述NodeSelectorSlot的:这个 slot 主要负责收集资源的路径,并将这些资源的调用路径以树状结构存储起来,用于根据调用路径进行流量控制。

2.源码解读

     ContextUtil.enter("entrance1", "appA");
     Entry nodeA = SphU.entry("nodeA");
     if (nodeA != null) {
         nodeA.exit();
     }
     ContextUtil.exit();
 
     ContextUtil.enter("entrance2", "appA");
     nodeA = SphU.entry("nodeA");
     if (nodeA != null) {
         nodeA.exit();
     }
     ContextUtil.exit();
 
 Above code will generate the following invocation structure in memory:

 
                   machine-root
                  /         \
                  /           \
          EntranceNode1   EntranceNode2
                /               \
              /                 \
       DefaultNode(nodeA)   DefaultNode(nodeA)
              |                    |
              +- - - - - - - - - - +- - - - - - -> ClusterNode(nodeA);
 
 
 
 @Override
    public void entry(Context context, ResourceWrapper resourceWrapper, Object obj, int count, boolean prioritized, Object... args)
        throws Throwable {
        DefaultNode node = map.get(context.getName());
        if (node == null) {
            synchronized (this) {//1
                node = map.get(context.getName());
                if (node == null) {
                    node = new DefaultNode(resourceWrapper, null);//2
                    HashMap<String, DefaultNode> cacheMap = new HashMap<String, DefaultNode>(map.size());
                    cacheMap.putAll(map);
                    cacheMap.put(context.getName(), node);
                    map = cacheMap;
                    // Build invocation tree
                    ((DefaultNode) context.getLastNode()).addChild(node);//3
                }

            }
        }

        context.setCurNode(node);//4
        fireEntry(context, resourceWrapper, node, count, prioritized, args);
    }

1.在创建时使用了dubbocheck+synchronized 保证了事物

2.这个slot会在Context创建成功时,在内存中创建一个DefaultNode,在这一步锁定了resource对象

3.获取最新节点,将当前节点插入到当前的父节点的子节点上

4.将上下文的当前节点设置为本次创建的节点

sentinel创建上下文时,映射的标识是context的名称而不是resource的名称。所以不管在哪个context中相同的rosource会共享同一个ProcessorSlotChain。简单说就是需要在限定的执行内容中,定义了一个全局逻辑,这个全局逻辑正是resourceName

多个相同的resource(name),对应着不同的context(name),那么我们就可以快速统计出某个资源的总统计信息。对应着多个(归属于同一个resource的)DefaultNode对应同一个ClusterNode

三、ClusterBuilderSlot分析

1.ClusterBuilderSlot介绍

官方文档是这样描述ClusterBuilderSlot的:用于存储资源的统计信息以及调用者信息,例如该资源的 RT, QPS, thread count 等等,这些信息将用作为多维度限流,降级的依据,指的就是ClusterNode;

2.源码解读

@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) {//1
                // Create the cluster node.
                clusterNode = new ClusterNode(resourceWrapper.getName(), resourceWrapper.getResourceType());
                HashMap<ResourceWrapper, ClusterNode> newMap = new HashMap<>(Math.max(clusterNodeMap.size(), 16));
                newMap.putAll(clusterNodeMap);
                newMap.put(node.getId(), clusterNode);//2

                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())) {//3
        Node originNode = node.getClusterNode().getOrCreateOriginNode(context.getOrigin());
        context.getCurEntry().setOriginNode(originNode);
    }

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

1.判断clusterNode是否需要创建,需要就根据rocourceName创建一个

2.将clusterNode插入clusterNodeMap

3.如果指定了origin还会创造来源维度的node,作为origin级别的统计

项目的resource共享了相同的ProcessorSlotChain,无论在关联了哪个上下围,信息都被收集在resource对应的ClusterNode中,clusterNodeMap则存储了所有resource的统计信息。

四、小结

本期我们讲述了Slot的子类LogSlotStatisticSlot的基本实现原理。

现在建立我们的知识树

实例化DefaultNode和ClusterNode,创建结构树


创建上下文时,首先会在NodeSelectorSlot中判断是否有DefaultNode

如果没有则新增一个基于resourceDefaultNode,然后执行下一个slot

下一个slotClusterBuilderSlotClusterBuilderSlot会判断是否有对应的ClusterNode,如果没有则新增一个基于resource的ClusterNode并继续下一个流程(slot)。

总结来说,这个两个slot奠定了一个基于resource进行全局控制的基调。

目录
相关文章
|
3月前
|
监控 开发者 Sentinel
Sentinel解密之SlotChain中的各大SLot
Sentinel解密之SlotChain中的各大SLot
33 0
|
3月前
|
数据可视化 Sentinel 微服务
Sentinel解密:SlotChain中的SLot大揭秘
Sentinel解密:SlotChain中的SLot大揭秘
29 0
M4Y
|
存储 监控 Java
《一起学sentinel》六、Slot的子类及实现之FlowSlot和DegradeSlot
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
M4Y
781 0
《一起学sentinel》六、Slot的子类及实现之FlowSlot和DegradeSlot
M4Y
|
存储 监控 算法
《一起学sentinel》五、Slot的子类及实现之AuthoritySlot和SystemSlot
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
M4Y
870 0
《一起学sentinel》五、Slot的子类及实现之AuthoritySlot和SystemSlot
M4Y
|
存储 监控 API
《一起学sentinel》四、Slot的子类及实现之LogSlot和StatisticSlot
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
M4Y
478 0
《一起学sentinel》四、Slot的子类及实现之LogSlot和StatisticSlot
M4Y
|
存储 监控 应用服务中间件
《一起学sentinel》二、初探sentinel的Slot
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
M4Y
1400 0
|
15天前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
|
16天前
|
Java API Nacos
第十二章 Spring Cloud Alibaba Sentinel
第十二章 Spring Cloud Alibaba Sentinel
27 0
|
1月前
|
SpringCloudAlibaba 监控 Java
SpringCloud Alibaba微服务-- Sentinel的使用(保姆级)
SpringCloud Alibaba微服务-- Sentinel的使用(保姆级)
|
2月前
|
Java Nacos Sentinel
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(九)Nacos+Sentinel+Seata
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(九)Nacos+Sentinel+Seata
225 0