当前位置: 代码迷 >> 综合 >> Lamport Clock 笔记
  详细解决方案

Lamport Clock 笔记

热度:90   发布时间:2023-12-21 12:17:04.0

Time, Clocks, and the Ordering of Events in a Distributed System 论文阅读笔记

之前看过一点分布式算法:Distributed Computing —— Principles, Algorithms, and System 笔记,看这篇就比较轻松了。

happens-before relation: a → b a\to b ab, event a a a happens before event b b b if

  • a a a b b b 在同一进程,并且 a a a sequences before b b b
  • a ? a? a? b ? b? b? 在不同进程,并且 a ? a? a? synchronizes before b ? b? b?,即 a ? a? a? 是一个消息发送事件, b ? b? b? 是一个消息接收事件
  • → \to 具有传递性

happens-before 是偏序关系(partial ordering),称 a a a b b b 是并发的,即 a ∣ ∣ b a||b ab,如果 a ? → b a\not \to b a??b 并且 b ? → a b\not\to a b??a

Logical Clock

logical clock 是每一个进程自己的递增的 counter(递增步幅不定),当一个事件发生时就产生一个 timestamp, C i ( a ) C_i(a) Ci?(a)

如果 a → b ? a\to b? ab?,那么

  • a a a b b b 在同一进程,有 C ( a ) &lt; C ( b ) C(a)&lt;C(b) C(a)<C(b)
  • 进程 p i p_i pi? 的发送事件 a a a 和进程 p j p_j pj? 的接受事件 b b b,有 C i ( a ) &lt; C j ( b ) C_i(a)&lt;C_j(b) Ci?(a)<Cj?(b)

于是有
a → b ? C ( a ) &lt; C ( b ) a\to b \Rightarrow C(a) &lt; C(b) ab?C(a)<C(b)
反之不成立

Ordering the Events Totally

在进程间定义全序关系 ? \prec ?,可以任意指定

定义事件全序关系 a ? b a\Rightarrow b a?b,事件 a ∈ p i a\in p_i api?,事件 b ∈ p j b\in p_j bpj?,如果

  • C i ( a ) &lt; C j ( b ) C_i(a)&lt;C_j(b) Ci?(a)<Cj?(b) 或者
  • C i ( a ) = C j ( b ) ∧ p i ? p j C_i(a)=C_j(b)\ \land\ p_i\prec p_j Ci?(a)=Cj?(b)  pi??pj?

于是有 a → b a\to b ab 推出 a ? b a\Rightarrow b a?b

有句话他在 intro(Ref[1]) 中强调了:

The synchronization is specified in terms of a State Machine.

在 FIFO 通信模型中)A process can execute a command timestamped T T T when it has learned of all commands issued by all other processes with timestamps less than or equal to T T T.

Physical Clocks

C i ( t ) C_i(t) Ci?(t) 表示进程 p i p_i pi? 在物理时刻 t t t 时的进程逻辑时钟的值。

  • ∣ C i ′ ( t ) ? 1 ∣ &lt; k |C_i^{&#x27;}(t)-1|&lt;k Ci?(t)?1<k
  • ∣ C i ( t ) ? C j ( t ) ∣ &lt; ? |C_i(t)-C_j(t)|&lt;\epsilon Ci?(t)?Cj?(t)<?

为了保证:在物理时刻 t t t p j p_j pj? 发出的信息到达 p i p_i pi? 的时钟 C i ( t + t 0 ) C_i(t+t_0) Ci?(t+t0?) 一定比 C j ( t ) C_j(t) Cj?(t) 大,我们需要找到一个 μ \mu μ,使得 C i ( t + μ ) ? C j ( t ) &gt; 0 C_i(t+\mu)-C_j(t) &gt; 0 Ci?(t+μ)?Cj?(t)>0,其中 μ \mu μ 是比传输时间小的值。

粗略估计,当 μ ≥ ? 1 ? k \mu \ge \frac{\epsilon}{1-k} μ1?k?? 时, C i ( t + μ ) ? C j ( t ) &gt; 0 C_i(t+\mu)-C_j(t) &gt; 0 Ci?(t+μ)?Cj?(t)>0 成立。(这保证了现实世界中的同步关系,即所有进程所组成的系统之外的 happens-before 关系)

最后给了个定理,没看懂说的啥,以后看吧。

Reference

  • Lamport introduction
  • Lamport’s “Time, Clocks and the Ordering of events in a Distributed System.”
  • Lamport timestamps - Wiki
  • 论文笔记:Time, clocks, and the ordering of events in a distributed system
  • Time and clocks and ordering of events in a distributed system
  • 【每周论文】Time, Clocks, and Ordering of Events in a Distributed System
  • time-clocks原版英文PPT
  • Week 7: Time, Clocks, and Ordering of Events in a Distributed System
  相关解决方案