类:Mongo::ServerSelector::Base

继承:
对象
  • 对象
显示全部
定义于:
lib/ Mongo/server_selector/base.rb

Overview

由于:

  • 2.0.0

直接已知子类

NearestPrimaryPrimaryPreferredsecondarysecondaryPreferred

实例属性摘要折叠

实例方法摘要折叠

构造函数详情

#initialize(options = nil) ⇒ Base

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

初始化服务器选择器。

例子:

初始化选择器。

Mongo::ServerSelector::Secondary.new(:tag_sets => [{'dc' => 'nyc'}])

在没有选项的情况下初始化偏好。

Mongo::ServerSelector::Secondary.new

参数:

  • 选项 哈希 (默认为: nil

    服务器偏好选项。

选项哈希 ( options ):

  • :local_threshold 整数

    最近选择的本地阈值边界(以秒为单位)。

  • :max_staleness 整数

    从节点可能遭受但仍有资格进行读取的最大复制延迟(以秒为单位)。 值 - 1的处理方式与 nil 相同,即没有最大陈旧度。

  • :hedge 哈希 | nil

    指定是否在服务器上启用对冲读的哈希值。 默认不启用对冲读。 指定此选项时,必须采用以下格式:{ enabled: true },其中 :enabled 键的值为布尔值。

引发:

由于:

  • 2.0.0



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ Mongo/server_selector/base.rb', line 49

def 初始化(选项 = nil)
  选项 = 选项 ? 选项.dup : {}
  if 选项[:max_staleness] == -1
    选项.删除(:max_staleness)
  end
  @options = 选项
  @tag_sets = 选项[:tag_sets] || []
  @max_staleness = 选项[:max_staleness]
  @hedge = 选项[:hedge]

  validate!
end

实例属性详细信息

#对冲哈希 | nil (只读)

返回对冲 指定是否启用对冲读的文档。

返回:

  • ( Hash | nil )

    对冲 指定是否启用对冲读的文档。

由于:

  • 2.0.0



76
77
78
# File 'lib/ Mongo/server_selector/base.rb', line 76

def 对冲
  @hedge
end

# max_staleness整数(只读)

返回从节点(secondary node from replica set)从节点可能遭受但仍有资格进行读取的最大复制延迟(以秒为单位)。

返回:

  • ( Integer )

    max_staleness 从节点可能遭受但仍有资格进行读取的最大复制延迟(以秒为单位)。

由于:

  • 2.4.0



72
73
74
# File 'lib/ Mongo/server_selector/base.rb', line 72

def max_staleness
  @max_staleness
end

# options哈希(只读)

返回 options 选项。

返回:

  • (哈希)

    options 选项。

由于:

  • 2.0.0



63
64
65
# File 'lib/ Mongo/server_selector/base.rb', line 63

def 选项
  @options
end

# tag_sets数组(只读)

返回用于选择服务器的标签集。

返回:

  • ( Array )

    tag_sets 用于选择服务器的标签集。

由于:

  • 2.0.0



66
67
68
# File 'lib/ Mongo/server_selector/base.rb', line 66

def tag_sets
  @tag_sets
end

实例方法详细信息

# == (other) ⇒ true , false

检查两个服务器选择器的相等性。

例子:

检查服务器选择器相等性。

preference == other

参数:

  • 其他 ( Object )

    其他偏好。

返回:

  • ( true , false )

    对象是否相等。

由于:

  • 2.0.0



136
137
138
139
# File 'lib/ Mongo/server_selector/base.rb', line 136

def ==(其他)
  名称 == 其他.名称 && 对冲 == 其他.对冲 &&
    max_staleness == 其他.max_staleness && tag_sets == 其他.tag_sets
end

#候选(集群) ⇒数组< MongoDB Server >

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

从集群中返回可接受类型的服务器。

不执行过时性验证、过时性筛选或延迟筛选。

参数:

  • 集群 (集群)

    集群。

返回:

由于:

  • 2.0.0



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/ Mongo/server_selector/base.rb', line 403

def 候选(集群)
  服务器 = 集群.服务器
  服务器. do |server|
    validate_max_staleness_support!(server)
  end
  if 集群.单身?
    服务器
  elsif 集群.分片的?
    服务器
  elsif 集群.replica_set?
    select_in_replica_set(服务器)
  else
    # 未知集群 - 无服务器
    []
  end
end

#检查string

检查服务器选择器。

例子:

检查服务器选择器。

selector.inspect

返回:

  • ( string )

    检查。

由于:

  • 2.2.0



122
123
124
# File 'lib/ Mongo/server_selector/base.rb', line 122

def 检查
  " #< #{ self . class . name } :0 x #{ object_id } tag_sets= #{ tag_sets .inspect } } max_staleness = # { max_staleness .inspect } } href= " } > "
