Skip to content

Commit 81e0623

Browse files
Furistoroboquat
authored andcommitted
[ws-daemon] Review comments
- Reuse workspace - Update metric name - Check if backup timed out - Ensure temp directory is deleted
1 parent fb41408 commit 81e0623

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

components/ws-daemon/pkg/controller/controller.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ import (
3232
"sigs.k8s.io/controller-runtime/pkg/predicate"
3333
)
3434

35-
// DefaultRetry is the recommended retry for a conflict where multiple clients
36-
// are making changes to the same resource.
3735
var retryParams = wait.Backoff{
3836
Steps: 10,
3937
Duration: 10 * time.Millisecond,
@@ -176,29 +174,28 @@ func (wsc *WorkspaceController) handleWorkspaceInit(ctx context.Context, ws *wor
176174
}
177175

178176
err = retry.RetryOnConflict(retryParams, func() error {
179-
var workspace workspacev1.Workspace
180-
if err := wsc.Get(ctx, req.NamespacedName, &workspace); err != nil {
177+
if err := wsc.Get(ctx, req.NamespacedName, ws); err != nil {
181178
return err
182179
}
183180

184181
if failure != "" {
185-
workspace.Status.Conditions = wsk8s.AddUniqueCondition(ws.Status.Conditions, metav1.Condition{
182+
ws.Status.Conditions = wsk8s.AddUniqueCondition(ws.Status.Conditions, metav1.Condition{
186183
Type: string(workspacev1.WorkspaceConditionContentReady),
187184
Status: metav1.ConditionFalse,
188185
Message: failure,
189186
Reason: "InitializationFailure",
190187
LastTransitionTime: metav1.Now(),
191188
})
192189
} else {
193-
workspace.Status.Conditions = wsk8s.AddUniqueCondition(ws.Status.Conditions, metav1.Condition{
190+
ws.Status.Conditions = wsk8s.AddUniqueCondition(ws.Status.Conditions, metav1.Condition{
194191
Type: string(workspacev1.WorkspaceConditionContentReady),
195192
Status: metav1.ConditionTrue,
196193
Reason: "InitializationSuccess",
197194
LastTransitionTime: metav1.Now(),
198195
})
199196
}
200197

201-
return wsc.Status().Update(ctx, &workspace)
198+
return wsc.Status().Update(ctx, ws)
202199
})
203200

204201
return ctrl.Result{}, err
@@ -243,31 +240,30 @@ func (wsc *WorkspaceController) handleWorkspaceStop(ctx context.Context, ws *wor
243240
}
244241

245242
err = retry.RetryOnConflict(retryParams, func() error {
246-
var workspace workspacev1.Workspace
247-
if err := wsc.Get(ctx, req.NamespacedName, &workspace); err != nil {
243+
if err := wsc.Get(ctx, req.NamespacedName, ws); err != nil {
248244
return err
249245
}
250246

251-
workspace.Status.GitStatus = toWorkspaceGitStatus(gitStatus)
247+
ws.Status.GitStatus = toWorkspaceGitStatus(gitStatus)
252248

253249
if disposeErr != nil {
254-
workspace.Status.Conditions = wsk8s.AddUniqueCondition(ws.Status.Conditions, metav1.Condition{
250+
ws.Status.Conditions = wsk8s.AddUniqueCondition(ws.Status.Conditions, metav1.Condition{
255251
Type: string(workspacev1.WorkspaceConditionBackupFailure),
256252
Status: metav1.ConditionTrue,
257253
Reason: "BackupFailed",
258254
Message: disposeErr.Error(),
259255
LastTransitionTime: metav1.Now(),
260256
})
261257
} else {
262-
workspace.Status.Conditions = wsk8s.AddUniqueCondition(ws.Status.Conditions, metav1.Condition{
258+
ws.Status.Conditions = wsk8s.AddUniqueCondition(ws.Status.Conditions, metav1.Condition{
263259
Type: string(workspacev1.WorkspaceConditionBackupComplete),
264260
Status: metav1.ConditionTrue,
265261
Reason: "BackupComplete",
266262
LastTransitionTime: metav1.Now(),
267263
})
268264
}
269265

270-
return wsc.Status().Update(ctx, &workspace)
266+
return wsc.Status().Update(ctx, ws)
271267
})
272268

273269
return ctrl.Result{}, err

components/ws-daemon/pkg/controller/workspace_operations.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type DisposeOptions struct {
6666
}
6767

6868
func NewWorkspaceOperations(config content.Config, store *session.Store, reg prometheus.Registerer) (*WorkspaceOperations, error) {
69-
waitingTimeHist, waitingTimeoutCounter, err := content.RegisterConcurrentBackupMetrics(reg, "mk2")
69+
waitingTimeHist, waitingTimeoutCounter, err := content.RegisterConcurrentBackupMetrics(reg, "_mk2")
7070
if err != nil {
7171
return nil, err
7272
}
@@ -274,19 +274,27 @@ func (wso *WorkspaceOperations) uploadWorkspaceLogs(ctx context.Context, opts Di
274274
func (wso *WorkspaceOperations) uploadWorkspaceContent(ctx context.Context, sess *session.Workspace) error {
275275
var backupName = storage.DefaultBackup
276276
// Avoid too many simultaneous backups in order to avoid excessive memory utilization.
277+
var timedOut bool
277278
waitStart := time.Now()
278279
select {
279280
case wso.backupWorkspaceLimiter <- struct{}{}:
280281
case <-time.After(15 * time.Minute):
281282
// we timed out on the rate limit - let's upload anyways, because we don't want to actually block
282283
// an upload. If we reach this point, chances are other things are broken. No upload should ever
283284
// take this long.
285+
timedOut = true
284286
wso.metrics.BackupWaitingTimeoutCounter.Inc()
285287
}
286288

287-
wso.metrics.BackupWaitingTimeHist.Observe(time.Since(waitStart).Seconds())
289+
waitTime := time.Since(waitStart)
290+
wso.metrics.BackupWaitingTimeHist.Observe(waitTime.Seconds())
288291

289292
defer func() {
293+
// timeout -> we did not add to the limiter
294+
if timedOut {
295+
return
296+
}
297+
290298
<-wso.backupWorkspaceLimiter
291299
}()
292300

@@ -312,12 +320,24 @@ func (wso *WorkspaceOperations) uploadWorkspaceContent(ctx context.Context, sess
312320
tmpfSize int64
313321
)
314322

323+
defer func() {
324+
if tmpf != nil {
325+
os.Remove(tmpf.Name())
326+
}
327+
}()
328+
315329
err = retryIfErr(ctx, wso.config.Backup.Attempts, glog.WithFields(sess.OWI()).WithField("op", "create archive"), func(ctx context.Context) (err error) {
316330
tmpf, err = os.CreateTemp(wso.config.TmpDir, fmt.Sprintf("wsbkp-%s-*.tar", sess.InstanceID))
317331
if err != nil {
318332
return
319333
}
320-
defer tmpf.Close()
334+
335+
defer func() {
336+
tmpf.Close()
337+
if err != nil {
338+
os.Remove(tmpf.Name())
339+
}
340+
}()
321341

322342
var opts []archive.TarOption
323343
opts = append(opts)
@@ -357,12 +377,6 @@ func (wso *WorkspaceOperations) uploadWorkspaceContent(ctx context.Context, sess
357377
if err != nil {
358378
return xerrors.Errorf("cannot create archive: %w", err)
359379
}
360-
defer func() {
361-
if tmpf != nil {
362-
// always remove the archive file to not fill up the node needlessly
363-
os.Remove(tmpf.Name())
364-
}
365-
}()
366380

367381
err = retryIfErr(ctx, wso.config.Backup.Attempts, glog.WithFields(sess.OWI()).WithField("op", "upload layer"), func(ctx context.Context) (err error) {
368382
_, _, err = rs.Upload(ctx, tmpf.Name(), backupName, opts...)

0 commit comments

Comments
 (0)