计算机的时钟(三):向量时钟

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本系列文章主要介绍计算机系统中时钟的处理。主要内容包含NTP,Lamport逻辑时钟,向量时钟,TrueTime等。本文是第三篇,介绍向量时钟。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在"},{"type":"link","attrs":{"href":"http://yang.observer/2020/07/26/time-lamport-logical-time/","title":null},"content":[{"type":"text","text":"《计算机的时钟(二):Lamport逻辑时钟》"}]},{"type":"text","text":"中,我们介绍过"},{"type":"text","marks":[{"type":"strong"}],"text":"对于任意两个事件a和b,如果 a → b,那么 C (a) < C (b),但是反向并不成立,C(a) < C(b)推不出来a→b。"},{"type":"text","text":"本文介绍的向量时钟(Vector Clocks)可以保证反向也能成立,也即充分条件变成了充分必要条件,这是如何做到的呢,且听我一一道来。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"向量时钟算法"},{"type":"link","attrs":{"href":"http://yang.observer/2020/09/12/vector-clock/#%E5%90%91%E9%87%8F%E6%97%B6%E9%92%9F%E7%AE%97%E6%B3%95","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"向量时钟是1988年由Colin Fidge和Friedemann Mattern提出的,比Lamport老爷子提出逻辑时钟晚了刚好十年。Lamport逻辑时钟存在两个问题,一个是上面说的C(a) < C(b)不能推导出a → b,这样导致即使知道了两个逻辑时钟值,但却不能确定这两个事件的因果关系。第二个问题是进程“看到的”事件发生顺序可能是错的。举个例子:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ab/abfcc05787784ba084ff672f1fc38a7b.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"图一中,a事件发生于c之前,但是由于网络延迟,P2先收到c事件再收到a事件,导致从P2的视角看先发生c事件再发生a事件。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"向量时钟可以解决这两个问题,它的思想是进程间通信的时候,不光同步本进程的时钟值,还同步自己知道的其他进程的时钟值。算法如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"分布式系统中每个进程Pi保存一个本地逻辑时钟向量值VCi,向量的长度是分布式系统中进程的总个数。VCi (j) 表示进程Pi知道的进程Pj的本地逻辑时钟值,VCi的更新算法如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"初始化VCi的值全为0:VCi = [0, … , 0]"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"进程Pi每发生一次事件,VCi[i]加1。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"进程Pi给进程Pj发送消息,需要带上自己的向量时钟VCi。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"进程Pj接收消息,需要做两步操作。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"向量时钟举例:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c1/c1f354c5afe46a60d9960d95e57f31af.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"向量时钟里的向量是一种偏序关系:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果向量VCi中的每个元素VCi[k]都小于等于VCj中的对应元素VCj[k],则VCi "},{"type":"text","marks":[{"type":"italic"}],"text":"≤"},{"type":"text","text":" VCj。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果VCi中的每个元素VCi[k]都和VCj中的对应元素VCj[k]相等,则VCi = VCj。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果VCi和VCj不能比较大小,则称两个向量是并发的 VCi || VCj。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"从以上算法可以很容易地得出下面两个结论:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"同一个进程内的两个事件a和b,如果 a → b,那么 VCi (a) < VCi (b)。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"a是Pi进程的消息发送事件,b是Pj进程该消息的接收事件,那么 VCi (a) < VCj (b)。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由以上两个结论又可以得出,"},{"type":"text","marks":[{"type":"strong"}],"text":"对于任意两个事件a和b,如果 a → b,那么 VC (a) < VC (b)"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"怎么证明VC(a) < VC(b) 可以推导a → b呢?(以下证明过程摘自"},{"type":"link","attrs":{"href":"https://writings.sh/post/logical-clocks","title":null},"content":[{"type":"text","text":"逻辑时钟 - 如何刻画分布式中的事件顺序"}]},{"type":"text","text":")"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果事件a和b在同一个进程内,很显然 a → b。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果事件a和b在不同进程内,比如Pa和Pb。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"设VCa = [m ,n], VCb = [s, t]。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因为VCa < VCb,所以m "},{"type":"text","marks":[{"type":"italic"}],"text":"≤"},{"type":"text","text":" s,所以必然在不早于a之前和不晚于b之后的时间内,Pa向Pb发送了消息,否则Pb对Pa的计数器得不到及时刷新,s就不会小于m。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/53/53c66c03a1a9e622306e1128c3efa2be.jpeg","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"实际上,可以分为以下几种情况:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/82/822212f58663a081a7294253be97ce6e.jpeg","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"当a = c且d = b,易得a → b。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"当a = c且d → b,由传递性,得a → b。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"同样对于d = b且a → c的情况。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"当a → c且d → b,根据进程内的算法逻辑性和传递性,也很容易得出结论。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"综上: VCa < VCb 推导出 a → b 得证。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"向量时钟将Lamport逻辑时钟的全序时钟值改成了向量时钟的偏序关系,可以准确刻画事件的顺序,"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"因果关系与光锥"},{"type":"link","attrs":{"href":"http://yang.observer/2020/09/12/vector-clock/#%E5%9B%A0%E6%9E%9C%E5%85%B3%E7%B3%BB%E4%B8%8E%E5%85%89%E9%94%A5","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"向量时钟可以反映事件之间的因果关系,下图中对于中间的B4事件,左边蓝色区域内的事件都是B4事件的“因”,右边红色区域内的事件都是B4事件的“果”。而上下白色区域内的事件则属于和B4事件没有因果关系的平行事件。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/8e/8eceaa63e61946c20c47f1b12ab6e214.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这让我想起了相对论中的"},{"type":"link","attrs":{"href":"https://zh.wikipedia.org/wiki/%E5%85%89%E9%94%A5","title":null},"content":[{"type":"text","text":"光锥"}]},{"type":"text","text":"。光锥是"},{"type":"link","attrs":{"href":"https://zh.wikipedia.org/wiki/%E9%96%94%E8%80%83%E6%96%AF%E5%9F%BA%E6%99%82%E7%A9%BA","title":null},"content":[{"type":"text","text":"闵可夫斯基时空"}]},{"type":"text","text":"下能够与一个单一事件通过光速存在因果联系的所有点的集合,简单地说就是事件所能影响的时空范围,它的图形如下:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/19/19942ef0921fbc5ca2bf9d0d6b81ad44.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"时空是由三维空间和一维时间构成的,由于人类是三维空间中的生物,没办法感受四维时空,所以光锥图中把三维的空间简化为了二维xy平面,时间是z轴。空间平面中的每一个维度都可以向正负两个方向移动,而时间不可能倒退,只能向正方向移动。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以当前观测者为原点,因为任何物体的运动速度都不可能超过光速,所以它产生的事件造成的影响也不可能超过光速。这个事件每秒钟在二维空间平面中的传播范围也不可能超过光每秒钟走过的地方。所以光锥呈现一个圆锥的形状。光锥分为未来光锥和过去光锥,未来光锥指当前发生的事件对未来造成影响的范围,而过去光锥指过去多大时空范围的事件能对现在造成影响。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/cb/cbc90cdc58c2270b8e216d7dd4cac6af.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上图中,把当前时刻的太阳作为光锥的座标原点,因为太阳光到地球需要8分钟,所以当前时刻太阳发出的光需要8分钟后才能对地球造成影响。这就是未来光锥,也就是当前时刻的事件对未来造成影响的范围。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/4b/4be90119f344ff21b8e150e4cb5213fb.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果把当前时刻的地球作为光锥的座标原点,那么只有8分钟之前的太阳光才会对地球造成影响,8分钟前的太阳处于过去光锥的范围内。而4分钟前的太阳不会对现在的地球造成影响,它处在过去光锥之外。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"“宇宙中的任何事件都只能影响它的将来光锥内的物体,凡事在事件的将来光锥外的物体不会受该事件的任何影响。”"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"向量时钟的应用"},{"type":"link","attrs":{"href":"http://yang.observer/2020/09/12/vector-clock/#%E5%90%91%E9%87%8F%E6%97%B6%E9%92%9F%E7%9A%84%E5%BA%94%E7%94%A8","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"向量时钟有什么作用呢?举两个使用例子。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作用之一是检测数据冲突。分布式数据库中数据存在多个副本,每个副本都可能提供写操作,如果两个副本都对同一个数据进行写操作,就可能造成冲突,向量时钟可以检测这种冲突,但不能解决冲突。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/4c/4c15cb250990b459351729a4262dc27d.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上图中 a → b → c → d → e 都有因果关系,不会产生冲突,但是e和f是并发关系 e || f,这两个会有数据冲突,向量时钟可以检测这种冲突,但是需要应用层来解决冲突。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作用之二是确定事件间的因果关系。举个微信朋友圈的例子。微信朋友圈不同地域的用户可能在不同的服务器上,通过网络消息来同步朋友圈好友发的图片以及对应的评论。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ec/eca881ed73e210e27c3f25ec8dee7f8f.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上海小王发了一个风景图片到朋友圈,香港Mary看到后评论了问“这是哪”,上海小王回复评论“梅里雪山”。由于网络原因,对评论的回复先到了加拿大的服务器,评论后到,导致加拿大Kate先看到回答“梅里雪山”,后看到提问“这是哪”。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6b/6b12d27efa6f8210a4347089d6a7b43e.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用向量时钟,VC(c) < VC(e),因此 c → e,加拿大的服务器知道c发生在e前面,可以对评论和回复正确排序后显示。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"参考"},{"type":"link","attrs":{"href":"http://yang.observer/2020/09/12/vector-clock/#%E5%8F%82%E8%80%83","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"http://www2.imm.dtu.dk/courses/02220/2015/L7/Logical_Time.pdf","title":null},"content":[{"type":"text","text":"Logical Time"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://en.wikipedia.org/wiki/Vector_clock","title":null},"content":[{"type":"text","text":"Vector Clock"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://zoo.cs.yale.edu/classes/cs426/2012/lab/bib/fidge88timestamps.pdf","title":null},"content":[{"type":"text","text":"Timestamps in Message-Passing Systems That Preserve the Partial Ordering"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://writings.sh/post/logical-clocks","title":null},"content":[{"type":"text","text":"逻辑时钟 - 如何刻画分布式中的事件顺序"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章