Fix leader rotation v2 implementation bug #4987
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes a critical bug in the leader rotation v2 implementation where the v2 logic was never actually being executed, even when the feature was enabled.
The issue was in the thread-safe wrapper for the decider. When NthNextValidatorV2 was called, it was incorrectly delegating to NthNextValidator instead of NthNextValidatorV2. This meant that even when the LeaderRotationV2Epoch was configured and the code path checked for v2, it would still use the v1 implementation with all its known corner cases and bugs.
The fix ensures that NthNextValidatorV2 properly calls the v2 implementation, which includes important improvements over v1 such as preventing infinite loops, proper index wrapping, and correct validator selection. This bug would have prevented v2 from being activated even after setting the activation epoch.
Additionally, this PR improves code readability by fixing variable shadowing in the rotateLeader function, where the inner loop variable was renamed from i to j to make the code clearer and less confusing.