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

[3.5]Replace unnecessary Lock()/Unlock()s with RLock()/RUnlock()s #16297

Open
wants to merge 1 commit into
base: release-3.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions server/auth/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ func (as *authStore) CheckPassword(username, password string) (uint64, error) {
// to avoid putting it in the critical section of the tx lock.
revision, err := func() (uint64, error) {
tx := as.be.ReadTx()
tx.Lock()
defer tx.Unlock()
tx.RLock()
defer tx.RUnlock()

user = getUser(as.lg, tx, username)
if user == nil {
Expand Down Expand Up @@ -361,7 +361,7 @@ func (as *authStore) Recover(be backend.Backend) {
enabled := false
as.be = be
tx := be.ReadTx()
tx.Lock()
tx.RLock()
_, vs := tx.UnsafeRange(buckets.Auth, enableFlagKey, nil, 0)
if len(vs) == 1 {
if bytes.Equal(vs[0], authEnabled) {
Expand All @@ -372,7 +372,7 @@ func (as *authStore) Recover(be backend.Backend) {
as.setRevision(getRevision(tx))
as.refreshRangePermCache(tx)

tx.Unlock()
tx.RUnlock()

as.enabledMu.Lock()
as.enabled = enabled
Expand Down Expand Up @@ -858,8 +858,8 @@ func (as *authStore) isOpPermitted(userName string, revision uint64, key, rangeE
}

tx := as.be.ReadTx()
tx.Lock()
defer tx.Unlock()
tx.RLock()
defer tx.RUnlock()

user := getUser(as.lg, tx, userName)
if user == nil {
Expand Down Expand Up @@ -900,9 +900,9 @@ func (as *authStore) IsAdminPermitted(authInfo *AuthInfo) error {
}

tx := as.be.ReadTx()
tx.Lock()
tx.RLock()
u := getUser(as.lg, tx, authInfo.Username)
tx.Unlock()
tx.RUnlock()

if u == nil {
return ErrUserNotFound
Expand Down
4 changes: 2 additions & 2 deletions server/etcdserver/api/membership/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,8 @@ func clusterVersionFromBackend(lg *zap.Logger, be backend.Backend) *semver.Versi
func downgradeInfoFromBackend(lg *zap.Logger, be backend.Backend) *DowngradeInfo {
dkey := backendDowngradeKey()
tx := be.ReadTx()
tx.Lock()
defer tx.Unlock()
tx.RLock()
defer tx.RUnlock()
keys, vals := tx.UnsafeRange(buckets.Cluster, dkey, nil, 0)
if len(keys) == 0 {
return nil
Expand Down
4 changes: 2 additions & 2 deletions server/etcdserver/cindex/cindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ func unsafeReadConsistentIndex(tx backend.ReadTx) (uint64, uint64) {
// ReadConsistentIndex loads consistent index and term from given transaction.
// returns 0 if the data are not found.
func ReadConsistentIndex(tx backend.ReadTx) (uint64, uint64) {
tx.Lock()
defer tx.Unlock()
tx.RLock()
defer tx.RUnlock()
return unsafeReadConsistentIndex(tx)
}

Expand Down
5 changes: 5 additions & 0 deletions server/mvcc/backend/read_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ import (
// is known to never overwrite any key so range is safe.

type ReadTx interface {
// Lock and Unlock should only be used in the following three cases:
// 1. When committing a transaction. Because it will reset the read tx, including the buffer;
// 2. When writing the pending data back to read buf, because obviously no reading is allowed when writing the read buffer;
// 3. When performing defragmentation, because again it will reset the read tx, including the read buffer.
Lock()
Unlock()
// RLock and RUnlock should be used for all other cases.
RLock()
RUnlock()

Expand Down
10 changes: 5 additions & 5 deletions server/mvcc/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ func (s *store) updateCompactRev(rev int64) (<-chan struct{}, int64, error) {
// checkPrevCompactionCompleted checks whether the previous scheduled compaction is completed.
func (s *store) checkPrevCompactionCompleted() bool {
tx := s.b.ReadTx()
tx.Lock()
defer tx.Unlock()
tx.RLock()
defer tx.RUnlock()
_, scheduledCompactBytes := tx.UnsafeRange(buckets.Meta, scheduledCompactKeyName, nil, 0)
scheduledCompact := int64(0)
if len(scheduledCompactBytes) != 0 {
Expand Down Expand Up @@ -341,7 +341,7 @@ func (s *store) restore() error {

// restore index
tx := s.b.ReadTx()
tx.Lock()
tx.RLock()

_, finishedCompactBytes := tx.UnsafeRange(buckets.Meta, finishedCompactKeyName, nil, 0)
if len(finishedCompactBytes) != 0 {
Expand Down Expand Up @@ -403,7 +403,7 @@ func (s *store) restore() error {

for key, lid := range keyToLease {
if s.le == nil {
tx.Unlock()
tx.RUnlock()
panic("no lessor to attach lease")
}
err := s.le.Attach(lid, []lease.LeaseItem{{Key: key}})
Expand All @@ -416,7 +416,7 @@ func (s *store) restore() error {
}
}

tx.Unlock()
tx.RUnlock()

s.lg.Info("kvstore restored", zap.Int64("current-rev", s.currentRev))

Expand Down