consistent_watchable_store.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 storage
  15. import (
  16. "encoding/binary"
  17. "log"
  18. "github.com/coreos/etcd/storage/storagepb"
  19. )
  20. var (
  21. consistentIndexKeyName = []byte("consistent_index")
  22. )
  23. // ConsistentIndexGetter is an interface that wraps the Get method.
  24. // Consistent index is the offset of an entry in a consistent replicated log.
  25. type ConsistentIndexGetter interface {
  26. // ConsistentIndex returns the consistent index of current executing entry.
  27. ConsistentIndex() uint64
  28. }
  29. type consistentWatchableStore struct {
  30. *watchableStore
  31. // The field is used to get the consistent index of current
  32. // executing entry.
  33. // When the store finishes executing current entry, it will
  34. // put the index got from ConsistentIndexGetter into the
  35. // underlying backend. This helps to recover consistent index
  36. // when restoring.
  37. ig ConsistentIndexGetter
  38. skip bool // indicate whether or not to skip an operation
  39. }
  40. func New(path string, ig ConsistentIndexGetter) ConsistentWatchableKV {
  41. return newConsistentWatchableStore(path, ig)
  42. }
  43. // newConsistentWatchableStore creates a new consistentWatchableStore
  44. // using the file at the given path.
  45. // If the file at the given path does not exist then it will be created automatically.
  46. func newConsistentWatchableStore(path string, ig ConsistentIndexGetter) *consistentWatchableStore {
  47. return &consistentWatchableStore{
  48. watchableStore: newWatchableStore(path),
  49. ig: ig,
  50. }
  51. }
  52. func (s *consistentWatchableStore) Put(key, value []byte) (rev int64) {
  53. id := s.TxnBegin()
  54. rev, err := s.TxnPut(id, key, value)
  55. if err != nil {
  56. log.Panicf("unexpected TxnPut error (%v)", err)
  57. }
  58. if err := s.TxnEnd(id); err != nil {
  59. log.Panicf("unexpected TxnEnd error (%v)", err)
  60. }
  61. return rev
  62. }
  63. func (s *consistentWatchableStore) DeleteRange(key, end []byte) (n, rev int64) {
  64. id := s.TxnBegin()
  65. n, rev, err := s.TxnDeleteRange(id, key, end)
  66. if err != nil {
  67. log.Panicf("unexpected TxnDeleteRange error (%v)", err)
  68. }
  69. if err := s.TxnEnd(id); err != nil {
  70. log.Panicf("unexpected TxnEnd error (%v)", err)
  71. }
  72. return n, rev
  73. }
  74. func (s *consistentWatchableStore) TxnBegin() int64 {
  75. id := s.watchableStore.TxnBegin()
  76. // If the consistent index of executing entry is not larger than store
  77. // consistent index, skip all operations in this txn.
  78. s.skip = s.ig.ConsistentIndex() <= s.consistentIndex()
  79. if !s.skip {
  80. // TODO: avoid this unnecessary allocation
  81. bs := make([]byte, 8)
  82. binary.BigEndian.PutUint64(bs, s.ig.ConsistentIndex())
  83. // put the index into the underlying backend
  84. // tx has been locked in TxnBegin, so there is no need to lock it again
  85. s.watchableStore.store.tx.UnsafePut(metaBucketName, consistentIndexKeyName, bs)
  86. }
  87. return id
  88. }
  89. func (s *consistentWatchableStore) TxnRange(txnID int64, key, end []byte, limit, rangeRev int64) (kvs []storagepb.KeyValue, rev int64, err error) {
  90. if s.skip {
  91. return nil, 0, nil
  92. }
  93. return s.watchableStore.TxnRange(txnID, key, end, limit, rangeRev)
  94. }
  95. func (s *consistentWatchableStore) TxnPut(txnID int64, key, value []byte) (rev int64, err error) {
  96. if s.skip {
  97. return 0, nil
  98. }
  99. return s.watchableStore.TxnPut(txnID, key, value)
  100. }
  101. func (s *consistentWatchableStore) TxnDeleteRange(txnID int64, key, end []byte) (n, rev int64, err error) {
  102. if s.skip {
  103. return 0, 0, nil
  104. }
  105. return s.watchableStore.TxnDeleteRange(txnID, key, end)
  106. }
  107. func (s *consistentWatchableStore) TxnEnd(txnID int64) error {
  108. // reset skip var
  109. s.skip = false
  110. return s.watchableStore.TxnEnd(txnID)
  111. }
  112. func (s *consistentWatchableStore) consistentIndex() uint64 {
  113. // get the index
  114. // tx has been locked in TxnBegin, so there is no need to lock it again
  115. _, vs := s.watchableStore.store.tx.UnsafeRange(metaBucketName, consistentIndexKeyName, nil, 0)
  116. if len(vs) == 0 {
  117. return 0
  118. }
  119. return binary.BigEndian.Uint64(vs[0])
  120. }