阅读偏好标签集列表

如果一个或多个副本集节点与 tags关联,您可以在读取首选项中指定一个标记集列表(标记集数组)以定位这些节点。

要使用标签配置节点,请设置members[n].tags为包含标签名称和值对的文档。标签的值必须是字符串。

{ "<tag1>": "<string1>", "<tag2>": "<string2>",... }

然后,您可以在读取首选项中包含一个标记集列表,以定位标记的成员。标签集列表是一组标签集,其中每个标签集包含一个或多个标签/值对。

[ { "<tag1>": "<string1>", "<tag2>": "<string2>",... }, ... ]

为了找到副本集节点,MongoDB 连续尝试每个文档,直到找到匹配项。看标签匹配顺序了解详情。

例如,如果从节点具有以下内容 members[n].tags

{ "region": "South", "datacenter": "A" }

然后,以下标签集列表可以将读取操作定向到上述次级(或具有相同标签的其他节点):

[ { "region": "South", "datacenter": "A" }, { } ]     // Find members with both tag values. If none are found, read from any eligible member.
[ { "region": "South" }, { "datacenter": "A" }, { } ] // Find members with the specified region tag. Only if not found, then find members with the specified datacenter tag. If none are found, read from any eligible member.
[ { "datacenter": "A" }, { "region": "South" }, { } ] // Find members with the specified datacenter tag. Only if not found, then find members with the specified region tag. If none are found, read from any eligible member.
[ { "region": "South" }, { } ]                        // Find members with the specified region tag value. If none are found, read from any eligible member.
[ { "datacenter": "A" }, { } ]                        // Find members with the specified datacenter tag value. If none are found, read from any eligible member.
[ { } ]                                               // Find any eligible member.

标签匹配顺序

如果标签集列表包含多个文档,MongoDB 会连续尝试每个文档,直到找到匹配项。找到匹配项后,该标记集将用于查找所有符合条件的匹配节点,而忽略其余标记集。如果没有节点与任何标记集匹配,则读取操作返回错误。

提示

为避免在没有节点匹配任何标记规范时出现错误,您可以添加一个空文档{ }作为标记集列表的最后一个元素,以从任何符合条件的节点处读取。

例如,考虑以下包含三个标签集的标签集列表:

[ { "region": "South", "datacenter": "A" },  { "rack": "rack-1" }, { } ]

首先,MongoDB 尝试查找同时标记有"region": "South""datacenter": "A"的节点。

{ "region": "South", "datacenter": "A" }
  • 如果找到一个节点,则不考虑剩余的标记集。相反,MongoDB 使用此标记集来查找所有符合条件的节点。

  • 否则,MongoDB 会尝试查找具有第二个文档中指定标签的节点

    { "rack": "rack-1" }
    
    • 如果发现某个节点被标记,则不考虑剩余的标记集。相反,MongoDB 使用此标记集来查找所有符合条件的节点。

    • 否则,考虑第三个文件。

      { }
      

      空文档匹配任何符合条件的节点。

标签集列表和阅读偏好模式

标签与主节点模式不兼容,并且通常仅在选择集合的从节点进行读取操作时才适用。 但是,最近读取模式在与标签集列表结合使用时,会选择网络延迟最低的匹配节点。 该成员可能是主节点或从节点。

模式 笔记
primaryPreferred 指定的标签集列表仅在选择符合条件的次级时适用。
secondary 指定的标签集列表始终适用。
secondaryPreferred 指定的标签集列表仅在选择符合条件的次级时适用。
nearest 指定的标签集列表适用于选择主要还是符合条件的次要。

有关模式和标签集列表之间交互的信息,请参阅 特定的阅读首选项模式文档。

有关配置标签集列表的信息,请参阅 配置副本集标签集教程。

阅读偏好阅读偏好maxStalenessSeconds

原文链接 -https://docs.mongodb.com/manual/core/read-preference-tags/

译者:陆文龙

Copyright © 上海锦木信息技术有限公司 all right reserved,powered by Gitbook文件修订时间: 2023-09-01 17:10:26

results matching ""

    No results matching ""