Skip to content

Conversation

@mhduiy
Copy link
Contributor

@mhduiy mhduiy commented May 13, 2025

  1. Added FallbackNTPServer configuration in dsg-configs with aliyun as default
  2. Extended Manager struct to include fallbackNTPServer field
  3. Implemented getDsgFallbackNTPServer method to retrieve fallback server
  4. Modified setNTPServerToTimeSyncd to append fallback server when default NTP is used
  5. Added logic to handle fallback server when primary NTP server fails

The changes provide redundancy for time synchronization by automatically falling back to a secondary NTP server (ntp.aliyun.com) when the primary server is unavailable or using default settings. This improves system time reliability.

feat: 添加后备NTP服务器支持

  1. 在dsg-configs中添加FallbackNTPServer配置,默认使用阿里云服务器
  2. 扩展Manager结构体包含fallbackNTPServer字段
  3. 实现getDsgFallbackNTPServer方法获取后备服务器
  4. 修改setNTPServerToTimeSyncd方法,在使用默认NTP时追加后备服务器
  5. 添加处理逻辑在主NTP服务器不可用时使用后备服务器

这些修改通过在主NTP服务器不可用或使用默认设置时自动回退到备用NTP服务器
(ntp.aliyun.com),为时间同步提供了冗余,提高了系统时间的可靠性。

pms: BUG-315181

Summary by Sourcery

Add support for a secondary NTP server by introducing a fallback configuration and automatically appending and switching to it when the primary NTP server is unavailable or using default settings

New Features:

  • Add FallbackNTPServer option in dsg-configs with default ntp.aliyun.com
  • Extend Manager to retrieve and store the fallback NTP server
  • Modify time-sync configuration to append the fallback server when using default NTP

Enhancements:

  • Automatically switch to the fallback NTP server if the primary server fails

@sourcery-ai
Copy link

sourcery-ai bot commented May 13, 2025

Reviewer's Guide

Adds fallback NTP server support by introducing a new configuration option, extending the Manager to store and retrieve a fallback server, and modifying the timesyncd setup logic to append the fallback NTP when using the default or when the primary fails.

Sequence Diagram: Setting NTP Server with Fallback Logic

sequenceDiagram
    participant Caller
    participant M as Manager
    participant DS_Manager as "ConfigManager.Manager"
    participant TSCFG as "timesyncd.conf"
    participant SD as systemd

    Caller->>M: setNTPServer(primaryNTP)
    M->>M: setNTPServerToTimeSyncd(primaryNTP)
    M->>DS_Manager: IsDefaultValue(0, dsettingsKeyNTPServer)
    DS_Manager-->>M: isDefault
    alt If isDefault is true
        M->>M: currentNTP = primaryNTP + " " + m.fallbackNTPServer
        M->>M: log("Set fallback NTP server: " + m.fallbackNTPServer)
    else Else (primaryNTP is not default)
        M->>M: currentNTP = primaryNTP
    end
    M->>TSCFG: Write NTP=currentNTP to timeSyncCfgFile
    M->>SD: Restart("systemd-timesyncd.service")
    SD-->>M: RestartResult
Loading

Class Diagram: Manager Update for Fallback NTP Support

classDiagram
    class Manager {
        %% Existing fields (abbreviated for brevity)
        +core timedate1.Timedate
        +service *dbusutil.Service
        +PropsMu sync.RWMutex
        +NTPServer string
        +timesyncd timesync1.Timesync1
        +systemd systemd1.Manager
        +setNTPServerMu sync.RWMutex
        +signalLoop *dbusutil.SignalLoop
        +dsManager ConfigManager.Manager

        %% New field
        +fallbackNTPServer string

        %% Existing methods (potentially modified)
        +start()
        +getDsgNTPServer() string
        +setDsgObsoleteNTPServer(server string) error
        +setNTPServer(value string) error  // Now calls setNTPServerToTimeSyncd

        %% New methods or significantly changed internal logic
        +getDsgFallbackNTPServer() string
        +setNTPServerToTimeSyncd(server string) error // Previously global setNTPServer, now a method with fallback logic
    }
    note for Manager "New field: fallbackNTPServer string\nNew method: getDsgFallbackNTPServer()\nNew method: setNTPServerToTimeSyncd() (incorporates fallback logic, was previously global setNTPServer)\nModified: start() initializes fallbackNTPServer\nModified: setNTPServer() now calls internal setNTPServerToTimeSyncd()"
Loading

File-Level Changes

Change Details Files
Introduce FallbackNTPServer configuration
  • Add FallbackNTPServer key with default value in JSON config
  • Define dsettingsFallbackNTPServer constant
