backend.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "fmt"
  17. "hash/crc32"
  18. "io"
  19. "log"
  20. "sync/atomic"
  21. "time"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/boltdb/bolt"
  23. )
  24. type Backend interface {
  25. BatchTx() BatchTx
  26. Snapshot() Snapshot
  27. Hash() (uint32, error)
  28. // Size returns the current size of the backend.
  29. Size() int64
  30. ForceCommit()
  31. Close() error
  32. }
  33. type Snapshot interface {
  34. // Size gets the size of the snapshot.
  35. Size() int64
  36. // WriteTo writes the snapshot into the given writter.
  37. WriteTo(w io.Writer) (n int64, err error)
  38. // Close closes the snapshot.
  39. Close() error
  40. }
  41. type backend struct {
  42. db *bolt.DB
  43. batchInterval time.Duration
  44. batchLimit int
  45. batchTx *batchTx
  46. size int64
  47. stopc chan struct{}
  48. donec chan struct{}
  49. }
  50. func New(path string, d time.Duration, limit int) Backend {
  51. return newBackend(path, d, limit)
  52. }
  53. func newBackend(path string, d time.Duration, limit int) *backend {
  54. db, err := bolt.Open(path, 0600, nil)
  55. if err != nil {
  56. log.Panicf("backend: cannot open database at %s (%v)", path, err)
  57. }
  58. b := &backend{
  59. db: db,
  60. batchInterval: d,
  61. batchLimit: limit,
  62. stopc: make(chan struct{}),
  63. donec: make(chan struct{}),
  64. }
  65. b.batchTx = newBatchTx(b)
  66. go b.run()
  67. return b
  68. }
  69. // BatchTx returns the current batch tx in coalescer. The tx can be used for read and
  70. // write operations. The write result can be retrieved within the same tx immediately.
  71. // The write result is isolated with other txs until the current one get committed.
  72. func (b *backend) BatchTx() BatchTx {
  73. return b.batchTx
  74. }
  75. // force commit the current batching tx.
  76. func (b *backend) ForceCommit() {
  77. b.batchTx.Commit()
  78. }
  79. func (b *backend) Snapshot() Snapshot {
  80. tx, err := b.db.Begin(false)
  81. if err != nil {
  82. log.Fatalf("storage: cannot begin tx (%s)", err)
  83. }
  84. return &snapshot{tx}
  85. }
  86. func (b *backend) Hash() (uint32, error) {
  87. h := crc32.New(crc32.MakeTable(crc32.Castagnoli))
  88. err := b.db.View(func(tx *bolt.Tx) error {
  89. c := tx.Cursor()
  90. for next, _ := c.First(); next != nil; next, _ = c.Next() {
  91. b := tx.Bucket(next)
  92. if b == nil {
  93. return fmt.Errorf("cannot get hash of bucket %s", string(next))
  94. }
  95. h.Write(next)
  96. b.ForEach(func(k, v []byte) error {
  97. h.Write(k)
  98. h.Write(v)
  99. return nil
  100. })
  101. }
  102. return nil
  103. })
  104. if err != nil {
  105. return 0, err
  106. }
  107. return h.Sum32(), nil
  108. }
  109. func (b *backend) Size() int64 {
  110. return atomic.LoadInt64(&b.size)
  111. }
  112. func (b *backend) run() {
  113. defer close(b.donec)
  114. for {
  115. select {
  116. case <-time.After(b.batchInterval):
  117. case <-b.stopc:
  118. b.batchTx.CommitAndStop()
  119. return
  120. }
  121. b.batchTx.Commit()
  122. }
  123. }
  124. func (b *backend) Close() error {
  125. close(b.stopc)
  126. <-b.donec
  127. return b.db.Close()
  128. }
  129. type snapshot struct {
  130. *bolt.Tx
  131. }
  132. func (s *snapshot) Close() error { return s.Tx.Rollback() }