read_tx.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2017 The etcd Authors
  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. "math"
  18. "sync"
  19. bolt "go.etcd.io/bbolt"
  20. )
  21. // safeRangeBucket is a hack to avoid inadvertently reading duplicate keys;
  22. // overwrites on a bucket should only fetch with limit=1, but safeRangeBucket
  23. // is known to never overwrite any key so range is safe.
  24. var safeRangeBucket = []byte("key")
  25. type ReadTx interface {
  26. Lock()
  27. Unlock()
  28. RLock()
  29. RUnlock()
  30. UnsafeRange(bucketName []byte, key, endKey []byte, limit int64) (keys [][]byte, vals [][]byte)
  31. UnsafeForEach(bucketName []byte, visitor func(k, v []byte) error) error
  32. }
  33. type readTx struct {
  34. // mu protects accesses to the txReadBuffer
  35. mu sync.RWMutex
  36. buf txReadBuffer
  37. // txmu protects accesses to buckets and tx on Range requests.
  38. txmu sync.RWMutex
  39. tx *bolt.Tx
  40. buckets map[string]*bolt.Bucket
  41. }
  42. func (rt *readTx) Lock() { rt.mu.Lock() }
  43. func (rt *readTx) Unlock() { rt.mu.Unlock() }
  44. func (rt *readTx) RLock() { rt.mu.RLock() }
  45. func (rt *readTx) RUnlock() { rt.mu.RUnlock() }
  46. func (rt *readTx) UnsafeRange(bucketName, key, endKey []byte, limit int64) ([][]byte, [][]byte) {
  47. if endKey == nil {
  48. // forbid duplicates for single keys
  49. limit = 1
  50. }
  51. if limit <= 0 {
  52. limit = math.MaxInt64
  53. }
  54. if limit > 1 && !bytes.Equal(bucketName, safeRangeBucket) {
  55. panic("do not use unsafeRange on non-keys bucket")
  56. }
  57. keys, vals := rt.buf.Range(bucketName, key, endKey, limit)
  58. if int64(len(keys)) == limit {
  59. return keys, vals
  60. }
  61. // find/cache bucket
  62. bn := string(bucketName)
  63. rt.txmu.RLock()
  64. bucket, ok := rt.buckets[bn]
  65. rt.txmu.RUnlock()
  66. if !ok {
  67. rt.txmu.Lock()
  68. bucket = rt.tx.Bucket(bucketName)
  69. rt.buckets[bn] = bucket
  70. rt.txmu.Unlock()
  71. }
  72. // ignore missing bucket since may have been created in this batch
  73. if bucket == nil {
  74. return keys, vals
  75. }
  76. rt.txmu.Lock()
  77. c := bucket.Cursor()
  78. rt.txmu.Unlock()
  79. k2, v2 := unsafeRange(c, key, endKey, limit-int64(len(keys)))
  80. return append(k2, keys...), append(v2, vals...)
  81. }
  82. func (rt *readTx) UnsafeForEach(bucketName []byte, visitor func(k, v []byte) error) error {
  83. dups := make(map[string]struct{})
  84. getDups := func(k, v []byte) error {
  85. dups[string(k)] = struct{}{}
  86. return nil
  87. }
  88. visitNoDup := func(k, v []byte) error {
  89. if _, ok := dups[string(k)]; ok {
  90. return nil
  91. }
  92. return visitor(k, v)
  93. }
  94. if err := rt.buf.ForEach(bucketName, getDups); err != nil {
  95. return err
  96. }
  97. rt.txmu.Lock()
  98. err := unsafeForEach(rt.tx, bucketName, visitNoDup)
  99. rt.txmu.Unlock()
  100. if err != nil {
  101. return err
  102. }
  103. return rt.buf.ForEach(bucketName, visitor)
  104. }
  105. func (rt *readTx) reset() {
  106. rt.buf.reset()
  107. rt.buckets = make(map[string]*bolt.Bucket)
  108. rt.tx = nil
  109. }