目录
正在加载目录…
正在阅读专栏 分布式共识系统专栏 第 48 篇 · 共 59 篇 · 81%

论文:Time, Clocks, and the Ordering of Events in a Distributed System

发布于 2026-09-21 10:09 · 最后编辑于 2026-09-21 18:15 · 版本 v2 · 字数 23,324 · 预计阅读 47 分钟 👁 17 次阅读

本篇整理 Leslie Lamport 的《Time, Clocks, and the Ordering of Events in a Distributed System》。每一页保留 Microsoft Research / ACM 官方 PDF 的原始页面图;页面图保留原文排版、Figure、space-time diagram、公式、脚注、版权声明和 References,下面严格按 PDF 页序给出中文译文。happened-before、logical clock、physical clock、concurrent、total ordering、state machine 等术语按语境保留 English 或中英并列。

论文:Time, Clocks, and the Ordering of Events in a Distributed System

相关文章:6.5840:Lecture 21 Byzantine Fault Tolerance · 论文:Paxos Made Simple · 论文:Practical Byzantine Fault Tolerance(PBFT) · 论文:Chain Replication for Supporting High Throughput and Availability

TL;DR

  • 核心问题:distributed system 没有天然的全局时钟;事件之间通常只有由进程顺序和 message 传递定义的 partial order。
  • happened-before:若事件 a 可以通过同一进程中的先后关系、message 的发送→接收关系以及传递性影响事件 b,则记为 a → b;互不影响的事件是 concurrent。
  • logical clock:每个进程在事件之间递增本地 clock,发送 message 时携带 timestamp,接收时把 clock 调整到大于 message timestamp,从而保证 a → b 时 C(a) < C(b)。
  • total ordering:再用进程编号打破 timestamp 相等的 tie,即可把 happened-before partial order 扩展成一致的 total order;这可用于 distributed mutual exclusion 和 replicated state machine。
  • physical clock:logical total order 可能违背用户从系统外部感知到的因果顺序;通过 physical clock、message delay 下界和不可回拨的同步规则,可以给出 clocks 的最大偏差界。

名词解释

名词含义本文译法
happened-before由进程内顺序、message 发送/接收和传递性定义的事件关系happened-before;先发生于
concurrent任意一个事件都不能因果影响另一个事件concurrent;并发
logical clock不必对应物理时间、只为事件赋予满足顺序约束的数字logical clock;逻辑时钟
Clock Conditiona → b 时要求 C(a) < C(b)Clock Condition;时钟条件
total ordering为全部事件建立的、与 happened-before 一致的全序total ordering;全序
Strong Clock Condition对系统外部相关事件的因果关系也要求 clock 保序Strong Clock Condition;强时钟条件
PC1 / PC2physical clock 的走时速率和同步误差约束physical-clock conditions
IR1 / IR2logical clock 的递增与 message timestamp 规则implementation rules

目录

章节内容
版本与来源论文出版信息、官方 PDF 与固定边界
原文页面与逐页翻译8 个 PDF 页面按原页保留并翻译
机制索引happened-before、logical clock、total order 与 state machine 的关系
本篇验收页数、图片、公式、算法和 References 覆盖范围

版本与来源

  • 论文标题:Time, Clocks, and the Ordering of Events in a Distributed System。
  • 作者与机构:Leslie Lamport;PDF 首页署名为 Massachusetts Computer Associates, Inc.。
  • 发表信息:Communications of the ACM 21(7),1978 年 7 月,pp. 558–565;官方页面还记录了 2000 PODC Influential Paper Award 和 2007 ACM SIGOPS Hall of Fame Award。
  • 官方来源:Microsoft Research publication page;Microsoft Research / ACM PDF。
  • 本地固定文件:source/time-clocks-ordering-events.pdf。
  • PDF 固定信息:8 页;SHA-256:1c006e4f2bf124991d6805aff60d8e669a26b188af05aa6fd5cfbc67066e8fc7。
  • 附件处理:每页以 2× 原比例渲染为 1224×1584 PNG,存放于 assets/47 Time Clocks Ordering Events/,正文以 600 宽度嵌入。Figure 1–3、space-time diagram 和公式均由原始页面图保留。
  • 翻译边界:覆盖 Abstract、Introduction、The Partial Ordering、Logical Clocks、Ordering the Events Totally、mutual exclusion algorithm、State Machine、Anomalous Behavior、Physical Clocks、Theorem、Conclusion、Appendix proof、Acknowledgment 和 References。第 8 页目标论文 References 之后出现下一篇 ACM 文章的开头;该部分只作为页面图保留,不纳入本篇译文。

原文页面与逐页翻译

第 1 页:Abstract、Introduction 与 The Partial Ordering 开始

time clocks p01

Time, Clocks, and the Ordering of Events in a Distributed System

作者:Leslie Lamport。
机构:Massachusetts Computer Associates, Inc.

Abstract

本文研究 distributed system 中一个事件发生在另一个事件之前的概念,并说明这一概念定义了事件的一个 partial ordering。文中给出一种 distributed algorithm,用于同步一组 logical clock;这些 logical clock 可以用来对事件建立 total ordering。

本文用一个解决 synchronization problem 的方法说明 total ordering 的用途。随后将该 algorithm 特化为 physical clock synchronization,并推导 clocks 最多能够偏离同步状态多远。

Key Words and Phrases:distributed systems、computer networks、clock synchronization、multiprocess systems。
CR Categories:4.32、5.29。

Introduction

时间的概念是我们思考方式的基础。但它又来自一个更加基本的概念:事件发生的顺序。我们说某件事发生在 3:15,是因为它发生在我们的 clock 读数为 3:15 之后、读数为 3:16 之前。事件的时间顺序这一概念,贯穿于我们对 system 的思考。

例如,在 airline reservation system 中,我们会规定:如果 reservation request 在航班已经满员之前发出,就应当批准它。不过,考虑 distributed system 中的事件时,我们会看到,必须谨慎地重新审视这个概念。

