Skip to content

Commit

Permalink
Follow up #16068 (comment)
Browse files Browse the repository at this point in the history
Replace unnecessary Lock()/Unlock()s with RLock()/RUnlock()s

Signed-off-by: caojiamingalan <[email protected]>
  • Loading branch information
CaojiamingAlan committed Jul 15, 2023
1 parent 882edb3 commit bc97a94
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 bc97a94

Please sign in to comment.