@@ -42,7 +42,7 @@ const (
42
42
43
43
type WorkspaceOperations struct {
44
44
config content.Config
45
- store * session. Store
45
+ provider * WorkspaceProvider
46
46
backupWorkspaceLimiter chan struct {}
47
47
metrics * content.Metrics
48
48
}
@@ -67,15 +67,15 @@ type DisposeOptions struct {
67
67
SnapshotName string
68
68
}
69
69
70
- func NewWorkspaceOperations (config content.Config , store * session. Store , reg prometheus.Registerer ) (* WorkspaceOperations , error ) {
70
+ func NewWorkspaceOperations (config content.Config , provider * WorkspaceProvider , reg prometheus.Registerer ) (* WorkspaceOperations , error ) {
71
71
waitingTimeHist , waitingTimeoutCounter , err := content .RegisterConcurrentBackupMetrics (reg , "_mk2" )
72
72
if err != nil {
73
73
return nil , err
74
74
}
75
75
76
76
return & WorkspaceOperations {
77
- config : config ,
78
- store : store ,
77
+ config : config ,
78
+ provider : provider ,
79
79
metrics : & content.Metrics {
80
80
BackupWaitingTimeHist : waitingTimeHist ,
81
81
BackupWaitingTimeoutCounter : waitingTimeoutCounter ,
@@ -85,34 +85,26 @@ func NewWorkspaceOperations(config content.Config, store *session.Store, reg pro
85
85
}, nil
86
86
}
87
87
88
- func (wso * WorkspaceOperations ) InitWorkspaceContent (ctx context.Context , options InitContentOptions ) (bool , string , error ) {
89
- res , err := wso .store .NewWorkspace (
90
- ctx , options .Meta .InstanceId , filepath .Join (wso .store .Location , options .Meta .InstanceId ),
88
+ func (wso * WorkspaceOperations ) InitWorkspaceContent (ctx context.Context , options InitContentOptions ) (string , error ) {
89
+ ws , err := wso .provider .Create (ctx , options .Meta .InstanceId , filepath .Join (wso .provider .Location , options .Meta .InstanceId ),
91
90
wso .creator (options .Meta .Owner , options .Meta .WorkspaceId , options .Meta .InstanceId , options .Initializer , false ))
92
- if errors .Is (err , storage .ErrNotFound ) {
93
- return false , "" , nil
94
- }
95
-
96
- if errors .Is (err , session .ErrAlreadyExists ) {
97
- return true , "" , nil
98
- }
99
91
100
92
if err != nil {
101
- return false , "bug: cannot add workspace to store" , xerrors .Errorf ("cannot add workspace to store: %w" , err )
93
+ return "bug: cannot add workspace to store" , xerrors .Errorf ("cannot add workspace to store: %w" , err )
102
94
}
103
95
104
- rs , ok := res .NonPersistentAttrs [session .AttrRemoteStorage ].(storage.DirectAccess )
96
+ rs , ok := ws .NonPersistentAttrs [session .AttrRemoteStorage ].(storage.DirectAccess )
105
97
if rs == nil || ! ok {
106
- return false , "bug: workspace has no remote storage" , xerrors .Errorf ("workspace has no remote storage" )
98
+ return "bug: workspace has no remote storage" , xerrors .Errorf ("workspace has no remote storage" )
107
99
}
108
100
ps , err := storage .NewPresignedAccess (& wso .config .Storage )
109
101
if err != nil {
110
- return false , "bug: no presigned storage available" , xerrors .Errorf ("no presigned storage available: %w" , err )
102
+ return "bug: no presigned storage available" , xerrors .Errorf ("no presigned storage available: %w" , err )
111
103
}
112
104
113
105
remoteContent , err := content .CollectRemoteContent (ctx , rs , ps , options .Meta .Owner , options .Initializer )
114
106
if err != nil {
115
- return false , "remote content error" , xerrors .Errorf ("remote content error: %w" , err )
107
+ return "remote content error" , xerrors .Errorf ("remote content error: %w" , err )
116
108
}
117
109
118
110
// Initialize workspace.
@@ -138,13 +130,18 @@ func (wso *WorkspaceOperations) InitWorkspaceContent(ctx context.Context, option
138
130
},
139
131
}
140
132
141
- err = content .RunInitializer (ctx , res .Location , options .Initializer , remoteContent , opts )
133
+ err = content .RunInitializer (ctx , ws .Location , options .Initializer , remoteContent , opts )
142
134
if err != nil {
143
135
glog .Infof ("error running initializer %v" , err )
144
- return false , err .Error (), err
136
+ return err .Error (), err
137
+ }
138
+
139
+ err = ws .Persist ()
140
+ if err != nil {
141
+ return "cannot persist workspace" , err
145
142
}
146
143
147
- return false , "" , nil
144
+ return "" , nil
148
145
}
149
146
150
147
func (wso * WorkspaceOperations ) creator (owner , workspaceId , instanceId string , init * csapi.WorkspaceInitializer , storageDisabled bool ) session.WorkspaceFactory {
@@ -166,32 +163,22 @@ func (wso *WorkspaceOperations) creator(owner, workspaceId, instanceId string, i
166
163
FullWorkspaceBackup : false ,
167
164
PersistentVolumeClaim : false ,
168
165
RemoteStorageDisabled : storageDisabled ,
166
+ IsMk2 : true ,
169
167
170
168
ServiceLocDaemon : filepath .Join (wso .config .WorkingArea , serviceDirName ),
171
169
ServiceLocNode : filepath .Join (wso .config .WorkingAreaNode , serviceDirName ),
172
170
}, nil
173
171
}
174
172
}
175
173
176
- func (wso * WorkspaceOperations ) DisposeWorkspace (ctx context.Context , opts DisposeOptions ) (bool , * csapi.GitStatus , error ) {
177
- sess := wso .store .Get (opts .Meta .InstanceId )
178
- if sess == nil {
179
- return false , nil , fmt .Errorf ("cannot find workspace %s during DisposeWorkspace" , opts .Meta .InstanceId )
180
- }
181
-
182
- // Maybe there's someone else already trying to dispose the workspace.
183
- // In that case we'll simply wait for that to happen.
184
- done , repo , err := sess .WaitOrMarkForDisposal (ctx )
174
+ func (wso * WorkspaceOperations ) DisposeWorkspace (ctx context.Context , opts DisposeOptions ) (* csapi.GitStatus , error ) {
175
+ ws , err := wso .provider .Get (ctx , opts .Meta .InstanceId )
185
176
if err != nil {
186
- return false , nil , fmt .Errorf ("failed to wait for workspace disposal of %s" , opts .Meta .InstanceId )
187
- }
188
-
189
- if done {
190
- return true , repo , nil
177
+ return nil , fmt .Errorf ("cannot find workspace %s during DisposeWorkspace: %w" , opts .Meta .InstanceId , err )
191
178
}
192
179
193
- if sess .RemoteStorageDisabled {
194
- return false , nil , xerrors .Errorf ("workspace has no remote storage" )
180
+ if ws .RemoteStorageDisabled {
181
+ return nil , fmt .Errorf ("workspace has no remote storage" )
195
182
}
196
183
197
184
if opts .BackupLogs {
@@ -202,14 +189,15 @@ func (wso *WorkspaceOperations) DisposeWorkspace(ctx context.Context, opts Dispo
202
189
}
203
190
}
204
191
205
- err = wso .uploadWorkspaceContent (ctx , sess , opts .SnapshotName )
192
+ err = wso .uploadWorkspaceContent (ctx , ws , opts .SnapshotName )
206
193
if err != nil {
207
- return false , nil , xerrors .Errorf ("final backup failed for workspace %s" , opts .Meta .InstanceId )
194
+ return nil , fmt .Errorf ("final backup failed for workspace %s" , opts .Meta .InstanceId )
208
195
}
209
196
197
+ var repo * csapi.GitStatus
210
198
if opts .UpdateGitStatus {
211
199
// Update the git status prior to deleting the workspace
212
- repo , err = sess .UpdateGitStatus (ctx , false )
200
+ repo , err = ws .UpdateGitStatus (ctx , false )
213
201
if err != nil {
214
202
// do not fail workspace because we were unable to get git status
215
203
// which can happen for various reasons, including user corrupting his .git folder somehow
@@ -219,22 +207,22 @@ func (wso *WorkspaceOperations) DisposeWorkspace(ctx context.Context, opts Dispo
219
207
}
220
208
}
221
209
222
- if err = sess .Dispose (ctx ); err != nil {
210
+ if err = ws .Dispose (ctx , wso . provider . hooks [ session . WorkspaceDisposed ] ); err != nil {
223
211
glog .WithError (err ).Error ("cannot dispose session" )
224
212
}
225
213
226
214
// remove workspace daemon directory in the node
227
- if err := os .RemoveAll (sess .ServiceLocDaemon ); err != nil {
215
+ if err := os .RemoveAll (ws .ServiceLocDaemon ); err != nil {
228
216
glog .WithError (err ).Error ("cannot delete workspace daemon directory" )
229
217
}
230
218
231
- return false , repo , nil
219
+ return repo , nil
232
220
}
233
221
234
- func (wso * WorkspaceOperations ) SnapshotIDs (workspaceID string ) (snapshotUrl , snapshotName string , err error ) {
235
- sess := wso .store .Get (workspaceID )
236
- if sess = = nil {
237
- return "" , "" , fmt .Errorf ("cannot find workspace %s during SnapshotName" , workspaceID )
222
+ func (wso * WorkspaceOperations ) SnapshotIDs (ctx context. Context , workspaceID string ) (snapshotUrl , snapshotName string , err error ) {
223
+ sess , err := wso .provider .Get (ctx , workspaceID )
224
+ if err ! = nil {
225
+ return "" , "" , fmt .Errorf ("cannot find workspace %s during SnapshotName: %w " , workspaceID , err )
238
226
}
239
227
240
228
baseName := fmt .Sprintf ("snapshot-%d" , time .Now ().UnixNano ())
@@ -258,16 +246,16 @@ func (wso *WorkspaceOperations) TakeSnapshot(ctx context.Context, workspaceID, s
258
246
return fmt .Errorf ("workspaceID is required" )
259
247
}
260
248
261
- sess := wso .store .Get (workspaceID )
262
- if sess = = nil {
249
+ ws , err := wso .provider .Get (ctx , workspaceID )
250
+ if err ! = nil {
263
251
return fmt .Errorf ("cannot find workspace %s during DisposeWorkspace" , workspaceID )
264
252
}
265
253
266
- if sess .RemoteStorageDisabled {
254
+ if ws .RemoteStorageDisabled {
267
255
return fmt .Errorf ("workspace has no remote storage" )
268
256
}
269
257
270
- err = wso .uploadWorkspaceContent (ctx , sess , snapshotName )
258
+ err = wso .uploadWorkspaceContent (ctx , ws , snapshotName )
271
259
if err != nil {
272
260
return fmt .Errorf ("snapshot failed for workspace %s" , workspaceID )
273
261
}
@@ -457,7 +445,6 @@ func (wsc *WorkspaceController) UpdateGitStatus(ctx context.Context, ws *workspa
457
445
return
458
446
}
459
447
460
- glog .Infof ("GIT LOCATION IS %v" , loc )
461
448
loc = filepath .Join (loc , s .CheckoutLocation )
462
449
if ! git .IsWorkingCopy (loc ) {
463
450
glog .WithField ("loc" , loc ).WithField ("checkout location" , s .CheckoutLocation ).WithFields (s .OWI ()).Debug ("did not find a Git working copy - not updating Git status" )
0 commit comments