Skip to content

增加监听地址scope和namespace,移除部分不常用的维度 - #64

Merged
owent merged 2 commits into
mainfrom
dev
Sep 11, 2026
Merged

owent merged 2 commits into
mainfrom
dev

Conversation

@owent

@owent owent commented Sep 11, 2026

Copy link
Copy Markdown
Owner

No description provided.

Copilot AI lite review requested due to automatic review settings September 11, 2026 03:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

atapp_conf.proto 删除字段后未 reserved 旧的 tag/name,存在后续误复用导致协议兼容性风险。

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

该 PR 为 libatapp 的服务发现/直连链路增加“监听地址隔离维度”:引入 metadata.scopenamespace_name 参与 gateway 匹配与 listen 回退地址规则,并新增 bus.inherited_labels 用于将 atapp 的部分 labels 下发到 atbus 节点/网关匹配;同时移除 metadata.uid/service_subset 等不常用维度并同步更新测试与配置样例。

Changes:

  • 协议与配置:新增 metadata.scopegateway.match_scopebus.inherited_labels,并移除 metadata.uid/service_subset 的使用路径
  • 运行时行为:atbus 配置映射下发 scope/namespace/labels;discovery listen 回退地址携带 scope/namespace 隔离规则;discovery 更新时刷新 atbus endpoint 的隔离信息与网关
  • 测试与样例:新增/更新多组单测与配置文件,覆盖 gateway 匹配、isolation 映射、listen 回退与直连跳过不可达地址等场景
File summaries
File Description
test/case/atapp_test_isolation_1.yaml 新增隔离映射测试用配置:scope/namespace/labels/inherited_labels/gateways 组合
test/case/atapp_test_direct_1.yaml 增加 metadata.labels 与 bus.inherited_labels 以覆盖 label 继承路径
test/case/atapp_setup_test.cpp 增加 gateway scope/namespace/labels 匹配与 atbus isolation 映射单测
test/case/atapp_discovery_test.cpp 将原 service_subset 维度切换为 scope,并新增 listen 回退携带隔离规则测试
test/case/atapp_direct_connect_test.cpp 新增 scope 不匹配跳过拨号、scope 匹配直连、discovery 更新刷新 bus endpoint 等测试
test/case/atapp_configure_loader_test.cpp 表达式加载用例从 uid/service_subset 调整为 kind/scope
test/case/atapp_configure_expression_test.yaml 更新表达式配置样例字段到 kind/scope
test/case/atapp_configure_expression_test.env.txt 更新 env 变量映射到 ATAPP_METADATA_KIND/ATAPP_METADATA_SCOPE
test/case/atapp_configure_expression_test.conf 更新 conf 样例字段到 metadata.kind/metadata.scope
src/CMakeLists.txt 为主库源文件追加对生成的 atapp_conf.pb.h 的对象级依赖,避免并行编译竞态
src/atframe/etcdcli/etcd_discovery.cpp 元数据等价/哈希/过滤维度切换到 scope;listen 回退 gateway 预置 scope/namespace 规则
src/atframe/connectors/atapp_endpoint.cpp discovery 更新时同步刷新 atbus endpoint 的 scope/namespace/labels/gateways(含 listen 回退规则)
src/atframe/atapp.cpp atbus 配置映射下发 scope/namespace/labels/gateways;gateway 匹配新增 scope;discovery 事件触发 endpoint 刷新
include/atframe/atapp.h 公共 API 增加 set_metadata_scope(),移除 uid/service_subset setters 声明
include/atframe/atapp_conf.proto 协议新增 scope/match_scope/inherited_labels,移除 uid/service_subset 字段
Review details
  • Files reviewed: 16/16 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 518 to 522
