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