客户端走进服务器并询问“新功能”。 –序列号问题 [英] A Client Walks Into a Server And Asks "What's New?" – Problems With Sequence Numbers

查看:62
本文介绍了客户端走进服务器并询问“新功能”。 –序列号问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种针对极端情况的解决方案,在这种情况下,客户端不断向服务器询问新功能将失败。

I'm looking for a solution to an edge case scenario where a client continually asking the server for what's new will fail.

在此示例中,由于存在另一个极端情况问题,因此不使用时间戳。此问题已解决:客户端走进服务器并询问新功能。 –时间戳问题

In this example, I'm not using timestamps because of another edge case problem. That's handled in this question: A Client Walks Into a Server And Asks "What's New?" – Problems With Timestamps

假设我们使用的是序列号。每次更改表格时,都会自动更新一个序列号。更新任何行时,它都会记录当前序列。然后,客户要问的是自上次请求以来的最新消息。简单?是的,但是...

Assume we're using sequence numbers. There's a single sequence number that is atomically updated every time the table is changed. When any row is updated it records the current sequence. Then it's just a matter of the client asking for what's new since the last sequence it asked for. Simple? Yes, but...

故障场景:

Sequence starts at 1
1) Client A starts update. Updates sequence to 2
2) Client B starts update. Updates sequence to 3
3) Client B updates rows with sequence 3
4) Client C requests changes >1.  Gets B's changes. Good.
5) Client A updates rows with sequence 2. 
6) Client C requests changes >3.  Gets nothing. Doesn’t get Client A’s changes.

由于我们使用的是MongoDB,我不认为我们可以轻松地在更新。而且,如果可以的话,我会担心性能。

Because we're using a MongoDB, I don't believe we can easily lock the sequence during an update. And, if we could, I worry about performance.

让客户反复询问新功能似乎是一个常见的用例,而且我发现没有找到更好的解决方案令人惊讶

Having clients ask "what's new" repeatedly seems like a common use case and I find it surprising not to find a better wealth of best practices on this.

是否有解决此情况的建议,或建议更好,最好与平台无关的解决方案来请求更改?

Any ideas on solving this scenario or recommending a better, preferably platform agnostic, solution for asking for changes?

推荐答案

您可以做的一件事就是维护一堆正在使用的序列号以及要分配的下一个序列号。请尝试以下操作:

One thing you can do is maintain a heap of sequence numbers that are in use as well as the "next sequence number to assign". Try the following:


  • 获取序列号时,请将其放在使用中的映射中。

  • 完成对该序列号的更改后,将其从使用中 std :: set
  • 中删除。
  • 保持跟踪集合中的最小值。每当最小值从 x更改为 y时,客户端C的请求值都将从x更改为y,但不大于y。

  • When you grab a sequence number, put it in an "in-use" map.
  • When you are done making changes with that sequence number, remove it from the "in-use" std::set
  • Keep track of the minimum in the set. Whenever the minimum changes from "x" to "y", have Client C request values from x to y, but no greater than y.

因此,在您的示例中,当您将序列更新为2时,会在使用中添加1。然后,当您更新到3时,将2放入其中,并且集合中包含1和2。完成2的工作后,将从集合中删除2,但是客户端C不会进行任何更改,因为最小值最小, 1,不变。当客户端A完成1之后,最小值从1变为3,客户端C可以读取从1到3的更改。

So, in your example, when you update the sequence to 2, 1 is put in the in-use set. Then, when you update to 3, 2 is put in there, and the set contains 1 and 2. When the work for 2 is done, 2 is removed from the set, but client C does not pick up any changes because the minimum, 1, is unchanged. When client A is done with 1, the minumum changes from 1 to 3, and client C may read the changes from 1 to 3.

对于更复杂的示例,假设您有6个客户端使用顺序号11、12、13、14、15和16,但按以下顺序完成:12、13、11、15、14、16(这是它们从 -use set。在此示例中,在11消失之后,客户端C可以读取11到13,因为最小值从11变为14。然后,在14消失之后,客户端C可以读取14和15,这是最小变化。从14到16。然后,当16消失时,客户端C可以读取16。

For a more complex example, suppose you have 6 clients use sequence numbers 11, 12, 13, 14, 15, and 16, yet finish in the following order: 12, 13, 11, 15, 14, 16 (which is the order they are removed from the "in-use" set. In this example, after 11 is gone, client C may read 11 through 13, because the minumum changes from 11 to 14. Then, after 14 is gone, client C may read 14 and 15, as the minimum changes from 14 to 16. Then, when 16 is gone, client C can read 16.

基本上,这是我们在TokuMX复制中使用的算法,它决定了可能的oplog条目是客户端A和客户端B是向操作日志进行写操作的线程,客户端C将是辅助操作提取操作日志数据的可尾游标。

This is basically the algorithm we use in TokuMX replication that decides what oplog entries may be replicated to secondaries. Clients A and B would be threads doing writes to the oplog and Client C would be a tailable cursor from the secondary pulling oplog data.

这篇关于客户端走进服务器并询问“新功能”。 –序列号问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