2 Phase Commit Protocol - JishanBaig/Consensus-Protocols GitHub Wiki

2PC Protocol- Design- First requested node will get the transaction to be performes as a input string from the user.Then the transaction string is sent to coordinator with prepared tagged message. After getting the prepared message coordinator will ask other nodes to prepare with prepare message.Other nodes will send prepared message with either 0 or 1 bit, showing their agreement or disagreement. Prepared with 0 represents disagreement and prepared with 1 represents agreement.If all other processes agree for prepared then only coordinator sends the commit message containign the transaction request string to every other process otherwise it sends commit with bit 0 embedded in it that represents abort operation. After getting the commit message, every process performs the transaction in store.txt file and commits.Or else getting the abort message transaction is aborted.

if(coordinator) { receive prepared from requesting process send prepare to other processes receive prepared from other processes send commit to requesting process send commit to other proces } else if(requesting process) { take transaction as input send prepared to coordinator receive commit from coordinator do commit } else(other processes) { receive prepare from coordinator send prepared to coordinator receive commit from coordinator do commit }