forked from google/cadvisor
-
Notifications
You must be signed in to change notification settings - Fork 3
/
watcher.go
113 lines (98 loc) · 2.78 KB
/
watcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zfs
import (
"fmt"
"sync"
"time"
"github.com/golang/glog"
zfs "github.com/mistifyio/go-zfs"
)
// zfsWatcher maintains a cache of filesystem -> usage stats for a
// zfs filesystem
type ZfsWatcher struct {
filesystem string
lock *sync.RWMutex
cache map[string]uint64
period time.Duration
stopChan chan struct{}
}
// NewThinPoolWatcher returns a new ThinPoolWatcher for the given devicemapper
// thin pool name and metadata device or an error.
func NewZfsWatcher(filesystem string) (*ZfsWatcher, error) {
return &ZfsWatcher{
filesystem: filesystem,
lock: &sync.RWMutex{},
cache: make(map[string]uint64),
period: 15 * time.Second,
stopChan: make(chan struct{}),
}, nil
}
// Start starts the ZfsWatcher.
func (w *ZfsWatcher) Start() {
err := w.Refresh()
if err != nil {
glog.Errorf("encountered error refreshing zfs watcher: %v", err)
}
for {
select {
case <-w.stopChan:
return
case <-time.After(w.period):
start := time.Now()
err = w.Refresh()
if err != nil {
glog.Errorf("encountered error refreshing zfs watcher: %v", err)
}
// print latency for refresh
duration := time.Since(start)
glog.V(5).Infof("zfs(%d) took %s", start.Unix(), duration)
}
}
}
// Stop stops the ZfsWatcher.
func (w *ZfsWatcher) Stop() {
close(w.stopChan)
}
// GetUsage gets the cached usage value of the given filesystem.
func (w *ZfsWatcher) GetUsage(filesystem string) (uint64, error) {
w.lock.RLock()
defer w.lock.RUnlock()
v, ok := w.cache[filesystem]
if !ok {
return 0, fmt.Errorf("no cached value for usage of filesystem %v", filesystem)
}
return v, nil
}
// Refresh performs a zfs get
func (w *ZfsWatcher) Refresh() error {
w.lock.Lock()
defer w.lock.Unlock()
newCache := make(map[string]uint64)
parent, err := zfs.GetDataset(w.filesystem)
if err != nil {
glog.Errorf("encountered error getting zfs filesystem: %s: %v", w.filesystem, err)
return err
}
children, err := parent.Children(0)
if err != nil {
glog.Errorf("encountered error getting children of zfs filesystem: %s: %v", w.filesystem, err)
return err
}
for _, ds := range children {
newCache[ds.Name] = ds.Used
}
w.cache = newCache
return nil
}