分布式Process之三: Time and Ordering

来源:互联网 发布:php session共享 编辑:程序博客网 时间:2024/05/18 16:57

1 two approach to order events across process

1)synchronize global clock, use absolute time

2)use logical time-stamp

本文关注第二个方法。

2 Happen-Before关系 

定义逻辑关系 happen-before (" -> ") among pairs of events,三条规则
1)同一process A -> B if time(A) < time(B) local time

2)  send(M) - > receive(M) 

3) transitivity: A -> B and B -> C, then A -> C

类比java 线程 的happen before规则:

1)同一线程,顺序的指令,前面的指令 -> 后面的指令

2)通过同步器:

对一个锁的unlock -> 同一个锁后续的lock

对volatile 变量的写 -> 后续的读

3)transitivity: A -> B and B -> C, then A -> C

是一种partial order 因为不是所有的event都有happen-before关系,没有happen-before关系的event 叫 concurrent event。

A -> B or B -> A or A ||| B

3 Lamport timestamp

用sequence number给每个事件赋timestamp,并在消息中携带自己的integer clock

Each process uses a local counter (clock) which is an integer• Initial value of counter is 0

– A process increments its counter when a send or an instruction happens at it. The counter is assigned to the event as its timestamp.

– A send (message) event carries its timestamp

– For a receive (message) event the counter is updated bymax(local clock, message timestamp) + 1


A -> B 则 => LT(A) < LT(B) 但是反过来不成立,

LT(A) < LT(B) = >  A -> B or A|||B

也就是说用Lamport stamp不能区分出 是A -> B还是 A|||B,只能排除不会是B -> A


4 Vector timestamp

每个process不是维护一个自己的integer clock, 而是它每一个process的最新sequence number

发消息前increment自己的 clock, 然后把整个vector include在消息里

收到消息的process, 先increment自己的clock,然后用message里的vector timestamp, 更新自己vector,(取每个process最新的clock)

1). On an instruction or send event at process i, it increments only its ithelement of its vector clock

2). Each message carries the send-event’s vector timestamp Vmessage[1…N]

3). On receiving a message at process i:Vi[i] = Vi[i] + 1Vi[j] = max(Vmessage[j], Vi[j]) for j ≠ i

VT(A) < VT(B) iif A -> B

Not (VT(A <= VT(B)) and Not (VT(B) <= VT(A)) iif A ||| B

也就是说,vector里,有的分量是A大,有的是B大,则A和B是Concurrent的





0 0
原创粉丝点击