batch.go 6.5 KB

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