snapshot.go 895 B

123456789101112131415161718192021222324252627282930313233343536
  1. package main
  2. import (
  3. "time"
  4. )
  5. // basic conf.
  6. // TODO: find a good policy to do snapshot
  7. type snapshotConf struct {
  8. // Etcd will check if snapshot is need every checkingInterval
  9. checkingInterval time.Duration
  10. // The number of writes when the last snapshot happened
  11. lastWrites uint64
  12. // If the incremental number of writes since the last snapshot
  13. // exceeds the write Threshold, etcd will do a snapshot
  14. writesThr uint64
  15. }
  16. var snapConf *snapshotConf
  17. func newSnapshotConf() *snapshotConf {
  18. // check snapshot every 3 seconds and the threshold is 20K
  19. return &snapshotConf{time.Second * 3, etcdStore.TotalWrites(), 20 * 1000}
  20. }
  21. func monitorSnapshot() {
  22. for {
  23. time.Sleep(snapConf.checkingInterval)
  24. currentWrites := etcdStore.TotalWrites() - snapConf.lastWrites
  25. if currentWrites > snapConf.writesThr {
  26. r.TakeSnapshot()
  27. snapConf.lastWrites = etcdStore.TotalWrites()
  28. }
  29. }
  30. }