feat(meta): Enhance the allocation and validation of txId to avoid potential duplicates.

Signed-off-by: Victor1319 <834863182@qq.com>
This commit is contained in:
Victor1319 2024-02-04 16:27:45 +08:00 committed by longerfly
parent 5076a34844
commit 152dca0fa2
2 changed files with 18 additions and 4 deletions

View File

@ -666,6 +666,11 @@ func (tm *TransactionManager) registerTransaction(txInfo *proto.TransactionInfo)
dentry.SetTimeout(txInfo.Timeout)
dentry.SetTxId(txInfo.TxID)
}
if info := tm.getTransaction(txInfo.TxID); info != nil {
log.LogWarnf("tx is already exist, txId %s, info %v", txInfo.TxID, info.String())
return fmt.Errorf("tx is already exist, txId %s", txInfo.TxID)
}
}
if info := tm.getTransaction(txInfo.TxID); info != nil {

View File

@ -32,21 +32,30 @@ func newTxIDAllocator() (alloc *TxIDAllocator) {
}
func (alloc *TxIDAllocator) Reset() {
alloc.txIDLock.Lock()
defer alloc.txIDLock.Unlock()
atomic.StoreUint64(&alloc.mpTxID, 0)
}
func (alloc *TxIDAllocator) setTransactionID(id uint64) {
alloc.txIDLock.Lock()
defer alloc.txIDLock.Unlock()
atomic.StoreUint64(&alloc.mpTxID, id)
}
func (alloc *TxIDAllocator) getTransactionID() uint64 {
return atomic.LoadUint64(&alloc.mpTxID)
alloc.txIDLock.RLock()
defer alloc.txIDLock.RUnlock()
return alloc.mpTxID
}
func (alloc *TxIDAllocator) allocateTransactionID() (mpTxID uint64) {
alloc.txIDLock.Lock()
defer alloc.txIDLock.Unlock()
mpTxID = atomic.LoadUint64(&alloc.mpTxID) + 1
alloc.setTransactionID(mpTxID)
return
alloc.mpTxID++
return alloc.mpTxID
}