Presentation on theme: "OSes: 16. Dist. Coord 1 Operating Systems v Objectives –introduce issues such as event ordering, mutual exclusion, atomicity, deadlock Certificate Program."— Presentation transcript:
OSes: 16. Dist. Coord 1 Operating Systems v Objectives –introduce issues such as event ordering, mutual exclusion, atomicity, deadlock Certificate Program in Software Development CSE-TC and CSIM, AIT September -- November, 2003 16. Distributed Coordination (S&G 6th ed., Ch. 17)
OSes: 16. Dist. Coord 3 1. Event Ordering v Happened-before relation (denoted by -->). –If A and B are events in the same process, and A was executed before B, then A --> B. –If A is the event of sending a message by one process and B is the event of receiving that message by another process, then A --> B. –If A --> B and B --> C then A --> C.
OSes: 16. Dist. Coord 4 Relative Time for 3 Concurrent Processes Figure 17.1, p.597 message event time
OSes: 16. Dist. Coord 5 1.1. Implementation of --> v Associate a timestamp with each system event. v Require that for every pair of events A and B, if A --> B, then the timestamp of A is less than the timestamp of B. v Each process P i has a logical clock, LC i –implemented as a counter, incremented when an event occurs continued
OSes: 16. Dist. Coord 6 v A process advances its logical clock when it receives a message whose timestamp is greater than the current value of its logical clock. v If the timestamps of two events A and B are the same, then the events are concurrent.
OSes: 16. Dist. Coord 7 2. Distributed Mutual Exclusion (DME) v Assumptions: –the system consists of n processes; each process P i resides at a different processor; –each process has a critical section that requires mutual exclusion v Requirement: –if P i is executing in its critical section, then no other process P j is executing in its own critical section
OSes: 16. Dist. Coord 8 2.1. DME: Centralized Approach v One of the processes in the system is chosen to coordinate the entry to the critical sections. v A process that wants to enter its critical section sends a request message to the coordinator. continued
OSes: 16. Dist. Coord 9 v The coordinator decides which process can enter the critical section next, and its sends that process a reply message. v When the process receives a reply message from the coordinator, it enters its critical section. continued
OSes: 16. Dist. Coord 10 v After exiting its critical section, the process sends a release message to the coordinator and proceeds with its execution. v This scheme requires three messages per critical-section entry: –request –reply –release
OSes: 16. Dist. Coord 11 2.2. DME: Fully Distributed Approach v When process P i wants to enter its critical section, it generates a new timestamp, TS, and sends the message request (P i, TS) to all other processes in the system. v When process P j receives a request message, it may reply immediately or it may defer sending a reply back. continued
OSes: 16. Dist. Coord 12 v When process P i receives a reply message from all other processes in the system, it can enter its critical section. v After exiting its critical section, the process sends reply messages to all its deferred requests. continued
OSes: 16. Dist. Coord 13 v The decision whether process P j replies immediately to a request(P i, TS) message or defers its reply is based on three factors: –1. if P j is in its critical section, then it defers its reply to P i –2. if P j does not want to enter its critical section, then it sends a reply immediately to P i. continued
OSes: 16. Dist. Coord 14 –If P j wants to enter its critical section but has not yet entered it, then it compares its own request timestamp with the timestamp TS. u If its own request timestamp is greater than TS, then it sends a reply immediately to P i (P i asked first). u Otherwise, the reply is deferred.
OSes: 16. Dist. Coord 15 Desirable Behavior of Fully Distributed Approach v Freedom from deadlock. v Freedom from starvation, since entry to the critical section is scheduled according to the timestamp ordering –the timestamp ordering means that processes are served in a FCFS order continued
OSes: 16. Dist. Coord 16 v The number of messages per critical-section entry is 2(n – 1). –this is the minimum number of required messages per critical-section entry when processes act independently and concurrently n is the number of processes
OSes: 16. Dist. Coord 17 Three Undesirable Consequences v 1. The processes need to know the identity of all other processes in the system –makes the dynamic addition and removal of processes more complex v 2. If one of the processes fails, then the entire scheme collapses –this can be dealt with by continuously monitoring the state of all the processes continued
OSes: 16. Dist. Coord 18 v 3. Processes that have not entered their critical section must pause frequently to tell other processes that they intend to enter their critical section v This approach is best suited for small, stable sets of cooperating processes.
OSes: 16. Dist. Coord 19 2.3. Token Passing v When a process has the token, it can enter its critical section (if it wants to), or pass it on. v Problems: ring breaks, token loss c.s P2 c.s P1 c.s P0 token
OSes: 16. Dist. Coord 20 3. Atomicity v Either all the operations associated with a program unit are executed to completion, or none are performed. v Ensuring atomicity in a distributed system requires a transaction coordinator. continued
OSes: 16. Dist. Coord 21 v Transaction Coordinator tasks: –start the transaction –break the transaction into subtransactions, and distribution them to appropriate sites for execution –coordinate the termination of the transaction u may result in commits or aborts at all sites
OSes: 16. Dist. Coord 22 3.1. Two-Phase Commit Protocol (2PC) v Assumes a fail-stop model. v Execution of 2PC starts after the last step of the transaction. v When 2PC starts, the transaction may still be executing at some sub-sites. continued
OSes: 16. Dist. Coord 23 v 2PC involves all the sub-sites at which the transaction executed. v Example: Let T be a transaction initiated at site S i and let the transaction coordinator at S i be C i.
OSes: 16. Dist. Coord 24 Phase 1: Obtaining a Decision v C i adds record to the log. v C i sends message to all sites. v When a sub-site receives a message, the transaction manager determines if it can commit the transaction. continued
OSes: 16. Dist. Coord 25 –If no: add record to the log and respond to C i with –If no: add record to the log and respond to C i with –If yes: u add record to the log. u force all log records for T onto stable storage. u transaction manager sends message to C i. continued
OSes: 16. Dist. Coord 26 v Coordinator collects responses –all respond “ready”, decision is commit –at least one response is “abort”, decision is abort –at least one participant fails to respond within a time-out period, decision is abort.
OSes: 16. Dist. Coord 27 Phase 2: Record Decision in DB v Coordinator adds a decision record or or to its log and records onto stable storage. v Once the info reaches stable storage, it is irrevocable (even if failure occur). continued
OSes: 16. Dist. Coord 28 v Coordinator sends a message to each participant informing it of the decision (commit or abort). v Participants take appropriate action locally.
OSes: 16. Dist. Coord 29 3.2. Failure Handling in 2PC – Site Failure v The log contains a record. In this case, the site executes redo(T). v The log contains an record. In this case, the site executes undo(T). continued
OSes: 16. Dist. Coord 30 v The log contains a record; consult C i. If C i is down, site sends query- status T message to the other sites. v The log contains no control records concerning T. In this case, the site executes undo(T).
OSes: 16. Dist. Coord 31 Failure Handling in 2PC – Coordinator C i Failure v If all active sites contain a record in its log, the T must be committed. v If an active site contains an record in its log, then T must be aborted. continued the main problem with 2PC
OSes: 16. Dist. Coord 32 v If some active site does not contain the record in its log then the failed coordinator C i cannot have decided to commit T. v Rather than wait for C i to recover, it is better to abort T. continued
OSes: 16. Dist. Coord 33 v All active sites have a record in their logs, but no additional control records. v In this case we must wait for the coordinator to recover. –blocking problem – T is blocked pending the recovery of site S i.
OSes: 16. Dist. Coord 34 4. Concurrency Control v Modify the centralized concurrency schemes to handle the distribution of transactions. v A transaction manager coordinates the execution of transactions (or subtransactions) which access data at local sites. continued
OSes: 16. Dist. Coord 35 v Local transaction only executes at that site. v Global transaction executes at several sites.
OSes: 16. Dist. Coord 36 4.1. Locking Protocols v We can use the two-phase locking protocol in a distributed environment by changing how the lock manager is implemented. v A non-replicated scheme –each site maintains a local lock manager which administers lock and unlock requests for data items stored at that site continued
OSes: 16. Dist. Coord 37 –a simple implementation involves two message transfers for handling lock requests, and one message transfer for handling unlock requests –deadlock handling is more complex
OSes: 16. Dist. Coord 38 Single-Coordinator Approach v A single lock manager resides at a single chosen site –all lock and unlock requests are made at that site v Simple implementation v Simple deadlock handling continued
OSes: 16. Dist. Coord 39 v Possibility of bottleneck v Vulnerable to loss of concurrency controller if single site fails v Multiple-coordinator approach distributes lock-manager function over several sites.
OSes: 16. Dist. Coord 40 Majority Protocol v Avoids drawbacks of central control by dealing with replicated data in a decentralized manner. v More complicated to implement v Deadlock handling must be modified –possible for deadlock to occur when locking only one data item for replicated data
OSes: 16. Dist. Coord 41 Biased Protocol v Similar to majority protocol, but requests for shared locks are prioritized over requests for exclusive locks. v Less overhead on read operations than in majority protocol; but has additional overheads for writes. v Like majority protocol, deadlock handling is complex.
OSes: 16. Dist. Coord 42 Primary Copy v One of the sites at which a replica resides is designated as the primary site. –a request to lock a data item is made at the primary site of that item v Concurrency control for replicated data is handled in a similar way to unreplicated data. continued
OSes: 16. Dist. Coord 43 v A simple implementation, but if the primary site fails, then the data item is unavailable, even though other sites may have a copy.
OSes: 16. Dist. Coord 44 5. Deadlock Handling v Deadlock Prevention v Deadlock Avoidance v Deadlock Detection
OSes: 16. Dist. Coord 45 5.1. Deadlock Prevention v Resource-ordering deadlock-prevention – define a global ordering among the system resources. –assign a unique number to all system resources –a process may request a resource with unique number i only if it is not holding a resource with a unique number greater than i –simple to implement; requires little overhead continued
OSes: 16. Dist. Coord 46 v Banker’s algorithm in a dist. system –designate one of the processes in the system as the process that maintains the information necessary to carry out the Banker’s algorithm –also implemented easily, but may require too much overhead
OSes: 16. Dist. Coord 47 Priority Deadlock-Prevention Scheme v Each process P i is assigned a unique priority number v Priority numbers are used to decide whether a process P i should wait for a process P j ; otherwise P i is rolled back. continued
OSes: 16. Dist. Coord 48 v The scheme prevents deadlocks. –for every edge P i --> P j in the wait-for graph, P i has a higher priority than P j. u a cycle cannot exist
OSes: 16. Dist. Coord 49 Wait-Die Scheme v Based on a non-preemptive technique. v If P i requests a resource currently held by P j, P i is allowed to wait only if it has a smaller timestamp than does P j (P i is older than P j ). Otherwise, P i is rolled back (dies). continued
OSes: 16. Dist. Coord 50 v Example: Suppose that processes P 1, P 2, and P 3 have timestamps 5, 10, and 15 respectively. –if P 1 request a resource held by P 2, then P 1 will wait. –If P 3 requests a resource held by P 2, then P 3 will be rolled back.
OSes: 16. Dist. Coord 51 Would-Wait Scheme v Based on a preemptive technique –counterpart to the wait-die system v If P i requests a resource currently held by P j, P i is allowed to wait only if it has a larger timestamp than does P j (P i is younger than P j ). Otherwise P j is rolled back (P j is wounded by P i ). continued
OSes: 16. Dist. Coord 52 v Example: Suppose that processes P 1, P 2, and P 3 have timestamps 5, 10, and 15 respectively. –If P 1 requests a resource held by P 2, then the resource will be preempted from P 2 and P 2 will be rolled back. –If P 3 requests a resource held by P 2, then P 3 will wait.
OSes: 16. Dist. Coord 53 5.2. Deadlock Detection – Centralized Approach v Each site keeps a local wait-for graph. –the nodes of the graph correspond to all the processes that are currently either holding or requesting any of the resources local to that site v A global wait-for graph is maintained in a single coordination process –this graph is the union of all local wait-for graphs continued
OSes: 16. Dist. Coord 54 v There are three different options (points in time) when the global wait-for graph may be constructed –1. Whenever a new edge is inserted or removed in one of the local wait-for graphs. –2. Periodically, when a number of changes have occurred in a wait-for graph. –3. Whenever the coordinator needs to invoke the cycle-detection algorithm. v Unnecessary rollbacks may occur as a result of false cycles.
OSes: 16. Dist. Coord 55 Two Local Wait-For Graphs Figure 17.3, p.612
OSes: 16. Dist. Coord 57 Detection Algorithm Based on Option 3 v Append unique identifiers (timestamps) to requests from different sites. v When process P i, at site A, requests a resource from process P j, at site B, a request message with timestamp TS is sent. continued
OSes: 16. Dist. Coord 58 v The edge P i --> P j with the label TS is inserted in the local wait-for of A. The edge is inserted in the local wait-for graph of B only if B has received the request message and cannot immediately grant the requested resource.
OSes: 16. Dist. Coord 59 The Algorithm 1.The controller sends an initiating message to each site in the system. 2.On receiving this message, a site sends its local wait-for graph to the coordinator. 3.When the controller has received a reply from each site, it constructs a graph as follows: continued
OSes: 16. Dist. Coord 60 –(a)The constructed graph contains a vertex for every process in the system. –(b)The graph has an edge P i --> P j if and only if (1) there is an edge P i --> P j in one of the wait-for graphs, or (2) an edge P i --> P j with some label TS appears in more than one wait-for graph. –If the constructed graph contains a cycle then system deadlock.
OSes: 16. Dist. Coord 61 Local and Global Wait-For Graphs Figure 17.5, p.614
OSes: 16. Dist. Coord 62 Fully Distributed Approach v All controllers share equally the responsibility for detecting deadlock. v Every site constructs a wait-for graph that represents a part of the total graph. v We add one additional node P ex to each local wait-for graph. continued
OSes: 16. Dist. Coord 63 v If a local wait-for graph contains a cycle that does not involve node P ex, then the system is in a deadlock state. v A cycle involving P ex implies the possibility of a deadlock. –to find out whether a deadlock does exist, a distributed deadlock-detection algorithm must be called
OSes: 16. Dist. Coord 65 Augmented Local Wait-For Graph in Site S2 Figure 17.7, p.617
OSes: 16. Dist. Coord 66 6. Election Algorithms v Determine where a new copy of the coordinator should be restarted. v Assume that a unique priority number is associated with each active process in the system, and assume that the priority number of process P i is i. v Assume a one-to-one correspondence between processes and sites. continued
OSes: 16. Dist. Coord 67 v The coordinator is always the process with the largest priority number –when a coordinator fails, the algorithm must elect the active process with the largest priority number v Two algorithms, the bully algorithm or the ring algorithm, can be used to elect a new coordinator in case of failures.
OSes: 16. Dist. Coord 68 6.1. Bully Algorithm v Applicable to systems where every process can send a message to every other process. v If process P i sends a request that is not answered by the coordinator within a time interval T, assume that the coordinator has failed; P i tries to elect itself as the new coordinator. continued
OSes: 16. Dist. Coord 69 v P i sends an election message to every process with a higher priority number, P i then waits for any of these processes to answer within T. continued
OSes: 16. Dist. Coord 70 v If no response within T, assume that all processes with numbers greater than i have failed; P i elects itself the new coordinator. v If answer is received, P i begins time interval T´, waiting to receive a message that a process with a higher priority number has been elected. continued
OSes: 16. Dist. Coord 71 v If no message is sent within T´, assume the process with a higher number has failed; P i should restart the algorithm continued
OSes: 16. Dist. Coord 72 v If P i is not the coordinator, then, at any time during execution, P i may receive one of the following two messages from process P j. –P j is the new coordinator (j > i). P i, in turn, records this information. –P j started an election (j > i). P i, sends a response to P j and begins its own election algorithm, provided that P i has not already initiated such an election. continued
OSes: 16. Dist. Coord 73 v After a failed process recovers, it immediately begins execution of the same algorithm. v If there are no active processes with higher numbers, the recovered process forces all processes with lower number to let it become the coordinator process, even if there is a currently active coordinator with a lower number.
OSes: 16. Dist. Coord 74 6.2. Ring Algorithm v Applicable to systems organized as a ring (logically or physically). v Assumes that the links are unidirectional, and that processes send their messages to their right neighbors. continued
OSes: 16. Dist. Coord 75 v Each process maintains an active list, consisting of all the priority numbers of all active processes in the system when the algorithm ends. v If process P i detects a coordinator failure, it creates a new active list that is initially empty. It then sends a message elect(i) to its right neighbor, and adds the number i to its active list. continued
OSes: 16. Dist. Coord 76 v If P i receives a message elect(j) from the process on the left, it must respond in one of three ways: 1.If this is the first elect message it has seen or sent, P i creates a new active list with the numbers i and j. It then sends the message elect(i), followed by the message elect(j). continued
OSes: 16. Dist. Coord 77 –If i = j, then add elect(j) to the active list for P i –If i = j, then P i store the message elect(i) u the active list for P i contains all the active processes in the system u P i can now determine the new coordinator process u based on priority
OSes: 16. Dist. Coord 78 7. Reaching Agreement v There are applications where a set of processes wish to agree on a common “value”. v Such agreement may not take place due to: –a faulty communication medium –faulty processes u processes may send garbled or incorrect messages to other processes u a subset of the processes may collaborate with each other in an attempt to defeat the scheme
OSes: 16. Dist. Coord 79 7.1. Faulty Communications v Process P i at site A, has sent a message to process P j at site B; to proceed, P i needs to know if P j has received the message. v Detect failures using a time-out scheme. –When P i sends out a message, it also specifies a time interval during which it is willing to wait for an acknowledgment message form P j. –When P j receives the message, it immediately sends an acknowledgment to P i. continued
OSes: 16. Dist. Coord 80 –If P i receives the acknowledgment message within the specified time interval, it concludes that P j has received its message. If a time-out occurs, P j needs to retransmit its message and wait for an acknowledgment. –Continue until P i either receives an acknowledgment, or is notified by the system that B is down. continued
OSes: 16. Dist. Coord 81 v Suppose that P j also needs to know that P i has received its acknowledgment message, in order to decide on how to proceed. –in the presence of failure, it is not possible to accomplish this task –it is very hard (time consuming) in a distributed environment for processes P i and P j to agree completely on their respective states.
OSes: 16. Dist. Coord 82 7.2. Faulty Processes (Byzantine Generals Problem) v Communication medium is reliable, but processes can fail in unpredictable ways. v Consider a system of n processes, of which no more than m are faulty. v Suppose that each process P i has some private value of V i. continued
OSes: 16. Dist. Coord 83 v Devise an algorithm that allows each non- faulty P i to construct a vector X i = (A i,1, A i,2, …, A i,n ) such that: –if P j is a non-faulty process, then A ij = V j. –if P i and P j are both non-faulty processes, then X i = X j. continued
OSes: 16. Dist. Coord 84 v Solutions share the following properties. –a correct algorithm can be devised only if n >= 3(m + 1). –the worst-case delay for reaching agreement is proportionate to m + 1 message-passing delays continued
OSes: 16. Dist. Coord 85 v An algorithm for the case where m = 1 and n = 4 requires two rounds of information exchange: –each process sends its private value to the other 3 processes –each process sends the information it has obtained in the first round to all other processes continued
OSes: 16. Dist. Coord 86 v If a faulty process refuses to send messages, a non-faulty process can choose an arbitrary value and pretend that that value was sent by that process. continued
OSes: 16. Dist. Coord 87 v After the two rounds are completed, a non- faulty process P i can construct its vector X i = (A i,1, A i,2, A i,3, A i,4 ) as follows: –A i,j = V i. –for j <> i, if at least two of the three values reported for process P j agree, then the majority value is used to set the value of A ij. Otherwise, a default value (nil) is used.