版权与使用声明(原文页内声明的译文):ACM 允许 individual readers 和代表其行事的 nonprofit libraries,为 teaching 或 research 对本材料的全部或部分作 fair use,前提是给出 ACM copyright notice,并注明该 publication、issue date,以及重印权限是经 Association for Computing Machinery 许可授予的。若要以其他方式重印 figure、table、其他 substantial excerpt 或整篇作品,包括 republication 或 systematic / multiple reproduction,则需要取得 specific permission。

本研究得到 Advanced Research Projects Agency of the Department of Defense 和 Rome Air Development Center 支持,并由 Rome Air Development Center 根据合同 F 30602-76-C-0094 进行监督。

作者地址:Computer Science Laboratory, SRI International, 333 Ravenswood Ave., Menlo Park, CA 94025。

© 1978 ACM 0001-0782/78/0700-0558 $0.75。

distributed system 由一组彼此分离、通过交换 messages 相互通信的 distinct processes 组成。ARPA net 这样的互联 computer network 是 distributed system。单台 computer 也可以看作 distributed system:其中 central control unit、memory units 和 input-output channels 是彼此分开的 processes。

当 message transmission delay 与单个 process 中两个事件之间的时间相比不可忽略时,system 就是 distributed 的。

本文主要讨论空间上彼此分离的 computers 组成的 system。不过,许多论述也适用于更一般的情形。特别是,单台 computer 上的 multiprocessing system 也会遇到与 distributed system 类似的问题,因为某些事件发生的顺序是不可预测的。

在 distributed system 中,有时无法判断两个事件究竟哪一个先发生。因此,关系 “happened before” 只是 system 中事件的一个 partial ordering。我们发现,许多问题的产生,正是因为人们没有充分意识到这一事实及其含义。

本文讨论由 “happened before” 关系定义的 partial ordering,并给出一种 distributed algorithm,把它扩展为所有事件的、一致的 total ordering。这个 algorithm 可以成为实现 distributed system 的有用机制。我们用一个简单的方法说明它如何解决 synchronization problem。

如果 algorithm 得到的 ordering 与 user 所感知的 ordering 不同,就可能出现意外的、异常的 behavior。引入真实的 physical clock 可以避免这种情况。本文描述一种同步这些 clocks 的简单方法,并推导 clocks 能够偏离同步状态多远的上界。

The Partial Ordering

大多数人可能会说,如果 event a 在比 event b 更早的时间发生,那么 a happened before b。他们可能会用 physical time 的理论来为这个定义辩护。然而,如果 system 要正确满足一个 specification,那么 specification 必须用 system 内部可观察的 events 来表达。

如果 specification 是用 physical time 表达的,那么 system 必须包含 real clocks。即便如此,仍然存在一个问题:这些 clocks 不可能完全准确,也不能保持精确的 physical time。因此,本文将不使用 physical clocks 来定义 “happened before” 关系。

我们先更精确地定义 system。假设 system 由一组 processes 组成。每个 process 都由一串 events 构成。根据 application 的不同,computer 上一个 subprogram 的执行可以算作一个 event,也可以把一条 machine instruction 的执行算作一个 event。

第 2 页:happened-before 定义、Figure 1/2 与 Logical Clocks 开始

time clocks p02

我们假设,一个 process 的 events 构成一个 sequence:如果 a 在这个 sequence 中位于 b 之前,就说 a happens before b。换句话说,一个 process 被定义为一组具有先验 total ordering 的 events。这似乎正是通常所说的 process。¹ 将定义扩展为允许一个 process 分裂成 distinct subprocesses 是很容易的,但本文不作此扩展。

我们假设发送或接收 message 都是 process 中的 event。于是可以定义 “happened before” relation,记作 →:

定义:在 system 的 events 集合上,relation → 是满足下列三个条件的最小 relation:

  1. 如果 a 和 b 是同一个 process 中的 events,且 a 在 b 之前,那么 a → b。
  2. 如果 a 是一个 process 发送 message 的 event,而 b 是另一个 process 接收同一 message 的 event,那么 a → b。
  3. 如果 a → b 且 b → c,那么 a → c。

如果两个 distinct events a 和 b 满足 a ↛ b 且 b ↛ a,就称它们是 concurrent 的。我们假设对于任意 event a,都有 a ↛ a。²(一个 event 能够 happened before 自己的 system,似乎没有物理意义。)因此,→ 是 system 全部 events 上的 irreflexive partial ordering。

将这个定义放到 Figure 1 那样的 “space-time diagram” 中观察会很有帮助。水平方向表示 space,垂直方向表示 time——越晚的时间画得越高。点表示 events,竖直线表示 processes,波浪线表示 messages。很容易看出,a → b 意味着:可以沿着 process line 和 message line 向时间前进,从 a 走到 b。例如,Figure 1 中有 p₁ → r₄。

另一种理解方式是:a → b 意味着 event a 可能因果地影响 event b。如果两个 events 都不能因果影响对方,它们就是 concurrent。例如,Figure 1 中的 p₂ 与 q₃ 是 concurrent 的。尽管图中画出的物理时间似乎暗示 q₃ 比 p₃ 更早发生,但 process P 在收到 p₄ 处的 message 之前,不可能知道 process Q 在 q₃ 做了什么。(在 event p₄ 之前,P 至多能够知道 Q 打算在 q₃ 做什么。)

熟悉 special relativity 不变的 space-time formulation 的读者,会觉得这个定义很自然,例如可以参见文献 [1] 或 [2] 的第一章。在 relativity 中,事件的 ordering 是依据可能发送的 messages 定义的。不过,本文采取了更务实的方式,只考虑确实发送过的 messages。我们应该能够只通过知道实际发生的 events,判断 system 是否正确运行,而不必知道哪些 events 本来可能发生。

Figure 1:distributed processes 与 messages 的 space-time diagram。原始图保留在本页图片中。

¹ 什么构成一个 event 会影响 process 中 events 的 ordering。例如,接收 message 可以表示 computer 设置 interrupt bit,也可以表示执行处理该 interrupt 的 subprogram。由于 interrupts 不一定按发生顺序被处理,这个选择会影响 process 接收 messages 的 events 的 ordering。

