backend.go 3.1 KB

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