Skip to content

Commit

Permalink
Optimize lock data and add unset instruction logic
Browse files Browse the repository at this point in the history
  • Loading branch information
snower committed Apr 21, 2024
1 parent 221d7d9 commit 008ef13
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
5 changes: 4 additions & 1 deletion protocol/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ const (
CALL_COMMAND_CHARSET_UTF8 = 1
)

const LOCK_DATA_COMMAND_TYPE_SET = 0
const (
LOCK_DATA_COMMAND_TYPE_SET = 0
LOCK_DATA_COMMAND_TYPE_UNSET = 1
)

var ERROR_MSG []string = []string{
"OK",
Expand Down
10 changes: 7 additions & 3 deletions server/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2230,12 +2230,12 @@ func (self *LockDB) DoAckLock(lock *Lock, succed bool) {
lock.expriedTime = lock.startTime + int64(lock.command.Expried)/1000 + 1
}

var lockData []byte
var lockData []byte = nil
if lockManager.currentData != nil {
currentData := lockManager.currentData
if currentData.recoverLock == lock {
if currentData.recoverData != nil {
lockData = currentData.recoverData.Data
lockData = currentData.recoverData.GetData()
}
currentData.recoverLock = nil
currentData.recoverData = nil
Expand Down Expand Up @@ -2264,7 +2264,11 @@ func (self *LockDB) DoAckLock(lock *Lock, succed bool) {
if lockManager.currentData != nil {
currentData := lockManager.currentData
if currentData.recoverLock == lock {
lockManager.currentData = currentData.recoverData
if currentData.recoverData == nil {
lockManager.currentData = NewLockDataUnsetData()
} else {
lockManager.currentData = currentData.recoverData
}
currentData.recoverLock = nil
currentData.recoverData = nil
}
Expand Down
25 changes: 20 additions & 5 deletions server/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,15 @@ func (self *LockManager) GetOrNewLock(serverProtocol ServerProtocol, command *pr

func (self *LockManager) GetLockData() []byte {
if self.currentData != nil {
return self.currentData.Data
return self.currentData.GetData()
}
return nil
}

func (self *LockManager) AofLockData(commandType uint8) []byte {
if self.currentData != nil && (commandType == protocol.COMMAND_LOCK || !self.currentData.isAof) {
self.currentData.isAof = true
return self.currentData.Data
return self.currentData.data
}
return nil
}
Expand All @@ -349,6 +349,8 @@ func (self *LockManager) ProcessLockData(command *protocol.LockCommand) {
switch lockCommandData.CommandType {
case protocol.LOCK_DATA_COMMAND_TYPE_SET:
self.currentData = NewLockData(lockCommandData.Data)
case protocol.LOCK_DATA_COMMAND_TYPE_UNSET:
self.currentData = NewLockDataUnsetData()
}
command.Data = nil
}
Expand Down Expand Up @@ -385,14 +387,27 @@ func (self *Lock) GetDB() *LockDB {
}

type LockData struct {
Data []byte
recoverData *LockData
data []byte
recoverLock *Lock
recoverData *LockData
commandType uint8
isAof bool
}

func NewLockData(data []byte) *LockData {
return &LockData{data, nil, nil, false}
return &LockData{data, nil, nil, data[4], false}
}

func NewLockDataUnsetData() *LockData {
return &LockData{[]byte{2, 0, 0, 0, protocol.LOCK_DATA_COMMAND_TYPE_UNSET, 0}, nil, nil,
protocol.LOCK_DATA_COMMAND_TYPE_UNSET, false}
}

func (self *LockData) GetData() []byte {
if self.data != nil && self.commandType != protocol.LOCK_DATA_COMMAND_TYPE_UNSET {
return self.data
}
return nil
}

type PriorityMutex struct {
Expand Down

0 comments on commit 008ef13

Please sign in to comment.