² 注意,messages 可能 out of order 地被接收。我们允许一次发送多个 messages 被视为一个 event;但为方便起见,假设一次接收一个 message 的 event 不会与发送或接收其他 message 的 event 重合。

Logical Clocks

现在把 clocks 引入 system。先从抽象角度看:clock 只是给 event 赋予一个 number 的方式,这个 number 被理解为 event 发生的时间。

更精确地说,为每个 process Pᵢ 定义一个 clock Cᵢ。它是一个 function,为该 process 中的任意 event a 赋予 number Cᵢ(a)。整个 system 的 clocks 可以用 function C 表示:它为任意 event b 赋予 number C(b);如果 b 是 process Pⱼ 中的 event,则 C(b) = Cⱼ(b)。

目前,我们不对 Cᵢ(a) 与 physical time 的关系作任何假设。因此,可以把 Cᵢ 看成 logical clocks,而不是 physical clocks。它们可以由完全没有实际 timing mechanism 的 counters 实现。

第 3 页:Clock Condition、IR1/IR2 与 total ordering

time clocks p03

现在考虑这样一组 clocks 怎样才算正确。不能用 physical time 来定义 correctness,因为那会要求我们引入保持 physical time 的 clocks。定义必须建立在 events 发生的顺序之上。

最强的合理条件是:如果 event a 发生在 event b 之前,那么 a 应当具有比 b 更早的 time。形式化地说:

Clock Condition:对于任意 events a、b,如果 a → b,那么 C(a) < C(b)。

注意,我们不能同时要求 converse condition 也成立,因为那会意味着任意两个 concurrent events 必须发生在同一时刻。Figure 1 中,p₂ 和 p₃ 都与 q₃ concurrent;如果 converse 也成立,它们就都必须与 q₃ 同时发生,这又会与 Clock Condition 矛盾,因为 p₂ → p₃。

从 → 的定义很容易看出,只要下面两个条件成立,Clock Condition 就成立:

C1. 如果 a、b 是 process Pᵢ 中的 events,且 a 在 b 之前,那么 Cᵢ(a) < Cᵢ(b)。

C2. 如果 a 是 process Pᵢ 发送某个 message 的 event,而 b 是 process Pⱼ 接收该 message 的 event,那么 Cᵢ(a) < Cⱼ(b)。

可以用 space-time diagram 来理解 clocks。想象某个 process 的 clock “tick” 过每一个 number,而 ticks 发生在该 process 的 events 之间。例如,如果 process Pᵢ 中连续的两个 events a、b 满足 Cᵢ(a) = 4、Cᵢ(b) = 7,那么 clock ticks 5、6 和 7 就发生在这两个 events 之间。我们画一条 dashed tick line,穿过不同 processes 中所有编号相同的 ticks。Figure 1 的 space-time diagram 于是可以得到 Figure 2 那样的图。

条件 C1 意味着:同一条 process line 上任意两个 events 之间都必须有一条 tick line;条件 C2 意味着:每条 message line 都必须穿过一条 tick line。从 → 的图形含义很容易看出,这两个条件为什么能推出 Clock Condition。

可以把 tick lines 看成 space-time 上某个 Cartesian coordinate system 的 time coordinate lines。把 Figure 2 重画,使这些 coordinate lines 变直,就得到 Figure 3。Figure 3 是对同一组 events 的一种有效的 alternate representation。只要不把 physical time 引入 system(引入它就需要 physical clocks),就没有办法判断哪一幅图更好。

对于由二维 spatial network 的 processes 组成的 system,也可以用类似方式想象:space-time diagram 变成三维;processes 和 messages 仍用 lines 表示,而 tick lines 变成二维 surfaces。

现在假设 processes 是 algorithms,events 表示它们执行过程中的某些 actions。我们将说明如何在 processes 中引入满足 Clock Condition 的 clocks。Process Pᵢ 的 clock 用 register Cᵢ 表示;Cᵢ(a) 是 event a 期间 Cᵢ 中保存的值。Cᵢ 的值会在 events 之间变化,但改变 Cᵢ 本身不构成 event。

为保证 system 的 clocks 满足 Clock Condition,我们保证它们满足 C1 和 C2。C1 很简单:process 只需遵守下列 implementation rule:

IR1. 每个 process Pᵢ 在任意两个 successive events 之间 increment Cᵢ。

为满足 C2,要求每个 message m 都包含一个 timestamp Tₘ,且该 timestamp 等于 message 发送时的时间。收到带有 timestamp Tₘ 的 message 后,process 必须把自己的 clock advance 到晚于 Tₘ 的值。更精确地说:

IR2.

(a) 如果 event a 是 process Pᵢ 发送 message m,那么 message m 包含 timestamp Tₘ = Cᵢ(a)。

(b) 收到 message m 时,process Pⱼ 将 Cⱼ 设置为一个大于或等于其当前值、且严格大于 Tₘ 的值。

在 IR2(b) 中,我们把代表接收 message m 的 event 视为发生在设置 Cⱼ 之后。(这只是 notation 上的麻烦,在实际 implementation 中无关紧要。)显然,IR2 保证 C2 成立。因此,简单的 implementation rules IR1 和 IR2 保证 Clock Condition 成立,也就保证了 logical clocks system 正确。

Ordering the Events Totally

我们可以使用满足 Clock Condition 的 clocks,为 system 的全部 events 建立 total ordering。只需按照 events 发生时的 times 排序;如果出现 tie,就用 processes 的任意 total ordering < 来打破 tie。

更精确地说,定义 relation ↦ 如下:如果 a 是 process Pᵢ 中的 event,b 是 process Pⱼ 中的 event,那么当且仅当以下条件之一成立时,a ↦ b:

  1. Cᵢ(a) < Cⱼ(b);或
  2. Cᵢ(a) = Cⱼ(b) 且 Pᵢ < Pⱼ。

很容易看出,这定义了一个 total ordering;而 Clock Condition 保证,如果 a → b,那么 a ↦ b。换句话说,relation ↦ 是把 happened-before partial ordering 补全为 total ordering 的一种方式。³

↦ 依赖于 clocks system Cᵢ,并不是唯一的。满足 Clock Condition 的不同 clock choices 会产生不同的 ↦ relations。反过来,给定任意一个扩展 → 的 total ordering ↦,总能找到一组满足 Clock Condition、并产生该 ↦ 的 clocks。由 events system 唯一确定的,只有 partial ordering。

第 4 页:分布式 mutual exclusion 与五条规则

time clocks p04

能够给 events 建立 total ordering,对于实现 distributed system 非常有用。事实上,实现正确 logical clocks 的原因,就是为了得到这种 total ordering。下面用解决一个 mutual exclusion problem 的方法说明 total ordering 的用途。

考虑一组固定的 processes,它们共享一个 single resource。任意时刻只有一个 process 可以使用 resource,因此 processes 必须同步,避免冲突。我们希望找到一种 grant resource 的 algorithm,满足以下三个条件:

  1. 已经获得 resource 的 process 必须先 release 它,resource 才能 grant 给另一个 process。
  2. 对 resource 的不同 requests 必须按照它们发出的顺序 grant。
  3. 如果每个获得 resource 的 process 最终都会 release 它,那么每个 request 最终都会得到 grant。

假设 resource 初始时恰好 grant 给一个 process。

这些都是很自然的要求;它们精确规定了一个 solution 怎样才算正确。注意,这些条件涉及 events 的 ordering。条件 II 没有说明两个 concurrent issued requests 中哪一个应该先 grant。

必须意识到,这并不是一个 trivial problem。除非增加额外假设,否则使用 central scheduling process、按照收到 requests 的顺序 grant 并不能解决问题。

令 P₀ 为 scheduling process。假设 P₁ 向 P₀ 发送一个 request,然后向 P₂ 发送一个 message。收到后一个 message,P₂ 再向 P₀ 发送一个 request。完全可能出现 P₂ 的 request 先于 P₁ 的 request 到达 P₀。如果此时先 grant P₂ 的 request,就违反了条件 II。

为解决这个问题,我们使用规则 IR1 和 IR2 实现 clocks system,并用它定义全部 events 的 total ordering ↦。这样就得到了所有 request 和 release operations 的 total ordering。寻找 solution 于是变成直接的 exercise:只要确保每个 process 都了解到其他 processes 的全部 operations 即可。

为简化问题,我们作如下假设。这些假设并非 essential,只是为了避免实现细节分散注意力:

  • 对任意两个 processes Pᵢ 和 Pⱼ,从 Pᵢ 发送到 Pⱼ 的 messages,接收顺序与发送顺序相同。
  • 每条 message 最终都会被接收。(如果不作这个假设,可以引入 message numbers 和 message acknowledgment protocols。)
  • 每个 process 都可以直接向其他每个 process 发送 messages。

每个 process 维护自己的 request queue,其他 processes 看不到这个 queue。假设 queues 初始包含唯一一条 message T₀:P₀ requests resource;其中 P₀ 是初始获得 resource 的 process,T₀ 小于任意 clock 的初始值。

algorithm 由下面五条 rules 定义。为方便起见,假设每条 rule 定义的 actions 构成一个 single event。

1. Request resource
Process Pᵢ 向其他每个 process 发送 message Tₘ:Pᵢ requests resource,并把该 message 放入自己的 request queue;Tₘ 是该 message 的 timestamp。

2. Receive request
Process Pⱼ 收到 message Tₘ:Pᵢ requests resource 后,将其放入自己的 request queue,并向 Pᵢ 发送一个(带 timestamp 的)acknowledgment message。⁴

3. Release resource
Process Pᵢ 要 release resource 时,从自己的 request queue 中删除任意 Tₘ:Pᵢ requests resource message,并向其他每个 process 发送 Tₘ:Pᵢ releases resource message。

4. Receive release
Process Pⱼ 收到 Pᵢ releases resource message 后,从自己的 request queue 中删除任意 Tₘ:Pᵢ requests resource message。

5. Grant resource
当下列两个条件都满足时,resource grant 给 process Pᵢ:

  1. Pᵢ 的 request queue 中存在一个 Tₘ:Pᵢ requests resource message,并且按照 relation ↦,它排在 queue 中所有其他 requests 之前。(定义 message 的 ↦ 时,把 message 与发送它的 event 视为同一对象。)
  2. Pᵢ 已从每个其他 process 收到一个 timestamp 晚于 Tₘ 的 message。⁵

注意,rule 5 的两个条件都由 Pᵢ 在本地测试。

³ 如果希望有更 “fair” 的方法,可以让 < 成为 clock value 的 function。例如,当 Cᵢ(a) = Cⱼ(b) 且 j < i 时,可令 a ↦ b 当且仅当 j < Cᵢ(a) mod N < i;否则令 b ↦ a,其中 N 是 processes 总数。

⁴ “eventually” 应该被精确定义,但那会使本文偏离主题太远。

⁵ 如果 Pⱼ < Pᵢ,那么 Pᵢ 只需从 Pⱼ 收到 timestamp ≥ Tₘ 的 message。

第 5 页:正确性、State Machine、异常行为与 Physical Clocks 开始

time clocks p05

很容易验证,上述 rules 定义的 algorithm 满足条件 I–III。首先,注意 rule 5 的条件 (ii) 与 messages 按顺序接收这一假设结合起来,保证 Pᵢ 已经了解到所有发生在它当前 request 之前的 requests。由于只有 rules 3 和 4 会从 request queue 删除 messages,因此很容易看出条件 I 成立。

条件 II 来自 total ordering ↦ 扩展了 partial ordering → 这一事实。rule 2 保证:在 Pᵢ 发出 request 之后,rule 5 的条件 (ii) 最终会满足。rules 3 和 4 则意味着,如果每个获得 resource 的 process 最终都会 release 它,那么 rule 5 的条件 (i) 最终也会满足,由此证明条件 III。

这是一个 distributed algorithm。每个 process 独立遵循这些 rules,没有 central synchronizing process,也没有 central storage。这种方法可以推广,用来为 distributed multiprocess system 实现任何希望的 synchronization。

这种 synchronization 可以用一个 State Machine 来规定:它由 possible commands 的集合 C、possible states 的集合 S 和 function e: C × S → S 组成。关系 e(C, S) = S′ 表示:当 machine 处于 state S 时执行 command C,会使 machine state 变为 S′。

在刚才的例子中,集合 C 由所有 commands Pᵢ requests resource 和 Pᵢ releases resource 组成;state 是 waiting request commands 的 queue,queue head 的 request 是当前获得 resource 的 request。执行 request command 会把 request 加到 queue 尾部;执行 release command 会从 queue 中删除 command。⁶

每个 process 都独立地用所有 processes 发出的 commands 模拟 State Machine 的执行。因为所有 processes 都按照 timestamps(使用 relation ↦)对 commands 排序,所以每个 process 使用同一串 commands。一个 process 在知道所有其他 processes 发出的、timestamp 小于或等于 T 的 commands 后,就可以执行 timestamp 为 T 的 command。精确 algorithm 很直接,本文不再展开。

这种方法可以在 distributed system 中实现任意形式的 multiprocess synchronization。不过,得到的 algorithm 要求所有 processes 都 active 地参与。一个 process 必须知道其他 processes 发出的所有 commands,因此单个 process 的 failure 就会使其他 processes 无法执行 State Machine commands,从而令 system 停止。

failure problem 很困难,超出了本文详细讨论的范围。这里仅指出:failure 的整个概念只有在 physical time 的语境中才有意义。没有 physical time,就无法区分 failed process 与只是暂时停在两个 events 之间的 process。user 之所以能判断 system “crashed”,只是因为他等待 response 的时间太久了。能够在 individual processes 或 communication lines failure 时继续工作的 method,见文献 [3]。

⁶ 如果每个 process 不严格交替执行 request 和 release commands,那么执行一个 release command 时,可能从 queue 中删除零个、一个或多于一个 request。

Anomalous Behavior

我们的 resource scheduling algorithm 按照 total ordering ↦ 对 requests 排序,因此可能出现下面这种 anomalous behavior。

考虑一个全国范围的 interconnected computers system。某人在 computer A 上发出 request A,然后打电话给另一个城市的朋友,让朋友在另一台 computer B 上发出 request B。完全可能出现 request B 得到一个比 request A 更小的 timestamp,并被排在 A 之前。这是因为 system 不知道 A 实际上先于 B 发生;这种 precedence information 来自 system 之外的 phone call。

更仔细地看问题的来源。令 Ω⁰ 为所有 system events 的集合。再引入一个包含 Ω⁰ 中 events 以及所有其他 relevant external events 的集合,例如上例中的 phone calls。令 →⁰ 表示 Ω 上的 happened-before relation。在这个例子中,A →⁰ B,但 A ↛ B。显然,任何完全基于 Ω⁰ 中 events、并且不以任何方式将这些 events 与 Ω 中其他 events 关联起来的 algorithm,都无法保证 request A 排在 request B 之前。

有两种方法可以避免这种 anomalous behavior。第一种方法是显式地把 ordering →⁰ 所需的信息引入 system。在我们的例子中,发出 request A 的人可以从 system 获得该 request 的 timestamp T_A;他的朋友发出 request B 时,可以指定 B 必须得到晚于 T_A 的 timestamp。这样就把避免 anomalous behavior 的责任交给了 user。

第二种方法是构造满足下列条件的 clocks system:

Strong Clock Condition:对于 Ω⁰ 中的任意 events a、b,如果 a →⁰ b,那么 C(a) < C(b)。

它比普通的 Clock Condition 更强,因为 →⁰ 比 → 更强。通常,本文前面给出的 logical clocks 不满足它。

把 Ω⁰ 与 physical space-time 中的一组 “real” events 对应起来,并令 →⁰ 为 special relativity 定义的 events partial ordering。宇宙中一个神秘的事实是:可以构造一组彼此独立运行、却满足 Strong Clock Condition 的 physical clocks。因此,我们可以使用 physical clocks 消除 anomalous behavior。下面转向研究这类 clocks。

Physical Clocks

把 physical time coordinate 引入 space-time 图,并令 Cᵢ(t) 表示 physical time t 时 clock Cᵢ 的读数。⁷ 为了数学方便,假设 clocks 连续运行,而不是以离散的 “ticks” 运行。(离散 clock 可以看成连续 clock,只是在读取时存在最多半个 tick 的误差。)更精确地说,假设 Cᵢ(t) 是 t 的 continuous、differentiable function,除了 clock reset 时孤立的 jump discontinuities。于是 dCᵢ(t)/dt 表示 clock 在 time t 的运行速率。

⁷ 我们假设 Newtonian space-time。如果 clocks 的相对运动或 gravitational effects 不可忽略,那么必须先把实际 clock reading 从 proper time 转换为任意选择的 time coordinate,才能得到 Cᵢ(t)。

第 6 页:Physical Clock Conditions 与 synchronization theorem

time clocks p06

为了使 clock Cᵢ 成为真正的 physical clock,它必须以大致正确的速率运行,即对所有 t,都应有 dCᵢ(t)/dt ≈ 1。更精确地,假设满足:

PC1. 存在一个 χ ≪ 1,使得对于所有 i:

|dCᵢ(t)/dt − 1| < χ。

对于典型的 crystal-controlled clocks,χ ≤ 10⁻⁶。

仅仅让每个 clock 单独以大致正确的速率运行还不够。它们还必须同步,使得所有 i、j 和 t 都有 Cᵢ(t) = Cⱼ(t)。更精确地,必须存在足够小的常数 ε,使下面条件成立:

PC2. 对所有 i、j:

|Cᵢ(t) − Cⱼ(t)| < ε。

如果把 Figure 2 中竖直距离理解为 physical time,那么 PC2 表示同一条 tick line 的高度变化小于 ε。

由于两个不同的 clocks 永远不会以完全相同的速率运行,它们会逐渐越走越远。因此,必须设计一种 algorithm,保证 PC2 始终成立。不过,先考察 χ 和 ε 必须小到什么程度,才能阻止 anomalous behavior。

