A distributed transaction system implementing both Raft consensus algorithm and Two-Phase Commit (2PC) protocol for maintaining consistency across distributed nodes.
-
Raft Consensus Implementation
- Leader Election
- Log Replication
- Log Persistence
- Cluster Management
- Crash Recovery
-
Two-Phase Commit (2PC)
- Atomic Transactions
- Account Balance Management
- Transaction Logging
- Crash Recovery Support
-
Multi-Cluster Architecture
- Separate Raft Clusters (A and B)
- Coordinator Node Management
- Cross-Cluster Transactions
coordinator.py- Implements the 2PC coordinatornode.py- Base Raft node implementationnode_2pc.py- Extended node with 2PC supportclient.py- Base client implementationclient_2pc.py- Extended client with 2PC operationsparticipant.py- 2PC participant nodeconfig.py- System configuration
- Ensure Python 3.x is installed
- Clone the repository
- Set up the configuration in
config.py
# Start coordinator
python coordinator.py node1
# Start cluster A nodes
python participant.py nodeA1
python participant.py nodeA2
python participant.py nodeA3
# Start cluster B nodes
python participant.py nodeB1
python participant.py nodeB2
python participant.py nodeB3# Perform transaction
python client_2pc.py transaction <value1: AccountA> <value2: AccountB> [bonus] [simulation_num]
# Check balances
python client_2pc.py get_balances
# Set balance
python client_2pc.py set_balance <account_name [AccountA/AccountB]> <balance>
# Print logs
python client_2pc.py print_logs- You need to open 8 terminals in total.
- Update the config file accordingly.
- For each terminal execute the corresponding file.
- All the simulation we introduce next can be performed using the client:
Sim 1: Ideal scenario
python3 client_2pc.py set_balance AccountA 200
python3 client_2pc.py set_balance AccountB 300
python3 client_2pc.py transaction -100 100
python3 client_2pc.py transaction 0 0 bonus
Sim 2: Not enough balance
python3 client_2pc.py set_balance AccountA 90
python3 client_2pc.py set_balance AccountB 50
python3 client_2pc.py transaction -100 100
python3 client_2pc.py transaction 0 0 bonus
Crash simulations:
Node-2 crashed before responding to the coordinator.
python3 client_2pc.py transaction -100 100 0 1Node-2 crashed after responding to the coordinator.
python3 client_2pc.py transaction -100 100 0 2Node-1 crashed after sending out the request and potential solutions to recovery from the crash (Coordinator crashes after sending the prepare messages)
python3 client_2pc.py transaction -100 100 0 3Node-1 crashed after sending out the request and potential solutions to recovery from the crash (Coordinator crashes after sending the commit messages)
python3 client_2pc.py transaction -100 100 0 4✅ Given that our system is totally based on RAFT from the ground up, we see that each node has replicas of its leader nodes.
- Coordinator Node: Manages 2PC protocol
- Cluster A: Handles Account A transactions
- Cluster B: Handles Account B transactions
- Each cluster implements Raft consensus internally
The system supports various crash scenarios:
- Crash before prepare
- Crash before commit
- Coordinator crash after sending prepare
- Coordinator crash after sending commit
node*_lab2Raft.txt- Persistent Raft logsnode*_account.txt- Account balance storagenode*_prepare_log.json- 2PC prepare phase logsnode*_commit_log.json- 2PC commit phase logs
Our implementation can handle the following errors:
- Network failures
- Node crashes
- Transaction failures
- Leader election timeouts