diff --git a/metanode/transaction.go b/metanode/transaction.go index 9adf86828..aed890433 100644 --- a/metanode/transaction.go +++ b/metanode/transaction.go @@ -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 { diff --git a/metanode/txid_allocator.go b/metanode/txid_allocator.go index 6916625c2..a64b0cdc9 100644 --- a/metanode/txid_allocator.go +++ b/metanode/txid_allocator.go @@ -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 }