我们必须保证 relevant physical events system Ω⁰ 满足 Strong Clock Condition。假设我们的 clocks 已经满足普通 Clock Condition,那么只需要求:当 a、b 是 Ω⁰ 中满足 a →⁰ b 的 events 时,Strong Clock Condition 成立。因此,只需考虑发生在不同 processes 中的 events。

令 μ 为这样一个 number:如果 event a 在 physical time t 发生,而另一个 process 中的 event b 满足 a →⁰ b,那么 b 会在 physical time t + μ 之后发生。换句话说,μ 小于 processes 间 message 的最短 transmission time。

总可以把两个 processes 之间的最短距离除以 light speed,作为 μ;但根据 Ω⁰ 中 messages 的传输方式,μ 可能大得多。

为了避免 anomalous behavior,必须保证对任意 i、j 和 t:

Cᵢ(t + μ) − Cⱼ(t) > 0。

结合 PC1 和 PC2 可以把 χ、ε 的所需大小与 μ 联系起来。假设 clock reset 时总是向前设置、从不向后设置。(向后设置会导致 C1 可能被破坏。)PC1 于是推出:

Cᵢ(t + μ) − Cᵢ(t) > (1 − χ)μ。

再利用 PC2,很容易得到:如果满足下列不等式,就有 Cᵢ(t + μ) − Cⱼ(t) > 0:

ε/(1 − χ) ≤ μ。

这个不等式连同 PC1、PC2,就保证 anomalous behavior 不会发生。

现在描述保证 PC2 成立的 algorithm。令 m 是在 physical time t 发送、在 physical time t′ 接收的 message。定义 message m 的 total delay 为 t′ − t。接收方当然不知道这个 delay;但假设 receiver 知道某个 minimum delay μₘ ≥ 0,使得 t′ − t ≥ μₘ。称 νₘ = (t′ − t) − μₘ 为 message 的 unpredictable delay。

现在把 rules IR1 和 IR2 特化到 physical clocks:

IR1′. 如果 process Pᵢ 在 physical time t 没有收到 message,那么 Cᵢ 在 t 可微,且 dCᵢ(t)/dt > 0。

IR2′.

(a) 如果 Pᵢ 在 physical time t 发送 message m,那么 m 包含 timestamp Tₘ = Cᵢ(t)。

(b) 如果 process Pⱼ 在 time t′ 收到 m,则令 Cⱼ(t′) = max(Cⱼ(t′ − 0), Tₘ + μₘ)。⁸

尽管 rules 是用 physical time parameter 正式表述的,process 实际上只需知道自己的 clock reading 和它收到的 messages 的 timestamps。为数学方便,假设每个 event 在 physical time 的一个精确 instant 发生,且同一 process 中不同 events 发生在不同 times。

这些 rules 是 rules IR1 和 IR2 的特化,因此我们的 clocks system 满足 Clock Condition。real events 具有有限 duration 并不会给实现带来困难。实现中真正需要注意的,是保证离散 clock ticks 足够频繁,使 C1 始终成立。

现在证明这个 clock-synchronizing algorithm 可以满足 PC2。假设 processes system 由一个 directed graph 描述:从 Pᵢ 到 Pⱼ 的 arc 表示一条 communication line,messages 可以沿它直接从 Pᵢ 发往 Pⱼ。

如果对任意 t,Pᵢ 都会在 physical times t 与 t + τ 之间至少向 Pⱼ 发送一条 message,就说 message 每隔 τ seconds 通过这条 arc 发送一次。

把 directed graph 的 diameter 定义为最小的 d:对任意两个 distinct processes Pᵢ、Pₖ,都存在一条从 Pᵢ 到 Pₖ、且最多包含 d 条 arcs 的 path。

除了建立 PC2 外,下面的 theorem 还界定了 system 初次启动时 clocks 达到同步需要多长时间。

THEOREM. 假设 processes graph 是 strongly connected,diameter 为 d,并且始终遵守 rules IR1′、IR2′。假设对任意 message m,都有 μₘ ≤ μ,其中 μ 是某个常数;并且对所有 t > t₀:

(a) PC1 成立;

(b) 存在常数 τ 和 ν,使得每隔 τ seconds,每条 arc 上都会发送一条 unpredictable delay 小于 ν 的 message。

那么,对于所有 t > t₀ + τd,PC2 成立,且 ε = d(2χτ + ν);这里的近似假设为 μ + ν ≪ τ。

这个 theorem 的 proof 出人意料地困难,见 Appendix。关于 synchronizing physical clocks 的问题,已有大量研究。这里把读者引向文献 [4] 作为入门。文献中的方法可用于估计 message delays μₘ,以及调整 clock frequencies dCᵢ/dt(对于允许调整频率的 clocks)。不过,要求 clocks 从不向后设置,似乎使本文的问题区别于此前研究的问题;作者认为这个 theorem 是一个新结果。

⁸ Cⱼ(t′ − 0) = lim Cⱼ(t′ − |δ|)。

第 7 页:Conclusion 与 Appendix proof 开始

time clocks p07

Conclusion

我们看到,“happened before” 的概念为 distributed multiprocess system 中的 events 定义了一个 invariant partial ordering。本文描述了一种 algorithm,把这个 partial ordering 扩展为一个 somewhat arbitrary 的 total ordering,并说明如何使用这个 total ordering 解决一个简单的 synchronization problem。后续论文将说明如何把这种方法扩展为解决任意 synchronization problem。

该 algorithm 定义的 total ordering 带有一定任意性。如果它与 system users 感知到的 ordering 不一致,就可能产生 anomalous behavior。使用 properly synchronized physical clocks 可以避免这种情况。本文的 theorem 说明了 clocks 能够被同步到多么接近。

在 distributed system 中,重要的是认识到 events 发生的顺序只是 partial ordering。我们相信,这个观点有助于理解任何 multiprocess system。它应当帮助人们独立于具体解决 mechanisms,理解 multiprocessing 的基本问题。

Appendix

Proof of the Theorem

对于任意 i 和 t,定义 Cᵢᵗ 为一个 clock:它在 time t 被设置为 Cᵢ 的值,之后以与 Cᵢ 相同的速率运行,但永远不再 reset。换句话说,对所有 t′ ≥ t:

text
Cᵢᵗ(t′) = Cᵢ(t) + ∫ₜᵗ′ [dCᵢ(s)/ds] ds

注意,对所有 t′ ≥ t,都有:

Cᵢ(t′) ≥ Cᵢᵗ(t′)。

假设 process Pᵢ 在 time t₁ 向 process Pⱼ 发送 message;该 message 在 time t₂ 被接收,且 unpredictable delay ≤ ν,其中 t₀ ≤ t₁ ≤ t₂。那么对所有 t ≥ t₂,有:

text
Cⱼ(t) ≥ Cⱼ(t₂) + (1 − χ)(t − t₂)       [由 (1) 和 PC1]
     ≥ Cᵢ(t₁) + μₘ + (1 − χ)(t − t₂)    [由 IR2′(b)]
     = Cᵢ(t₁) + (1 − χ)(t − t₁)
       − [(t₂ − t₁) − μₘ] + χ(t₂ − t₁)
     ≥ Cᵢ(t₁) + (1 − χ)(t − t₁) − ν。

因此,在这些假设下,对所有 t ≥ t₂:

Cⱼ(t) ≥ Cᵢ(t₁) + (1 − χ)(t − t₁) − ν。 (3)

现在假设对于 i = 1, …, n,有 tᵢ ≤ tᵢ′ < tᵢ₊₁,t₀ ≤ tᵢ′,并且 process Pᵢ 在 time tᵢ′ 向 Pᵢ₊₁ 发送 message;该 message 在 time tᵢ₊₁ 被接收,unpredictable delay 小于 ν。重复应用不等式 (3),对 t ≥ tₙ₊₁ 得到:

text
Cₙ₊₁(t) ≥ C₁(t₁′) + (1 − χ)(t − t₁′) − nν。  (4)

由 PC1、IR1′ 和 IR2′ 可以推出:

C₁(t₁′) ≥ C₁(t₁) + (1 − χ)(t₁′ − t₁)。

结合 (4) 并使用 (2),对 t ≥ tₙ₊₁ 得到:

text
Cₙ₊₁(t) ≥ C₁(t₁) + (1 − χ)(t − t₁) − nν。  (5)

对于任意两个 processes P 和 P′,都可以找到一串 processes P = P₀, P₁, …, Pₙ₊₁ = P′,其中每个 Pᵢ 到 Pᵢ₊₁ 都有 communication arc,且 n ≤ d。

根据 hypothesis (b),可以找到 times tᵢ、tᵢ′,使得 tᵢ′ − tᵢ ≤ τ 且 tᵢ₊₁ − tᵢ′ ≤ ν + μ。因此,只要 t > t₁ + d(τ + ν + μ),就能得到 n ≤ d 的不等式 (5)。于是,对于任意 i、j 以及 t₁ > t₀、t ≥ t₁ + d(τ + ν + μ),都有:

text
Cᵢ(t) ≥ Cⱼ(t₁) + (1 − χ)(t − t₁) − dν。  (6)

现在取任意一个 timestamp 为 Tₘ 的 message m,假设它在 time t 发送、在 time t′ 接收。我们假想 m 有一个 clock Cₘ,以恒定速率运行,满足 Cₘ(t) = Tₘ、Cₘ(t′) = Tₘ + μₘ。由于 μₘ ≤ t′ − t,所以 dCₘ/dt ≤ 1。rule IR2′(b) 只是把 Cⱼ(t′) 设置为 max(Cⱼ(t′ − 0), Cₘ(t′)),所以 clocks 只会通过设置为其他 clocks 的值而 reset。

对于任意 tₓ ≥ t₀ + μ/(1 − χ),令 Cₓ 为 time tₓ 具有最大 value 的 clock。由于所有 clocks 的运行速率都小于 1 + χ,对所有 i 和 t ≥ tₓ:

Cᵢ(t) ≤ Cₓ(tₓ) + (1 + χ)(t − tₓ)。 (7)

现在考虑两种情况:

  1. Cₓ 是 process P_q 的 clock C_q。
  2. Cₓ 是一个 message m 的 clock Cₘ;message m 由 process P_q 在 time t₁ 发送。

在情况 (1) 中,(7) 直接变成:

Cᵢ(t) ≤ C_q(tₓ) + (1 + χ)(t − tₓ)。 (8i)

在情况 (2) 中,因为 Cₘ(tₓ) = C_q(t₁) 且 dCₘ/dt ≤ 1,所以:

Cₓ(tₓ) ≤ C_q(t₁) + (tₓ − t₁)。

因此 (7) 给出:

Cᵢ(t) ≤ C_q(t₁) + (1 + χ)(t − t₁)。 (8ii)

由于 tₓ ≥ t₀ + μ/(1 − χ),并由 PC1、μₘ ≤ μ 等条件,可以推出 tₓ − t₁ ≤ μ/(1 − χ);因此,这两种情况都可以归纳为:对于任意 t、tₓ,只要 t ≥ tₓ ≥ t₀ + μ/(1 − χ),就存在一个 process P_q 和一个 time t₁,满足

tₓ − μ/(1 − χ) ≤ t₁ ≤ tₓ,且对所有 i:

text
Cᵢ(t) ≤ C_q(t₁) + (1 + χ)(t − t₁)。  (9)

Appendix 中 Figure 3 及 proof 的公式推导均保留于本页原始页面图;上面按正文顺序翻译其可读文字和公式。

第 8 页:Appendix proof 结束、初始化方法与 References

time clocks p08

令 t₁ = tₓ(情况 (1)),就可以合并 (8i) 和 (8ii),得到上面的 (9)。选择满足 t ≥ tₓ + d(τ + ν + μ) 的 t、tₓ,再结合 (6) 和 (9),可以推出存在某个 t₁ 和某个 process P_q,使得对于所有 i:

text
C_q(t₁) + (1 − χ)(t − t₁) − dν ≤ Cᵢ(t)
                         ≤ C_q(t₁) + (1 + χ)(t − t₁)。  (10)

令 t = tₓ + d(τ + ν + μ)。于是:

