kvstore.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // Copyright 2015 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 mvcc
  15. import (
  16. "encoding/binary"
  17. "errors"
  18. "math"
  19. "sync"
  20. "sync/atomic"
  21. "time"
  22. "github.com/coreos/etcd/lease"
  23. "github.com/coreos/etcd/mvcc/backend"
  24. "github.com/coreos/etcd/mvcc/mvccpb"
  25. "github.com/coreos/etcd/pkg/schedule"
  26. "github.com/coreos/pkg/capnslog"
  27. "golang.org/x/net/context"
  28. )
  29. var (
  30. keyBucketName = []byte("key")
  31. metaBucketName = []byte("meta")
  32. consistentIndexKeyName = []byte("consistent_index")
  33. scheduledCompactKeyName = []byte("scheduledCompactRev")
  34. finishedCompactKeyName = []byte("finishedCompactRev")
  35. ErrCompacted = errors.New("mvcc: required revision has been compacted")
  36. ErrFutureRev = errors.New("mvcc: required revision is a future revision")
  37. ErrCanceled = errors.New("mvcc: watcher is canceled")
  38. ErrClosed = errors.New("mvcc: closed")
  39. plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "mvcc")
  40. )
  41. const (
  42. // markedRevBytesLen is the byte length of marked revision.
  43. // The first `revBytesLen` bytes represents a normal revision. The last
  44. // one byte is the mark.
  45. markedRevBytesLen = revBytesLen + 1
  46. markBytePosition = markedRevBytesLen - 1
  47. markTombstone byte = 't'
  48. )
  49. var restoreChunkKeys = 10000 // non-const for testing
  50. // ConsistentIndexGetter is an interface that wraps the Get method.
  51. // Consistent index is the offset of an entry in a consistent replicated log.
  52. type ConsistentIndexGetter interface {
  53. // ConsistentIndex returns the consistent index of current executing entry.
  54. ConsistentIndex() uint64
  55. }
  56. type store struct {
  57. ReadView
  58. WriteView
  59. // consistentIndex caches the "consistent_index" key's value. Accessed
  60. // through atomics so must be 64-bit aligned.
  61. consistentIndex uint64
  62. // mu read locks for txns and write locks for non-txn store changes.
  63. mu sync.RWMutex
  64. ig ConsistentIndexGetter
  65. b backend.Backend
  66. kvindex index
  67. le lease.Lessor
  68. // revMuLock protects currentRev and compactMainRev.
  69. // Locked at end of write txn and released after write txn unlock lock.
  70. // Locked before locking read txn and released after locking.
  71. revMu sync.RWMutex
  72. // currentRev is the revision of the last completed transaction.
  73. currentRev int64
  74. // compactMainRev is the main revision of the last compaction.
  75. compactMainRev int64
  76. // bytesBuf8 is a byte slice of length 8
  77. // to avoid a repetitive allocation in saveIndex.
  78. bytesBuf8 []byte
  79. fifoSched schedule.Scheduler
  80. stopc chan struct{}
  81. }
  82. // NewStore returns a new store. It is useful to create a store inside
  83. // mvcc pkg. It should only be used for testing externally.
  84. func NewStore(b backend.Backend, le lease.Lessor, ig ConsistentIndexGetter) *store {
  85. s := &store{
  86. b: b,
  87. ig: ig,
  88. kvindex: newTreeIndex(),
  89. le: le,
  90. currentRev: 1,
  91. compactMainRev: -1,
  92. bytesBuf8: make([]byte, 8),
  93. fifoSched: schedule.NewFIFOScheduler(),
  94. stopc: make(chan struct{}),
  95. }
  96. s.ReadView = &readView{s}
  97. s.WriteView = &writeView{s}
  98. if s.le != nil {
  99. s.le.SetRangeDeleter(func() lease.TxnDelete { return s.Write() })
  100. }
  101. tx := s.b.BatchTx()
  102. tx.Lock()
  103. tx.UnsafeCreateBucket(keyBucketName)
  104. tx.UnsafeCreateBucket(metaBucketName)
  105. tx.Unlock()
  106. s.b.ForceCommit()
  107. if err := s.restore(); err != nil {
  108. // TODO: return the error instead of panic here?
  109. panic("failed to recover store from backend")
  110. }
  111. return s
  112. }
  113. func (s *store) compactBarrier(ctx context.Context, ch chan struct{}) {
  114. if ctx == nil || ctx.Err() != nil {
  115. s.mu.Lock()
  116. select {
  117. case <-s.stopc:
  118. default:
  119. f := func(ctx context.Context) { s.compactBarrier(ctx, ch) }
  120. s.fifoSched.Schedule(f)
  121. }
  122. s.mu.Unlock()
  123. return
  124. }
  125. close(ch)
  126. }
  127. func (s *store) Hash() (hash uint32, revision int64, err error) {
  128. s.b.ForceCommit()
  129. h, err := s.b.Hash(DefaultIgnores)
  130. return h, s.currentRev, err
  131. }
  132. func (s *store) Compact(rev int64) (<-chan struct{}, error) {
  133. s.mu.Lock()
  134. defer s.mu.Unlock()
  135. s.revMu.Lock()
  136. defer s.revMu.Unlock()
  137. if rev <= s.compactMainRev {
  138. ch := make(chan struct{})
  139. f := func(ctx context.Context) { s.compactBarrier(ctx, ch) }
  140. s.fifoSched.Schedule(f)
  141. return ch, ErrCompacted
  142. }
  143. if rev > s.currentRev {
  144. return nil, ErrFutureRev
  145. }
  146. start := time.Now()
  147. s.compactMainRev = rev
  148. rbytes := newRevBytes()
  149. revToBytes(revision{main: rev}, rbytes)
  150. tx := s.b.BatchTx()
  151. tx.Lock()
  152. tx.UnsafePut(metaBucketName, scheduledCompactKeyName, rbytes)
  153. tx.Unlock()
  154. // ensure that desired compaction is persisted
  155. s.b.ForceCommit()
  156. keep := s.kvindex.Compact(rev)
  157. ch := make(chan struct{})
  158. var j = func(ctx context.Context) {
  159. if ctx.Err() != nil {
  160. s.compactBarrier(ctx, ch)
  161. return
  162. }
  163. if !s.scheduleCompaction(rev, keep) {
  164. s.compactBarrier(nil, ch)
  165. return
  166. }
  167. close(ch)
  168. }
  169. s.fifoSched.Schedule(j)
  170. indexCompactionPauseDurations.Observe(float64(time.Since(start) / time.Millisecond))
  171. return ch, nil
  172. }
  173. // DefaultIgnores is a map of keys to ignore in hash checking.
  174. var DefaultIgnores map[backend.IgnoreKey]struct{}
  175. func init() {
  176. DefaultIgnores = map[backend.IgnoreKey]struct{}{
  177. // consistent index might be changed due to v2 internal sync, which
  178. // is not controllable by the user.
  179. {Bucket: string(metaBucketName), Key: string(consistentIndexKeyName)}: {},
  180. }
  181. }
  182. func (s *store) Commit() {
  183. s.mu.Lock()
  184. defer s.mu.Unlock()
  185. tx := s.b.BatchTx()
  186. tx.Lock()
  187. s.saveIndex(tx)
  188. tx.Unlock()
  189. s.b.ForceCommit()
  190. }
  191. func (s *store) Restore(b backend.Backend) error {
  192. s.mu.Lock()
  193. defer s.mu.Unlock()
  194. close(s.stopc)
  195. s.fifoSched.Stop()
  196. atomic.StoreUint64(&s.consistentIndex, 0)
  197. s.b = b
  198. s.kvindex = newTreeIndex()
  199. s.currentRev = 1
  200. s.compactMainRev = -1
  201. s.fifoSched = schedule.NewFIFOScheduler()
  202. s.stopc = make(chan struct{})
  203. return s.restore()
  204. }
  205. func (s *store) restore() error {
  206. reportDbTotalSizeInBytesMu.Lock()
  207. b := s.b
  208. reportDbTotalSizeInBytes = func() float64 { return float64(b.Size()) }
  209. reportDbTotalSizeInBytesMu.Unlock()
  210. min, max := newRevBytes(), newRevBytes()
  211. revToBytes(revision{main: 1}, min)
  212. revToBytes(revision{main: math.MaxInt64, sub: math.MaxInt64}, max)
  213. keyToLease := make(map[string]lease.LeaseID)
  214. // restore index
  215. tx := s.b.BatchTx()
  216. tx.Lock()
  217. _, finishedCompactBytes := tx.UnsafeRange(metaBucketName, finishedCompactKeyName, nil, 0)
  218. if len(finishedCompactBytes) != 0 {
  219. s.compactMainRev = bytesToRev(finishedCompactBytes[0]).main
  220. plog.Printf("restore compact to %d", s.compactMainRev)
  221. }
  222. _, scheduledCompactBytes := tx.UnsafeRange(metaBucketName, scheduledCompactKeyName, nil, 0)
  223. scheduledCompact := int64(0)
  224. if len(scheduledCompactBytes) != 0 {
  225. scheduledCompact = bytesToRev(scheduledCompactBytes[0]).main
  226. }
  227. // index keys concurrently as they're loaded in from tx
  228. rkvc, revc := restoreIntoIndex(s.kvindex)
  229. for {
  230. keys, vals := tx.UnsafeRange(keyBucketName, min, max, int64(restoreChunkKeys))
  231. if len(keys) == 0 {
  232. break
  233. }
  234. // rkvc blocks if the total pending keys exceeds the restore
  235. // chunk size to keep keys from consuming too much memory.
  236. restoreChunk(rkvc, keys, vals, keyToLease)
  237. if len(keys) < restoreChunkKeys {
  238. // partial set implies final set
  239. break
  240. }
  241. // next set begins after where this one ended
  242. newMin := bytesToRev(keys[len(keys)-1][:revBytesLen])
  243. newMin.sub++
  244. revToBytes(newMin, min)
  245. }
  246. close(rkvc)
  247. s.currentRev = <-revc
  248. // keys in the range [compacted revision -N, compaction] might all be deleted due to compaction.
  249. // the correct revision should be set to compaction revision in the case, not the largest revision
  250. // we have seen.
  251. if s.currentRev < s.compactMainRev {
  252. s.currentRev = s.compactMainRev
  253. }
  254. if scheduledCompact <= s.compactMainRev {
  255. scheduledCompact = 0
  256. }
  257. for key, lid := range keyToLease {
  258. if s.le == nil {
  259. panic("no lessor to attach lease")
  260. }
  261. err := s.le.Attach(lid, []lease.LeaseItem{{Key: key}})
  262. if err != nil {
  263. plog.Errorf("unexpected Attach error: %v", err)
  264. }
  265. }
  266. tx.Unlock()
  267. if scheduledCompact != 0 {
  268. s.Compact(scheduledCompact)
  269. plog.Printf("resume scheduled compaction at %d", scheduledCompact)
  270. }
  271. return nil
  272. }
  273. type revKeyValue struct {
  274. key []byte
  275. kv mvccpb.KeyValue
  276. kstr string
  277. }
  278. func restoreIntoIndex(idx index) (chan<- revKeyValue, <-chan int64) {
  279. rkvc, revc := make(chan revKeyValue, restoreChunkKeys), make(chan int64, 1)
  280. go func() {
  281. currentRev := int64(1)
  282. defer func() { revc <- currentRev }()
  283. // restore the tree index from streaming the unordered index.
  284. kiCache := make(map[string]*keyIndex, restoreChunkKeys)
  285. for rkv := range rkvc {
  286. ki, ok := kiCache[rkv.kstr]
  287. // purge kiCache if many keys but still missing in the cache
  288. if !ok && len(kiCache) >= restoreChunkKeys {
  289. i := 10
  290. for k := range kiCache {
  291. delete(kiCache, k)
  292. if i--; i == 0 {
  293. break
  294. }
  295. }
  296. }
  297. // cache miss, fetch from tree index if there
  298. if !ok {
  299. ki = &keyIndex{key: rkv.kv.Key}
  300. if idxKey := idx.KeyIndex(ki); idxKey != nil {
  301. kiCache[rkv.kstr], ki = idxKey, idxKey
  302. ok = true
  303. }
  304. }
  305. rev := bytesToRev(rkv.key)
  306. currentRev = rev.main
  307. if ok {
  308. if isTombstone(rkv.key) {
  309. ki.tombstone(rev.main, rev.sub)
  310. continue
  311. }
  312. ki.put(rev.main, rev.sub)
  313. } else if !isTombstone(rkv.key) {
  314. ki.restore(revision{rkv.kv.CreateRevision, 0}, rev, rkv.kv.Version)
  315. idx.Insert(ki)
  316. kiCache[rkv.kstr] = ki
  317. }
  318. }
  319. }()
  320. return rkvc, revc
  321. }
  322. func restoreChunk(kvc chan<- revKeyValue, keys, vals [][]byte, keyToLease map[string]lease.LeaseID) {
  323. for i, key := range keys {
  324. rkv := revKeyValue{key: key}
  325. if err := rkv.kv.Unmarshal(vals[i]); err != nil {
  326. plog.Fatalf("cannot unmarshal event: %v", err)
  327. }
  328. rkv.kstr = string(rkv.kv.Key)
  329. if isTombstone(key) {
  330. delete(keyToLease, rkv.kstr)
  331. } else if lid := lease.LeaseID(rkv.kv.Lease); lid != lease.NoLease {
  332. keyToLease[rkv.kstr] = lid
  333. } else {
  334. delete(keyToLease, rkv.kstr)
  335. }
  336. kvc <- rkv
  337. }
  338. }
  339. func (s *store) Close() error {
  340. close(s.stopc)
  341. s.fifoSched.Stop()
  342. return nil
  343. }
  344. func (s *store) saveIndex(tx backend.BatchTx) {
  345. if s.ig == nil {
  346. return
  347. }
  348. bs := s.bytesBuf8
  349. ci := s.ig.ConsistentIndex()
  350. binary.BigEndian.PutUint64(bs, ci)
  351. // put the index into the underlying backend
  352. // tx has been locked in TxnBegin, so there is no need to lock it again
  353. tx.UnsafePut(metaBucketName, consistentIndexKeyName, bs)
  354. atomic.StoreUint64(&s.consistentIndex, ci)
  355. }
  356. func (s *store) ConsistentIndex() uint64 {
  357. if ci := atomic.LoadUint64(&s.consistentIndex); ci > 0 {
  358. return ci
  359. }
  360. tx := s.b.BatchTx()
  361. tx.Lock()
  362. defer tx.Unlock()
  363. _, vs := tx.UnsafeRange(metaBucketName, consistentIndexKeyName, nil, 0)
  364. if len(vs) == 0 {
  365. return 0
  366. }
  367. v := binary.BigEndian.Uint64(vs[0])
  368. atomic.StoreUint64(&s.consistentIndex, v)
  369. return v
  370. }
  371. // appendMarkTombstone appends tombstone mark to normal revision bytes.
  372. func appendMarkTombstone(b []byte) []byte {
  373. if len(b) != revBytesLen {
  374. plog.Panicf("cannot append mark to non normal revision bytes")
  375. }
  376. return append(b, markTombstone)
  377. }
  378. // isTombstone checks whether the revision bytes is a tombstone.
  379. func isTombstone(b []byte) bool {
  380. return len(b) == markedRevBytesLen && b[markBytePosition] == markTombstone
  381. }
  382. // revBytesRange returns the range of revision bytes at
  383. // the given revision.
  384. func revBytesRange(rev revision) (start, end []byte) {
  385. start = newRevBytes()
  386. revToBytes(rev, start)
  387. end = newRevBytes()
  388. endRev := revision{main: rev.main, sub: rev.sub + 1}
  389. revToBytes(endRev, end)
  390. return start, end
  391. }