Commit 2df76d73 authored by Kevin Atkinson's avatar Kevin Atkinson

Insure disk usage is written to disk within 2 seconds.

parent 69ad27b5
...@@ -28,6 +28,7 @@ var log = logging.Logger("flatfs") ...@@ -28,6 +28,7 @@ var log = logging.Logger("flatfs")
const ( const (
extension = ".data" extension = ".data"
diskUsageCheckpointPercent = 1.0 diskUsageCheckpointPercent = 1.0
diskUsageCheckpointTimeout = 2.0 * time.Second
) )
var ( var (
...@@ -804,28 +805,38 @@ func (fs *Datastore) checkpointDiskUsage() { ...@@ -804,28 +805,38 @@ func (fs *Datastore) checkpointDiskUsage() {
} }
func (fs *Datastore) checkpointLoop() { func (fs *Datastore) checkpointLoop() {
timerActive := true
timer := time.NewTimer(0)
for { for {
_, more := <-fs.checkpointCh select {
fs.dirty = true case _, more := <-fs.checkpointCh:
du := atomic.LoadInt64(&fs.diskUsage) du := atomic.LoadInt64(&fs.diskUsage)
if more { if !more { // shutting down
fs.writeDiskUsageFile(du)
fs.done <- true
return
}
fs.dirty = true
// If the difference between the checkpointed disk usage and
// current one is larger than than `diskUsageCheckpointPercent`
// of the checkpointed: store it.
newDu := float64(du) newDu := float64(du)
lastCheckpointDu := float64(fs.storedValue.diskUsage) lastCheckpointDu := float64(fs.storedValue.diskUsage)
diff := math.Abs(newDu - lastCheckpointDu) diff := math.Abs(newDu - lastCheckpointDu)
// If the difference between the checkpointed disk usage and
// current one is larger than than 1% of the checkpointed: store it.
if (lastCheckpointDu * diskUsageCheckpointPercent / 100.0) < diff { if (lastCheckpointDu * diskUsageCheckpointPercent / 100.0) < diff {
fs.writeDiskUsageFile(du) fs.writeDiskUsageFile(du)
} }
// Otherwise insure the value will be written to disk after
// FIXME: If dirty set a timer to write the diskusage // `diskUsageCheckpointTimeout`
// anyway after X seconds of inactivity. if fs.dirty && !timerActive {
} else { timer.Reset(diskUsageCheckpointTimeout)
// shutting down, write the final value }
case <-timer.C:
timerActive = false
if fs.dirty {
du := atomic.LoadInt64(&fs.diskUsage)
fs.writeDiskUsageFile(du) fs.writeDiskUsageFile(du)
fs.done <- true }
return
} }
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment