Skip to content

Commit

Permalink
optimize: skip check lock (#101)
Browse files Browse the repository at this point in the history
Co-authored-by: scott lewis <33612882+dk-lockdown@users.noreply.github.com>
  • Loading branch information
xjlgod and dk-lockdown committed Jan 28, 2022
1 parent 53db855 commit 7c56270
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 53 deletions.
14 changes: 13 additions & 1 deletion pkg/tc/lock/lock_manager.go
@@ -1,6 +1,8 @@
package lock

import (
"encoding/json"

"github.com/opentrx/seata-golang/v2/pkg/apis"
"github.com/opentrx/seata-golang/v2/pkg/tc/model"
"github.com/opentrx/seata-golang/v2/pkg/tc/storage"
Expand Down Expand Up @@ -30,7 +32,17 @@ func (locker *LockManager) AcquireLock(branchSession *apis.BranchSession) bool {
return true
}

return locker.manager.AcquireLock(locks)
applicationData := branchSession.ApplicationData
if applicationData != nil && branchSession.Type == apis.AT {
applicationDataMap := make(map[string]bool)
err := json.Unmarshal(applicationData, &applicationDataMap)
if err != nil {
return locker.manager.AcquireLock(locks, false)
}
skipCheckLock := applicationDataMap["skipCheckLock"]
return locker.manager.AcquireLock(locks, skipCheckLock)
}
return locker.manager.AcquireLock(locks, false)
}

func (locker *LockManager) ReleaseLock(branchSession *apis.BranchSession) bool {
Expand Down
2 changes: 1 addition & 1 deletion pkg/tc/storage/driver/inmemory/inmemory.go
Expand Up @@ -199,7 +199,7 @@ func (driver *driver) RemoveBranchSession(globalSession *apis.GlobalSession, ses
}

// AcquireLock Acquire lock boolean.
func (driver *driver) AcquireLock(rowLocks []*apis.RowLock) bool {
func (driver *driver) AcquireLock(rowLocks []*apis.RowLock, skipCheckLock bool) bool {
if rowLocks == nil {
return true
}
Expand Down
55 changes: 30 additions & 25 deletions pkg/tc/storage/driver/mysql/mysql.go
Expand Up @@ -409,7 +409,7 @@ func (driver *driver) RemoveBranchSession(globalSession *apis.GlobalSession, ses
}

// AcquireLock acquires row locks.
func (driver *driver) AcquireLock(rowLocks []*apis.RowLock) bool {
func (driver *driver) AcquireLock(rowLocks []*apis.RowLock, skipCheckLock bool) bool {
locks, rowKeyArgs := distinctByKey(rowLocks)
var existedRowLocks []*apis.RowLock
whereCond := fmt.Sprintf("row_key in %s", sql.MysqlAppendInParam(len(rowKeyArgs)))
Expand All @@ -418,36 +418,41 @@ func (driver *driver) AcquireLock(rowLocks []*apis.RowLock) bool {
log.Errorf(err.Error())
}

currentXID := locks[0].XID
canLock := true
existedRowKeys := make([]string, 0)
unrepeatedLocks := make([]*apis.RowLock, 0)
for _, rowLock := range existedRowLocks {
if rowLock.XID != currentXID {
log.Infof("row lock [%s] on %s:%s is holding by xid {%s} branchID {%d}", rowLock.RowKey, driver.lockTable, rowLock.TableName,
rowLock.PK, rowLock.XID, rowLock.BranchID)
canLock = false
break
var unrepeatedLocks []*apis.RowLock
if !skipCheckLock {
currentXID := locks[0].XID
canLock := true
existedRowKeys := make([]string, 0)
unrepeatedLocks = make([]*apis.RowLock, 0)
for _, rowLock := range existedRowLocks {
if rowLock.XID != currentXID {
log.Infof("row lock [%s] on %s:%s is holding by xid {%s} branchID {%d}", rowLock.RowKey, driver.lockTable, rowLock.TableName,
rowLock.PK, rowLock.XID, rowLock.BranchID)
canLock = false
break
}
existedRowKeys = append(existedRowKeys, rowLock.RowKey)
}
existedRowKeys = append(existedRowKeys, rowLock.RowKey)
}
if !canLock {
return false
}
if len(existedRowKeys) > 0 {
for _, lock := range locks {
if !contains(existedRowKeys, lock.RowKey) {
unrepeatedLocks = append(unrepeatedLocks, lock)
if !canLock {
return false
}
if len(existedRowKeys) > 0 {
for _, lock := range locks {
if !contains(existedRowKeys, lock.RowKey) {
unrepeatedLocks = append(unrepeatedLocks, lock)
}
}
} else {
unrepeatedLocks = locks
}
if len(unrepeatedLocks) == 0 {
return true
}
} else {
unrepeatedLocks = locks
}

if len(unrepeatedLocks) == 0 {
return true
if unrepeatedLocks == nil {
unrepeatedLocks = rowLocks
}

var (
sb strings.Builder
args []interface{}
Expand Down
55 changes: 30 additions & 25 deletions pkg/tc/storage/driver/pgsql/pgsql.go
Expand Up @@ -408,7 +408,7 @@ func (driver *driver) RemoveBranchSession(globalSession *apis.GlobalSession, ses
}

// AcquireLock acquires row locks.
func (driver *driver) AcquireLock(rowLocks []*apis.RowLock) bool {
func (driver *driver) AcquireLock(rowLocks []*apis.RowLock, skipCheckLock bool) bool {
locks, rowKeyArgs := distinctByKey(rowLocks)
var existedRowLocks []*apis.RowLock
whereCond := fmt.Sprintf("row_key in %s", sql.PgsqlAppendInParam(len(rowKeyArgs)))
Expand All @@ -417,36 +417,41 @@ func (driver *driver) AcquireLock(rowLocks []*apis.RowLock) bool {
log.Errorf(err.Error())
}

currentXID := locks[0].XID
canLock := true
existedRowKeys := make([]string, 0)
unrepeatedLocks := make([]*apis.RowLock, 0)
for _, rowLock := range existedRowLocks {
if rowLock.XID != currentXID {
log.Infof("row lock [%s] on %s:%s is holding by xid {%s} branchID {%d}", rowLock.RowKey, driver.lockTable, rowLock.TableName,
rowLock.PK, rowLock.XID, rowLock.BranchID)
canLock = false
break
var unrepeatedLocks []*apis.RowLock
if !skipCheckLock {
currentXID := locks[0].XID
canLock := true
existedRowKeys := make([]string, 0)
unrepeatedLocks = make([]*apis.RowLock, 0)
for _, rowLock := range existedRowLocks {
if rowLock.XID != currentXID {
log.Infof("row lock [%s] on %s:%s is holding by xid {%s} branchID {%d}", rowLock.RowKey, driver.lockTable, rowLock.TableName,
rowLock.PK, rowLock.XID, rowLock.BranchID)
canLock = false
break
}
existedRowKeys = append(existedRowKeys, rowLock.RowKey)
}
existedRowKeys = append(existedRowKeys, rowLock.RowKey)
}
if !canLock {
return false
}
if len(existedRowKeys) > 0 {
for _, lock := range locks {
if !contains(existedRowKeys, lock.RowKey) {
unrepeatedLocks = append(unrepeatedLocks, lock)
if !canLock {
return false
}
if len(existedRowKeys) > 0 {
for _, lock := range locks {
if !contains(existedRowKeys, lock.RowKey) {
unrepeatedLocks = append(unrepeatedLocks, lock)
}
}
} else {
unrepeatedLocks = locks
}
if len(unrepeatedLocks) == 0 {
return true
}
} else {
unrepeatedLocks = locks
}

if len(unrepeatedLocks) == 0 {
return true
if unrepeatedLocks == nil {
unrepeatedLocks = rowLocks
}

var (
sb strings.Builder
args []interface{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/tc/storage/storagedriver.go
Expand Up @@ -48,7 +48,7 @@ type SessionManager interface {

type LockManager interface {
// AcquireLock Acquire lock boolean.
AcquireLock(rowLocks []*apis.RowLock) bool
AcquireLock(rowLocks []*apis.RowLock, skipCheckLock bool) bool

// ReleaseLock Unlock boolean.
ReleaseLock(rowLocks []*apis.RowLock) bool
Expand Down

0 comments on commit 7c56270

Please sign in to comment.