Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Name:Suyash Jadhav

DIV:B Batch:C
ID:vu1f2021100
Distributed Computing

Experiment No. 4

Aim: Program for Vector Timestamp ordering.

Theory:
Vector timestamps provide a more refined approach to capturing causal relationships among
events in distributed systems compared to Lamport's Logical Clocks. Here's a deeper dive
into the theory behind Vector Timestamps:

Vector Timestamp Structure:


In Vector Timestamps, each process maintains a vector of logical clocks. If there are 'n'
processes in the system, the vector will have 'n' elements, one for each process.
The vector timestamp for a particular event represents the logical clocks of all processes at
the time of that event.

Incrementing Vector Clocks:


When a local event occurs in a process, the logical clock for that process is incremented.
Unlike Lamport's Logical Clocks, only the logical clock of the process generating the event is
incremented.

The process increments its own logical clock to reflect the occurrence of the event.

Message Exchange and Updating Clocks:

When a process sends a message, it includes its current vector timestamp in the message.
Upon receiving a message, the recipient process updates its vector timestamp. For each
element in the vector, the recipient takes the maximum of its current value and the
corresponding value from the received timestamp.

After updating the vector timestamp, the logical clock of the recipient process is incremented.

Comparison for Event Ordering:


Vector timestamps enable a more precise comparison of events across processes. If vector
timestamp Va is less than or equal to vector timestamp Vb for all elements in the vector (and
at least one element is strictly less), then event a causally precedes event b.

Two events are considered concurrent if neither causally precedes the other.

Vector Timestamp Properties:


Name:Suyash Jadhav
DIV:B Batch:C
ID:vu1f2021100
Distributed Computing

Vector timestamps respect the happens-before relationship more accurately than Lamport's
Logical Clocks. They provide a partial ordering of events that reflects the causal
dependencies in a distributed system.
Vector timestamps can be used to implement causal consistency and other consistency models
in distributed databases.

Limitations and Considerations:


Vector timestamps assume a fixed set of processes, and dynamic changes may require
additional mechanisms.
Implementing Vector Timestamps might involve additional overhead in terms of message size
and processing.

CODE:

Java Code: Vector Timestamp Ordering

import java.util.Arrays;

class VectorTimestamp {
private int[] timestamp;

public VectorTimestamp(int processes) {


this.timestamp = new int[processes];
Arrays.fill(timestamp, 0);
}

public int[] getTimestamp() { return


Arrays.copyOf(timestamp, timestamp.length);
}

public void updateTimestamp(int[] receivedTimestamp) {


for (int i = 0; i < timestamp.length; i++) { timestamp[i] =
Math.max(timestamp[i], receivedTimestamp[i]);
}
}

public void increment(int processId) {


timestamp[processId]++;
}
Name:Suyash Jadhav
DIV:B Batch:C
ID:vu1f2021100
Distributed Computing

public static void main(String[] args) {


int totalProcesses = 3; int processId =
1; // Current process ID

VectorTimestamp vectorTimestamp = new VectorTimestamp(totalProcesses);

// Simulate local events


vectorTimestamp.increment(processId);
int[] event1Timestamp = vectorTimestamp.getTimestamp();
System.out.println("Event 1 timestamp: " + Arrays.toString(event1Timestamp));

// Simulate sending a message


int[] messageTimestamp = vectorTimestamp.getTimestamp();
vectorTimestamp.increment(processId);
System.out.println("Message timestamp: " + Arrays.toString(messageTimestamp));

// Simulate receiving a message


int[] receivedTimestamp = Arrays.copyOf(messageTimestamp,
messageTimestamp.length); receivedTimestamp[processId]++;
vectorTimestamp.updateTimestamp(receivedTimestamp); int[]
event2Timestamp = vectorTimestamp.getTimestamp();
System.out.println("Event 2 timestamp: " + Arrays.toString(event2Timestamp));
}
}
OUTPUT:

Conclusion: Understanding Vector Timestamps is crucial for designing distributed systems


that require precise event ordering. This more advanced approach addresses some of the
Name:Suyash Jadhav
DIV:B Batch:C
ID:vu1f2021100
Distributed Computing

limitations of Lamport's Logical Clocks, providing a foundation for developing consistency


mechanisms and ensuring correct execution in distributed environments.

You might also like