end

# local_threshold浮点

已弃用。

现在,当选择服务器时,会从集群选项中获取此设置。 将在版本3.0中删除。

获取最近选择的本地阈值边界(以秒为单位)。

例子:

获取本地阈值。

selector.local_threshold

返回:

  • (浮点)

    本地阈值。

由于:

  • 2.0.0



105
106
107
# File 'lib/ Mongo/server_selector/base.rb', line 105

def local_threshold
  @local_threshold ||= (选项[:local_threshold] || ServerSelector::LOCAL_THRESHOLD)
end

# local_threshold_with_cluster (集群) ⇒对象

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

由于:

  • 2.0.0



110
111
112
# File 'lib/ Mongo/server_selector/base.rb', line 110

def local_threshold_with_cluster(集群)
  选项[:local_threshold] || 集群.选项[:local_threshold] || LOCAL_THRESHOLD
end

# select_server (集群, 网络探测(ping) = nil, session = nil, write_aggregation: false, deprioritized: [], timeout: nil) ⇒ Mongo::Server

从指定集群中选择一台服务器,同时考虑指定会话的mongos固定。

如果已给定会话且具有固定的服务器,则该服务器是考虑选择的唯一服务器。 如果服务器类型为mongos ,则立即返回;否则将启动对此服务器的监控检查以更新其状态,如果服务器在服务器选择超时时间内成为mongos ,则返回。

如果没有给出会话或会话没有固定的服务器,则在指定集群中与此服务器选择器对象的偏好匹配的所有服务器之间执行正常的服务器选择进程。 对集群中的服务器启动监控检查,直到找到合适的服务器,直到服务器选择超时。

如果在服务器选择超时时间内未找到合适的服务器,则此方法将引发 Error::NoServerAvailable。

