backend.go 2.8 KB

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