Skip to content

Commit

Permalink
Merge pull request #16248 from CaojiamingAlan/replace_lock_with_rlock
Browse files Browse the repository at this point in the history
Replace unnecessary Lock()/Unlock()s with RLock()/RUnlock()s
  • Loading branch information
serathius committed Jul 27, 2023
2 parents af07f18 + bc97a94 commit 424ced9
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
5 changes: 5 additions & 0 deletions server/storage/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/storage/mvcc/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,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()
scheduledCompact, scheduledCompactFound := UnsafeReadScheduledCompact(tx)
finishedCompact, finishedCompactFound := UnsafeReadFinishedCompact(tx)
return scheduledCompact == finishedCompact && scheduledCompactFound == finishedCompactFound
Expand Down Expand Up @@ -328,7 +328,7 @@ func (s *store) restore() error {

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

finishedCompact, found := UnsafeReadFinishedCompact(tx)
if found {
Expand Down Expand Up @@ -384,7 +384,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 @@ -397,7 +397,7 @@ func (s *store) restore() error {
}
}

tx.Unlock()
tx.RUnlock()

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

Expand Down
4 changes: 2 additions & 2 deletions server/storage/schema/alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (s *alarmBackend) mustUnsafeDeleteAlarm(tx backend.BatchTx, alarm *etcdserv

func (s *alarmBackend) GetAllAlarms() ([]*etcdserverpb.AlarmMember, error) {
tx := s.be.ReadTx()
tx.Lock()
defer tx.Unlock()
tx.RLock()
defer tx.RUnlock()
return s.unsafeGetAllAlarms(tx)
}

Expand Down
4 changes: 2 additions & 2 deletions server/storage/schema/membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ func (s *membershipBackend) ClusterVersionFromBackend() *semver.Version {
func (s *membershipBackend) DowngradeInfoFromBackend() *version.DowngradeInfo {
dkey := ClusterDowngradeKeyName
tx := s.be.ReadTx()
tx.Lock()
defer tx.Unlock()
tx.RLock()
defer tx.RUnlock()
keys, vals := tx.UnsafeRange(Cluster, dkey, nil, 0)
if len(keys) == 0 {
return nil
Expand Down
4 changes: 2 additions & 2 deletions server/storage/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (

// Validate checks provided backend to confirm that schema used is supported.
func Validate(lg *zap.Logger, tx backend.ReadTx) error {
tx.Lock()
defer tx.Unlock()
tx.RLock()
defer tx.RUnlock()
return unsafeValidate(lg, tx)
}

Expand Down
4 changes: 2 additions & 2 deletions server/storage/schema/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
// ReadStorageVersion loads storage version from given backend transaction.
// Populated since v3.6
func ReadStorageVersion(tx backend.ReadTx) *semver.Version {
tx.Lock()
defer tx.Unlock()
tx.RLock()
defer tx.RUnlock()
return UnsafeReadStorageVersion(tx)
}

Expand Down

0 comments on commit 424ced9

Please sign in to comment.