batch_tx.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package backend
  15. import (
  16. "bytes"
  17. "log"
  18. "sync"
  19. "sync/atomic"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/boltdb/bolt"
  21. )
  22. type BatchTx interface {
  23. Lock()
  24. Unlock()
  25. UnsafeCreateBucket(name []byte)
  26. UnsafePut(bucketName []byte, key []byte, value []byte)
  27. UnsafeRange(bucketName []byte, key, endKey []byte, limit int64) (keys [][]byte, vals [][]byte)
  28. UnsafeDelete(bucketName []byte, key []byte)
  29. Commit()
  30. CommitAndStop()
  31. }
  32. type batchTx struct {
  33. sync.Mutex
  34. tx *bolt.Tx
  35. backend *backend
  36. pending int
  37. }
  38. func newBatchTx(backend *backend) *batchTx {
  39. tx := &batchTx{backend: backend}
  40. tx.Commit()
  41. return tx
  42. }
  43. func (t *batchTx) UnsafeCreateBucket(name []byte) {
  44. _, err := t.tx.CreateBucket(name)
  45. if err != nil && err != bolt.ErrBucketExists {
  46. log.Fatalf("storage: cannot create bucket %s (%v)", string(name), err)
  47. }
  48. t.pending++
  49. }
  50. // before calling unsafePut, the caller MUST hold the lock on tx.
  51. func (t *batchTx) UnsafePut(bucketName []byte, key []byte, value []byte) {
  52. bucket := t.tx.Bucket(bucketName)
  53. if bucket == nil {
  54. log.Fatalf("storage: bucket %s does not exist", string(bucketName))
  55. }
  56. if err := bucket.Put(key, value); err != nil {
  57. log.Fatalf("storage: cannot put key into bucket (%v)", err)
  58. }
  59. t.pending++
  60. }
  61. // before calling unsafeRange, the caller MUST hold the lock on tx.
  62. func (t *batchTx) UnsafeRange(bucketName []byte, key, endKey []byte, limit int64) (keys [][]byte, vs [][]byte) {
  63. bucket := t.tx.Bucket(bucketName)
  64. if bucket == nil {
  65. log.Fatalf("storage: bucket %s does not exist", string(bucketName))
  66. }
  67. if len(endKey) == 0 {
  68. if v := bucket.Get(key); v == nil {
  69. return keys, vs
  70. } else {
  71. return append(keys, key), append(vs, v)
  72. }
  73. }
  74. c := bucket.Cursor()
  75. for ck, cv := c.Seek(key); ck != nil && bytes.Compare(ck, endKey) < 0; ck, cv = c.Next() {
  76. vs = append(vs, cv)
  77. keys = append(keys, ck)
  78. if limit > 0 && limit == int64(len(keys)) {
  79. break
  80. }
  81. }
  82. return keys, vs
  83. }
  84. // before calling unsafeDelete, the caller MUST hold the lock on tx.
  85. func (t *batchTx) UnsafeDelete(bucketName []byte, key []byte) {
  86. bucket := t.tx.Bucket(bucketName)
  87. if bucket == nil {
  88. log.Fatalf("storage: bucket %s does not exist", string(bucketName))
  89. }
  90. err := bucket.Delete(key)
  91. if err != nil {
  92. log.Fatalf("storage: cannot delete key from bucket (%v)", err)
  93. }
  94. t.pending++
  95. }
  96. // Commit commits a previous tx and begins a new writable one.
  97. func (t *batchTx) Commit() {
  98. t.Lock()
  99. defer t.Unlock()
  100. t.commit(false)
  101. }
  102. // CommitAndStop commits the previous tx and do not create a new one.
  103. func (t *batchTx) CommitAndStop() {
  104. t.Lock()
  105. defer t.Unlock()
  106. t.commit(true)
  107. }
  108. func (t *batchTx) Unlock() {
  109. if t.pending >= t.backend.batchLimit {
  110. t.commit(false)
  111. t.pending = 0
  112. }
  113. t.Mutex.Unlock()
  114. }
  115. func (t *batchTx) commit(stop bool) {
  116. var err error
  117. // commit the last tx
  118. if t.tx != nil {
  119. if t.pending == 0 && !stop {
  120. return
  121. }
  122. err = t.tx.Commit()
  123. atomic.AddInt64(&t.backend.commits, 1)
  124. t.pending = 0
  125. if err != nil {
  126. log.Fatalf("storage: cannot commit tx (%s)", err)
  127. }
  128. }
  129. if stop {
  130. return
  131. }
  132. // begin a new tx
  133. t.tx, err = t.backend.db.Begin(true)
  134. if err != nil {
  135. log.Fatalf("storage: cannot begin tx (%s)", err)
  136. }
  137. atomic.StoreInt64(&t.backend.size, t.tx.Size())
  138. }