watcher.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. package zfs
  15. import (
  16. "fmt"
  17. "sync"
  18. "time"
  19. zfs "github.com/mistifyio/go-zfs"
  20. "k8s.io/klog"
  21. )
  22. // zfsWatcher maintains a cache of filesystem -> usage stats for a
  23. // zfs filesystem
  24. type ZfsWatcher struct {
  25. filesystem string
  26. lock *sync.RWMutex
  27. cache map[string]uint64
  28. period time.Duration
  29. stopChan chan struct{}
  30. }
  31. // NewThinPoolWatcher returns a new ThinPoolWatcher for the given devicemapper
  32. // thin pool name and metadata device or an error.
  33. func NewZfsWatcher(filesystem string) (*ZfsWatcher, error) {
  34. return &ZfsWatcher{
  35. filesystem: filesystem,
  36. lock: &sync.RWMutex{},
  37. cache: make(map[string]uint64),
  38. period: 15 * time.Second,
  39. stopChan: make(chan struct{}),
  40. }, nil
  41. }
  42. // Start starts the ZfsWatcher.
  43. func (w *ZfsWatcher) Start() {
  44. err := w.Refresh()
  45. if err != nil {
  46. klog.Errorf("encountered error refreshing zfs watcher: %v", err)
  47. }
  48. for {
  49. select {
  50. case <-w.stopChan:
  51. return
  52. case <-time.After(w.period):
  53. start := time.Now()
  54. err = w.Refresh()
  55. if err != nil {
  56. klog.Errorf("encountered error refreshing zfs watcher: %v", err)
  57. }
  58. // print latency for refresh
  59. duration := time.Since(start)
  60. klog.V(5).Infof("zfs(%d) took %s", start.Unix(), duration)
  61. }
  62. }
  63. }
  64. // Stop stops the ZfsWatcher.
  65. func (w *ZfsWatcher) Stop() {
  66. close(w.stopChan)
  67. }
  68. // GetUsage gets the cached usage value of the given filesystem.
  69. func (w *ZfsWatcher) GetUsage(filesystem string) (uint64, error) {
  70. w.lock.RLock()
  71. defer w.lock.RUnlock()
  72. v, ok := w.cache[filesystem]
  73. if !ok {
  74. return 0, fmt.Errorf("no cached value for usage of filesystem %v", filesystem)
  75. }
  76. return v, nil
  77. }
  78. // Refresh performs a zfs get
  79. func (w *ZfsWatcher) Refresh() error {
  80. w.lock.Lock()
  81. defer w.lock.Unlock()
  82. newCache := make(map[string]uint64)
  83. parent, err := zfs.GetDataset(w.filesystem)
  84. if err != nil {
  85. klog.Errorf("encountered error getting zfs filesystem: %s: %v", w.filesystem, err)
  86. return err
  87. }
  88. children, err := parent.Children(0)
  89. if err != nil {
  90. klog.Errorf("encountered error getting children of zfs filesystem: %s: %v", w.filesystem, err)
  91. return err
  92. }
  93. for _, ds := range children {
  94. newCache[ds.Name] = ds.Used
  95. }
  96. w.cache = newCache
  97. return nil
  98. }