Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of...

16
Models and Clocks

Transcript of Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of...

Page 1: Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of shared memory Absence of failure detection.

Models and Clocks

Page 2: Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of shared memory Absence of failure detection.

Characteristics of a Distributed System

Absence of a shared clockAbsence of shared memoryAbsence of failure detection

Page 3: Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of shared memory Absence of failure detection.

Model of a Distributed System

AsynchronousMessage Passing

Page 4: Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of shared memory Absence of failure detection.

A Simple Distributed Program

Each process is defined as a set of states

Page 5: Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of shared memory Absence of failure detection.

Model of a Distributed Computation

Interleaving model• A global sequence of events

• eg.• P1 sends “what is my checking balance” to P2

• P1 sends “what is my savings balance to P2”

• P2 receives “what is my checking balance” from P1

• P1 sets total to 0

• P2 receives “what is my savings balance” from P1

• P2 sends “checking balance = 40” to P1

• P1 receives “checking balance = 40” from P2 . . .

Page 6: Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of shared memory Absence of failure detection.

Model of a Distributed Computation

Happened before model• Happened before relation

• If e occurred before f in the same process, then

e --> f

• If e is the send event of a message and f is the receive event of the same message, then e --> f

• If there exists an event g such that e --> g and g --> f, then e --> f

Page 7: Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of shared memory Absence of failure detection.

A run in the happened-before model

Page 8: Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of shared memory Absence of failure detection.

Logical Clocks

A logical clock C is a map from the set of events E to N (the set of natural numbers) with the following constraint:

Page 9: Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of shared memory Absence of failure detection.

Lamport's logical clock algorithm

public class LamportClock { int c; public LamportClock() { c = 1; } public int getValue() { return c; } public void tick() { // on internal actions c = c + 1; } public void sendAction() { // include c in message c = c + 1; } public void receiveAction(int src, int sentValue) { c = Util.max(c, sentValue) + 1; }}

Page 10: Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of shared memory Absence of failure detection.

Vector Clocks

Map from the set of states to vectors of natural numbers with the constraint:• For all s, t: s--> t iff s.v < t.v ...... where s.v is

the vector assigned to the state s.

Given vectors x,y : x < y• All elements of x are less than or equal to the

corresponding elements of y

• At least one element of x is strictly less than the corresponding element of y

Page 11: Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of shared memory Absence of failure detection.

A Vector clock algorithmpublic class VectorClock { public int[] v; int myId; int N; public VectorClock(int numProc, int id) { myId = id; N = numProc; v = new int[numProc]; for (int i = 0; i < N; i++) v[i] = 0; v[myId] = 1; } public void tick() { v[myId]++; } public void sendAction() { //include the vector in the message v[myId]++; } public void receiveAction(int[] sentValue) { for (int i = 0; i < N; i++) v[i] = Util.max(v[i], sentValue[i]); v[myId]++;} }

Page 12: Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of shared memory Absence of failure detection.

An execution of the vector clock algorithm

Page 13: Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of shared memory Absence of failure detection.

Direct-Dependency Clocks

Weaker version of the vector clock Maintain a vector clock locally Process sends only its local component of the

clock Directly precedes relation: only one message

in the happened-before diagram of the computation

Direct-dependency clocks satisfy

Page 14: Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of shared memory Absence of failure detection.

A Direct-dependency clock algorithmpublic class DirectClock { public int[] clock; int myId; public DirectClock(int numProc, int id) { myId = id; clock = new int[numProc]; for (int i = 0; i < numProc; i++) clock[i] = 0; clock[myId] = 1; } public int getValue(int i) { return clock[i]; } public void tick() { clock[myId]++; } public void sendAction() { // sentValue = clock[myId]; tick(); } public void receiveAction(int sender, int sentValue) { clock[sender] = Util.max(clock[sender], sentValue); clock[myId] = Util.max(clock[myId], sentValue) + 1; }}

Page 15: Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of shared memory Absence of failure detection.

Matrix Clocks

N x N matrix Gives processes additional knowledge

Page 16: Models and Clocks. Characteristics of a Distributed System Absence of a shared clock Absence of shared memory Absence of failure detection.

The matrix clock algorithmpublic class MatrixClock { int[][] M; int myId; int N; public MatrixClock(int numProc, int id) { myId = id; N = numProc; M = new int[N][N]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) M[i][j] = 0; M[myId][myId] = 1; } public void tick() { M[myId][myId]++; } public void sendAction() { //include the matrix in the message M[myId][myId]++; } public void receiveAction(int[][] W, int srcId) { // component-wise maximum of matrices for (int i = 0; i < N; i++) if (i != myId) { for (int j = 0; j < N; j++) M[i][j] = Util.max(M[i][j], W[i][j]); }} }