Skip to content

Commit 1ca4473

Browse files
author
anton.voskresensky
committed
fix sort
1 parent cd3a14e commit 1ca4473

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

commands/snapshots.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"osctl/pkg/logging"
99
"osctl/pkg/opensearch"
1010
"osctl/pkg/utils"
11+
"sort"
12+
"strconv"
1113
"strings"
1214
"time"
1315

@@ -55,6 +57,7 @@ func runSnapshot(cmd *cobra.Command, args []string) error {
5557
var indicesToSnapshot []string
5658
repoGroups := map[string]utils.SnapshotGroup{}
5759
var unknownIndices []string
60+
indexSizes := make(map[string]int64)
5861

5962
systemConfigs := make([]config.IndexConfig, 0)
6063
regularConfigs := make([]config.IndexConfig, 0)
@@ -81,6 +84,9 @@ func runSnapshot(cmd *cobra.Command, args []string) error {
8184

8285
for _, idx := range allSystemIndices {
8386
indexName := idx.Index
87+
if size, err := strconv.ParseInt(idx.Size, 10, 64); err == nil {
88+
indexSizes[indexName] = size
89+
}
8490
indexConfig := utils.FindMatchingIndexConfig(indexName, systemConfigs)
8591
if indexConfig != nil && indexConfig.Snapshot && !indexConfig.ManualSnapshot {
8692
utils.AddIndexToSnapshotGroups(indexName, *indexConfig, today, repoGroups, &indicesToSnapshot)
@@ -102,6 +108,9 @@ func runSnapshot(cmd *cobra.Command, args []string) error {
102108

103109
for _, idx := range allRegularIndices {
104110
indexName := idx.Index
111+
if size, err := strconv.ParseInt(idx.Size, 10, 64); err == nil {
112+
indexSizes[indexName] = size
113+
}
105114
indexConfig := utils.FindMatchingIndexConfig(indexName, regularConfigs)
106115
if indexConfig != nil && indexConfig.Snapshot && !indexConfig.ManualSnapshot {
107116
utils.AddIndexToSnapshotGroups(indexName, *indexConfig, today, repoGroups, &indicesToSnapshot)
@@ -136,6 +145,17 @@ func runSnapshot(cmd *cobra.Command, args []string) error {
136145
})
137146
}
138147

148+
sort.Slice(snapshotGroups, func(i, j int) bool {
149+
var sizeI, sizeJ int64
150+
for _, idx := range snapshotGroups[i].Indices {
151+
sizeI += indexSizes[idx]
152+
}
153+
for _, idx := range snapshotGroups[j].Indices {
154+
sizeJ += indexSizes[idx]
155+
}
156+
return sizeI > sizeJ
157+
})
158+
139159
if cfg.GetDryRun() {
140160
existingMain, err := utils.GetSnapshotsIgnore404(client, defaultRepo, "*"+today+"*")
141161
if err != nil {
@@ -340,6 +360,16 @@ func runSnapshot(cmd *cobra.Command, args []string) error {
340360
perRepo[repo] = append(perRepo[repo], g)
341361
}
342362
for repo, groups := range perRepo {
363+
sort.Slice(groups, func(i, j int) bool {
364+
var sizeI, sizeJ int64
365+
for _, idx := range groups[i].Indices {
366+
sizeI += indexSizes[idx]
367+
}
368+
for _, idx := range groups[j].Indices {
369+
sizeJ += indexSizes[idx]
370+
}
371+
return sizeI > sizeJ
372+
})
343373
existing, err := utils.GetSnapshotsIgnore404(client, repo, "*"+today+"*")
344374
if err != nil {
345375
logger.Error(fmt.Sprintf("Failed to get snapshots from repo repo=%s error=%v", repo, err))

commands/snapshotsbackfill.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"osctl/pkg/opensearch"
1010
"osctl/pkg/utils"
1111
"sort"
12+
"strconv"
1213
"strings"
1314
"time"
1415

@@ -195,13 +196,17 @@ func runSnapshotsBackfill(cmd *cobra.Command, args []string) error {
195196
indicesSet[idx] = true
196197
}
197198

199+
indexSizes := make(map[string]int64)
198200
pattern := "*" + dateKey + "*"
199201
indicesWithSize, err := client.GetIndicesWithFields(pattern, "index,ss", "ss:asc")
200202
if err != nil {
201203
logger.Warn(fmt.Sprintf("Failed to get indices with size for date date=%s error=%v, using unsorted list", dateKey, err))
202204
} else {
203205
var sortedIndices []string
204206
for _, idx := range indicesWithSize {
207+
if size, err := strconv.ParseInt(idx.Size, 10, 64); err == nil {
208+
indexSizes[idx.Index] = size
209+
}
205210
if indicesSet[idx.Index] {
206211
sortedIndices = append(sortedIndices, idx.Index)
207212
delete(indicesSet, idx.Index)
@@ -312,6 +317,17 @@ func runSnapshotsBackfill(cmd *cobra.Command, args []string) error {
312317
Kind: "unknown",
313318
})
314319
}
320+
321+
sort.Slice(snapshotGroups, func(i, j int) bool {
322+
var sizeI, sizeJ int64
323+
for _, idx := range snapshotGroups[i].Indices {
324+
sizeI += indexSizes[idx]
325+
}
326+
for _, idx := range snapshotGroups[j].Indices {
327+
sizeJ += indexSizes[idx]
328+
}
329+
return sizeI < sizeJ
330+
})
315331
if len(snapshotGroups) == 0 && len(repoGroups) == 0 {
316332
logger.Info(fmt.Sprintf("No snapshots to create for date date=%s", dateKey))
317333
continue
@@ -382,6 +398,16 @@ func runSnapshotsBackfill(cmd *cobra.Command, args []string) error {
382398
filteredPerRepo := map[string][]utils.SnapshotGroup{}
383399
inProgressPerRepo := make([]string, 0)
384400
for repo, groups := range perRepo {
401+
sort.Slice(groups, func(i, j int) bool {
402+
var sizeI, sizeJ int64
403+
for _, idx := range groups[i].Indices {
404+
sizeI += indexSizes[idx]
405+
}
406+
for _, idx := range groups[j].Indices {
407+
sizeJ += indexSizes[idx]
408+
}
409+
return sizeI < sizeJ
410+
})
385411
existing, err := utils.GetSnapshotsIgnore404(client, repo, "*"+snapshotDate+"*")
386412
if err != nil {
387413
existing = nil
@@ -598,6 +624,16 @@ func runSnapshotsBackfill(cmd *cobra.Command, args []string) error {
598624
perRepo[repo] = append(perRepo[repo], g)
599625
}
600626
for repo, groups := range perRepo {
627+
sort.Slice(groups, func(i, j int) bool {
628+
var sizeI, sizeJ int64
629+
for _, idx := range groups[i].Indices {
630+
sizeI += indexSizes[idx]
631+
}
632+
for _, idx := range groups[j].Indices {
633+
sizeJ += indexSizes[idx]
634+
}
635+
return sizeI < sizeJ
636+
})
601637
existing, err := utils.GetSnapshotsIgnore404(client, repo, "*"+snapshotDate+"*")
602638
if err != nil {
603639
logger.Error(fmt.Sprintf("Failed to get snapshots from repo repo=%s error=%v", repo, err))

pkg/utils/indices.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ func ShouldSkipIndex(indexName string) bool {
4545
}
4646

4747
func ShouldSkipIndexRetention(indexName string) bool {
48-
if strings.HasPrefix(indexName, ".") {
49-
return true
50-
}
51-
return false
48+
return strings.HasPrefix(indexName, ".")
5249
}
5350

5451
func FindMatchingIndexConfig(indexName string, indicesConfig []config.IndexConfig) *config.IndexConfig {

0 commit comments

Comments
 (0)