AlexStocks commented on code in PR #994:
URL: https://github.com/apache/dubbo-go-pixiu/pull/994#discussion_r4505145941
##########
pkg/config/xds/xds.go:
##########
@@ -107,34 +116,55 @@ func (a *Xds) readDubboServiceFromListener() ([]string,
error) {
return dubboServices, nil
}
-func (a *Xds) Start() {
+func (a *Xds) Start() error {
if a.dynamicResourceMg == nil { // if dm is nil, then config not
initialized.
logger.Infof("can not get dynamic resource manager. maybe the
config has not initialized")
- return
+ return nil
}
apiclient.Init(a.clusterMg)
+ // lds and cds are independent dynamic resource watches: a failure in
one must not skip the
+ // other's initialization. Collect each error and fail startup if any
occurred, so a missing
+ // config or xDS connection failure surfaces to the caller instead of
leaving the process in a
+ // false-healthy state with parts of the dynamic config unavailable.
+ var errs []error
+
// lds fetch just run on init phase.
if a.dynamicResourceMg.GetLds() != nil {
- a.lds = &LdsManager{
- DiscoverApi:
a.createApiManager(a.dynamicResourceMg.GetLds(), a.dynamicResourceMg.GetNode(),
constant.ListenerType),
- listenerMg: a.listenerMg,
- }
- if err := a.lds.Delta(); err != nil {
- logger.Errorf("can not fetch lds err is %+v", err)
+ discoverApi, err :=
a.createApiManager(a.dynamicResourceMg.GetLds(), a.dynamicResourceMg.GetNode(),
constant.ListenerType)
+ if err != nil {
+ errs = append(errs, errors.Wrap(err, "create LDS api
manager"))
+ } else {
+ a.lds = &LdsManager{
+ DiscoverApi: discoverApi,
+ listenerMg: a.listenerMg,
+ }
+ if err := a.lds.Delta(); err != nil {
+ errs = append(errs, errors.Wrap(err, "fetch
lds"))
+ }
}
}
// catch the ongoing cds config change.
if a.dynamicResourceMg.GetCds() != nil {
- a.cds = &CdsManager{
- DiscoverApi:
a.createApiManager(a.dynamicResourceMg.GetCds(), a.dynamicResourceMg.GetNode(),
constant.ClusterType),
- clusterMg: a.clusterMg,
- }
- if err := a.cds.Delta(); err != nil {
- logger.Errorf("can not fetch lds")
+ discoverApi, err :=
a.createApiManager(a.dynamicResourceMg.GetCds(), a.dynamicResourceMg.GetNode(),
constant.ClusterType)
+ if err != nil {
+ errs = append(errs, errors.Wrap(err, "create CDS api
manager"))
+ } else {
+ a.cds = &CdsManager{
+ DiscoverApi: discoverApi,
+ clusterMg: a.clusterMg,
+ }
+ if err := a.cds.Delta(); err != nil {
+ errs = append(errs, errors.Wrap(err, "fetch
cds"))
+ }
}
}
+ if err := stderr.Join(errs...); err != nil {
Review Comment:
💭 `errors.Join` 会把多条错误拼成换行分隔的多行消息,一路冒泡到 CLI 后会被 `errors.Wrap(startErr,
"failed to start gateway")` 再包一层。这本身没错,但日志里会出现「主错误 +
若干子错误」混排的样子,`logger.Errorf("%+v")` 下尤其乱。
如果希望运维一眼看到根因,可以考虑只返回第一条、其余用 `logger.Errorf` 记录(现在的 `errs` 顺序是 LDS →
CDS,稳定可预期):
```go
if len(errs) > 0 {
a.Stop()
for _, e := range errs[1:] {
logger.Errorf("xds startup: additional error: %+v", e)
}
return errs[0]
}
```
是否这么做取决于你们希望「启动失败时对外暴露多少信息」。如果用 `Join` 保留全部,建议在 CLI 侧改用单行格式化(把 `\n` 换成 `;
`),避免破坏日志的行结构。
##########
pkg/config/xds/xds.go:
##########
@@ -153,17 +184,26 @@ type Client interface {
}
// StartXdsClient create XdsClient and run. only one xds client create at
first(singleton)
-func StartXdsClient(listenerMg controls.ListenerManager, clusterMg
controls.ClusterManager, drm controls.DynamicResourceManager) Client {
+func StartXdsClient(listenerMg controls.ListenerManager, clusterMg
controls.ClusterManager, drm controls.DynamicResourceManager) (Client, error) {
+ // A startup failure is terminal for this singleton. Xds.Start rolls
back partial
Review Comment:
🟡 **「首次失败即终态」这个决策建议写清楚前置条件。**
`once.Do` 被消费之后,后面所有调用都拿到同一个 `startErr`,没有重试机会。如果 xDS 管理面比网关晚起来(滚动发布、K8s 里两个
Deployment 并行起来时很常见),第一次 `Dial` 失败就意味着这个进程永久没有动态配置——目前靠「启动失败 → 进程退出 →
由编排重启」兜底,这是可以接受的策略,但它是**依赖外部重启**才成立的。
两个建议:
1. 在注释里补一句「调用方必须把错误视为致命并终止进程;本函数不提供重试」——现在只写了 "terminal for this
singleton",容易被读成「单例只初始化一次」而不是「失败不可恢复」。
2. 如果想支持自愈,可以把 `Start` 的失败结果单独缓存(例如 `sync.Once` 只保护「构造 + 首次尝试」,失败时把
`startErr` 清空、允许下次重新 CAS),代价是要处理并发重入。这不属于本 PR 必须做的事,但值得留个 TODO。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]