阅读关注点“快照”和相应的群集时间

发布于 2025-02-10 17:27:59 字数 588 浏览 0 评论 0 原文

MongoDB 5.X中有一个非常方便的添加功能,它允许执行快照在某些读取操作中读取关注,并指定通过 atclustertime 进行快照的时间戳。如果未提供时间戳,则 mongo 将自动选择它,并将其值返回给用户以备将来参考。请参阅:

以来 - 但是,只有具有写入问题的内部交易 - 我想知道是否也可以获取一些有关何时拍摄快照的信息,类似于5.x的操作。我了解我无法明确指定 atoperationTime ,但是 mongo 需要单独选择时间戳,因此似乎可以合理地期望此信息应在某个地方提供。

There's been a really handy addition in MongoDB 5.x that allows enforcing snapshot read concern in some read operations outside transactions and specify the timestamp at which the snapshot is taken via atClusterTime. If the timestamp is not provided, mongo will select it automatically and kindly return its value to the user for future reference. See:

https://www.mongodb.com/docs/v6.0/reference/read-concern-snapshot/#read-concern-and-atclustertime

Since MongoDB 4.x also supports read concern snapshot - but only inside transactions with write concern majority - I am wondering if it is also possible to obtain some information about when exactly the snapshot was taken, similarly to what 5.x is doing. I understand that I cannot explicitly specify atOperationTime but somehow mongo needs to select the timestamp on its own, so it seems reasonable to expect that this information should be available somewhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦旅人picnic 2025-02-17 17:27:59

恐怕没有方便的方法可以为Mongo&lt提供集群时间; 5.0。您可以随时检查日志,但我不会认为它是可靠的解决方案。无论如何,这是您可以在mongo shell上运行的脚本:

db.adminCommand( { getLog : "global" } ).log
    .map(x => JSON.parse(x))
    .filter(x => x.c === "TXN")
    .forEach(x => print(JSON.stringify(x.attr.parameters.readConcern)))

首先,让我们说 getlog 命令 global 过滤器仅返回最新日志,因此,如果您正在寻找旧交易物理日志文件。该脚本:

  1. 解析日志条目以将其从字符串转换为JS对象
  2. 过滤条目,仅保留与交易相关的条目,请参见此处对于所有类型和附加信息
  3. 检索 readconcern 参数并打印到控制台,

您应该看到输出类似对此:

{"level":"snapshot","provenance":"clientSupplied"}
{"level":"snapshot","afterClusterTime":{"$timestamp":{"t":1657483320,"i":1}},"provenance":"clientSupplied"}
{"level":"snapshot","afterClusterTime":{"$timestamp":{"t":1657483977,"i":1}},"provenance":"clientSupplied"}

我们找到了群集时间:库集团时间

在第一次交易中,我将文档添加到一个空集合中,这可能就是为什么它不显示群集时间(当然也没有使用)。我不知道每个参数的含义以及您无法获得群集时间的所有情况,但是有个好消息。要获取属于给定事务ID的日志,您需要应用此过滤器(而不是上面显示的一个过滤器):

.filter(x => x.c === "TXN" && x.attr.parameters.txnNumber === your_txn_id)

I am afraid there is no convenient way to get the cluster time for Mongo < 5.0. You can always inspect the logs, but I would not consider it a reliable solution. Anyway, here is a possible script you could run on the mongo shell:

db.adminCommand( { getLog : "global" } ).log
    .map(x => JSON.parse(x))
    .filter(x => x.c === "TXN")
    .forEach(x => print(JSON.stringify(x.attr.parameters.readConcern)))

Let's start by saying that the getLog command with the global filter returns only the most recent logs, so if you are looking for old transactions I guess you need to parse the physical log files. That script:

  1. parses the log entries to convert them from strings to JS objects
  2. filters the entries and keeps only those related to transactions, see here for all the types and additional info
  3. retrieves the readConcern parameters and print them to console

You should see an output similar to this one:

{"level":"snapshot","provenance":"clientSupplied"}
{"level":"snapshot","afterClusterTime":{"$timestamp":{"t":1657483320,"i":1}},"provenance":"clientSupplied"}
{"level":"snapshot","afterClusterTime":{"$timestamp":{"t":1657483977,"i":1}},"provenance":"clientSupplied"}

We found the cluster time: afterClusterTime.

In the first transaction, I added a document to an empty collection, which could be why it does not show the cluster time (and was not used, of course). I don't know the meaning of each parameter and all the cases in which you will not get the cluster time, but there is good news. To get the logs, if exist, belonging to a given transaction id, you need to apply this filter (instead of the one shown above):

.filter(x => x.c === "TXN" && x.attr.parameters.txnNumber === your_txn_id)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文