[优化] 增加 noop apply 报告逻辑,确保在配置未变更时记录应用日志

This commit is contained in:
ryan
2026-05-30 10:27:10 +08:00
parent 4e339caa9a
commit 5d7a4469ea
4 changed files with 148 additions and 2 deletions
+43
View File
@@ -92,6 +92,7 @@ func (s *Service) sync(ctx context.Context, startup bool, target *protocol.Activ
if currentChecksum == target.Checksum {
slog.Debug("local openresty config already up to date", "mode", mode, "version", target.Version)
shouldReport := shouldReportNoopApply(snapshot, target.Version, target.Checksum)
if startup {
slog.Debug("ensuring openresty runtime on startup", "version", target.Version)
if err = s.nginxManager.EnsureRuntime(ctx, true); err != nil {
@@ -104,6 +105,11 @@ func (s *Service) sync(ctx context.Context, startup bool, target *protocol.Activ
snapshot.OpenrestyStatus = protocol.OpenrestyStatusHealthy
snapshot.OpenrestyMessage = ""
}
if shouldReport {
if err = s.reportNoopApply(ctx, snapshot.NodeID, target.Version, target.Checksum, "", "", 0); err != nil {
return err
}
}
snapshot.CurrentVersion = target.Version
snapshot.CurrentChecksum = target.Checksum
clearBlockedTarget(snapshot)
@@ -152,6 +158,7 @@ func (s *Service) ForceSyncOnce(ctx context.Context, target *protocol.ActiveConf
func (s *Service) applyIfNeeded(ctx context.Context, mode string, startup bool, snapshot *state.Snapshot, currentChecksum string, target *protocol.ActiveConfigMeta, config *protocol.ActiveConfigResponse) error {
if currentChecksum == config.Checksum {
slog.Debug("local openresty config already up to date", "mode", mode, "version", config.Version)
shouldReport := shouldReportNoopApply(snapshot, config.Version, config.Checksum)
if startup {
slog.Debug("ensuring openresty runtime on startup", "version", config.Version)
if err := s.nginxManager.EnsureRuntime(ctx, true); err != nil {
@@ -164,6 +171,15 @@ func (s *Service) applyIfNeeded(ctx context.Context, mode string, startup bool,
snapshot.OpenrestyStatus = protocol.OpenrestyStatusHealthy
snapshot.OpenrestyMessage = ""
}
if shouldReport {
routeConfig := config.RouteConfig
if routeConfig == "" {
routeConfig = config.RenderedConfig
}
if err := s.reportNoopApply(ctx, snapshot.NodeID, config.Version, config.Checksum, checksumString(config.MainConfig), checksumString(routeConfig), len(config.SupportFiles)); err != nil {
return err
}
}
snapshot.CurrentVersion = config.Version
snapshot.CurrentChecksum = config.Checksum
clearBlockedTarget(snapshot)
@@ -266,6 +282,33 @@ func (s *Service) applyIfNeeded(ctx context.Context, mode string, startup bool,
return nil
}
func shouldReportNoopApply(snapshot *state.Snapshot, version string, checksum string) bool {
if snapshot == nil {
return false
}
return strings.TrimSpace(snapshot.CurrentVersion) != strings.TrimSpace(version) ||
strings.TrimSpace(snapshot.CurrentChecksum) != strings.TrimSpace(checksum)
}
func (s *Service) reportNoopApply(ctx context.Context, nodeID string, version string, checksum string, mainConfigChecksum string, routeConfigChecksum string, supportFileCount int) error {
message := "local config already matches active version; apply skipped"
if err := s.client.ReportApplyLog(ctx, protocol.ApplyLogPayload{
NodeID: nodeID,
Version: strings.TrimSpace(version),
Result: ApplyResultSuccess,
Message: message,
Checksum: strings.TrimSpace(checksum),
MainConfigChecksum: strings.TrimSpace(mainConfigChecksum),
RouteConfigChecksum: strings.TrimSpace(routeConfigChecksum),
SupportFileCount: supportFileCount,
}); err != nil {
slog.Error("report noop apply log failed", "version", version, "error", err)
return err
}
slog.Debug("noop apply log reported", "version", version)
return nil
}
func outcomeError(version string, message string) error {
trimmed := strings.TrimSpace(message)
if trimmed == "" {