Distributed Transactions ■Local transactions Access/update data at only one database Global transactions Access/update data at more than one database Key issue:how to ensure ACID properties for transactions in a system with global transactions spanning multiple database Database System Concepts-7th Edition 23.2 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 23.2 ©Silberschatz, Korth and Sudarshan th Edition Distributed Transactions ▪ Local transactions • Access/update data at only one database ▪ Global transactions • Access/update data at more than one database ▪ Key issue: how to ensure ACID properties for transactions in a system with global transactions spanning multiple database
Distributed Transactions Transaction may access data at several sites. Each site has a local transaction manager Each site has a transaction coordinator Global transactions submitted to any transaction coordinator TC1 TCn transaction coordinator TM1 TMn) transaction manager computer 1 computer n Database System Concepts-7th Edition 23.3 @Silberschatz,Korth and Sudarshan
Database System Concepts - 7 23.3 ©Silberschatz, Korth and Sudarshan th Edition Distributed Transactions ▪ Transaction may access data at several sites. • Each site has a local transaction manager • Each site has a transaction coordinator ▪ Global transactions submitted to any transaction coordinator
Distributed Transactions Each transaction coordinator is responsible for: Starting the execution of transactions that originate at the site. 。 Distributing subtransactions at appropriate sites for execution. Coordinating the termination of each transaction that originates at the site transaction must be committed at all sites or aborted at all sites. Each local transaction manager responsible for: Maintaining a log for recovery purposes Coordinating the execution and commit/abort of the transactions executing transaction at that site. coordinator TM transaction manager computer 1 computer n Database System Concepts-7th Edition 23.4 @Silberschatz,Korth and Sudarshan
Database System Concepts - 7 23.4 ©Silberschatz, Korth and Sudarshan th Edition Distributed Transactions ▪ Each transaction coordinator is responsible for: • Starting the execution of transactions that originate at the site. • Distributing subtransactions at appropriate sites for execution. • Coordinating the termination of each transaction that originates at the site ▪ transaction must be committed at all sites or aborted at all sites. ▪ Each local transaction manager responsible for: • Maintaining a log for recovery purposes • Coordinating the execution and commit/abort of the transactions executing at that site
System Failure Modes Failures unique to distributed systems: ·Failure of a site. ·Loss of massages Handled by network transmission control protocols such as TCP-IP Failure of a communication link Handled by network protocols,by routing messages via alternative links Network partition A network is said to be partitioned when it has been split into two or more subsystems that lack any connection between them Note:a subsystem may consist of a single node Network partitioning and site failures are generally indistinguishable. Database System Concepts-7th Edition 23.5 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 23.5 ©Silberschatz, Korth and Sudarshan th Edition System Failure Modes ▪ Failures unique to distributed systems: • Failure of a site. • Loss of massages ▪ Handled by network transmission control protocols such as TCP-IP • Failure of a communication link ▪ Handled by network protocols, by routing messages via alternative links • Network partition ▪ A network is said to be partitioned when it has been split into two or more subsystems that lack any connection between them • Note: a subsystem may consist of a single node ▪ Network partitioning and site failures are generally indistinguishable
Commit Protocols Commit protocols are used to ensure atomicity across sites a transaction which executes at multiple sites must either be committed at all the sites,or aborted at all the sites. cannot have transaction committed at one site and aborted at another The two-phase commit(2PC)protocol is widely used ■ Three-phase commit(3PC)protocol avoids some drawbacks of 2PC,but is more complex Consensus protocols solve a more general problem,but can be used for atomic commit More on these later in the chapter The protocols we study all assume fail-stop model-failed sites simply stop working,and do not cause any other harm,such as sending incorrect messages to other sites. Protocols that can tolerate some number of malicious sites discussed in bibliographic notes online Database System Concepts-7th Edition 23.6 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 23.6 ©Silberschatz, Korth and Sudarshan th Edition Commit Protocols ▪ Commit protocols are used to ensure atomicity across sites • a transaction which executes at multiple sites must either be committed at all the sites, or aborted at all the sites. ▪ cannot have transaction committed at one site and aborted at another ▪ The two-phase commit (2PC) protocol is widely used ▪ Three-phase commit (3PC) protocol avoids some drawbacks of 2PC, but is more complex ▪ Consensus protocols solve a more general problem, but can be used for atomic commit • More on these later in the chapter ▪ The protocols we study all assume fail-stop model – failed sites simply stop working, and do not cause any other harm, such as sending incorrect messages to other sites. • Protocols that can tolerate some number of malicious sites discussed in bibliographic notes online
Two Phase Commit Protocol (2PC) ■ Execution of the protocol is initiated by the coordinator after the last step of the transaction has been reached. The protocol involves all the local sites at which the transaction executed Protocol has two phases Let T be a transaction initiated at site S,and let the transaction coordinator at S;be C Database System Concepts-7th Edition 23.7 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 23.7 ©Silberschatz, Korth and Sudarshan th Edition Two Phase Commit Protocol (2PC) ▪ Execution of the protocol is initiated by the coordinator after the last step of the transaction has been reached. ▪ The protocol involves all the local sites at which the transaction executed ▪ Protocol has two phases ▪ Let T be a transaction initiated at site Si , and let the transaction coordinator at Si be Ci
Phase 1:Obtaining a Decision Coordinator asks all participants to prepare to commit transaction T. C adds the records to the log and forces log to stable storage sends prepare T messages to all sites at which T executed Upon receiving message,transaction manager at site determines if it can commit the transaction if not,add a record to the log and send abort T message to C if the transaction can be committed,then: add the record to the log force all records for Tto stable storage send ready 7 message to C Transaction is now in ready state at the site Database System Concepts-7th Edition 23.8 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 23.8 ©Silberschatz, Korth and Sudarshan th Edition Phase 1: Obtaining a Decision ▪ Coordinator asks all participants to prepare to commit transaction Ti . • Ci adds the records to the log and forces log to stable storage • sends prepare T messages to all sites at which T executed ▪ Upon receiving message, transaction manager at site determines if it can commit the transaction • if not, add a record to the log and send abort T message to Ci • if the transaction can be committed, then: ▪ add the record to the log ▪ force all records for T to stable storage ▪ send ready T message to Ci Transaction is now in ready state at the site
Phase 2:Recording the Decision 7 can be committed of C,received a ready 7 message from all the participating sites:otherwise 7 must be aborted. Coordinator adds a decision record,or ,to the log and forces record onto stable storage.Once the record stable storage it is irrevocable (even if failures occur) Coordinator sends a message to each participant informing it of the decision(commit or abort) Participants take appropriate action locally Database System Concepts-7th Edition 23.9 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 23.9 ©Silberschatz, Korth and Sudarshan th Edition Phase 2: Recording the Decision ▪ T can be committed of Ci received a ready T message from all the participating sites: otherwise T must be aborted. ▪ Coordinator adds a decision record, or , to the log and forces record onto stable storage. Once the record stable storage it is irrevocable (even if failures occur) ▪ Coordinator sends a message to each participant informing it of the decision (commit or abort) ▪ Participants take appropriate action locally
Two-Phase Commit Protocol Coordinator Node N1 Node N2 Force log record Send message prepare T Force log record Force log record ready T> Send message Send message ready T ready T Force log record commit T> Send message commit T Force log record Force log record commit T> Database System Concepts-7th Edition 23.10 @Silberschatz,Korth and Sudarshan
Database System Concepts - 7 23.10 ©Silberschatz, Korth and Sudarshan th Edition Two-Phase Commit Protocol
Handling of Failures-Site Failure When site Sk recovers,it examines its log to determine the fate of transactions active at the time of the failure. ■Log containrecord:site executes undo(7) ■ Log contains record:site must consult C;to determine the fate of T. If T committed,redo() ·If T aborted,undo(刀 The log contains no control records concerning T implies that Sk failed before responding to the prepare T message from Ci since the failure of Sk precludes the sending of such a response C;must abort T ·Skmust execute undo(T刀 Database System Concepts-7th Edition 23.11 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 23.11 ©Silberschatz, Korth and Sudarshan th Edition Handling of Failures - Site Failure When site Sk recovers, it examines its log to determine the fate of transactions active at the time of the failure. ▪ Log contain record: site executes redo (T) ▪ Log contains record: site executes undo (T) ▪ Log contains record: site must consult Ci to determine the fate of T. • If T committed, redo (T) • If T aborted, undo (T) ▪ The log contains no control records concerning T implies that Sk failed before responding to the prepare T message from Ci • since the failure of Sk precludes the sending of such a response Ci must abort T • Sk must execute undo (T)