Skip to content

Commit b31ca19

Browse files
author
Bogdan Tsechoev
committed
Merge branch '605-branching-with-selected-pool' into 'dle-4-0'
fix: filter branches by available pools (#605) See merge request postgres-ai/database-lab!1030
2 parents 096212e + 1047e8f commit b31ca19

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

engine/internal/provision/mode_local_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (m mockFSManager) ListBranches() (map[string]string, error) {
142142
return nil, nil
143143
}
144144

145-
func (m mockFSManager) ListAllBranches() ([]models.BranchEntity, error) {
145+
func (m mockFSManager) ListAllBranches(_ []string) ([]models.BranchEntity, error) {
146146
return nil, nil
147147
}
148148

engine/internal/provision/pool/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type Branching interface {
5959
CreateBranch(branchName, snapshotID string) error
6060
DestroyDataset(branchName string) (err error)
6161
ListBranches() (map[string]string, error)
62-
ListAllBranches() ([]models.BranchEntity, error)
62+
ListAllBranches(filterPools []string) ([]models.BranchEntity, error)
6363
GetRepo() (*models.Repo, error)
6464
GetAllRepo() (*models.Repo, error)
6565
SetRelation(parent, snapshotName string) error

engine/internal/provision/thinclones/lvm/lvmanager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func (m *LVManager) ListBranches() (map[string]string, error) {
199199
}
200200

201201
// ListAllBranches lists all branches.
202-
func (m *LVManager) ListAllBranches() ([]models.BranchEntity, error) {
202+
func (m *LVManager) ListAllBranches(_ []string) ([]models.BranchEntity, error) {
203203
log.Msg("ListAllBranches is not supported for LVM. Skip the operation")
204204

205205
return nil, nil

engine/internal/provision/thinclones/zfs/branching.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,17 @@ func (m *Manager) ListBranches() (map[string]string, error) {
236236
}
237237

238238
// ListAllBranches lists all branches.
239-
func (m *Manager) ListAllBranches() ([]models.BranchEntity, error) {
239+
func (m *Manager) ListAllBranches(poolList []string) ([]models.BranchEntity, error) {
240+
poolFilter := ""
241+
242+
if len(poolList) > 0 {
243+
poolFilter += "-r " + strings.Join(poolList, " ")
244+
}
245+
240246
cmd := fmt.Sprintf(
241247
// Get all ZFS snapshots (-t) with options (-o) without output headers (-H).
242248
// Excluding snapshots without "dle:branch" property ("grep -v").
243-
`zfs list -H -t snapshot -o %s,name | grep -v "^-" | cat`, branchProp,
249+
`zfs list -H -t snapshot -o %s,name %s | grep -v "^-" | cat`, branchProp, poolFilter,
244250
)
245251

246252
out, err := m.runner.Run(cmd)

engine/internal/srv/branch.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (s *Server) listBranches(w http.ResponseWriter, r *http.Request) {
3333
return
3434
}
3535

36-
branches, err := fsm.ListAllBranches()
36+
branches, err := s.getAllAvailableBranches(fsm)
3737
if err != nil {
3838
api.SendBadRequestError(w, r, err.Error())
3939
return
@@ -85,6 +85,21 @@ func (s *Server) listBranches(w http.ResponseWriter, r *http.Request) {
8585
}
8686
}
8787

88+
func (s *Server) getAllAvailableBranches(fsm pool.FSManager) ([]models.BranchEntity, error) {
89+
if fsm == nil {
90+
return nil, fmt.Errorf("no available pools")
91+
}
92+
93+
// Filter by available pools in case if two or more DLE is running on the same pool and use the selectedPool feature.
94+
poolNames := []string{}
95+
96+
for _, fsManager := range s.pm.GetFSManagerList() {
97+
poolNames = append(poolNames, fsManager.Pool().Name)
98+
}
99+
100+
return fsm.ListAllBranches(poolNames)
101+
}
102+
88103
func findBranchParent(snapshots map[string]models.SnapshotDetails, parentID, branch string) (int, string) {
89104
snapshotCounter := 0
90105

@@ -121,13 +136,13 @@ func containsString(slice []string, s string) bool {
121136
}
122137

123138
func (s *Server) getFSManagerForBranch(branchName string) (pool.FSManager, error) {
124-
allBranches, err := s.pm.First().ListAllBranches()
139+
allBranches, err := s.getAllAvailableBranches(s.pm.First())
125140
if err != nil {
126141
return nil, fmt.Errorf("failed to get branch list: %w", err)
127142
}
128143

129144
for _, branchEntity := range allBranches {
130-
if branchEntity.Name == branchName {
145+
if branchEntity.Name == branchName { // TODO: filter by pool name as well because branch name is ambiguous.
131146
return s.getFSManagerForSnapshot(branchEntity.SnapshotID)
132147
}
133148
}

0 commit comments

Comments
 (0)