vmware-archive / go-pmem-transaction

Golang library for using persistent memory
Other
29 stars 5 forks source link

abort transaction after commit? #6

Closed photoszzt closed 5 years ago

photoszzt commented 5 years ago

https://github.com/vmware/go-pmem-transaction/blob/b8f43e61a7b5cc28401531d31fc97b60e1d4a5a7/examples/database.go#L83

Is there any reason to issue an abort after the transaction has committed by End()?

photoszzt commented 5 years ago

I see this pattern

tx.End()
transaction.Release(tx)

all over the list example.

jerrinsg commented 5 years ago

Hi @photoszzt ,

transaction.NewUndoTx() gives the caller an undo transaction object to transactionally update data. When the caller is done using the object, it is released back to the pool of undo transaction objects using transaction.Release(tx). The number of undo transaction objects is limited using the constant LOGNUM (see the latest changes at https://github.com/vmware/go-pmem-transaction/pull/2/files#diff-2529b46de3caa866c44a5831efd018f4R88). The idea is that each goroutine use a different transaction object, and the number of transaction objects limits the number of parallel transactions. Using a transaction object, each transaction boundary is specified usingtx.Begin() and tx.End().

In the example application database.go, each function is using a new transaction object. This is actually not necessary, and a single transaction object could be passed to each of those functions. The example application just shows the basic usage. We will soon open source a Go version of Redis designed for persistent memory that uses the go-pmem-transaction library. Hopefully that project will help you get a better idea on the transaction library usage patterns.

jerrinsg commented 5 years ago

As to why the Release() function calls abort(), consider a code flow as below:

tx := transaction.NewUndoTx()
tx.Begin()
..
tx.Log(data1)
..
// update data1
..
tx.Log(data2)
..
// update data2
..
transaction.Release(tx)

Here tx is released before the transaction is committed. In this case we want any uncommitted changes (changes to data1 and data2) to be reverted.

photoszzt commented 5 years ago

I see. Thanks for the explanation. I guess some of what you said should be in the documentation. There's no explanation on what the transaction.release() does besides it will abort the transaction.

jerrinsg commented 5 years ago

Thanks for the suggestion. Yes we will work on improving the documentation. I will close this issue but feel free to reach out if you find any other issue.