// kubernetes ENV: ATAPP_METADATA_NAME: valueFrom: fieldRef: fieldPath: metadata.name
string name = 11 [(atframework.atapp.protocol.CONFIGURE) = { enable_expression: true }];
// kubernetes ENV: ATAPP_METADATA_NAMESPACE_NAME: valueFrom: fieldRef: fieldPath: metadata.namespace
string namespace_name = 12 [(atframework.atapp.protocol.CONFIGURE) = { enable_expression: true }];
// kubernetes ENV: ATAPP_METADATA_UID: valueFrom: fieldRef: fieldPath: metadata.uid
string uid = 13 [(atframework.atapp.protocol.CONFIGURE) = { enable_expression: true }];

// `service_subset` is similar to Destination in
// https://istio.io/latest/docs/reference/config/networking/virtual-service/#Destination
string service_subset = 23 [(atframework.atapp.protocol.CONFIGURE) = { enable_expression: true }];

@atframework-aicr

Copy link
Copy Markdown

AI Code Review

Updated: 2026-09-11 03:41:11 UTC | Commit: d75787d

AI Code Review Summary

集群隔离维度改造评审:match_labels 过滤口径不一致等 3 项

Target: 增加监听地址scope和namespace,移除部分不常用的维度
Author: @owent
Branch: dev
Reviewers: @owent


评审范围

PR #64(f71037b → d75787d):新增 metadata.scopebus.inherited_labelsatapp_gateway.match_scope 集群隔离维度;apply_atbus_configure/reload_self_endpoint/update_discovery 下发链路;etcd discovery listen 回退地址的隔离规则;CMake 生成头文件的 OBJECT_DEPENDS 依赖修复;配套单测;libatbus 子模块升级到 d46c04d。

问题汇总(3 项)

  • medium × 1(correctness)atapp_endpoint::update_discovery 按本端 inherited_labels 丢弃对端 gateway 的 match_labels,会把 atbus 注册路径判"不可达"的地址翻转为"可达",label 隔离可被绕过,且同一 endpoint 的 gateway 视图随注册/发现事件来回翻转(src/atframe/connectors/atapp_endpoint.cpp:186)。
  • low × 2
    • 运行期 set_metadata_scope/set_metadata_namespace_name 只更新 discovery 重发布、不下发本端 atbus 层,两端隔离视图分叉(src/atframe/atapp.cpp:1734);
    • atapp_metadata 删除 uid = 13/service_subset = 23 未加 reserved,存在字段号复用误读持久化 etcd 历史数据的风险(include/atframe/atapp_conf.proto:510)。

上下文说明

  • libatbus 子模块目录在本地工作区未物化,本次通过 GitHub 以评审指定提交 d46c04d 拉取 atbus_node.hatbus_endpoint.hlibatbus_channel_types.hatbus_node.cppatbus_endpoint.cppatbus_message_handler.cpp 完成了跨仓库契约核对(conf_t::scope/namespace_name/node_labels/gatewayendpoint::reload/get_gatewaycheck_reachabledump_listen_to_channel_data);libatbus 仓库内部实现的完整 diff 不在本仓库评审范围内,上述结论仅涉及其对外契约。
  • CMake OBJECT_DEPENDS 变更(生成头文件路径为 ${CMAKE_CURRENT_BINARY_DIR}/_generated 绝对路径)与各测试文件改动核对后未发现问题。

Problems (3)

