-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Raft State Management for Load Balancers (#641)
* feat(raft): add round-robin state management - Implemented round-robin state to enhance leader election and task distribution. - Updated Raft state machine to incorporate round-robin logic. - Added tests to ensure correct round-robin behavior in various scenarios. This change improves the efficiency and fairness of task handling within the Raft cluster. * Fix race condition in server test by adding mutex for proxy state checks - Introduced a mutex (`proxyStateMutex`) to synchronize access to proxy state checks in `testProxy` function. - Modified the `testProxy` function to lock the mutex before checking the state of `AvailableConnections` and `busyConnections`. - Updated the test logic to ensure that one of the proxies is in the expected state, preventing race conditions where the second goroutine could access the connection state prematurely. - Removed the `proxy` parameter from `testProxy` function calls as it is no longer needed. This change addresses a race condition that could cause the second goroutine to access connection states before they are properly synchronized, ensuring reliable test results. * feat: Add weighted round-robin state to Raft Add support for storing weighted round-robin load balancer state in Raft FSM to ensure consistency across cluster nodes. Changes include: - Add WeightedProxy and WeightedRRPayload structs for state management - Store proxy weights in Raft FSM using weightedRRStates map - Update WeightedRoundRobin to use Raft for weight tracking - Add new CommandUpdateWeightedRR command type - Remove local weight tracking in favor of distributed state This change ensures that proxy weights remain consistent across cluster nodes during failover and leader changes. * perf(network): optimize weighted round-robin with batch updates Improve performance of WeightedRoundRobin.NextProxy by reducing Raft operations: - Replace multiple individual Raft updates with a single batch operation - Introduce new CommandUpdateWeightedRRBatch command type - Collect all proxy weight updates in memory before applying - Reduce number of Raft.Apply calls from N+1 to 1 (where N is number of proxies) This change significantly reduces the number of Raft consensus operations needed for weight updates in the weighted round-robin load balancer. * fix: improve error handling and code readability in network package - Add JSON marshaling error handling in WeightedRoundRobin - Simplify proxy state validation logic in server tests - Clean up test formatting The main changes improve error propagation and code clarity by: - Properly handling JSON marshaling errors in NextProxy method - Refactoring conditional logic in server tests to be more readable - Removing unnecessary empty lines in test files * feat: Add weighted round-robin state to FSM snapshots Add support for persisting and restoring weighted round-robin load balancer states in the Raft FSM snapshots. This ensures the weighted round-robin configuration survives cluster restarts and leader changes. Changes: - Add weightedRRStates to FSMSnapshot struct - Update Snapshot() to copy weighted round-robin states - Extend Restore() and Persist() to handle weighted round-robin data * Add grpcAddress field to raft peer configuration Add commented example of grpcAddress field in peer configuration, which specifies the gRPC endpoint for raft peer communication. * feat(raft): Add comprehensive test coverage for FSM operations Add test cases covering: - Weighted round-robin operations (single and batch updates) - Round-robin index management - Invalid command handling - FSM snapshot restoration - Node shutdown scenarios * refactor: introduce dedicated ApplyTimeout constant for Raft operations Replace usage of LeaderElectionTimeout with a new dedicated ApplyTimeout constant (2 seconds) for Raft command applications across different load balancing strategies (ConsistentHash, RoundRobin, WeightedRoundRobin). This change provides better separation of concerns by using a more appropriate timeout value for command applications rather than reusing the leader election timeout.
- Loading branch information
1 parent
32c80f7
commit 86ec724
Showing
11 changed files
with
626 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,3 +115,4 @@ raft: | |
peers: [] | ||
# - id: node2 | ||
# address: 127.0.0.1:2223 | ||
# grpcAddress: 127.0.0.1:50052 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.