@@ -41,8 +41,9 @@ const (
41
41
)
42
42
43
43
type WorkspaceOperations struct {
44
- config content.Config
45
- store * session.Store
44
+ config content.Config
45
+ //store *session.Store
46
+ provider * WorkspaceProvider
46
47
backupWorkspaceLimiter chan struct {}
47
48
metrics * content.Metrics
48
49
}
@@ -67,15 +68,15 @@ type DisposeOptions struct {
67
68
SnapshotName string
68
69
}
69
70
70
- func NewWorkspaceOperations (config content.Config , store * session. Store , reg prometheus.Registerer ) (* WorkspaceOperations , error ) {
71
+ func NewWorkspaceOperations (config content.Config , store * WorkspaceProvider , reg prometheus.Registerer ) (* WorkspaceOperations , error ) {
71
72
waitingTimeHist , waitingTimeoutCounter , err := content .RegisterConcurrentBackupMetrics (reg , "_mk2" )
72
73
if err != nil {
73
74
return nil , err
74
75
}
75
76
76
77
return & WorkspaceOperations {
77
78
config : config ,
78
- store : store ,
79
+ // store: store,
79
80
metrics : & content.Metrics {
80
81
BackupWaitingTimeHist : waitingTimeHist ,
81
82
BackupWaitingTimeoutCounter : waitingTimeoutCounter ,
@@ -85,34 +86,26 @@ func NewWorkspaceOperations(config content.Config, store *session.Store, reg pro
85
86
}, nil
86
87
}
87
88
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 ),
89
+ func (wso * WorkspaceOperations ) InitWorkspaceContent (ctx context.Context , options InitContentOptions ) (string , error ) {
90
+ ws , err := wso .provider .Create (ctx , options .Meta .InstanceId , filepath .Join (wso .provider .Location , options .Meta .InstanceId ),
91
91
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
92
100
93
if err != nil {
101
- return false , "bug: cannot add workspace to store" , xerrors .Errorf ("cannot add workspace to store: %w" , err )
94
+ return "bug: cannot add workspace to store" , xerrors .Errorf ("cannot add workspace to store: %w" , err )
102
95
}
103
96
104
- rs , ok := res .NonPersistentAttrs [session .AttrRemoteStorage ].(storage.DirectAccess )
97
+ rs , ok := ws .NonPersistentAttrs [session .AttrRemoteStorage ].(storage.DirectAccess )
105
98
if rs == nil || ! ok {
106
- return false , "bug: workspace has no remote storage" , xerrors .Errorf ("workspace has no remote storage" )
99
+ return "bug: workspace has no remote storage" , xerrors .Errorf ("workspace has no remote storage" )
107
100
}
108
101
ps , err := storage .NewPresignedAccess (& wso .config .Storage )
109
102
if err != nil {
110
- return false , "bug: no presigned storage available" , xerrors .Errorf ("no presigned storage available: %w" , err )
103
+ return "bug: no presigned storage available" , xerrors .Errorf ("no presigned storage available: %w" , err )
111
104
}
112
105
113
106
remoteContent , err := content .CollectRemoteContent (ctx , rs , ps , options .Meta .Owner , options .Initializer )
114
107
if err != nil {
115
- return false , "remote content error" , xerrors .Errorf ("remote content error: %w" , err )
108
+ return "remote content error" , xerrors .Errorf ("remote content error: %w" , err )
116
109
}
117
110
118
111
// Initialize workspace.
@@ -138,13 +131,18 @@ func (wso *WorkspaceOperations) InitWorkspaceContent(ctx context.Context, option
138
131
},
139
132
}
140
133
141
- err = content .RunInitializer (ctx , res .Location , options .Initializer , remoteContent , opts )
134
+ err = content .RunInitializer (ctx , ws .Location , options .Initializer , remoteContent , opts )
142
135
if err != nil {
143
136
glog .Infof ("error running initializer %v" , err )
144
- return false , err .Error (), err
137
+ return err .Error (), err
138
+ }
139
+
140
+ err = ws .Persist ()
141
+ if err != nil {
142
+ return "cannot persist workspace" , err
145
143
}
146
144
147
- return false , "" , nil
145
+ return "" , nil
148
146
}
149
147
150
148
func (wso * WorkspaceOperations ) creator (owner , workspaceId , instanceId string , init * csapi.WorkspaceInitializer , storageDisabled bool ) session.WorkspaceFactory {
@@ -173,25 +171,14 @@ func (wso *WorkspaceOperations) creator(owner, workspaceId, instanceId string, i
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" , opts .Meta .InstanceId )
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 ); 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
}
0 commit comments