# Severity Category Location Message
0 MEDIUM correctness src/atframe/connectors/atapp_endpoint.cpp:186-189 update_discovery 把对端 gateway 的 match_labels本端 inherited_labels 过滤后存入 atbus endpoint,这会把本应"不可达"的地址翻转为"可达",与 atbus 注册路径的判定相反。atbus 侧数据连接选址用 check_reachable(gw, ..., n.get_conf().node_labels)(libatbus d46c04d src/atbus_message_handler.cpp:1187-1230)评估对端规则:本端 node_labels 只含 inherited 标签,规则 key 不在本端 inherited_labelsfind 失败 → 判不可达;而注册路径存入的 gateway 规则来自 reg_data.channels(),不做本端过滤(atbus_message_handler.cpp:1015/1050/1065)。触发场景:对端 gateway 配 match_labels: {zone: west}(对端 inherited 含 zone,本端 inherited_labels 不含 zone)——注册路径下本端不会选用该地址;一旦收到对端 discovery PUT(本 PR 新增的 trigger_event_on_discovery_eventupdate_discoverybus_ep->reload 整体覆盖 gateway 视图),规则被丢弃,同一 endpoint 变为无规则 → check_reachable 通过 → 本端把数据连接建到该地址,绕过对端的 label 隔离;且该视图随注册/发现事件先后到达来回翻转。另外 app::pack 通告的是未过滤的原始 bus.gateways,与 atbus 注册通告(经 apply_atbus_configure 按对端自身 inherited_labels 过滤,atapp.cpp:328)口径也不一致。修复方向:本端不要丢弃对端规则(保留原样,让 check_reachable 对缺失 key 自然判不可达),并统一 discovery 通告与 atbus 注册通告的过滤口径;至少在配置校验阶段对 match_labels/对端规则中未列入 inherited_labels 的 key 告警。
1 LOW lifecycle src/atframe/atapp.cpp:1734-1744 新增的 set_metadata_scope(及同批 set_metadata_namespace_name)在运行期调用后只置 set_maybe_update_keepalive_discovery_metadata() 触发 discovery 重发布(对端会经本 PR 新增的 PUT 处理 trigger_event_on_discovery_eventupdate_discovery 刷新 bus endpoint 视图),但本端 atbus 层的 scope/namespace 只在 reload() 时下发——reload_self_endpoint 的全库唯一产品调用点是 atapp.cpp:1219,atbus 注册通告与 dump_listen_to_channel_data 仍使用旧的 conf_.scope。触发场景:应用在 init 之后调用 set_metadata_scope("new")(本 PR 新增的公共 API)——对端从 discovery 看到新 scope 并据此合成 listen 隔离规则,而本端 listen 地址在 atbus 握手中仍通告旧(或空=通配)scope,隔离语义在两端视图间分叉。另外 apply_configure() 在 yaml 含 metadata 段时会用文件值整体覆盖 conf_.metadata(atapp.cpp:2960-2963),程序设值只有 yaml 无 metadata 段时才能经 reload 传导到 atbus;新增测试 B.10 需要手动调用 bus->reload_self_endpoint(...) 才能让 scope 生效,也印证产品层缺少这条传播路径。修复方向:在 set_metadata_scope/set_metadata_namespace_name(或 keepalive 重发布前)同步调用 bus_node_->reload_self_endpoint,或在头文件注释中明确这些 setter 仅在 init 前生效、运行期变更不作用于 atbus 层。
2 LOW api_schema_compatibility include/atframe/atapp_conf.proto:510-526 本次从 atapp_metadata 删除了 uid = 13service_subset = 23,但没有添加 reserved。该消息的序列化数据会持久化到 etcd discovery key 并在滚动升级期间由新旧节点混写:旧节点写入的 field 13/23 会作为未知字段随 CopyFrom 在链路上保留。若日后有人复用字段号 13 或 23 定义新字段(整个文件目前没有任何 reserved 约束),历史 etcd 数据中的旧值会被误读为新字段。建议在 atapp_metadata 中补充 reserved 13, 23;reserved uid, service_subset;

Code reference: src/atframe/connectors/atapp_endpoint.cpp:186-189

              (std::min)(static_cast<size_t>(gateway.match_labels_size()), inherited_labels_set.size()));
          for (const auto &label_kv : gateway.match_labels()) {
            if (inherited_labels_set.find(label_kv.first) != inherited_labels_set.end() && !label_kv.second.empty()) {
              gw.match_labels.emplace(label_kv.first, label_kv.second);
            }
          }
        }
      }

Code reference: src/atframe/atapp.cpp:1734-1744

}

