@@ -110,36 +110,62 @@ func (ctrl *FSScrubController) Run(ctx context.Context, r controller.Runtime, lo
110110 logger .Error ("error running filesystem scrub" , zap .Error (err ))
111111 }
112112 case <- r .EventCh ():
113- err := ctrl .updateSchedule (ctx , r , logger )
113+ err := ctrl .updateSchedule (ctx , r )
114114 if err != nil {
115115 return err
116116 }
117117 }
118118
119- r .StartTrackingOutputs ()
119+ if err := ctrl .reportStatus (ctx , r ); err != nil {
120+ return err
121+ }
122+ }
123+ }
120124
121- for _ , entry := range ctrl .status {
122- if err := safe .WriterModify (ctx , r , runtimeres .NewFSScrubStatus (entry .id ), func (status * runtimeres.FSScrubStatus ) error {
123- status .TypedSpec ().Mountpoint = entry .mountpoint
124- status .TypedSpec ().Period = entry .period
125- status .TypedSpec ().Time = entry .time
126- status .TypedSpec ().Duration = entry .duration
127- status .TypedSpec ().Status = entry .result .Error ()
125+ func (ctrl * FSScrubController ) reportStatus (ctx context.Context , r controller.Runtime ) error {
126+ r .StartTrackingOutputs ()
128127
129- return nil
130- }); err != nil {
131- return fmt .Errorf ("error updating filesystem scrub status: %w" , err )
128+ presentStatuses , err := safe .ReaderListAll [* runtimeres.FSScrubStatus ](ctx , r )
129+ if err != nil && ! state .IsNotFoundError (err ) {
130+ return fmt .Errorf ("error getting existing FS scrub statuses: %w" , err )
131+ }
132+
133+ for entry := range presentStatuses .All () {
134+ if _ , ok := ctrl .status [entry .TypedSpec ().Mountpoint ]; ! ok {
135+ if err := r .Destroy (ctx , runtimeres .NewFSScrubStatus (entry .Metadata ().ID ()).Metadata ()); err != nil {
136+ return fmt .Errorf ("error destroying old FS scrub status: %w" , err )
132137 }
133138 }
139+ }
134140
135- if err := safe .CleanupOutputs [* runtimeres.FSScrubStatus ](ctx , r ); err != nil {
136- return err
141+ for _ , entry := range ctrl .status {
142+ if err := safe .WriterModify (ctx , r , runtimeres .NewFSScrubStatus (entry .id ), func (status * runtimeres.FSScrubStatus ) error {
143+ status .TypedSpec ().Mountpoint = entry .mountpoint
144+ status .TypedSpec ().Period = entry .period
145+ status .TypedSpec ().Time = entry .time
146+ status .TypedSpec ().Duration = entry .duration
147+
148+ if entry .result != nil {
149+ status .TypedSpec ().Status = entry .result .Error ()
150+ } else {
151+ status .TypedSpec ().Status = "success"
152+ }
153+
154+ return nil
155+ }); err != nil {
156+ return fmt .Errorf ("error updating filesystem scrub status: %w" , err )
137157 }
138158 }
159+
160+ if err := safe .CleanupOutputs [* runtimeres.FSScrubStatus ](ctx , r ); err != nil {
161+ return err
162+ }
163+
164+ return nil
139165}
140166
141167//nolint:gocyclo,cyclop
142- func (ctrl * FSScrubController ) updateSchedule (ctx context.Context , r controller.Runtime , logger * zap. Logger ) error {
168+ func (ctrl * FSScrubController ) updateSchedule (ctx context.Context , r controller.Runtime ) error {
143169 volumesStatus , err := safe .ReaderListAll [* block.VolumeStatus ](ctx , r )
144170 if err != nil && ! state .IsNotFoundError (err ) {
145171 return fmt .Errorf ("error getting volume status: %w" , err )
@@ -207,8 +233,6 @@ func (ctrl *FSScrubController) updateSchedule(ctx context.Context, r controller.
207233 _ , ok := ctrl .schedule [mountpoint ]
208234
209235 if period == nil {
210- logger .Warn ("!!! scrub !!! not in config, descheduling" , zap .String ("mountpoint" , mountpoint ))
211-
212236 if ok {
213237 ctrl .cancelScrub (mountpoint )
214238 }
@@ -218,12 +242,10 @@ func (ctrl *FSScrubController) updateSchedule(ctx context.Context, r controller.
218242
219243 if ! ok {
220244 firstTimeout := time .Duration (rand .Int64N (int64 (period .Seconds ()))) * time .Second
221- logger .Warn ("!!! scrub !!! firstTimeout" , zap .Duration ("firstTimeout" , firstTimeout ))
222245
223246 // When scheduling the first scrub, we use a random time to avoid all scrubs running in a row.
224247 // After the first scrub, we use the period defined in the config.
225248 cb := func () {
226- logger .Warn ("!!! scrub !!! ding" , zap .String ("path" , mountpoint ))
227249 ctrl .c <- mountpoint
228250 ctrl .schedule [mountpoint ].timer .Reset (ctrl .schedule [mountpoint ].period )
229251 }
@@ -242,12 +264,8 @@ func (ctrl *FSScrubController) updateSchedule(ctx context.Context, r controller.
242264 duration : 0 ,
243265 result : fmt .Errorf ("scheduled" ),
244266 }
245-
246- logger .Warn ("!!! scrub !!! scheduled" , zap .String ("path" , mountpoint ), zap .Duration ("period" , * period ))
247267 } else if ctrl .schedule [mountpoint ].period != * period {
248268 // reschedule if period has changed
249- logger .Warn ("!!! scrub !!! reschedule" , zap .String ("path" , mountpoint ), zap .Duration ("period" , * period ))
250-
251269 ctrl .schedule [mountpoint ].timer .Stop ()
252270 ctrl .schedule [mountpoint ].timer .Reset (* period )
253271 ctrl .schedule [mountpoint ] = scrubSchedule {
@@ -259,7 +277,7 @@ func (ctrl *FSScrubController) updateSchedule(ctx context.Context, r controller.
259277 id : item .Metadata ().ID (),
260278 mountpoint : mountpoint ,
261279 period : * period ,
262- time : time . Now (). Add ( * period ) ,
280+ time : ctrl . status [ mountpoint ]. time ,
263281 duration : ctrl .status [mountpoint ].duration ,
264282 result : ctrl .status [mountpoint ].result ,
265283 }
@@ -304,7 +322,7 @@ func (ctrl *FSScrubController) runScrub(mountpoint string, opts []string) error
304322 mountpoint : mountpoint ,
305323 period : ctrl .schedule [mountpoint ].period ,
306324 time : start ,
307- duration : time .Now (). Sub (start ),
325+ duration : time .Since (start ),
308326 result : err ,
309327 }
310328
0 commit comments