Thinking Above Code:TLA+思维概述

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文是《Thinking Above Code:TLA+从入门到实践》系列文章第一篇。"}]}]},{"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:\/\/lamport.azurewebsites.net\/tla\/tla.html","title":"xxx","type":null},"content":[{"type":"text","text":"TLA+"}]},{"type":"text","text":"是Paxos算法发明者Leslie Lamport的大作。它是一套数学建模工具箱,用于给分布式系统建模。这个工具箱主要包括形式化建模语言TLA+和形式化验证工具TLC model checker。"}]},{"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":"如今Paxos算法几乎成了分布式系统一致性算法的事实标准。市面上流行的大多数分布式系统都直接使用了Paxos算法或其某个变种。但相信搞过分布式系统的朋友们都会对Paxos算法坎坷的问世过程有所了解。"}]},{"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为了能生动形象的表述算法,假借考古学家的口吻讲述了古希腊Paxos岛上兼职议会(The Part-Time Parliament)的运作过程。论文投出去后,审稿者觉得这种口吻过于随意,不符合技术文章的风格,没有给出积极的回应。Lamport自己甚至还假扮考古学家做了几次演讲,讲述Paxos算法,均收效甚微。大家觉得Paxos这个东西太复杂,没有人知道他到底在说什么。"}]},{"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":"link","attrs":{"href":"https:\/\/www.microsoft.com\/en-us\/research\/publication\/paxos-made-simple\/","title":null,"type":null},"content":[{"type":"text","text":"《Paxos Made Simple》"}]},{"type":"text","text":"进一步介绍Paxos,通篇几乎没有任何数学符号。可这时人们又觉得Paxos算法不够严谨,缺少严格的数学证明。"}]},{"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":"直到新世纪之后,互联网浪潮风起云涌,席卷全球。Google作为互联网时代的领军者,声称在自家的分布式锁服务"},{"type":"link","attrs":{"href":"https:\/\/static.googleusercontent.com\/media\/research.google.com\/zh-CN\/\/archive\/chubby-osdi06.pdf","title":null,"type":null},"content":[{"type":"text","text":"Chubby"}]},{"type":"text","text":"中引入了Paxos算法。此后,Paxos算法一战成名,成为分布式系统一致性算法的事实标准。"}]},{"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":"2013年,Lamport也因为对分布式系统的重大贡献,成为当年的图灵奖得主。曾经有不少人质疑Paxos算法的数学严谨性。殊不知,Lamport本人曾就读于麻省理工的数学专业,对于数学和物理学都有着极高的造诣。Paxos算法也是他多年使用数学思维为分布式系统建模,进而设计出来的。"}]},{"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":"多年后,他又把自己的数学思维形式化成为一套具体的数学语言,也就是TLA+。同时,也开发出了一套可用的形式化验证工具软件,也就是TLC model checker。Lamport曾经穿着一件印有“You Want Proof? I'll Give You Proof!”的T恤衫演讲,不知是否是对这些人的回击。看来这老爷子是个老顽童。^_^"}]},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/1c\/23\/1c549d11cf8cdf5141fdef228c2eec23.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1. TLA+简介"}]},{"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":"相信开发过分布式系统的朋友们都遇到过这种场景:系统开发完成后,通过了所有的测试用例,于是我们信心满满的将其上线了。可是线上系统跑着跑着,不知哪一天突然莫名其妙地出现了一些bug。当我们打开日志,打开GDB,准备追踪定位这些问题时,它们又无法复现了。可如果放任不管,不知什么时候,它们又会诡异地冒出来。这种bug非常棘手,甚至让开发者很崩溃。这个问题本质上是因为分布式系统过于复杂,测试case很难覆盖所有可能的运行场景所导致的。再怎么细心,总会存在某些corner case。当系统运行到这些场景时,bug就出现了。"}]},{"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":"怎么系统地解决这种问题呢?答案就是本文的主角——TLA+。"}]},{"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":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/f7\/dc\/f75b9077d81fef4cfb375d47066b19dc.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"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":"TLA+就是为分布式系统建模的数学语言(主要用到了数理逻辑、集合论和图论三个数学工具)。我们使用TLA+为我们的分布式算法建模,同时针对实际系统的运行场景给出若干约束条件,然后再使用TLC model checker去验证模型是否满足这些约束条件。如果验证通过,我们再用具体的编程语言(比如C\/C++)把算法编程实现出来。这样系统开发完成后,即使再遇到某些bug,也都是编程过程中本身的bug,都比较好处理。分布式算法上不会再有问题。"}]},{"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:\/\/github.com\/taosdata\/TDengine","title":null,"type":null},"content":[{"type":"text","text":"TDengine"}]},{"type":"text","text":"作为一款开源的分布式时序数据库软件,主要面对的是物联网这个复杂的运行环境。在物联网场景下,怎么保证数据的一致性,同时兼顾读写性能,是一个非常大的挑战。我们对TLA+做了一些探索,用TLA+为TDengine的分布式算法建模(基于物联网场景下改进的Raft算法),以保证其性能和正确性。后续我们会通过一系列文章,讲述探索和实践的过程,供大家参考。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/4f\/c6\/4fcc72ce5acb3faee325495724d86ec6.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2. 状态机模型(state machine)"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.1 状态(state)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以用状态(state)来描述某个系统在某一时刻的性质。状态是什么呢?就是一堆变量。比如用长宽高描述一个箱子,用速度与加速度描述一个运动中的物体。在计算机世界中,同样可以用一堆变量描述系统当下的状态。比如用msgs、maxBal、maxVBal和maxVal 4个变量描述一个Paxos节点,用currentTerm、state等9个状态描述一个Raft节点。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/04\/6f\/043f274255c179e65a31b0ac2889db6f.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.2 状态转变"}]},{"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":"在自然界中,物体的状态是可以随时间发生改变的,通常用时间t的函数f(t)来描述物体的当下状态。比如位移 = 速度 x 时间,s=f(t) = vt。这里的时间t是一个连续的变量。大部分的物理学定律都是这样,将某些物理量定义为时间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":"在计算机世界中,同样也可以把系统状态描述为时间t的函数,只是这里的时间是一个离散量,具体来说,就是CPU中的时钟滴答。系统随时间的改变,就可以看成一系列离散的状态变化。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/3d\/da\/3d6a7a892452deeec0e35187bf88fbda.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3. 分布式系统"}]},{"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":"分布式系统包括多个节点,我们把所有节点状态的集合看成一个统一的全局状态,用这个统一的全局状态来描述一个分布式系统。下面是一个Raft系统的全局状态。"}]},{"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":"通过对比可以看到,原来Raft单节点变量变成了数组,包括currentTerm、state、votedFor和commitIndex。而原来的数组变量变成了二维数组,包括log、votesResponded、votesGranted、nextIndex和matchIndex。另外增加了一个msg变量,用来描述系统节点发出的网络消息。"}]},{"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":"看到这里,大家可能会有点晕。不过没关系,这里只是概要地介绍一下,让大家对TLA+的思维方式先有个印象。后续这些变量究竟是什么意思、怎么使用,我们还会详细介绍。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/f5\/07\/f549d89572329c39b096464106cc9407.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"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":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/55\/2c\/556767b0b248eecb9e14ba8778bfcc2c.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"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}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当我们能定义分布式系统全局状态改变之后,就可以进一步描述分布式系统的运行。分布式系统的运行可以看成是全局状态从某个初始状态Init起,一系列可能的改变过程。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/a4\/de\/a4f18d3b3583472aac3feee6a7a19ede.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"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}},{"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":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/96\/ae\/96f4c1a7763e0d101fdd54fbbe5ef7ae.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4.使用数学语言精准刻画状态改变"}]},{"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":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/95\/f8\/950665fc754581ea49be3854f9ddabf8.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"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":"Lamport天才地提出了这种思想,而且重要的是,他精确定义了语法,同时实现了“数学编译器”,也就是TLA+。于是我们就可以通过TLA+语言来描述系统状态的转换了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/ef\/22\/ef25f4035cc5250dfec06f49424bf822.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"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}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面的例子是用TLA+语言描述在Paxos协议中,Proposer接收到Accept消息后做出的反应。"}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"Phase2a(b) ==\n \/\\ ~ \\E m \\in msgs : (m.type = \"2a\") \/\\ (m.bal = b)\n \/\\ \\E v \\in Values :\n \/\\ \\E Q \\in Quorums :\n \\E S \\in SUBSET {m \\in msgs : (m.type = \"1b\") \/\\ (m.bal = b)} :\n \/\\ \\A a \\in Q : \\E m \\in S : m.acc = a\n \/\\ \\\/ \\A m \\in S : m.maxVBal = -1\n \\\/ \\E c \\in 0..(b-1) :\n \/\\ \\A m \\in S : m.maxVBal =< c\n \/\\ \\E m \\in S : \/\\ m.maxVBal = c\n \/\\ m.maxVal = v\n \/\\ Send([type |-> \"2a\", bal |-> b, val |-> v])\n \/\\ UNCHANGED <>"}]},{"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":"下面的例子是TLA+语言描述的在Raft协议中,节点更新commit_index时做出的反应。"}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"AdvanceCommitIndex(i) ==\n \/\\ state[i] = Leader\n \/\\ LET \\* The set of servers that agree up through index.\n Agree(index) == {i} \\cup {k \\in Server :\n matchIndex[i][k] >= index}\n \\* The maximum indexes for which a quorum agrees\n agreeIndexes == {index \\in 1..Len(log[i]) :\n Agree(index) \\in Quorum}\n \\* New value for commitIndex'[i]\n newCommitIndex ==\n IF \/\\ agreeIndexes \/= {}\n \/\\ log[i][Max(agreeIndexes)].term = currentTerm[i]\n THEN\n Max(agreeIndexes)\n ELSE\n commitIndex[i]\n IN commitIndex' = [commitIndex EXCEPT ![i] = newCommitIndex]\n \/\\ UNCHANGED <>"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"5.属性判断"}]},{"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":"只描述系统状态转换用途不大。关键的是,我们想知道系统在各种可能发生的状态转换中,具备什么样的属性。同样,通过TLA+可以描述某一时刻系统的状态。下面的例子就是TLA+描述Paxos节点value chosen的语句。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"VotedForIn(a, v, b) == \\E m \\in msgs : \/\\ m.type = \"2b\"\n \/\\ m.val = v\n \/\\ m.bal = b\n \/\\ m.acc = a\n \nChosenIn(v, b) == \\E Q \\in Quorums :\n \\A a \\in Q : VotedForIn(a, v, b)\n \nChosen(v) == \\E b \\in Ballots : ChosenIn(v, 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":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1. 某种属性从未发生"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2. 某种属性一直在发生"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"3. 某种属性开始时发生了,某一时刻起又永远地消失了"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"4. 某种属性一旦发生后,永远不会消失"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"5. 某种属性时有时无"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"6. 当属性1发生后,属性2就一直发生"}]},{"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.infoq.cn\/resource\/image\/94\/43\/94b345e63eb0a1f6caf4f83411856743.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"5.1 不变式(Invariance)"}]},{"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":"系统在每条可能的演化路径上,都具备的某种性质,称为不变式(Invariance)。比如Paxos的single value chosen不变式如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"Consistency == \\A v1, v2 \\in Values : Chosen(v1) \/\\ Chosen(v2) => (v1 = v2)"}]},{"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":"也就是说,无论系统怎么运行,在任意时刻,系统的全局状态都会满足Consistency属性。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"5.2 时序逻辑(Temporal Logic)"}]},{"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":"系统在某条路径的演化过程里,可以通过时序逻辑判断前后状态的关系。比如某一时刻某个value被chosen,那么后续时刻,永远只有这一个value被chosen。TLA+中的TL,就是Temporal Logic。时序逻辑比较复杂,这里就不展开了。后续用到时会详细介绍。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"5.3 safety与liveness"}]},{"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":"safety与liveness这两个名词经常出现在分布式理论的论文中,但却从来没有被精确定义过。通过TLA+,可以很显然地看出它们的含义。同样,这里也不展开,后续用到时再详细介绍。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"6. 系统设计与验证"}]},{"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":"有了TLA+,我们就可以大胆地设计分布式系统了。"}]},{"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":"我们可以先用TLA+精确定义出系统,比如Paxos、Raft,或者自己改造后的一些分布式算法。然后我们根据实际的运行场景和自己的要求,定义出若干检验条件,比如数据是否永远一致,系统中是否会出现双主等。最后,将定义好的模型和待检验条件扔进TLC model checker。TLC model checker会穷举系统每个可能的演化路径(内部会生成一张图,进行广度优先搜索),同时使用逻辑计算来判断状态的属性与状态之间的关系。如果某个属性不满足,系统就会报错,而且会给出这种错误在怎样的演化路径下会出现。如果模型通过了所有的检验,我们再用合适的编程语言把模型编程实现就可以了。"}]},{"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":"这时,开发者就可以非常自信地面对自己做出的系统了。就这样,分布式系统corner case这种几乎无解的难题,被Lamport老爷子使用数学工具给解决了。"}]},{"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":"TLA+ Tools截图如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/80\/74\/80f118211c930255831694ce0ca79874.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"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":"这是一篇概述性的文章,让大家先对TLA+有一个大概的了解。后续会详细介绍TLA+的技术细节,以及TLA+在TDengine中的实践过程。欢迎大家一起交流,共同提高。也欢迎大家拥抱开源技术,关注TDengine,一个为物联网而生的时序大数据引擎。"}]},{"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","marks":[{"type":"strong"}],"text":"参考资料:"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"http:\/\/lamport.org\/","title":null,"type":null},"content":[{"type":"text","text":"http:\/\/lamport.org"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"《Specifying Systems》by Leslie Lamport"}]}]}]},{"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","marks":[{"type":"strong"}],"text":"作者介绍:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"李明昊,涛思数据工程师。曾就职于百度、360的基础架构部门,负责大规模分布式存储系统的研发工作。"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章