envoy源代码分析(五)--从配置开始

配置

配置文件示例

 

node:
  cluster: tong-cluster
  id: tongxxtongxxtongxxahaha

dynamic_resources:
  ads_config:
    api_type: DELTA_GRPC
    transport_api_version: v3
    grpc_services:
      - envoy_grpc:
          cluster_name: control-plane
  cds_config:
    ads: {}
  lds_config:
    ads: {}

static_resources:
  clusters:
  - name: control-plane
    type: STATIC
    load_assignment:
      cluster_name: control-plane-upstream
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: 127.0.0.1
                port_value: 5000

admin:
  address:
    socket_address:
      address: 0.0.0.0
      port_value: 5001
View Code

 

配置代码入口

~/Source/envoy-1.33.2/api/envoy/config/bootstrap/v3/bootstrap.proto::{message Bootstrap}

bootstrap的类:MainImpl

 bootstrap作为入参,最终用来创建clusterManager

关键函数:

absl::Status MainImpl::initialize(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
                                  Instance& server,
                                  Upstream::ClusterManagerFactory& cluster_manager_factory)

 

absl::StatusOr<ClusterManagerPtr> ProdClusterManagerFactory::clusterManagerFromProto(
    const envoy::config::bootstrap::v3::Bootstrap& bootstrap)

 

主要代码调用流程

ClusterManager 

有两个GRPC实现可以选择,envoy grpc与google grpc,推荐默认使用 envoy grpc

https://www.envoyproxy.io/docs/envoy/v1.34.0/intro/arch_overview/other_protocols/grpc#arch-overview-grpc-services

 

XDS配置变更管理,是clustermanager的主要功能,GRPC机制也由其进行管理,接下来通过初始化流程的分析,勾画其配置变更相关的主要抽象关系。 

见下图:ClusterManager的初始化流程图,以及初始化cluster配置管理相关的主要类关系图

 

 

ThreadLocal

threadlocal结构与配置的同步变更有关,类似于RCU机制。

核心数据结构的实现类是ThreadLocal::InstanceImpl

下图是初始化过程中,该类的主要初始化流程与类抽象。

 

RCU的核心逻辑:(写者变更)

read-copy-update。1写多读。

a 准备新数据,b 切换新旧指针。c 清除旧数据。

同步机制:1 gwlb的实现里,mgt阻塞一下,等所有worker再bitmap里写一个1,说明转了一圈了。

2 worker每次都要通过指针拿数据。3 要用refcnt。

 

TLS的核心逻辑:(读者变更)

a 多写一读的聚合。(读者遍历)

b 一写多读的分发。

1 准备一份全局数据,2 post一个初始化任务给worker。3 post一个切换并释放的任务给worker。

 

 

GrpcGoogClient线程

在《主框架》分析中,我们提到了每worker与master一个的GrpcGoogClient线程,

其作用是用于线程间的配置同步,类似TLS。

该线程的启动与类关系主要示意图如下:

 

GRPC的封装

 主要通过GRPC库完成与control-plane的通信,消息处理完成后,通过GrpcGoogClient线程以及perthread变量与每个worker通信。

与control-plane的通信在mastet thread中完成,详见示意图:

 

posted on 2025-04-27 22:06  toong  阅读(1)  评论(0)    收藏  举报