200 likes | 210 Views
Synchronization. 5.1 clock synchronization * 5.2 logical clocks * 5.3 global state * 5.4 election algorithm * 5.5 mutual exclusion * 5.6 distributed transaction. Chapter 5. Issues. Multiple processes do not simultaneously access a shared resource
E N D
Synchronization 5.1 clock synchronization * 5.2 logical clocks * 5.3 global state * 5.4 election algorithm * 5.5 mutual exclusion * 5.6 distributed transaction Chapter 5
Issues • Multiple processes do not simultaneously access a shared resource • Multiple processes need to agree on the ordering of events • Synchronization based on relative ordering • How to record a distributed global state • How to elect a coordinator among a group of processes • Distributed mutual exclusion allows shared resources to be protected against simultaneous access by multiple processes • Distributed transactions, in addition, optimize access through advanced concurrency control mechanisms
Logical clocks • For a certain class of algorithms, it is the internal consistency of the clocks that matters, not whether they are particularly close to the real time. • If two processes do not interact, it is not necessary that their clocks be synchronized. (Lamport) • What usually matters is that they agree on the order in which events occur. (Lamport) • Happens before relation: ( ) • If a and b are in the same process, and a occurs before b, then • If a is sending a message, and b is receiving that message by another process, then • Two events are concurrent if neithernor • We need a way of assign a time value to all events that all processes agree that preserves and always increases.
Lamport Timestamps Fig 5-7 (p. 254) • Sometimes an additional requirement: • no two events occur at exactly the same time • Solution: attach the number of the process in which the event occurs to the low-order end of the time, separated by a decimal point. • Three processes, each with its own clock. The clocks run at different rates. • Lamport's algorithm corrects the clocks.
Example: Totally-Ordered Multicasting • Updating a replicated database and leaving it in an inconsistent state. • Lamport timestamps can be used to implement totally-ordered multicast in a distributed fashion.
Totally-ordered multicast • Assume that messages from the same sender are received in the order they were sent, and that no messages are lost. • When a process receives a message, it is put into a local queue, ordered according to its timestamp. The receiver multicasts an acknowledgement to the other processes. • All processes will eventually have the same copy of the local queue. • The timestamps reflect a consistent global ordering of events. • A process can deliver a queued message to the application it is running only when that message is at the head of the queue and has been acknowledged by each other process.
Vector timestamp • With Lamport timestamps, nothing can be said about the relationship between two events a and b by comparing their time values C(a) and C(b). • The problem is that Lamport timestamps do not capture causality. • A vector timestamp VT(a) assigned to an event a has the property that if VT(a) < VT(b) then a is known to causally precede b. It is constructed by letting each process maintain a vector such that • is the number of events that have occurred so far at • If then knows that k events have occurred at
Vector timestamp • The first property is maintained by incrementing • at the occurrence of each new event at • The second property is maintained by piggybacking vectors vt along with messages that are sent. • When process receives a message m, it adjusts its own vector by setting all to • Then is increased by 1 • Messages could be delivered only when no causality constrains are violated. • Consider the case in p. 257.
Global State – distributed snapshot The global state of a distributed system consists of the local state of each process, plus the messages that are currently in transit. • A consistent cut • An inconsistent cut
Global State (2) • Organization of a process and channels for a distributed snapshot
Global State (3) When Q has received a marker along each incoming channel, and processed each one, its recorded local state and the state for each incoming channel can be collected and sent to the process that initiated the snapshot. • Process Q receives a marker for the first time and records its local state • Q records all incoming message • Q receives a marker for its incoming channel and finishes recording the state of the incoming channel
Termination Detection • If Q receives the marker from P requesting a snapshot for the first time, it considers P its predecessor. • When Q completes its part of the snapshot, it sends its predecessor a DONE message. • When the initiator has received a DONE message from all its successors, it knows the snapshot is complete. • However, a snapshot may show a global state in which messages are still in transit. • What is needed is a snapshot in which all channels are empty.
Termination Detection • A modification: • When Q finishes its part of the snapshot, it returns a DONE to its predecessor only when • All of Q’s successors have returned a DONE message • Q has not received any message between the point it recorded its state, and the point it had received the marker along each of its incoming channels. • Otherwise, Q returns a CONTINUE message to its predecessor • When the initiator receives only DONE messages from its successors, the computation is complete. Otherwise, it initiates another snapshot.
Election Algorithms • Election algorithms attempt to locate the process with the highest process number and designate it as coordinator. • Assumes that every process knows the process number of every other process. • What the processes do not know is which ones are currently up and which ones are currently down. • The goal of an election algorithm is to ensure that when an election starts, it concludes with all processes agreeing on who the new coordinator is to be.
The Bully Algorithm • When any process notices that the coordinator is no longer responding to requests, it initiates an election. • A process P holds an election as follows: • P sends an ELECTION message to all processes with higher numbers. • If no one responds, P wins the election and becomes coordinator • If one of the higher-ups answers, it takes over. P’s job is done. • When an ELECTION message is received by a process, the receiver sends an OK message back. It then holds an election, unless it is already holding one. • Eventually, the winner announces its victory by sending all processes a COORDINATOR message. • If a process that was previously down comes back up, it holds an election.
The Bully Algorithm • The bully election algorithm • Process 4 holds an election • Process 5 and 6 respond, telling 4 to stop • Now 5 and 6 each hold an election
Global State (3) • Process 6 tells 5 to stop • Process 6 wins and tells everyone
A Ring Algorithm • Election algorithm using a ring, in which both 2 and • 5 discover the previous coordinator 7 has crashed.
Mutual Exclusion: A Centralized Algorithm • Process 1 asks the coordinator for permission to enter a critical region. Permission is granted • Process 2 then asks permission to enter the same critical region. The coordinator does not reply. • When process 1 exits the critical region, it tells the coordinator, when then replies to 2
A Centralized Algorithm • Advantages: • Guarantees mutual exclusion • Fair • Requires only three messages per use of a critical region (request, grant, release) • Shortcomings • Single point of failure • If processes normally blocks after making request, they cannot distinguish a dead coordinator from “permission denied”. • In a large system, a single coordinator becomes a performance bottleneck.