Class: Datadog::Core::Cloudwise::TimeSyncWorker
- Includes:
- Workers::Polling
- Defined in:
- lib/datadog/core/cloudwise/time_sync_worker.rb
Overview
Worker that synchronizes time with server every 3 minutes 时间同步 Worker,每 3 分钟执行一次 整合了时间偏移量管理和定时同步功能
Constant Summary collapse
- DEFAULT_INTERVAL =
3 minutes interval (180 seconds)
180- OFFSET_THRESHOLD_MS =
最大偏移量阈值(毫秒)- 超过此值才进行校正 小于 100ms 的偏移可以忽略
100
Constants included from Workers::Polling
Workers::Polling::DEFAULT_SHUTDOWN_TIMEOUT
Class Attribute Summary collapse
-
.last_sync_time ⇒ Integer?
上次同步时间.
-
.state_mutex ⇒ Object
readonly
Returns the value of attribute state_mutex.
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Attributes inherited from Worker
Class Method Summary collapse
-
.adjust_time(time) ⇒ Time
校正 Time 对象.
-
.adjust_timestamp_ns(local_time_ns) ⇒ Integer
校正时间戳(纳秒) 将本地时间戳转换为服务器时间戳.
-
.disable_sync! ⇒ Object
禁用时间同步.
-
.enable_sync! ⇒ Object
启用时间同步.
-
.offset_ms ⇒ Integer
获取当前时间偏移量(毫秒).
-
.offset_ms=(value) ⇒ Object
设置时间偏移量.
-
.offset_ns ⇒ Integer
获取时间偏移量(纳秒).
-
.reset! ⇒ Object
重置状态(用于测试).
-
.status ⇒ Hash
获取同步状态信息.
-
.sync_enabled? ⇒ Boolean
检查是否启用时间同步.
Instance Method Summary collapse
-
#initialize(client:, logger:, probe_state:, **options) ⇒ TimeSyncWorker
constructor
A new instance of TimeSyncWorker.
- #perform ⇒ Object
-
#start ⇒ Object
Public method to start the worker.
Methods included from Workers::Polling
#enabled=, #enabled?, included, #stop
Constructor Details
#initialize(client:, logger:, probe_state:, **options) ⇒ TimeSyncWorker
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/datadog/core/cloudwise/time_sync_worker.rb', line 116 def initialize(client:, logger:, probe_state:, **) @client = client @logger = logger @probe_state = probe_state # Workers::Async::Thread settings self.fork_policy = .fetch(:fork_policy, Workers::Async::Thread::FORK_POLICY_STOP) # Workers::IntervalLoop settings self.loop_base_interval = .fetch(:interval, DEFAULT_INTERVAL) self.enabled = .fetch(:enabled, true) end |
Class Attribute Details
.last_sync_time ⇒ Integer?
上次同步时间
69 70 71 |
# File 'lib/datadog/core/cloudwise/time_sync_worker.rb', line 69 def last_sync_time @last_sync_time end |
.state_mutex ⇒ Object (readonly)
Returns the value of attribute state_mutex.
30 31 32 |
# File 'lib/datadog/core/cloudwise/time_sync_worker.rb', line 30 def state_mutex @state_mutex end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
114 115 116 |
# File 'lib/datadog/core/cloudwise/time_sync_worker.rb', line 114 def client @client end |
Class Method Details
.adjust_time(time) ⇒ Time
校正 Time 对象
88 89 90 91 92 93 |
# File 'lib/datadog/core/cloudwise/time_sync_worker.rb', line 88 def adjust_time(time) return time unless sync_enabled? return time if offset_ms.abs < OFFSET_THRESHOLD_MS time + (offset_ms / 1000.0) end |
.adjust_timestamp_ns(local_time_ns) ⇒ Integer
校正时间戳(纳秒) 将本地时间戳转换为服务器时间戳
78 79 80 81 82 83 |
# File 'lib/datadog/core/cloudwise/time_sync_worker.rb', line 78 def (local_time_ns) return local_time_ns unless sync_enabled? return local_time_ns if offset_ms.abs < OFFSET_THRESHOLD_MS local_time_ns + offset_ns end |
.disable_sync! ⇒ Object
禁用时间同步
62 63 64 65 |
# File 'lib/datadog/core/cloudwise/time_sync_worker.rb', line 62 def disable_sync! @sync_enabled = false @offset_ms = 0 end |
.enable_sync! ⇒ Object
启用时间同步
57 58 59 |
# File 'lib/datadog/core/cloudwise/time_sync_worker.rb', line 57 def enable_sync! @sync_enabled = true end |
.offset_ms ⇒ Integer
获取当前时间偏移量(毫秒)
34 35 36 |
# File 'lib/datadog/core/cloudwise/time_sync_worker.rb', line 34 def offset_ms @offset_ms ||= 0 end |
.offset_ms=(value) ⇒ Object
设置时间偏移量
40 41 42 |
# File 'lib/datadog/core/cloudwise/time_sync_worker.rb', line 40 def offset_ms=(value) @offset_ms = value.to_i end |
.offset_ns ⇒ Integer
获取时间偏移量(纳秒)
46 47 48 |
# File 'lib/datadog/core/cloudwise/time_sync_worker.rb', line 46 def offset_ns offset_ms * 1_000_000 end |
.reset! ⇒ Object
重置状态(用于测试)
96 97 98 99 100 |
# File 'lib/datadog/core/cloudwise/time_sync_worker.rb', line 96 def reset! @offset_ms = 0 @sync_enabled = false @last_sync_time = nil end |
.status ⇒ Hash
获取同步状态信息
104 105 106 107 108 109 110 111 |
# File 'lib/datadog/core/cloudwise/time_sync_worker.rb', line 104 def status { enabled: sync_enabled?, offset_ms: offset_ms, offset_ns: offset_ns, last_sync_time: last_sync_time } end |
.sync_enabled? ⇒ Boolean
检查是否启用时间同步
52 53 54 |
# File 'lib/datadog/core/cloudwise/time_sync_worker.rb', line 52 def sync_enabled? @sync_enabled ||= false end |
Instance Method Details
#perform ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/datadog/core/cloudwise/time_sync_worker.rb', line 130 def perform # 传统模式下,只有在 License 校验通过后才执行时间同步 # DOCC 模式下不需要 License 校验 unless probe_state.using_docc_mode? || probe_state.license_valid? Cloudwise.log_debug { 'Cloudwise: Waiting for license validation before time sync' } return true end Cloudwise.log_debug { 'Cloudwise: Performing server time synchronization' } if sync_time! Cloudwise.log_debug { "Cloudwise: Time sync completed, offset=#{self.class.offset_ms}ms" } else Cloudwise.log_warn { 'Cloudwise: Time sync failed' } end true rescue => e Cloudwise.log_error { "Cloudwise: Time sync worker error: #{e.class.name} #{e.}" } true # Continue running end |
#start ⇒ Object
Public method to start the worker
153 154 155 156 157 158 |
# File 'lib/datadog/core/cloudwise/time_sync_worker.rb', line 153 def start return false if !enabled? || started? # Start the async worker thread perform end |