Models and Clocks Characteristics of a Distributed System
Models and Clocks
Characteristics of a Distributed System Absence of a shared clock l Absence of shared memory l Absence of failure detection l
Model of a Distributed System Asynchronous l Message Passing l
A Simple Distributed Program l Each process is defined as a set of states
Model of a Distributed Computation l Interleaving model • A global sequence of events • eg. • P 1 sends “what is my checking balance” to P 2 • P 1 sends “what is my savings balance to P 2” • P 2 receives “what is my checking balance” from P 1 • P 1 sets total to 0 • P 2 receives “what is my savings balance” from P 1 • P 2 sends “checking balance = 40” to P 1 • P 1 receives “checking balance = 40” from P 2. . .
Model of a Distributed Computation l 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
A run in the happened-before model
Logical Clocks l A logical clock C is a map from the set of events E to N (the set of natural numbers) with the following constraint:
Lamport's logical clock algorithm public class Lamport. Clock { int c; public Lamport. Clock() { c = 1; } public int get. Value() { return c; } public void tick() { // on internal actions c = c + 1; } public void send. Action() { // include c in message c = c + 1; } public void receive. Action(int src, int sent. Value) { c = Util. max(c, sent. Value) + 1; } }
Vector Clocks l 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. l 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
A Vector clock algorithm public class Vector. Clock { public int[] v; int my. Id; int N; public Vector. Clock(int num. Proc, int id) { my. Id = id; N = num. Proc; v = new int[num. Proc]; for (int i = 0; i < N; i++) v[i] = 0; v[my. Id] = 1; } public void tick() { v[my. Id]++; } public void send. Action() { //include the vector in the message v[my. Id]++; } public void receive. Action(int[] sent. Value) { for (int i = 0; i < N; i++) v[i] = Util. max(v[i], sent. Value[i]); v[my. Id]++; } }
An execution of the vector clock algorithm
Direct-Dependency Clocks l l l 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
A Direct-dependency clock algorithm public class Direct. Clock { public int[] clock; int my. Id; public Direct. Clock(int num. Proc, int id) { my. Id = id; clock = new int[num. Proc]; for (int i = 0; i < num. Proc; i++) clock[i] = 0; clock[my. Id] = 1; } public int get. Value(int i) { return clock[i]; } public void tick() { clock[my. Id]++; } public void send. Action() { // sent. Value = clock[my. Id]; tick(); } public void receive. Action(int sender, int sent. Value) { clock[sender] = Util. max(clock[sender], sent. Value); clock[my. Id] = Util. max(clock[my. Id], sent. Value) + 1; } }
Matrix Clocks l l N x N matrix Gives processes additional knowledge
The matrix clock algorithm public class Matrix. Clock { int[][] M; int my. Id; int N; public Matrix. Clock(int num. Proc, int id) { my. Id = id; N = num. Proc; M = new int[N][N]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) M[i][j] = 0; M[my. Id] = 1; } public void tick() { M[my. Id]++; } public void send. Action() { //include the matrix in the message M[my. Id]++; } public void receive. Action(int[][] W, int src. Id) { // component-wise maximum of matrices for (int i = 0; i < N; i++) if (i != my. Id) { for (int j = 0; j < N; j++) M[i][j] = Util. max(M[i][j], W[i][j]); } } }
- Slides: 16