batch_tx.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. UnsafeSeqPut(bucketName []byte, key []byte, value []byte)
  28. UnsafeRange(bucketName []byte, key, endKey []byte, limit int64) (keys [][]byte, vals [][]byte)
  29. UnsafeDelete(bucketName []byte, key []byte)
  30. Commit()
  31. CommitAndStop()
  32. }
  33. type batchTx struct {
  34. sync.Mutex
  35. tx *bolt.Tx
  36. backend *backend
  37. pending int
  38. }
  39. func newBatchTx(backend *backend) *batchTx {
  40. tx := &batchTx{backend: backend}
  41. tx.Commit()
  42. return tx
  43. }
  44. func (t *batchTx) UnsafeCreateBucket(name []byte) {
  45. _, err := t.tx.CreateBucket(name)
  46. if err != nil && err != bolt.ErrBucketExists {
  47. log.Fatalf("storage: cannot create bucket %s (%v)", string(name), err)
  48. }
  49. t.pending++
  50. }
  51. // UnsafePut must be called holding the lock on the tx.
  52. func (t *batchTx) UnsafePut(bucketName []byte, key []byte, value []byte) {
  53. t.unsafePut(bucketName, key, value, false)
  54. }
  55. // UnsafeSeqPut must be called holding the lock on the tx.
  56. func (t *batchTx) UnsafeSeqPut(bucketName []byte, key []byte, value []byte) {
  57. t.unsafePut(bucketName, key, value, true)
  58. }
  59. func (t *batchTx) unsafePut(bucketName []byte, key []byte, value []byte, seq bool) {
  60. bucket := t.tx.Bucket(bucketName)
  61. if bucket == nil {
  62. log.Fatalf("storage: bucket %s does not exist", string(bucketName))
  63. }
  64. if seq {
  65. // it is useful to increase fill percent when the workloads are mostly append-only.
  66. // this can delay the page split and reduce space usage.
  67. bucket.FillPercent = 0.9
  68. }
  69. if err := bucket.Put(key, value); err != nil {
  70. log.Fatalf("storage: cannot put key into bucket (%v)", err)
  71. }
  72. t.pending++
  73. }
  74. // UnsafeRange must be called holding the lock on the tx.
  75. func (t *batchTx) UnsafeRange(bucketName []byte, key, endKey []byte, limit int64) (keys [][]byte, vs [][]byte) {
  76. bucket := t.tx.Bucket(bucketName)
  77. if bucket == nil {
  78. log.Fatalf("storage: bucket %s does not exist", string(bucketName))
  79. }
  80. if len(endKey) == 0 {
  81. if v := bucket.Get(key); v == nil {
  82. return keys, vs
  83. } else {
  84. return append(keys, key), append(vs, v)
  85. }
  86. }
  87. c := bucket.Cursor()
  88. for ck, cv := c.Seek(key); ck != nil && bytes.Compare(ck, endKey) < 0; ck, cv = c.Next() {
  89. vs = append(vs, cv)
  90. keys = append(keys, ck)
  91. if limit > 0 && limit == int64(len(keys)) {
  92. break
  93. }
  94. }
  95. return keys, vs
  96. }
  97. // UnsafeDelete must be called holding the lock on the tx.
  98. func (t *batchTx) UnsafeDelete(bucketName []byte, key []byte) {
  99. bucket := t.tx.Bucket(bucketName)
  100. if bucket == nil {
  101. log.Fatalf("storage: bucket %s does not exist", string(bucketName))
  102. }
  103. err := bucket.Delete(key)
  104. if err != nil {
  105. log.Fatalf("storage: cannot delete key from bucket (%v)", err)
  106. }
  107. t.pending++
  108. }
  109. // Commit commits a previous tx and begins a new writable one.
  110. func (t *batchTx) Commit() {
  111. t.Lock()
  112. defer t.Unlock()
  113. t.commit(false)
  114. }
  115. // CommitAndStop commits the previous tx and do not create a new one.
  116. func (t *batchTx) CommitAndStop() {
  117. t.Lock()
  118. defer t.Unlock()
  119. t.commit(true)
  120. }
  121. func (t *batchTx) Unlock() {
  122. if t.pending >= t.backend.batchLimit {
  123. t.commit(false)
  124. t.pending = 0
  125. }
  126. t.Mutex.Unlock()
  127. }
  128. func (t *batchTx) commit(stop bool) {
  129. var err error
  130. // commit the last tx
  131. if t.tx != nil {
  132. if t.pending == 0 && !stop {
  133. return
  134. }
  135. err = t.tx.Commit()
  136. atomic.AddInt64(&t.backend.commits, 1)
  137. t.pending = 0
  138. if err != nil {
  139. log.Fatalf("storage: cannot commit tx (%s)", err)
  140. }
  141. }
  142. if stop {
  143. return
  144. }
  145. t.backend.mu.RLock()
  146. defer t.backend.mu.RUnlock()
  147. // begin a new tx
  148. t.tx, err = t.backend.db.Begin(true)
  149. if err != nil {
  150. log.Fatalf("storage: cannot begin tx (%s)", err)
  151. }
  152. atomic.StoreInt64(&t.backend.size, t.tx.Size())
  153. }