-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add support for custom election tick #9725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LakshimiRam-073
wants to merge
6
commits into
dgraph-io:main
Choose a base branch
from
LakshimiRam-073:feature/custom-election-tick
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
377228d
Add support for custom election tick
LakshimiRam-073 40ac0d3
Merge branch 'main' into feature/custom-election-tick
LakshimiRam-073 ee4d6ff
fix: add issues:write permission to labeler workflow
LakshimiRam-073 f26e195
Fail fast with a clear error message instead of letting etcd raft
LakshimiRam-073 6542e18
fix(raft): warn on negative and low election-tick values
LakshimiRam-073 7071d3b
revert: labeler workflow changes
LakshimiRam-073 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -40,6 +40,34 @@ var ( | |
| ErrNoNode = errors.Errorf("No node has been set up yet") | ||
| ) | ||
|
|
||
| const ( | ||
| heartbeatTick = 1 | ||
| defaultElectionTick = 20 | ||
| recommendedElectionTick = 10 | ||
| ) | ||
|
|
||
| func normalizeElectionTick(electionTick int) (tick int, warning string) { | ||
| if electionTick < 0 { | ||
| return defaultElectionTick, fmt.Sprintf( | ||
| "--raft election-tick=%d is invalid; defaulting to %d. Use 0 or omit the flag to accept the default.", | ||
| electionTick, defaultElectionTick) | ||
| } | ||
| if electionTick == 0 { | ||
| return defaultElectionTick, "" | ||
| } | ||
| if electionTick <= heartbeatTick { | ||
| glog.Fatalf("invalid --raft election-tick=%d: must be greater than internal heartbeat tick (%d).", | ||
| electionTick, heartbeatTick) | ||
| } | ||
| if electionTick < recommendedElectionTick { | ||
| return electionTick, fmt.Sprintf( | ||
| "--raft election-tick=%d gives a %dms minimum election timeout. Values below %d (1s) "+ | ||
| "may cause spurious leader elections under GC pauses or network jitter.", | ||
| electionTick, electionTick*100, recommendedElectionTick) | ||
| } | ||
| return electionTick, "" | ||
| } | ||
|
|
||
| // Node represents a node participating in the RAFT protocol. | ||
| type Node struct { | ||
| x.SafeMutex | ||
|
|
@@ -79,7 +107,18 @@ type Node struct { | |
| } | ||
|
|
||
| // NewNode returns a new Node instance. | ||
| func NewNode(rc *pb.RaftContext, store *raftwal.DiskStorage, tlsConfig *tls.Config) *Node { | ||
| // electionTick controls how many Raft Tick() calls pass before election timeout. | ||
| // In production Alpha/Zero, Tick() runs every 100ms and Raft randomizes the timeout. | ||
| // If electionTick <= 0, defaults to 20. | ||
| func NewNode(rc *pb.RaftContext, store *raftwal.DiskStorage, tlsConfig *tls.Config, | ||
| electionTick int) *Node { | ||
|
|
||
| var warning string | ||
| electionTick, warning = normalizeElectionTick(electionTick) | ||
| if warning != "" { | ||
| glog.Warningf(warning) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The formatted version is not needed (and should be failing govet pass)... please just use |
||
| } | ||
|
|
||
| snap, err := store.Snapshot() | ||
| x.Check(err) | ||
|
|
||
|
|
@@ -90,8 +129,8 @@ func NewNode(rc *pb.RaftContext, store *raftwal.DiskStorage, tlsConfig *tls.Conf | |
| Store: store, | ||
| Cfg: &raft.Config{ | ||
| ID: rc.Id, | ||
| ElectionTick: 20, // 2s if we call Tick() every 100 ms. | ||
| HeartbeatTick: 1, // 100ms if we call Tick() every 100 ms. | ||
| ElectionTick: electionTick, | ||
| HeartbeatTick: heartbeatTick, // 100ms if we call Tick() every 100 ms. | ||
| Storage: store, | ||
| MaxInflightMsgs: 256, | ||
| MaxSizePerMsg: 256 << 10, // 256 KB should allow more batching. | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
election-tick=1handling innormalizeElectionTickis inconsistent with how the function treats every other invalid value, and I'd pull it into line.Right now a negative tick coerces to the default
20and logs a warning, butelectionTick == 1callsglog.Fatalfand takes the process down. Those are both equally-invalid inputs, so it's odd that-100gets a friendly default while a single fat-fingered1is fatal. An operator who typoselection-tick=1shouldn't lose the node over it.The
Fatalfalso blocks the test. The table covers0, -1, -100, 2, 10, 20, 100but skips1, and it has to: exercising that path wouldos.Exitthe test binary. So the one genuinely-fatal branch is the one branch with no coverage.The intent behind it is right. Failing with a clear message beats letting etcd/raft panic with something cryptic. But coercing
1to the default and warning, exactly like the negative case, gets you that clear message while keeping the behavior consistent, fully unit-testable, and crash-free on a typo.Suggest dropping the
glog.Fatalfbranch and folding1into the same coerce-and-warn path as the negatives.