misc/dsg-configs/org.deepin.dde.daemon.timedated.json
system/timedate1/manager.go
Extend Manager to include fallbackNTPServer
  • Add fallbackNTPServer field to Manager struct
  • Assign m.fallbackNTPServer in start()
system/timedate1/manager.go
Implement fallback retrieval method
  • Create getDsgFallbackNTPServer() to fetch the fallback value from dsManager
  • Handle nil dsManager and errors by returning empty string
system/timedate1/manager.go
Refactor NTP setting to use fallback
  • Rename setNTPServer to setNTPServerToTimeSyncd
  • Update setNTPServer handler to call the new method
  • Detect default NTP configuration and append fallback server
  • Log fallback assignment before writing to timesyncd config
system/timedate1/manager.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @mhduiy - I've reviewed your changes - here's some feedback:

  • Translate the inline Chinese comment in setNTPServerToTimeSyncd into English and clarify the rationale behind using this fallback approach.
  • Rename the variable setFallback to something more descriptive like useFallbackServer or isDefaultNTPServer to improve readability.
  • Add a guard to only append fallbackNTPServer when it’s non-empty to avoid introducing trailing spaces or invalid NTP entries.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

fly602
fly602 previously approved these changes May 13, 2025
1. Added FallbackNTPServer configuration in dsg-configs with aliyun
as default
2. Extended Manager struct to include fallbackNTPServer field
3. Implemented getDsgFallbackNTPServer method to retrieve fallback
server
4. Modified setNTPServerToTimeSyncd to append fallback server when
default NTP is used
5. Added logic to handle fallback server when primary NTP server fails

The changes provide redundancy for time synchronization by automatically
falling back to a secondary NTP server (ntp.aliyun.com) when the primary
server is unavailable or using default settings. This improves system
time reliability.

feat: 添加后备NTP服务器支持

1. 在dsg-configs中添加FallbackNTPServer配置,默认使用阿里云服务器
2. 扩展Manager结构体包含fallbackNTPServer字段
3. 实现getDsgFallbackNTPServer方法获取后备服务器
4. 修改setNTPServerToTimeSyncd方法,在使用默认NTP时追加后备服务器
5. 添加处理逻辑在主NTP服务器不可用时使用后备服务器

这些修改通过在主NTP服务器不可用或使用默认设置时自动回退到备用NTP服务器
(ntp.aliyun.com),为时间同步提供了冗余,提高了系统时间的可靠性。

pms: BUG-315181
@deepin-ci-robot
Copy link

deepin pr auto review

代码审查意见:

  1. 代码注释:在新增的getDsgFallbackNTPServersetNTPServerToTimeSyncd函数中,应该添加注释说明函数的用途和功能,以便其他开发者理解代码逻辑。

  2. 错误处理:在getDsgFallbackNTPServer函数中,当m.dsManagernil时,返回空字符串。这种情况下,应该抛出错误或者记录错误日志,而不是静默处理。

  3. 日志记录:在setNTPServerToTimeSyncd函数中,当setFallbacktruem.fallbackNTPServer不为空时,记录了日志。建议添加日志级别,例如使用logger.Infoflogger.Debugf,以便于调试和问题追踪。

  4. 代码重复setNTPServerToTimeSyncd函数中,kf.LoadFromFile(timeSyncCfgFile)kf.SetString("Time", "NTP", server)的调用与setNTPServer函数中的类似。建议提取公共代码到单独的函数中,以减少代码重复。

  5. 配置管理:在setNTPServerToTimeSyncd函数中,通过m.dsManager.IsDefaultValue检查dsettingsKeyNTPServer是否为默认值。这种配置管理方式可能不够灵活,建议考虑使用更通用的配置管理策略。

  6. 安全性:在setNTPServerToTimeSyncd函数中,没有对server参数进行任何验证或清理。如果server参数来自用户输入,应该进行验证,以防止注入攻击。

  7. 性能:在setNTPServerToTimeSyncd函数中,每次设置NTP服务器时都会加载和保存配置文件。如果这个操作频繁发生,可能会导致性能问题。建议考虑缓存配置或使用更高效的数据存储方式。

  8. 代码风格:在setNTPServerToTimeSyncd函数中,kf.SetString的调用应该使用defer语句来确保配置文件在函数结束时被保存,以避免数据丢失。

综上所述,代码在功能实现上基本正确,但在错误处理、日志记录、代码重复、配置管理、安全性、性能和代码风格方面存在一些改进空间。

@mhduiy mhduiy requested a review from fly602 May 13, 2025 08:20
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: fly602, mhduiy

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deepin-bot
Copy link
Contributor

deepin-bot bot commented May 13, 2025

TAG Bot

New tag: 6.1.31
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #784

@mhduiy mhduiy merged commit c712b12 into linuxdeepin:master May 13, 2025
14 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants