github.com/kafkaliu/etcd@v0.1.2-0.20131007164923-44c16dd30d69/snapshot.go (about)

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