LIBATAPP_MACRO_API void app::set_metadata_scope(gsl::string_view value) {
  if (gsl::string_view(conf_.metadata.scope().c_str(), conf_.metadata.scope().size()) == value) {
    return;
  }

Code reference: include/atframe/atapp_conf.proto:510-526

  string kind = 2 [(atframework.atapp.protocol.CONFIGURE) = { enable_expression: true }];
  string group = 3 [(atframework.atapp.protocol.CONFIGURE) = { enable_expression: true }];

  // Scope of listen addresses, usually set to cluster.metadata.uid or cluster name of Kubernetes cluster
  string scope = 4 [(atframework.atapp.protocol.CONFIGURE) = { enable_expression: true }];

  // kubernetes ENV: ATAPP_METADATA_NAME: valueFrom: fieldRef: fieldPath: metadata.name
  string name = 11 [(atframework.atapp.protocol.CONFIGURE) = { enable_expression: true }];
  // kubernetes ENV: ATAPP_METADATA_NAMESPACE_NAME: valueFrom: fieldRef: fieldPath: metadata.namespace
  string namespace_name = 12 [(atframework.atapp.protocol.CONFIGURE) = { enable_expression: true }];

  // https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set

  • Powered by AICodeReviewer*

Open Issues (3)

1. [MEDIUM] correctnesssrc/atframe/connectors/atapp_endpoint.cpp:186-189 (new in d75787d)
2. [LOW] lifecyclesrc/atframe/atapp.cpp:1734-1744 (new in d75787d)
3. [LOW] api_schema_compatibilityinclude/atframe/atapp_conf.proto:510-526 (new in d75787d)

Copilot AI review requested due to automatic review settings September 11, 2026 05:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Gateway rule translation into atbus currently forwards empty host/namespace entries without filtering, which can diverge from the documented “empty means skip” semantics and affect reachability decisions.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 16/16 changed files
  • Comments generated: 2
  • Review effort level: Lite

Comment thread src/atframe/atapp.cpp
Comment on lines +319 to +324
gw_cfg.address = gw.address();
gw_cfg.match_scope = gw.match_scope();
gw_cfg.match_hosts.reserve(static_cast<size_t>(gw.match_hosts().size()));
gw_cfg.match_hosts = {gw.match_hosts().begin(), gw.match_hosts().end()};
gw_cfg.match_namespaces.reserve(static_cast<size_t>(gw.match_namespaces().size()));
gw_cfg.match_namespaces = {gw.match_namespaces().begin(), gw.match_namespaces().end()};
Comment on lines +167 to +180
gw.address = gateway.address();
gw.match_scope = gateway.match_scope();
if (gateway.match_namespaces_size() > 0) {
gw.match_namespaces.reserve(static_cast<size_t>(gateway.match_namespaces_size()));
for (const auto &ns : gateway.match_namespaces()) {
gw.match_namespaces.insert(ns);
}
}
if (gateway.match_hosts_size() > 0) {
gw.match_hosts.reserve(static_cast<size_t>(gateway.match_hosts_size()));
for (const auto &host : gateway.match_hosts()) {
gw.match_hosts.insert(host);
}
}
@owent
owent merged commit 253c354 into main Sep 11, 2026
15 checks passed
@atframework-aicr

atframework-aicr Bot commented Sep 11, 2026

Copy link
Copy Markdown

AI Code Review

Updated: 2026-09-11 07:16:10 UTC | Commit: 253c354

AI Code Review Summary

PR #64 集群隔离功能评审:1 个 high 问题

Target: 增加监听地址scope和namespace,移除部分不常用的维度
Author: @atframework-aicr[bot]
Branch: dev
Reviewers: @atframework-aicr[bot]


评审范围

本 PR 引入的集群隔离能力:metadata.scope/bus.inherited_labels/bus.gateways 下发到 atbus(apply_atbus_configurereload_self_endpoint)、atapp_gateway.match_scope 匹配语义、discovery listen 回退地址的隔离规则、discovery PUT 事件刷新 atbus endpoint,以及配套 proto/CMake/测试/libatbus 子模块更新。

问题(1 个)

  • high / correctnessatapp_endpoint::update_discovery 新增的 atbus endpoint 刷新逻辑在真实 etcd 更新路径上不可达——watcher 对已存在节点原地复用同一 etcd_discovery_node 实例,discovery_ == discovery 早退使 bus_ep->reload(...) 永不执行;对端更新 scope/namespace/labels/gateways 后本端隔离规则与入口地址保持陈旧,新增测试因每次构造全新实例而未覆盖该路径。

不确定性说明

  • libatbus 子模块(d46c04d)仅有部分头文件可读,channel::gateway_tendpoint::reload 的实现不可见;上述结论不依赖这些实现(问题在于 reload 从未被调用)。

Problems (1)

# Severity Category Location Message
0 HIGH correctness src/atframe/connectors/atapp_endpoint.cpp:143-213 新增的 atbus endpoint 刷新逻辑(第 143-213 行,含 bus_ep->reload(metadata.scope(), ...))在真实 etcd 更新路径上永远不会执行:函数开头的指针相等检查 if (discovery_ == discovery) return;(第 130-132 行)会先返回。service_discovery_module::update_internal_watcher_event 对已存在的节点是原地复用同一个 etcd_discovery_node 实例(service_discovery_module.cpp:1569-1576 与 1623-1639:new_inst = local_cache_by_idcopy_from 原地覆盖),随后把同一指针传入 app::trigger_event_on_discovery_event(service_discovery_module.cpp:1670)→ ep->update_discovery(node)(atapp.cpp:3753/3758);而 endpoint 的 discovery_ 在创建时就已经指向该实例(mutable_endpointupdate_discovery,atapp.cpp:2564),因此指针相等必然成立,新增的刷新分支被整体跳过。atapp_connector_atbus::on_discovery_event 只会对带 kWaitForDiscoveryToConnect 标记的 handle 触发重连,不存在其他刷新路径。触发场景:已连接对端在 etcd 中更新 metadata.scope/namespace_name/labelsbus.gateways 后,本端 atbus endpoint 持续使用旧的隔离规则和入口地址,直到断线重连或进程重启,隔离变更不生效。新增测试 direct_discovery_update_refresh_bus_endpoint 能通过只是因为测试每次都新建 etcd_discovery_node 实例,恰好绕过了指针相等检查,未覆盖真实 watcher 路径。

Code reference: src/atframe/connectors/atapp_endpoint.cpp:143-213

            discovery->get_discovery_info().id(), discovery->get_discovery_info().name());

  if (owner_->get_bus_node() && discovery->get_discovery_info().id() != 0) {
    auto *bus_ep = owner_->get_bus_node()->get_endpoint(discovery->get_discovery_info().id());
    if (bus_ep != nullptr) {
      std::unordered_map<std::string, std::string> labels;
      std::vector<atbus::node::gateway_t> gateways;
      const auto &metadata = discovery->get_discovery_info().metadata();
      std::unordered_set<std::string> inherited_labels_set;
      inherited_labels_set.reserve(static_cast<size_t>(owner_->get_origin_configure().bus().inherited_labels().size()));
      for (const auto &label_k : owner_->get_origin_configure().bus().inherited_labels()) {
        inherited_labels_set.insert(label_k);

  • Powered by AICodeReviewer*

Open Issues (5)

1. [HIGH] correctnesssrc/atframe/connectors/atapp_endpoint.cpp:143-213 (new in 253c354)
2. [MEDIUM] correctnesssrc/atframe/connectors/atapp_endpoint.cpp:130-133 (resolution not confirmed)
3. [LOW] api_schema_compatibilityinclude/atframe/atapp_conf.proto:510-526 (resolution not confirmed)
4. [HIGH] correctnesssrc/atframe/connectors/atapp_endpoint.cpp:211-212 (resolution not confirmed)
5. [LOW] schema_compatibilityinclude/atframe/atapp_conf.proto:521 (resolution not confirmed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants