Skip to content

Commit 9772eb4

Browse files
committed
Merge branch 'dle-4-0-multi-dataset-snapshots' into 'dle-4-0'
fix: get the relevant FSManager for the requested snapshot or branch See merge request postgres-ai/database-lab!858
2 parents 3786a66 + cc931a8 commit 9772eb4

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

engine/internal/embeddedui/embedded_ui_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestStartExistingContainer(t *testing.T) {
3535
embeddedUI := New(
3636
Config{
3737
// "mock" UI image
38-
DockerImage: "gcr.io/google_containers/pause-amd64:3.0",
38+
DockerImage: "alpine:3.19",
3939
},
4040
engProps,
4141
runners.NewLocalRunner(false),

engine/internal/srv/branch.go

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ func containsString(slice []string, s string) bool {
9494
return false
9595
}
9696

97+
//nolint:unused
98+
func (s *Server) getFSManagerForBranch(branchName string) (pool.FSManager, error) {
99+
allBranches, err := s.pm.First().ListAllBranches()
100+
if err != nil {
101+
return nil, fmt.Errorf("failed to get branch list: %w", err)
102+
}
103+
104+
for _, branchEntity := range allBranches {
105+
if branchEntity.Name == branchName {
106+
return s.getFSManagerForSnapshot(branchEntity.SnapshotID)
107+
}
108+
}
109+
110+
return nil, fmt.Errorf("failed to found dataset of the branch: %s", branchName)
111+
}
112+
97113
func (s *Server) createBranch(w http.ResponseWriter, r *http.Request) {
98114
var createRequest types.BranchCreateRequest
99115
if err := api.ReadJSON(r, &createRequest); err != nil {
@@ -111,10 +127,20 @@ func (s *Server) createBranch(w http.ResponseWriter, r *http.Request) {
111127
return
112128
}
113129

130+
var err error
131+
114132
fsm := s.pm.First()
115133

134+
if createRequest.BaseBranch != "" {
135+
fsm, err = s.getFSManagerForBranch(createRequest.BaseBranch)
136+
if err != nil {
137+
api.SendBadRequestError(w, r, err.Error())
138+
return
139+
}
140+
}
141+
116142
if fsm == nil {
117-
api.SendBadRequestError(w, r, "no available pools")
143+
api.SendBadRequestError(w, r, "no pool manager found")
118144
return
119145
}
120146

@@ -197,10 +223,9 @@ func (s *Server) getCommit(w http.ResponseWriter, r *http.Request) {
197223
return
198224
}
199225

200-
fsm := s.pm.First()
201-
202-
if fsm == nil {
203-
api.SendBadRequestError(w, r, "no available pools")
226+
fsm, err := s.getFSManagerForSnapshot(snapshotID)
227+
if err != nil {
228+
api.SendBadRequestError(w, r, err.Error())
204229
return
205230
}
206231

@@ -223,6 +248,20 @@ func (s *Server) getCommit(w http.ResponseWriter, r *http.Request) {
223248
}
224249
}
225250

251+
func (s *Server) getFSManagerForSnapshot(snapshotID string) (pool.FSManager, error) {
252+
poolName, err := s.detectPoolName(snapshotID)
253+
if err != nil {
254+
return nil, fmt.Errorf("failed to detect pool name for the snapshot %s: %w", snapshotID, err)
255+
}
256+
257+
fsm, err := s.pm.GetFSManager(poolName)
258+
if err != nil {
259+
return nil, fmt.Errorf("pool manager not available %s: %w", poolName, err)
260+
}
261+
262+
return fsm, nil
263+
}
264+
226265
func (s *Server) snapshot(w http.ResponseWriter, r *http.Request) {
227266
var snapshotRequest types.SnapshotCloneCreateRequest
228267
if err := api.ReadJSON(r, &snapshotRequest); err != nil {
@@ -325,19 +364,23 @@ func (s *Server) snapshot(w http.ResponseWriter, r *http.Request) {
325364
}
326365

327366
func (s *Server) log(w http.ResponseWriter, r *http.Request) {
328-
fsm := s.pm.First()
329-
330-
if fsm == nil {
331-
api.SendBadRequestError(w, r, "no available pools")
367+
var logRequest types.LogRequest
368+
if err := api.ReadJSON(r, &logRequest); err != nil {
369+
api.SendBadRequestError(w, r, err.Error())
332370
return
333371
}
334372

335-
var logRequest types.LogRequest
336-
if err := api.ReadJSON(r, &logRequest); err != nil {
373+
fsm, err := s.getFSManagerForBranch(logRequest.BranchName)
374+
if err != nil {
337375
api.SendBadRequestError(w, r, err.Error())
338376
return
339377
}
340378

379+
if fsm == nil {
380+
api.SendBadRequestError(w, r, "no pool manager found")
381+
return
382+
}
383+
341384
repo, err := fsm.GetRepo()
342385
if err != nil {
343386
api.SendBadRequestError(w, r, err.Error())
@@ -377,10 +420,14 @@ func (s *Server) deleteBranch(w http.ResponseWriter, r *http.Request) {
377420
return
378421
}
379422

380-
fsm := s.pm.First()
423+
fsm, err := s.getFSManagerForBranch(deleteRequest.BranchName)
424+
if err != nil {
425+
api.SendBadRequestError(w, r, err.Error())
426+
return
427+
}
381428

382429
if fsm == nil {
383-
api.SendBadRequestError(w, r, "no available pools")
430+
api.SendBadRequestError(w, r, "no pool manager found")
384431
return
385432
}
386433

engine/internal/srv/routes.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,14 @@ func (s *Server) createClone(w http.ResponseWriter, r *http.Request) {
331331
}
332332

333333
if cloneRequest.Branch != "" {
334-
fsm := s.pm.First()
334+
fsm, err := s.getFSManagerForBranch(cloneRequest.Branch)
335+
if err != nil {
336+
api.SendBadRequestError(w, r, err.Error())
337+
return
338+
}
335339

336340
if fsm == nil {
337-
api.SendBadRequestError(w, r, "no available pools")
341+
api.SendBadRequestError(w, r, "no pool manager found")
338342
return
339343
}
340344

0 commit comments

Comments
 (0)