backend.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. "io/ioutil"
  20. "log"
  21. "os"
  22. "path"
  23. "sync/atomic"
  24. "time"
  25. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/boltdb/bolt"
  26. )
  27. var (
  28. defaultBatchLimit = 10000
  29. defaultBatchInterval = 100 * time.Millisecond
  30. // InitialMmapSize is the initial size of the mmapped region. Setting this larger than
  31. // the potential max db size can prevent writer from blocking reader.
  32. // This only works for linux.
  33. InitialMmapSize = 10 * 1024 * 1024 * 1024
  34. )
  35. type Backend interface {
  36. BatchTx() BatchTx
  37. Snapshot() Snapshot
  38. Hash() (uint32, error)
  39. // Size returns the current size of the backend.
  40. Size() int64
  41. ForceCommit()
  42. Close() error
  43. }
  44. type Snapshot interface {
  45. // Size gets the size of the snapshot.
  46. Size() int64
  47. // WriteTo writes the snapshot into the given writer.
  48. WriteTo(w io.Writer) (n int64, err error)
  49. // Close closes the snapshot.
  50. Close() error
  51. }
  52. type backend struct {
  53. db *bolt.DB
  54. batchInterval time.Duration
  55. batchLimit int
  56. batchTx *batchTx
  57. size int64
  58. // number of commits since start
  59. commits int64
  60. stopc chan struct{}
  61. donec chan struct{}
  62. }
  63. func New(path string, d time.Duration, limit int) Backend {
  64. return newBackend(path, d, limit)
  65. }
  66. func NewDefaultBackend(path string) Backend {
  67. return newBackend(path, defaultBatchInterval, defaultBatchLimit)
  68. }
  69. func newBackend(path string, d time.Duration, limit int) *backend {
  70. db, err := bolt.Open(path, 0600, boltOpenOptions)
  71. if err != nil {
  72. log.Panicf("backend: cannot open database at %s (%v)", path, err)
  73. }
  74. b := &backend{
  75. db: db,
  76. batchInterval: d,
  77. batchLimit: limit,
  78. stopc: make(chan struct{}),
  79. donec: make(chan struct{}),
  80. }
  81. b.batchTx = newBatchTx(b)
  82. go b.run()
  83. return b
  84. }
  85. // BatchTx returns the current batch tx in coalescer. The tx can be used for read and
  86. // write operations. The write result can be retrieved within the same tx immediately.
  87. // The write result is isolated with other txs until the current one get committed.
  88. func (b *backend) BatchTx() BatchTx {
  89. return b.batchTx
  90. }
  91. // ForceCommit forces the current batching tx to commit.
  92. func (b *backend) ForceCommit() {
  93. b.batchTx.Commit()
  94. }
  95. func (b *backend) Snapshot() Snapshot {
  96. b.batchTx.Commit()
  97. tx, err := b.db.Begin(false)
  98. if err != nil {
  99. log.Fatalf("storage: cannot begin tx (%s)", err)
  100. }
  101. return &snapshot{tx}
  102. }
  103. func (b *backend) Hash() (uint32, error) {
  104. h := crc32.New(crc32.MakeTable(crc32.Castagnoli))
  105. err := b.db.View(func(tx *bolt.Tx) error {
  106. c := tx.Cursor()
  107. for next, _ := c.First(); next != nil; next, _ = c.Next() {
  108. b := tx.Bucket(next)
  109. if b == nil {
  110. return fmt.Errorf("cannot get hash of bucket %s", string(next))
  111. }
  112. h.Write(next)
  113. b.ForEach(func(k, v []byte) error {
  114. h.Write(k)
  115. h.Write(v)
  116. return nil
  117. })
  118. }
  119. return nil
  120. })
  121. if err != nil {
  122. return 0, err
  123. }
  124. return h.Sum32(), nil
  125. }
  126. func (b *backend) Size() int64 {
  127. return atomic.LoadInt64(&b.size)
  128. }
  129. func (b *backend) run() {
  130. defer close(b.donec)
  131. for {
  132. select {
  133. case <-time.After(b.batchInterval):
  134. case <-b.stopc:
  135. b.batchTx.CommitAndStop()
  136. return
  137. }
  138. b.batchTx.Commit()
  139. }
  140. }
  141. func (b *backend) Close() error {
  142. close(b.stopc)
  143. <-b.donec
  144. return b.db.Close()
  145. }
  146. // Commits returns total number of commits since start
  147. func (b *backend) Commits() int64 {
  148. return atomic.LoadInt64(&b.commits)
  149. }
  150. // NewTmpBackend creates a backend implementation for testing.
  151. func NewTmpBackend(batchInterval time.Duration, batchLimit int) (*backend, string) {
  152. dir, err := ioutil.TempDir(os.TempDir(), "etcd_backend_test")
  153. if err != nil {
  154. log.Fatal(err)
  155. }
  156. tmpPath := path.Join(dir, "database")
  157. return newBackend(tmpPath, batchInterval, batchLimit), tmpPath
  158. }
  159. func NewDefaultTmpBackend() (*backend, string) {
  160. return NewTmpBackend(defaultBatchInterval, defaultBatchLimit)
  161. }
  162. type snapshot struct {
  163. *bolt.Tx
  164. }
  165. func (s *snapshot) Close() error { return s.Tx.Rollback() }