分布式Process之三: Time and Ordering

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 ith element 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] + 1,Vi[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的

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值