kv: use atomic bool to avoid read lock in Replica.IsInitialized#84110
Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom Jul 12, 2022
Merged
kv: use atomic bool to avoid read lock in Replica.IsInitialized#84110craig[bot] merged 1 commit intocockroachdb:masterfrom
craig[bot] merged 1 commit intocockroachdb:masterfrom
Conversation
According to a CPU profile acquired while running TPC-E, the read lock acquired
in `Replica.IsInitialized` is the second most expensive read lock in terms of
CPU cycles consumed. This doesn't mean that it's the most expensive in terms of
blocking, but it still indicates that the lock is frequently acquired.
```
----------------------------------------------------------+-------------
0.29s 43.28% | github.com/cockroachdb/pebble/internal/cache.(*shard).Get github.com/cockroachdb/pebble/internal/cache/external/com_github_cockroachdb_pebble/internal/cache/clockpro.go:117 (inline)
0.05s 7.46% | github.com/cockroachdb/cockroach/pkg/kv/kvserver.(*Replica).IsInitialized github.com/cockroachdb/cockroach/pkg/kv/kvserver/pkg/kv/kvserver/replica_init.go:249 (inline)
...
0.67s 0.39% 24.00% 0.67s 0.39% | sync.(*RWMutex).RLock GOROOT/src/sync/rwmutex.go:61
----------------------------------------------------------+-------------
```
The two main callers of this method are `Store.Send` (on each request) and
`Replica.handleRaftReadyRaftMuLocked` (on each raft ready iteration):
```
----------------------------------------------------------+-------------
0.04s 80.00% | github.com/cockroachdb/cockroach/pkg/kv/kvserver.(*Store).Send github.com/cockroachdb/cockroach/pkg/kv/kvserver/pkg/kv/kvserver/store_send.go:176
0.01s 20.00% | github.com/cockroachdb/cockroach/pkg/kv/kvserver.(*Replica).handleRaftReadyRaftMuLocked github.com/cockroachdb/cockroach/pkg/kv/kvserver/pkg/kv/kvserver/replica_raft.go:829
0.04s 0.023% 0.023% 0.05s 0.029% | github.com/cockroachdb/cockroach/pkg/kv/kvserver.(*Replica).IsInitialized github.com/cockroachdb/cockroach/pkg/kv/kvserver/pkg/kv/kvserver/replica_init.go:251
0.01s 20.00% | sync.(*RWMutex).RUnlock GOROOT/src/sync/rwmutex.go:81
----------------------------------------------------------+-------------
```
This commit makes the method cheaper by replacing the lock with an atomic
read. Once a replica is initialized, it stays initialized, so this state
is well suited to be stored in an atomic value.
Member
knz
approved these changes
Jul 8, 2022
Contributor
Author
|
TFTRs! bors r=knz,erikgrinaker |
Contributor
|
Build succeeded: |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
According to a CPU profile acquired while running TPC-E, the read lock acquired in
Replica.IsInitializedis the second most expensive read lock in terms of CPU cycles consumed. This doesn't mean that it's the most expensive in terms of blocking, but it still indicates that the lock is frequently acquired.The two main callers of this method are
Store.Send(on each request) andReplica.handleRaftReadyRaftMuLocked(on each raft ready iteration):This commit makes the method cheaper by replacing the lock with an atomic read. Once a replica is initialized, it stays initialized, so this state is well suited to be stored in an atomic value.
At a minimum, this offsets the new rlock acquired by #78980.