类:Mongo::Server::Monitor Private

继承:
对象
  • 对象
显示全部
扩展方式:
可转发
包括:
BackgroundThreadEvent::PublisherLoggable
定义于:
lib/ Mongo/ 服务器/ 监控.rb
lib/ Mongo/ 服务器/ 监控/connection.rb、
lib/ Mongo/ 服务器/ 监控/app_metadata.rb

Overview

此类是私有 API 的一部分。 应尽可能避免使用此类,因为它将来可能会被删除或更改。

负责通过 hello 命令定期轮询服务器,以使服务器保持最新状态。

是否所有工作都在背景线程中进行,以免干扰驱动程序执行的其他操作。

由于:

  • 2.0.0

在命名空间下定义

类: AppMetadata , Connection

常量摘要折叠

DEFAULT_HEARTBEAT_INTERVAL =

此常量是私有 API 的一部分。 应尽可能避免使用此常量,因为它将来可能会被删除或更改。

服务器状态刷新之间的默认间隔为10秒。

由于:

  • 2.0.0

10
MIN_SCAN_INTERVAL =

此常量是私有 API 的一部分。 应尽可能避免使用此常量,因为它将来可能会被删除或更改。

强制服务器扫描之间的最小时间间隔。 minHeartbeatFrequencyMS 是否位于 SDAM 规范中。

由于:

  • 2.0.0

0.5
RTT_WEIGHT_FACTOR =

此常量是私有 API 的一部分。 应尽可能避免使用此常量,因为它将来可能会被删除或更改。

已弃用。

将在版本3.0中删除。

用于计算平均移动往返时间的权重因子 (Alpha)。

由于:

  • 2.0.0

0.2

Loggable中包含的常量

Loggable::PREFIX

实例属性摘要折叠

Event::Publisher中包含的属性

#event_listeners

实例方法摘要折叠

包含在BackgroundThread中的方法

# 运行! , # 运行?

Loggable中包含的方法

#log_debug#log_error#log_ Fatal#log_info#log_warn#logger

Event::Publisher中包含的方法

#publish

构造函数详情

#initialize (服务器, event_listeners, 监控, options = {}) ⇒监控

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

注意:

决不能在服务器之外直接实例化监视器。

创建新的服务器监控。

例子:

创建服务器监控。

Mongo::Server::Monitor.new(address, listeners, monitoring)

参数:

  • server ( MongoDB Server )

    要监控的服务器。

  • event_listeners ( Event::Listeners )

    事件侦听器。

  • 监控 (监控)

    监控。

  • 选项 哈希 (默认为: {}

    选项。

选项哈希 ( options ):

  • :connect_timeout 浮点

    建立监控连接时使用的超时时间(以秒为单位)。

  • :heartbeat_interval 浮点

    定期服务器检查之间的间隔。

  • :logger 记录器

    要使用的自定义记录器。

  • :monitor_app_metadata Mongo::Server::Monitor::AppMetadata

    用于常规监控连接的元数据。

  • :push_monitor_app_metadata Mongo::Server::Monitor::AppMetadata

    用于推送监控连接的元数据。

  • :socket_timeout 浮点

    在监控连接上执行操作的超时时间(以秒为单位)。

引发:

  • ( ArgumentError )

由于:

  • 2.0.0



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ Mongo/ 服务器/ 监控.rb', line 76

def 初始化(server, event_listeners, 监控, 选项 = {})
  提高 ArgumentError, "错误的监控类型: #{ 监控 . Inspect } " 除非 监控.is_a?(监控)
  提高 ArgumentError, ' App 元数据 is required ' 除非 选项[:app_metadata]
  提高 ArgumentError, '需要推送监控应用元数据' 除非 选项[:push_monitor_app_metadata]

  @server = server
  @event_listeners = event_listeners
  @monitoring = 监控
  @options = 选项.冻结
  @mutex = 互斥锁.new
  @sdam_mutex = 互斥锁.new
  @next_earliest_scan = @next_wanted_scan = 时间.now
  @update_mutex = 互斥锁.new
  # Guards reads and 写入 of @connection so the polling connection can
  # 可以从另一个线程中取消。根据服务器监控规格,
  # cancelCheck 伪代码,锁的持有时间足以复制或
  # 分配引用 - 从不跨越阻塞检查。
  @connection_lock = 互斥锁.new
end

实例属性详细信息

# Monitoring监控(只读)

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

返回监控 监控。

返回:

  • (监控)

    监控 监控。

由于:

  • 2.0.0



127
128
129
# File 'lib/ Mongo/ 服务器/ 监控.rb', line 127

def 监控
  @monitoring
end

# options哈希(只读)

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

返回 options服务器选项。

返回:

  • (哈希)

    options服务器选项。

由于:

  • 2.0.0



108
109
110
# File 'lib/ Mongo/ 服务器/ 监控.rb', line 108

def 选项
  @options
end

# 服务器Server (readonly)

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

返回服务器此监控正在监控的服务器。

返回:

  • ( MongoDB Server )

    服务器此监控正在监控的服务器。

由于:

  • 2.0.0



98
99
100
# File 'lib/ Mongo/ 服务器/ 监控.rb', line 98

def server
  @server
end

实例方法详细信息

#cancel_check!Object

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

取消进行中的检查并关闭监控连接。

当服务器因网络错误而被标记为未知时(根据服务器监控规范("hello 或旧版 hello 取消"))调用。停止流媒体 PushMonitor(中断其等待的 hello 读取)并关闭轮询连接,以便下一个检查必须建立新连接,而不是通过可能已死亡的套接字重新验证服务器。

由于:

  • 2.0.0



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/ Mongo/ 服务器/ 监控.rb', line 210

def cancel_check!
  stop_push_monitor!

  # 复制锁下的连接引用,然后中断并关闭
  # 在锁外面。关闭套接字会中断任何进行中的
  # 在监控线程上读取;将引用置为 nil 会强制下一个
  # 检查是否重新连接。监控线程是新的唯一写入器
  # 连接,因此此线程可以安全地清除它。
  连接 = @connection_lock.同步 do
    conn = @connection
    @connection = nil
    conn
  end
  连接&。断开连接!
end

#连接Mongo::服务器::监控::Connection | nil

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

返回要使用的连接,在 @connection_lock 下读取,以便调用者在并发 cancel_check! 后从不观察过时引用。清除。

返回:

  • (Mongo::服务器::监控::Connection | nil)

    要使用的连接,在 @connection_lock 下读取,以便调用者在并发 cancel_check! 清除过程中不会观察到过时引用。清除。

由于:

  • 2.0.0



103
104
105
# File 'lib/ Mongo/ 服务器/ 监控.rb', line 103

def 连接
  @connection_lock.同步 { @connection }
end

#create_push_monitor!(topology_version, connection) ⇒ 对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

参数:

  • 连接 ( Mongo::Server::Monitor::Connection )

    新建的监控连接。显式传递(而不是从 @connection 读取),因此并发 cancel_check!无法在建立连接和构建 PushMonitor 之间将其归零。

由于:

  • 2.0.0



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ Mongo/ 服务器/ 监控.rb', line 175

def create_push_monitor!(topology_version, 连接)
  @update_mutex.同步 do
    @push_monitor = nil if @push_monitor && !@push_monitor.运行?

    @push_monitor ||= PushMonitor.new(
      self,
      topology_version,
      监控,
      **Utils.shallow_symbolize_keys(选项.合并(merge)(
                                       socket_timeout: heartbeat_interval + 连接.socket_timeout,
                                       app_metadata: 选项[:push_monitor_app_metadata],
                                       check_document: 连接.check_document
                                     ))
    )
  end
end

# do_work对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

检查服务器。

由于:

  • 2.0.0



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/ Mongo/ 服务器/ 监控.rb', line 140

def do_work
  scan!
  # @next_wanted_scan 可能会被推送监控更新。
  # 但是,我们需要检查终止标志,以便监控
  # 线程在请求时退出。
  循环 do
    增量 = @next_wanted_scan - 时间.now
    中断 除非 增量 > 0

    已发出信号 = server.scan_semaphore.wait(增量)
    中断 if 已发出信号 || @stop_requested
  end
end

# heartbeat_intervalFloat

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

定期服务器检查之间的间隔。

返回:

  • (浮点)

    心跳间隔,以秒为单位。

由于:

  • 2.0.0



113
114
115
# File 'lib/ Mongo/ 服务器/ 监控.rb', line 113

def heartbeat_interval
  选项[:heartbeat_interval] || DEFAULT_HEARTBEAT_INTERVAL
end

# push_monitorServer::PushMonitor | nil

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

返回 推送监控(如果正在使用)。

返回:

由于:

  • 2.0.0



131
132
133
134
135
# File 'lib/ Mongo/ 服务器/ 监控.rb', line 131

def push_monitor
  @update_mutex.同步 do
    @push_monitor
  end
end

#重新启动!线程

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

除非当前线程处于活动状态,否则重新启动服务器监控。

例子:

重新启动监控。

monitor.restart!

返回:

  • (线程)

    监控运行所在的线程。

由于:

  • 2.1.0



308
309
310
311
312
313
314
# File 'lib/ Mongo/ 服务器/ 监控.rb', line 308

def 重新启动!
  if @thread && @thread.alive?
    @thread
  else
    运行!
  end
end

#run_sdam_flow(result, awaited: false, scan_error: nil, rtt_only: false) ⇒ 对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

由于:

  • 2.0.0



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/ Mongo/ 服务器/ 监控.rb', line 268

def run_sdam_flow(结果, 等待: false, scan_error: nil, rtt_only: false)
  @sdam_mutex.同步 do
    old_description = server.描述

    # 仅 RTT 测量(流媒体协议活动)不得更新
    # 拓扑结构或发布 SDAM 事件。RTT 收集的是
    # 通过下一个流媒体您好说明将其集成到
    # 共享 RTT 计算器。以下安排仍在运行,因此
    # 监控 keeps pacing its checks.
    除非 rtt_only
      new_description = 说明.new(
        server.地址,
        结果,
        average_round_trip_time: server.round_trip_time_calculator.average_round_trip_time,
        minimum_round_trip_time: server.round_trip_time_calculator.minimum_round_trip_time
      )
      server.集群.run_sdam_flow(server.描述, new_description, 等待: 等待, scan_error: scan_error)
    end

    server.描述.点击 do |new_description|
      除非 等待
        if new_description.未知? && !old_description.未知?
          @next_earliest_scan = @next_wanted_scan = 时间.now
        else
          @next_earliest_scan = 时间.now + MIN_SCAN_INTERVAL
          @next_wanted_scan = 时间.now + heartbeat_interval
        end
      end
    end
  end
end

# scan!说明

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

注意:

如果系统时钟向后移动,此方法可能会休眠很长时间。

注意:

此方法的返回值已弃用。 在版本3.0.0中,此方法没有返回值。

对具有限制的服务器执行检查,并更新服务器的描述和平均往返时间。

如果在 MIN_SCAN_INTERVAL 秒内检查服务器,则休眠直到自上次检查以来已经过 MIN_SCAN_INTERVAL 秒。 然后执行检查,其中包括在受监控的服务器上运行 hello 并更新服务器描述。

返回:

  • (描述)

    更新后的描述。

由于:

  • 2.0.0



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/ Mongo/ 服务器/ 监控.rb', line 244

def scan!
  # 通常,背景线程会调用此方法。
  # 但也可以调用 scan!直接在监控上显示。
  # 一次只允许执行一次扫描。
  @mutex.同步 do
    Threat_scan_Frequency!

    # 当流媒体协议处于活动状态时,PushMonitor 是
    # 权威 SDAM 源,此扫描仅测量 RTT
    # 专用连接。根据服务器监控规范,RTT
    # 命令不得发布事件或更新拓扑结构。计算
    # this before do_scan, which may (re)connect and change the 省/市/自治区.
    rtt_only = rtt_measurement_only?

    开始
      结果 = do_scan(publish_heartbeat: !rtt_only)
    救援 StandardError => e
      run_sdam_flow({}, scan_error: e, rtt_only: rtt_only)
    else
      run_sdam_flow(结果, rtt_only: rtt_only)
    end
  end
end

# stop!true | false

停止背景线程,并在合理的时间内等待其终止。

返回:

  • ( true | false )

    线程是否已终止。

由于:

  • 2.0.0



160
161
162
163
164
165
166
167
168
169
# File 'lib/ Mongo/ 服务器/ 监控.rb', line 160

def 停止!
  stop_push_monitor!

  # 转发父节点的返回值
  .点击 do
    # 重要提示:断开连接应在后台线程之后发生
    # 终止。
    连接&。断开连接!
  end
end

# stop_push_monitor!对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

由于:

  • 2.0.0



192
193
194
195
196
197
198
199
# File 'lib/ Mongo/ 服务器/ 监控.rb', line 192

def stop_push_monitor!
  @update_mutex.同步 do
    if @push_monitor
      @push_monitor.停止!
      @push_monitor = nil
    end
  end
end

# to_s对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

由于:

  • 2.0.0



316
317
318
# File 'lib/ Mongo/ 服务器/ 监控.rb', line 316

def to_s
  " #< #{ self . class . name } : #{ object_id } #{ 服务器 .解决} > "
end