-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This task is mostly implemented to be used in `erigon/erigon-lib/downloader/mdbx_piece_completion.go` and maybe in `nodesDB` (where we need many parallel RwTx) I was agains adding this "trick"/"api" last years, because thought that we can implement our App to be more 1-big-rwtx-friendly. And we did it in Erigon - StagedSync. TxPool also did, but with a bit less happy face - by "map+mutex with periodic flush to db". But `anacrolix/torrent` is external library and unlikely will survive such big mind-model-change. Maybe it's time to add `db.Batch()`. #### Batch Rw transactions Each `DB.Update()` waits for disk to commit the writes. This overhead can be minimized by combining multiple updates with the `DB.Batch()` function: ```go err := db.Batch(func(tx *bolt.Tx) error { ... return nil }) ``` Concurrent Batch calls are opportunistically combined into larger transactions. Batch is only useful when there are multiple goroutines calling it. The trade-off is that `Batch` can call the given function multiple times, if parts of the transaction fail. The function must be idempotent and side effects must take effect only after a successful return from `DB.Batch()`. For example: don't display messages from inside the function, instead set variables in the enclosing scope: ```go var id uint64 err := db.Batch(func(tx *bolt.Tx) error { // Find last key in bucket, decode as bigendian uint64, increment // by one, encode back to []byte, and add new key. ... id = newValue return nil }) if err != nil { return ... } fmt.Println("Allocated ID %d", id) ``` ---- Implementation mostly taken from https://github.com/etcd-io/bbolt/?tab=readme-ov-file#batch-read-write-transactions Maybe in future can push-down it to https://github.com/erigontech/mdbx-go
- Loading branch information
1 parent
e7d67fd
commit 122f9f8
Showing
5 changed files
with
406 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.