参数:

  • 集群 ( Mongo::Cluster )

    从中选择符合条件的服务器的集群。

  • ping ( true , false ) (默认为: nil

    是否在选择之前对服务器进行 ping 操作。 已弃用并忽略。

  • 会话 (会话| nil ) (默认为: nil

    考虑 mongos 固定的可选会话。 在版本2.10.0中添加。

  • write_aggregation ( true | false ) (默认为: false

    我们是否需要一个支持在从节点上写入聚合(例如使用 $merge/$out)的服务器。

  • deprioritized (Array ) (默认为: []

    仅当没有其他可用服务器时才应选择的服务器列表。 用于避免在重试命令时连续两次选择同一服务器。

  • :timeout ( Float | nil )

    操作超时(以秒为单位)(如有)。

返回:

引发:

由于:

  • 2.0.0



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ Mongo/server_selector/base.rb', line 183

def select_server(
  集群,
  ping = nil,
  会话 = nil,
  write_aggregation: false,
  取消优先级: [],
  timeout: nil
)
  select_server_impl(集群, ping, 会话, write_aggregation, deprioritized, 超时).点击 do |server|
    if Lint.已启用? && !server..准备好了吗?
      提高 错误::LintError, '服务器选择器返回带有未就绪池的服务器'
    end
  end
end

# server_selection_timeout浮点

已弃用。

现在,当选择服务器时,会从集群选项中获取此设置。 将在版本3.0中删除。

获取服务器选择的超时时间。

例子:

获取服务器选择超时时间(以秒为单位)。

selector.server_selection_timeout

返回:

  • (浮点)

    超时。

由于:

  • 2.0.0



89
90
91
92
# File 'lib/ Mongo/server_selector/base.rb', line 89

def server_selection_timeout
  @server_selection_timeout ||=
    (选项[:server_selection_timeout] || ServerSelector::SERVER_SELECTION_TIMEOUT)
end

#合适的_servers (集群) ⇒数组< Server >

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

从集群中返回满足服务器选择器的服务器。

参数:

  • 集群 (集群)

    集群。

返回:

由于:

  • 2.0.0



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/ Mongo/server_selector/base.rb', line 427

def 合适的服务器(集群)
  if 集群.单身?
    候选(集群)
  elsif 集群.分片的?
    local_threshold = local_threshold_with_cluster(集群)
    服务器 = 候选(集群)
    near_servers(服务器, local_threshold)
  elsif 集群.replica_set?
    validate_max_staleness_value!(集群)
    候选(集群)
  else
    # 未知集群 - 无服务器
    []
  end
end

# try_select_server (集群, write_aggregation: false, deprioritized: []) ⇒ Server | nil

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

尝试查找合适的服务器,如果有可用的服务器,则返回该服务器;如果没有合适的服务器,则返回 nil。

参数:

  • 集群 ( Mongo::Cluster )

    从中选择符合条件的服务器的集群。

  • write_aggregation ( true | false ) (默认为: false

    我们是否需要一个支持在从节点上写入聚合(例如使用 $merge/$out)的服务器。

  • deprioritized (Array ) (默认为: []

    仅当没有其他可用服务器时才应选择的服务器列表。 用于避免在重试命令时连续两次选择同一服务器。

返回:

  • ( MongoDB Server | nil )

    合适的服务器(如果存在)。

由于:

  • 2.0.0



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/ Mongo/server_selector/base.rb', line 352

def try_select_server(集群, write_aggregation: false, 取消优先级: [])
  服务器 = if write_aggregation && 集群.replica_set?
    # 1 . 检查集群从节点(secondary node from replica set)的所有服务器是否都支持从节点写入。
    is_write_supported = 集群.服务器.化简(reduce)(true) do |res, server|
      res && server.功能.merge_out_on_secondary_enabled?
    end

    if is_write_supported
      # 2 . 如果所有服务器都支持从从节点(secondary node from replica set)写入,则我们会尊重读取偏好(read preference)。
      合适的服务器(集群)
    else
      # 3 . 否则,我们将回退到副本集的主节点。
      [集群.服务器.检测(:主节点 (primary node in the replica set)?)]
    end
  else
    合适的服务器(集群)
  end

  # 此服务器列表可以按特定方式排序
  # 通过选择器(例如,对于从节点(secondary node from replica set)首选,第一个
  # 服务器可能是从节点,第二个服务器可能是主节点)
  # 我们应该按照顺序选择这里的第一台服务器
  server = 合适的服务器(服务器, deprioritized)

  if server
    if Lint.已启用?
      # 此处服务器的平均 RTT 可能为零
      # 因为 ARTT 来自可能更新的描述
      # 在服务器选择运行时由后台线程执行。
      # 目前 lint模式不是公共功能,如果/当此
      # 项更改 (https://jira.mongodb.org/browse/RUBY- 1576 ) the
      # 需要删除对 ARTT 不为零的要求。
      if server.average_round_trip_time.nil?
        提高 错误::LintError, "MongoDB Server #{ server .address } has nil average rtt "
      end
    end
  end

  server
end