Committer is a Go implementation of the Two-Phase Commit (2PC) and Three-Phase Commit (3PC) protocols for distributed systems.
The system consists of two types of nodes: Coordinator and Followers. The Coordinator is responsible for initiating and managing the commit protocols (2PC or 3PC), while the Followers (or cohorts) participate in the protocol by responding to the coordinator's requests. The communication between nodes is handled using gRPC, and the state of each node is managed using a state machine.
- 2PC and 3PC support: Implements two widely used consensus protocols for distributed transactions.
- Persistence: Uses BadgerDB and WAL for reliable data storage and transaction logs.
- Configurable: All options can be specified using command-line flags.
- Flexible Hook System: Extensible hook system for custom validation, metrics, auditing, and business logic without code changes.
- Built-in Hooks: Ready-to-use hooks for metrics collection, validation, and audit logging.
- gRPC-based communication: Efficient inter-node communication using gRPC.
All configuration parameters can be set using command-line flags:
Flag | Description | Default | Example |
---|---|---|---|
role |
Node role: coordinator or follower |
follower |
-role=coordinator |
nodeaddr |
Address of the current node | localhost:3050 |
-nodeaddr=localhost:3051 |
coordinator |
Coordinator address (required for followers) | "" |
-coordinator=localhost:3050 |
committype |
Commit protocol: two-phase or three-phase |
three-phase |
-committype=two-phase |
timeout |
Timeout (ms) for unacknowledged messages (3PC only) | 1000 |
-timeout=500 |
dbpath |
Path to the BadgerDB database on the filesystem | ./badger |
-dbpath=/tmp/badger |
followers |
Comma-separated list of follower addresses | "" |
-followers=localhost:3052,3053 |
whitelist |
Comma-separated list of allowed hosts | 127.0.0.1 |
-whitelist=192.168.0.1,192.168.0.2 |
./committer -role=follower -nodeaddr=localhost:3001 -committype=three-phase -timeout=1000 -dbpath=/tmp/badger/follower
./committer -role=coordinator -nodeaddr=localhost:3000 -followers=localhost:3001 -committype=three-phase -timeout=1000 -dbpath=/tmp/badger/coordinator
The hooks system allows you to add custom validation and business logic during the Propose and Commit stages without modifying the core code. Hooks are executed in the order they were registered, and if any hook returns false
, the operation is rejected.
// Default usage (with built-in logging)
committer := commitalgo.NewCommitter(database, "3pc", wal, timeout)
// With custom hooks
metricsHook := hooks.NewMetricsHook()
validationHook := hooks.NewValidationHook(100, 1024)
auditHook := hooks.NewAuditHook("audit.log")
committer := commitalgo.NewCommitter(database, "3pc", wal, timeout,
metricsHook,
validationHook,
auditHook,
)
// Dynamic registration
committer.RegisterHook(myCustomHook)
make tests
- Compile executables:
make prepare
- Run the coordinator:
make run-example-coordinator
- Run a follower in another terminal:
make run-example-follower
- Start the example client:
go run ./examples/client/client.go
Contributions are welcome! Feel free to submit a PR or open an issue if you find bugs or have suggestions for improvement.
This project is licensed under the Apache License.