Merge pull request #16248 from CaojiamingAlan/replace_lock_with_rlock

Replace unnecessary Lock()/Unlock()s with RLock()/RUnlock()s
This commit is contained in:
Marek Siarkowicz 2023-07-27 12:15:46 +02:00 committed by GitHub
commit 424ced9ff3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 13 deletions

View File

@ -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()

View File

@ -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
@ -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 {
@ -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}})
@ -397,7 +397,7 @@ func (s *store) restore() error {
}
}
tx.Unlock()
tx.RUnlock()
s.lg.Info("kvstore restored", zap.Int64("current-rev", s.currentRev))

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}

View File

@ -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)
}