text
d(τ + ν + μ) ≤ t − t₁
             ≤ d(τ + ν + μ) + μ/(1 − χ)。

将其代入 (10),得到:

text
C_q(t₁) + (t − t₁) − χd(τ + μ) − dν − dχ(ν + μ)
    ≤ Cᵢ(t)
    ≤ C_q(t₁) + (t − t₁) + χ[d(τ + ν + μ) + μ/(1 − χ)]。  (11)

使用 χ ≪ 1、μ ≤ ν ≪ τ 这些 hypothesis,可以把 (11) 改写为近似不等式:

text
C_q(t₁) + (t − t₁) − d(χτ + ν)  ≲  Cᵢ(t)
    ≲  C_q(t₁) + (t − t₁) + dχτ。  (12)

因为这对所有 i 都成立,所以:

|Cᵢ(t) − Cⱼ(t)| ≤ d(2χτ + ν),

并且这一结论对所有 t > t₀ + dτ 成立。证毕。

注意,proof 中的关系 (11) 在 μ + ν ≪ τ 这一假设无效时,也给出了 |Cᵢ(t) − Cⱼ(t)| 的 exact upper bound。

检查 proof 可以发现一种简单方法:如果 clocks 初始时没有同步,或者因为任何原因失去同步,可以快速将它们重新同步。任意一个 process 都可以发出一条 message,并让它 relay 给其他每个 process。假设每条 message 的 unpredictable delay 都小于 ν,那么这个 procedure 从任意 process 启动后,不到 2d(τ + ν) seconds 就可以完成 synchronization。

Acknowledgment

使用 timestamps 对 operations 排序,以及 anomalous behavior 的概念,来自 Paul Johnson 和 Robert Thomas。

收到:1976 年 3 月;修订:1977 年 10 月。

References

  1. Schwartz, J. T. Relativity in Illustrations. New York University Press, New York, 1962。
  2. Taylor, E. F., and Wheeler, J. A. Space-Time Physics. W. H. Freeman, San Francisco, 1966。
  3. Lamport, L. The Implementation of Reliable Distributed Multiprocess Systems. 即将在 Computer Networks 发表。
  4. Ellingson, C., and Kulpinski, R. J. “Dissemination of System-Time.” IEEE Transactions on Communications Com-23, 5(May 1973),605–624。

页面图在 References 之后还包含下一篇 ACM 文章 Shallow Binding in Lisp 1.5 的版权页与开头;该内容不是本篇论文的一部分,因此不在本篇正文中翻译。

机制索引

机制本文位置核心含义
a → bThe Partial Orderinga 可以沿 process/message 因果路径影响 b
concurrentThe Partial Ordering两个 events 互不因果影响
Clock ConditionLogical Clockshappened-before 必须映射为严格递增的 clock values
IR1Logical Clocksprocess 在 successive events 之间递增本地 clock
IR2Logical Clocksmessage 携带发送 timestamp;接收方把 clock 调到 timestamp 之后
↦Ordering the Events Totally用 clock value 和 process priority 将 partial order 扩展为 total order
Mutual exclusion rules 1–5Ordering the Events Totally用 total order 协调共享 resource 的 request/release
State MachineOrdering the Events Totally所有 processes 按相同 command sequence 独立模拟同一状态机
Strong Clock ConditionAnomalous Behavior把 system 外部可感知的因果关系也纳入 clock ordering
PC1Physical Clocks每个 physical clock 的 rate 接近真实时间
PC2Physical Clocks任意两个 physical clocks 的读数差保持在 ε 内
IR1′ / IR2′Physical Clocks不回拨 clock,并用 message 的 minimum delay 修正接收方 clock
Synchronization theoremPhysical Clocks / Appendix在 strongly connected graph、bounded delay 和 periodic message 条件下给出 clock skew 上界

与本专栏其他内容的关系

  • 与 Byzantine Generals 的关系:Byzantine Generals 讨论在 adversarial participants 下怎样达成 agreement;本文先解决更基础的 event causality、clock ordering 和 deterministic command order。
  • 与 Paxos / Raft 的关系:本文的 State Machine 段落说明“所有 replicas 按相同 command sequence 执行同一 state machine”的抽象,但本文的 total-order broadcast 不提供 Paxos / Raft 所需的 failure tolerance。
  • 与 vector clock 的关系:本文的 scalar logical clock 能保持 a → b 时的严格递增,但 C(a) < C(b) 不能反推出 a → b;需要表达 causal concurrency 时,vector clock 提供更精确的 partial-order information。
  • 与 timeout 的关系:本文明确指出,没有 physical time,就无法从“process 暂停”中识别“process failure”;这正是 distributed failure detector、lease 和 timeout 设计必须面对的边界。

本篇验收

  • [x] Microsoft Research / ACM 官方 PDF 已固定到 source/time-clocks-ordering-events.pdf。
  • [x] 8 个 PDF 页面均已按原始 page rectangle 渲染为 PNG,并保存到 assets/47 Time Clocks Ordering Events/。
  • [x] 8 个页面图片均在对应译文前按原页顺序嵌入,显示宽度统一为 600。
  • [x] Abstract、Introduction、The Partial Ordering、happened-before definition、concurrent、Figure 1–3、Logical Clocks、Clock Condition、C1/C2、IR1/IR2、total ordering、mutual exclusion 五条 rules、State Machine、failure boundary、Anomalous Behavior、Strong Clock Condition、Physical Clocks、PC1/PC2、IR1′/IR2′、Theorem、Conclusion、Appendix proof、Acknowledgment 和 4 条 References 均已覆盖。
  • [x] happened-before、logical clock、physical clock、concurrent、total ordering、State Machine 等术语按本系列约定保留 English 或中英并列。
  • [x] 本地源文件 SHA-256:1c006e4f2bf124991d6805aff60d8e669a26b188af05aa6fd5cfbc67066e8fc7。

参考资料

← 返回列表
← 上一篇 论文:The Byzantine Generals Problem 下一篇 → 论文:Distributed Snapshots: Determining Global States of Distributed Systems

评论 (0)

暂无评论,来留下第一条吧。
登录 或 注册 后才能发表评论