Lamport clock logic

  Foreword

  "Time, Clocks, and the Ordering of Events in a Distributed System" is the brother of Lamport paper, paper in July 1978 published in the "Communication of ACM", won the inaugural PODC most influential paper in 2000 Award, in 2007 won the "ACM SIGOPS Hall of Fame Award". This paper is cited many times, which put forward a number of key concepts of distributed systems, such as: total order of events has happen-before, partial order defined sequence of events associated with the defined sequence of events of all logic clock, physical time synchronization, the distributed mutual exclusion algorithm, state machine and the like.

  In this paper we only relate to the relevant part of the logic clock.

  

 

  Special Relativity

  He says you may not believe, the paper will appear in the computer field theory of relativity, but in fact is said to be obtained from the physics of inspiration. In the special theory of relativity, that with changes in the reference department, the order of the observed events may change. Such as the following figure, there is a movement of the vehicle, t = 0, the center of the vehicle emitting a beam of light to the sides. It seems people in the car, the event is the speed of light in the end of the two ends of the same time, people in the car seems light to the rear in the end, after reaching the front.

  

image description

 

  But if the association that two events have a causal relationship between the occurrence of an event, then the causation would be retained.

  More details, please refer to the theory of relativity related articles.

  Among the distributed system, it is difficult to obtain precise physical time, the authors propose a logic clock to determine causality of events to determine the sequence of events.

  happen before partial order

  First, the author defines a partial order happen before. Happen before the definition of the relationship - meet the following conditions:

  Events a and b occur in the same process, a occurs before b, then ab

  Event is to send a message, b is the event to receive the information, then ab

  ab and BC, i.e., if it ab ac does not exist, there is no BA, then a and b are concurrent.

  

image description

 

  作者用上面这个图片展示了时序关系,竖直方向代表时间,从下向上增长,圆点代表事件,波浪线代表发送消息。可以看出p1-r4,但是p3和q3是并发的。

  逻辑时钟

  定义Ci作为进程Pi的时钟,Ci(a)用来给事件a产生一个数字,代表事件的时间。

  定义C作为系统的时钟,如果事件b属于进程Pi,那么C(b)=Ci(b)

  系统内时钟条件如下:如果a-b,那么C(a) C(b)。

  注意反之则不然,因为那样会推导出对于并发的两个事件a和b,存在C(a)=C(b)。上图中p2和p3都和q3并发,就意味着C(p2)=C(p3),这显然不合理。

  从-的定义可以推出如下结论:

  事件a和b在同一个进程Pi中发生,a在b之前发生,那么Ci(a) Ci(b)

  事件a是进程Pi发送信息,事件b是进程Pj接收该信息,那么Ci(a) Cj(b)为了实现逻辑时钟,假设每个进程i有个变量Ci,用来产生事件的时间。Ci按如下规则变化:

  每个进程i在节点内事件发生时将Ci递增

  假设事件a是进程i发送消息m的事件,m需要携带时间戳t=Ci(a);进程j接收到m之后,将Cj设置为比t大并且不小于Cj当前值的值作为一个实现,我们设置Ci的初始值为0,并且:

  每个进程i在节点内事件发生时将Ci加1

  假设事件a是进程i发送消息m的事件,m需要携带时间戳t=Ci(a);进程j接收到m之后,将Cj设置为max(t,Cj)+1全序关系

  有了逻辑时钟,我们可以定义所有事件的全序关系了。注意,因为存在两个事件的逻辑时钟一致的情况,所以这里在先任意的选择一个进程全序关系-,例如我们可以将进程编号的大小关系作为-。

  进程i中的事件a和进程j中的事件b的全序关系=定义为满足如下任意条件时,a=b:

  Ci(a) Cj(b)

  Ci(a) = Cj(b) 并且 Pi-Pj值得注意的是,这个全序关系不是唯一的,和-的选择有很大的关系。

  后记

  In the back of the paper, the author introduces the distributed mutual exclusion algorithm and clock synchronization algorithm with events total order and state machine implemented, interested readers can see themselves.

  

Guess you like

Origin blog.csdn.net/qianfeng_dashuju/article/details/93630394