batch.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. package badger
  6. import (
  7. "errors"
  8. "fmt"
  9. "sync"
  10. "sync/atomic"
  11. "google.golang.org/protobuf/proto"
  12. "github.com/dgraph-io/badger/v4/pb"
  13. "github.com/dgraph-io/badger/v4/y"
  14. "github.com/dgraph-io/ristretto/v2/z"
  15. )
  16. // WriteBatch holds the necessary info to perform batched writes.
  17. type WriteBatch struct {
  18. sync.Mutex
  19. txn *Txn
  20. db *DB
  21. throttle *y.Throttle
  22. err atomic.Value
  23. isManaged bool
  24. commitTs uint64
  25. finished bool
  26. }
  27. // NewWriteBatch creates a new WriteBatch. This provides a way to conveniently do a lot of writes,
  28. // batching them up as tightly as possible in a single transaction and using callbacks to avoid
  29. // waiting for them to commit, thus achieving good performance. This API hides away the logic of
  30. // creating and committing transactions. Due to the nature of SSI guaratees provided by Badger,
  31. // blind writes can never encounter transaction conflicts (ErrConflict).
  32. func (db *DB) NewWriteBatch() *WriteBatch {
  33. if db.opt.managedTxns {
  34. panic("cannot use NewWriteBatch in managed mode. Use NewWriteBatchAt instead")
  35. }
  36. return db.newWriteBatch(false)
  37. }
  38. func (db *DB) newWriteBatch(isManaged bool) *WriteBatch {
  39. return &WriteBatch{
  40. db: db,
  41. isManaged: isManaged,
  42. txn: db.newTransaction(true, isManaged),
  43. throttle: y.NewThrottle(16),
  44. }
  45. }
  46. // SetMaxPendingTxns sets a limit on maximum number of pending transactions while writing batches.
  47. // This function should be called before using WriteBatch. Default value of MaxPendingTxns is
  48. // 16 to minimise memory usage.
  49. func (wb *WriteBatch) SetMaxPendingTxns(max int) {
  50. wb.throttle = y.NewThrottle(max)
  51. }
  52. // Cancel function must be called if there's a chance that Flush might not get
  53. // called. If neither Flush or Cancel is called, the transaction oracle would
  54. // never get a chance to clear out the row commit timestamp map, thus causing an
  55. // unbounded memory consumption. Typically, you can call Cancel as a defer
  56. // statement right after NewWriteBatch is called.
  57. //
  58. // Note that any committed writes would still go through despite calling Cancel.
  59. func (wb *WriteBatch) Cancel() {
  60. wb.Lock()
  61. defer wb.Unlock()
  62. wb.finished = true
  63. if err := wb.throttle.Finish(); err != nil {
  64. wb.db.opt.Errorf("WatchBatch.Cancel error while finishing: %v", err)
  65. }
  66. wb.txn.Discard()
  67. }
  68. func (wb *WriteBatch) callback(err error) {
  69. // sync.WaitGroup is thread-safe, so it doesn't need to be run inside wb.Lock.
  70. defer wb.throttle.Done(err)
  71. if err == nil {
  72. return
  73. }
  74. if err := wb.Error(); err != nil {
  75. return
  76. }
  77. wb.err.Store(err)
  78. }
  79. func (wb *WriteBatch) writeKV(kv *pb.KV) error {
  80. e := Entry{Key: kv.Key, Value: kv.Value}
  81. if len(kv.UserMeta) > 0 {
  82. e.UserMeta = kv.UserMeta[0]
  83. }
  84. y.AssertTrue(kv.Version != 0)
  85. e.version = kv.Version
  86. return wb.handleEntry(&e)
  87. }
  88. func (wb *WriteBatch) Write(buf *z.Buffer) error {
  89. wb.Lock()
  90. defer wb.Unlock()
  91. err := buf.SliceIterate(func(s []byte) error {
  92. kv := &pb.KV{}
  93. if err := proto.Unmarshal(s, kv); err != nil {
  94. return err
  95. }
  96. return wb.writeKV(kv)
  97. })
  98. return err
  99. }
  100. func (wb *WriteBatch) WriteList(kvList *pb.KVList) error {
  101. wb.Lock()
  102. defer wb.Unlock()
  103. for _, kv := range kvList.Kv {
  104. if err := wb.writeKV(kv); err != nil {
  105. return err
  106. }
  107. }
  108. return nil
  109. }
  110. // SetEntryAt is the equivalent of Txn.SetEntry but it also allows setting version for the entry.
  111. // SetEntryAt can be used only in managed mode.
  112. func (wb *WriteBatch) SetEntryAt(e *Entry, ts uint64) error {
  113. if !wb.db.opt.managedTxns {
  114. return errors.New("SetEntryAt can only be used in managed mode. Use SetEntry instead")
  115. }
  116. e.version = ts
  117. return wb.SetEntry(e)
  118. }
  119. // Should be called with lock acquired.
  120. func (wb *WriteBatch) handleEntry(e *Entry) error {
  121. if err := wb.txn.SetEntry(e); err != ErrTxnTooBig {
  122. return err
  123. }
  124. // Txn has reached it's zenith. Commit now.
  125. if cerr := wb.commit(); cerr != nil {
  126. return cerr
  127. }
  128. // This time the error must not be ErrTxnTooBig, otherwise, we make the
  129. // error permanent.
  130. if err := wb.txn.SetEntry(e); err != nil {
  131. wb.err.Store(err)
  132. return err
  133. }
  134. return nil
  135. }
  136. // SetEntry is the equivalent of Txn.SetEntry.
  137. func (wb *WriteBatch) SetEntry(e *Entry) error {
  138. wb.Lock()
  139. defer wb.Unlock()
  140. return wb.handleEntry(e)
  141. }
  142. // Set is equivalent of Txn.Set().
  143. func (wb *WriteBatch) Set(k, v []byte) error {
  144. e := &Entry{Key: k, Value: v}
  145. return wb.SetEntry(e)
  146. }
  147. // DeleteAt is equivalent of Txn.Delete but accepts a delete timestamp.
  148. func (wb *WriteBatch) DeleteAt(k []byte, ts uint64) error {
  149. e := Entry{Key: k, meta: bitDelete, version: ts}
  150. return wb.SetEntry(&e)
  151. }
  152. // Delete is equivalent of Txn.Delete.
  153. func (wb *WriteBatch) Delete(k []byte) error {
  154. wb.Lock()
  155. defer wb.Unlock()
  156. if err := wb.txn.Delete(k); err != ErrTxnTooBig {
  157. return err
  158. }
  159. if err := wb.commit(); err != nil {
  160. return err
  161. }
  162. if err := wb.txn.Delete(k); err != nil {
  163. wb.err.Store(err)
  164. return err
  165. }
  166. return nil
  167. }
  168. // Caller to commit must hold a write lock.
  169. func (wb *WriteBatch) commit() error {
  170. if err := wb.Error(); err != nil {
  171. return err
  172. }
  173. if wb.finished {
  174. return y.ErrCommitAfterFinish
  175. }
  176. if err := wb.throttle.Do(); err != nil {
  177. wb.err.Store(err)
  178. return err
  179. }
  180. wb.txn.CommitWith(wb.callback)
  181. wb.txn = wb.db.newTransaction(true, wb.isManaged)
  182. wb.txn.commitTs = wb.commitTs
  183. return wb.Error()
  184. }
  185. // Flush must be called at the end to ensure that any pending writes get committed to Badger. Flush
  186. // returns any error stored by WriteBatch.
  187. func (wb *WriteBatch) Flush() error {
  188. wb.Lock()
  189. err := wb.commit()
  190. if err != nil {
  191. wb.Unlock()
  192. return err
  193. }
  194. wb.finished = true
  195. wb.txn.Discard()
  196. wb.Unlock()
  197. if err := wb.throttle.Finish(); err != nil {
  198. if wb.Error() != nil {
  199. return fmt.Errorf("wb.err: %w err: %w", wb.Error(), err)
  200. }
  201. return err
  202. }
  203. return wb.Error()
  204. }
  205. // Error returns any errors encountered so far. No commits would be run once an error is detected.
  206. func (wb *WriteBatch) Error() error {
  207. // If the interface conversion fails, the err will be nil.
  208. err, _ := wb.err.Load().(error)
  209. return err
  210. }