snapshot.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. Copyright 2013 CoreOS Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "time"
  16. )
  17. // basic conf.
  18. // TODO: find a good policy to do snapshot
  19. type snapshotConf struct {
  20. // Etcd will check if snapshot is need every checkingInterval
  21. checkingInterval time.Duration
  22. // The number of writes when the last snapshot happened
  23. lastWrites uint64
  24. // If the incremental number of writes since the last snapshot
  25. // exceeds the write Threshold, etcd will do a snapshot
  26. writesThr uint64
  27. }
  28. var snapConf *snapshotConf
  29. func newSnapshotConf() *snapshotConf {
  30. // check snapshot every 3 seconds and the threshold is 20K
  31. return &snapshotConf{time.Second * 3, etcdStore.TotalWrites(), 20 * 1000}
  32. }
  33. func monitorSnapshot() {
  34. for {
  35. time.Sleep(snapConf.checkingInterval)
  36. currentWrites := etcdStore.TotalWrites() - snapConf.lastWrites
  37. if currentWrites > snapConf.writesThr {
  38. r.TakeSnapshot()
  39. snapConf.lastWrites = etcdStore.TotalWrites()
  40. }
  41. }
  42. }