Skip to content
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

raft: count storage reads in MsgAppResp test #139907

Conversation

hakuuww
Copy link
Contributor

@hakuuww hakuuww commented Jan 27, 2025

count storage reads in MsgAppResp test

Currently, when the raft leader wants to commit an entry(advance commit index), it must check the term of the entry it wants to commit and ensure the term number is the same as the current leader term(term of itself). This may involve a storage access for the term number if the entry has been persisted on stable storage and no longer in the unstable log of the leader node.

This change adds an assertion for this existing storage access for term by adding a scenario that triggers storage access in TestLeaderAppResp() and asserting storage has been called(logged by MemoryStorage.callStats.term).

This is a characterization test that prepares for a following change that will avoid storage access for term in leader commit process.

Jira issue: CRDB-45763
Epic: None

Release note: None

@hakuuww hakuuww requested a review from a team as a code owner January 27, 2025 21:22
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@hakuuww hakuuww requested a review from pav-kv January 27, 2025 21:26
@hakuuww hakuuww changed the title raft: adding a characterization test check for asserting storage acce… raft: characterization test check for asserting storage access when leader commits Jan 27, 2025
@hakuuww hakuuww force-pushed the characterizationTestForLeaderCommitTermStorageAccess branch from 6043734 to b48db84 Compare January 27, 2025 22:10
Copy link
Collaborator

@pav-kv pav-kv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. Some style suggestions, and request to fix comments.

Also, "characterization test" in the commit name seems to be something else (from description, it sounds more like our datadriven tests). Consider using simpler language, e.g. raft: count storage reads in MsgAppResp test.

pkg/raft/raft_test.go Outdated Show resolved Hide resolved
pkg/raft/raft_test.go Outdated Show resolved Hide resolved
pkg/raft/raft_test.go Outdated Show resolved Hide resolved
pkg/raft/raft_test.go Outdated Show resolved Hide resolved
pkg/raft/raft_test.go Outdated Show resolved Hide resolved
pkg/raft/raft_test.go Outdated Show resolved Hide resolved
pkg/raft/raft_test.go Outdated Show resolved Hide resolved
@hakuuww hakuuww force-pushed the characterizationTestForLeaderCommitTermStorageAccess branch from b48db84 to 27d717f Compare January 27, 2025 22:53
@hakuuww hakuuww requested a review from pav-kv January 27, 2025 22:55
@hakuuww
Copy link
Contributor Author

hakuuww commented Jan 27, 2025

amended the commit

Copy link
Collaborator

@pav-kv pav-kv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM % a couple of nits.


// Follower 2 says term2, index5 is already replicated
// The leader responds with the updated commit index to follower 2
// The leader sends entry{term:2, index:6} to follower 2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure the leader sends any entry here, only a MsgApp with an updated commit index. Everything should be already in-flight to this follower 2, so things won't be sent again for a while.

Please double-check and fix the comment.

Copy link
Contributor Author

@hakuuww hakuuww Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I investigated the the behavior of the test. In the test code the leader does send additional entries along with the updated commit index after receiving MsgAppResp. (Although it shouldn't send any)

Leader sends the following after receiving MsgAppResp for follower acking entry{index:4, term:2}

1->2 MsgApp Term:2 Log:2/4 Commit:4 Entries:[ 2/5 EntryNormal "" 2/6 EntryNormal "" ]

This occurs because the inflight object in the leader's tracker was not updated correctly to remember we sent out all messages when we initially broadcasted it. Because this is test code.

I will remove the comments saying we send additional entries after receiving MsgApp from a follower.

pkg/raft/raft_test.go Show resolved Hide resolved
…der commits

Currently, when the raft leader wants to commit an entry(advance commit index), it must check the term of the entry it wants to commit and ensure the term number is the same as the current leader term(term of itself). This may involve a storage access for the term number if the entry has been persisted on stable storage and no longer in the unstable log of the leader node.

This change adds an assertion for this existing storage access for term by adding a scenario that triggers storage access in TestLeaderAppResp() and asserting storage has been called(logged by MemoryStorage.callStats.term)

Jira issue: CRDB-45763
Fixes: cockroachdb#137826

Release note: None
@hakuuww hakuuww force-pushed the characterizationTestForLeaderCommitTermStorageAccess branch from 27d717f to 73fd0a4 Compare January 28, 2025 16:14
@hakuuww hakuuww changed the title raft: characterization test check for asserting storage access when leader commits raft: count storage reads in MsgAppResp test Jan 28, 2025
@hakuuww
Copy link
Contributor Author

hakuuww commented Jan 28, 2025

TYFTR!

bors r=pav-kv

@craig craig bot merged commit 5e1c20f into cockroachdb:master Jan 28, 2025
22 checks passed
craig bot pushed a commit that referenced this pull request Jan 29, 2025
139609: raft: optimize storage access for term when leader tries to commit r=pav-kv a=hakuuww

When the raft leader wants to commit an entry(advance commit index), it must check the term of the entry it wants to commit and ensure the term number is the same as the current leader term(term of itself).
Previously, this process may involve a storage access for the term number if the entry has been persisted on stable storage and no longer in the unstable log of the leader node.

In fact this above scenario happens quite often. Which makes an optimization improving leader commit latency fruitful.

This PR introduces an optimization that utilizes an invariant that is logically equivalent to checking whether the want-to-be-committed entry has the same term as the leader doing the commit.
This is done by keeping an entry index in leader node's memory that signifies the point in time where the current leader became leader. When the leader wants to commit an entry, the leader compares the want-to-be-committed entry's index(available in memory) with the newly added index in this PR to determine whether the term matches.
Thus completely eliminating storage access on this code path.

A corresponding unit test is also modified to account for the elimination of storage access when leader tries to commit.

A previous related PR(merged): #139907
Jira issue: [CRDB-45763](https://cockroachlabs.atlassian.net/browse/CRDB-45763)
Fixes: #137826

Release note (performance improvement): removed a potential storage read from raft commit pipeline. This reduces the worst-case KV write latency.

Co-authored-by: Anthony Xu <[email protected]>
craig bot pushed a commit that referenced this pull request Jan 30, 2025
139609: raft: optimize storage access for term when leader tries to commit r=pav-kv a=hakuuww

When the raft leader wants to commit an entry(advance commit index), it must check the term of the entry it wants to commit and ensure the term number is the same as the current leader term(term of itself).
Previously, this process may involve a storage access for the term number if the entry has been persisted on stable storage and no longer in the unstable log of the leader node.

In fact this above scenario happens quite often. Which makes an optimization improving leader commit latency fruitful.

This PR introduces an optimization that utilizes an invariant that is logically equivalent to checking whether the want-to-be-committed entry has the same term as the leader doing the commit.
This is done by keeping an entry index in leader node's memory that signifies the point in time where the current leader became leader. When the leader wants to commit an entry, the leader compares the want-to-be-committed entry's index(available in memory) with the newly added index in this PR to determine whether the term matches.
Thus completely eliminating storage access on this code path.

A corresponding unit test is also modified to account for the elimination of storage access when leader tries to commit.

A previous related PR(merged): #139907
Jira issue: [CRDB-45763](https://cockroachlabs.atlassian.net/browse/CRDB-45763)
Fixes: #137826

Release note (performance improvement): removed a potential storage read from raft commit pipeline. This reduces the worst-case KV write latency.

Co-authored-by: Anthony Xu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants