@@ -22,7 +22,8 @@ import (
22
22
"github.com/golang/glog"
23
23
)
24
24
25
- // ThinPoolWatcher maintains a cache of device name -> usage stats for a devicemapper thin-pool using thin_ls.
25
+ // ThinPoolWatcher maintains a cache of device name -> usage stats for a
26
+ // devicemapper thin-pool using thin_ls.
26
27
type ThinPoolWatcher struct {
27
28
poolName string
28
29
metadataDevice string
@@ -34,20 +35,26 @@ type ThinPoolWatcher struct {
34
35
thinLsClient thinLsClient
35
36
}
36
37
37
- // NewThinPoolWatcher returns a new ThinPoolWatcher for the given devicemapper thin pool name and metadata device.
38
- func NewThinPoolWatcher (poolName , metadataDevice string ) * ThinPoolWatcher {
38
+ // NewThinPoolWatcher returns a new ThinPoolWatcher for the given devicemapper
39
+ // thin pool name and metadata device or an error.
40
+ func NewThinPoolWatcher (poolName , metadataDevice string ) (* ThinPoolWatcher , error ) {
41
+ thinLsClient , err := newThinLsClient ()
42
+ if err != nil {
43
+ return nil , fmt .Errorf ("encountered error creating thin_ls client: %v" , err )
44
+ }
45
+
39
46
return & ThinPoolWatcher {poolName : poolName ,
40
47
metadataDevice : metadataDevice ,
41
48
lock : & sync.RWMutex {},
42
49
cache : make (map [string ]uint64 ),
43
50
period : 15 * time .Second ,
44
51
stopChan : make (chan struct {}),
45
52
dmsetup : NewDmsetupClient (),
46
- thinLsClient : newThinLsClient () ,
47
- }
53
+ thinLsClient : thinLsClient ,
54
+ }, nil
48
55
}
49
56
50
- // Start starts the thin pool watcher .
57
+ // Start starts the ThinPoolWatcher .
51
58
func (w * ThinPoolWatcher ) Start () {
52
59
err := w .Refresh ()
53
60
if err != nil {
@@ -72,6 +79,7 @@ func (w *ThinPoolWatcher) Start() {
72
79
}
73
80
}
74
81
82
+ // Stop stops the ThinPoolWatcher.
75
83
func (w * ThinPoolWatcher ) Stop () {
76
84
close (w .stopChan )
77
85
}
@@ -80,6 +88,7 @@ func (w *ThinPoolWatcher) Stop() {
80
88
func (w * ThinPoolWatcher ) GetUsage (deviceId string ) (uint64 , error ) {
81
89
w .lock .RLock ()
82
90
defer w .lock .RUnlock ()
91
+
83
92
v , ok := w .cache [deviceId ]
84
93
if ! ok {
85
94
return 0 , fmt .Errorf ("no cached value for usage of device %v" , deviceId )
@@ -115,7 +124,8 @@ func (w *ThinPoolWatcher) Refresh() error {
115
124
}
116
125
117
126
glog .Infof ("reserving metadata snapshot for thin-pool %v" , w .poolName )
118
- // NOTE: "0" in the call below is for the 'sector' argument to 'dmsetup message'. It's not needed for thin pools.
127
+ // NOTE: "0" in the call below is for the 'sector' argument to 'dmsetup
128
+ // message'. It's not needed for thin pools.
119
129
if output , err := w .dmsetup .Message (w .poolName , 0 , reserveMetadataMessage ); err != nil {
120
130
err = fmt .Errorf ("error reserving metadata for thin-pool %v: %v output: %v" , w .poolName , err , string (output ))
121
131
return err
@@ -144,7 +154,8 @@ const (
144
154
thinPoolDmsetupStatusHeldMetadataRoot = 6
145
155
)
146
156
147
- // checkReservation checks to see whether the thin device is currently holding userspace metadata.
157
+ // checkReservation checks to see whether the thin device is currently holding
158
+ // userspace metadata.
148
159
func (w * ThinPoolWatcher ) checkReservation (poolName string ) (bool , error ) {
149
160
glog .V (5 ).Infof ("checking whether the thin-pool is holding a metadata snapshot" )
150
161
output , err := w .dmsetup .Status (poolName )
@@ -153,7 +164,8 @@ func (w *ThinPoolWatcher) checkReservation(poolName string) (bool, error) {
153
164
}
154
165
155
166
tokens := strings .Split (string (output ), " " )
156
- // Split returns the input as the last item in the result, adjust the number of tokens by one
167
+ // Split returns the input as the last item in the result, adjust the
168
+ // number of tokens by one
157
169
if len (tokens ) != thinPoolDmsetupStatusTokens + 1 {
158
170
return false , fmt .Errorf ("unexpected output of dmsetup status command; expected 11 fields, got %v; output: %v" , len (tokens ), string (output ))
159
171
}
0 commit comments