github.com/alpe/etcd@v0.1.2-0.20130915230056-09f31af88aeb/snapshot.go (about)

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