Skip to content

Commit

Permalink
Optimize multiple lock queues and wait queues in delayed initializati…
Browse files Browse the repository at this point in the history
…on LockManager to save memory usage
  • Loading branch information
snower committed May 20, 2024
1 parent 2872514 commit 2b1ce01
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
16 changes: 7 additions & 9 deletions server/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,9 +1014,9 @@ func (self *LockDB) initNewLockManager(dbId uint8) {

lockManagers[i].lockDb = self
lockManagers[i].dbId = dbId
lockManagers[i].locks = NewLockQueue(4, 16, 4)
lockManagers[i].lockMaps = make(map[[16]byte]*Lock, 8)
lockManagers[i].waitLocks = NewLockQueue(4, 32, 4)
lockManagers[i].locks = nil
lockManagers[i].lockMaps = nil
lockManagers[i].waitLocks = nil
lockManagers[i].glock = self.managerGlocks[self.managerGlockIndex]
lockManagers[i].glockIndex = self.managerGlockIndex
lockManagers[i].freeLocks = self.freeLocks[self.managerGlockIndex]
Expand Down Expand Up @@ -1133,9 +1133,8 @@ func (self *LockDB) RemoveLockManager(lockManager *LockManager) {
if self.freeLockManagers[freeLockManagerHead] == nil {
self.freeLockManagers[freeLockManagerHead] = lockManager

if lockManager.locks != nil {
_ = lockManager.locks.Rellac()
}
lockManager.locks = nil
lockManager.lockMaps = nil
if lockManager.waitLocks != nil {
_ = lockManager.waitLocks.Rellac()
}
Expand Down Expand Up @@ -1176,9 +1175,8 @@ func (self *LockDB) RemoveLockManager(lockManager *LockManager) {
if self.freeLockManagers[freeLockManagerHead] == nil {
self.freeLockManagers[freeLockManagerHead] = lockManager

if lockManager.locks != nil {
_ = lockManager.locks.Rellac()
}
lockManager.locks = nil
lockManager.lockMaps = nil
if lockManager.waitLocks != nil {
_ = lockManager.waitLocks.Rellac()
}
Expand Down
22 changes: 19 additions & 3 deletions server/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type LockManager struct {
}

func NewLockManager(lockDb *LockDB, command *protocol.LockCommand, glock *PriorityMutex, glockIndex uint16, freeLocks *LockQueue, state *protocol.LockDBState) *LockManager {
return &LockManager{lockDb, command.LockKey,
nil, nil, nil, nil, nil, glock, freeLocks, nil, state, 0, 0,
return &LockManager{lockDb, command.LockKey, nil, nil, nil, nil,
nil, glock, freeLocks, nil, state, 0, 0,
glockIndex, command.DbId, false}
}

Expand Down Expand Up @@ -85,6 +85,10 @@ func (self *LockManager) AddLock(lock *Lock) *Lock {
if self.currentLock == nil {
self.currentLock = lock
} else {
if self.locks == nil {
self.locks = NewLockQueue(4, 16, 4)
self.lockMaps = make(map[[16]byte]*Lock, 8)
}
_ = self.locks.Push(lock)
self.lockMaps[lock.command.LockId] = lock
}
Expand All @@ -98,6 +102,9 @@ func (self *LockManager) RemoveLock(lock *Lock) *Lock {
if self.currentLock == lock {
self.currentLock = nil
lock.refCount--
if self.locks == nil {
return lock
}

lockedLock := self.locks.Pop()
for lockedLock != nil {
Expand All @@ -120,6 +127,9 @@ func (self *LockManager) RemoveLock(lock *Lock) *Lock {
return lock
}

if self.locks == nil {
return lock
}
delete(self.lockMaps, lock.command.LockId)
lockedLock := self.locks.Head()
for lockedLock != nil {
Expand Down Expand Up @@ -217,7 +227,7 @@ func (self *LockManager) UpdateLockedLock(lock *Lock, command *protocol.LockComm
}
}

if !lock.isAof && self.currentLock == lock && self.locks.Head() == nil {
if !lock.isAof && self.currentLock == lock && (self.locks == nil || self.locks.Head() == nil) {
switch command.ExpriedFlag & 0x1300 {
case protocol.EXPRIED_FLAG_ZEOR_AOF_TIME:
lock.aofTime = 0
Expand All @@ -233,13 +243,19 @@ func (self *LockManager) UpdateLockedLock(lock *Lock, command *protocol.LockComm
}

func (self *LockManager) AddWaitLock(lock *Lock) *Lock {
if self.waitLocks == nil {
self.waitLocks = NewLockQueue(4, 32, 4)
}
_ = self.waitLocks.Push(lock)
lock.refCount++
self.waited = true
return lock
}

func (self *LockManager) GetWaitLock() *Lock {
if self.waitLocks == nil {
return nil
}
lock := self.waitLocks.Head()
for lock != nil {
if lock.timeouted {
Expand Down

0 comments on commit 2b1ce01

Please sign in to comment.