consistent_watchable_store.go 4.4 KB

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