|
9 | 9 | "osctl/pkg/opensearch" |
10 | 10 | "osctl/pkg/utils" |
11 | 11 | "sort" |
| 12 | + "strconv" |
12 | 13 | "strings" |
13 | 14 | "time" |
14 | 15 |
|
@@ -195,13 +196,17 @@ func runSnapshotsBackfill(cmd *cobra.Command, args []string) error { |
195 | 196 | indicesSet[idx] = true |
196 | 197 | } |
197 | 198 |
|
| 199 | + indexSizes := make(map[string]int64) |
198 | 200 | pattern := "*" + dateKey + "*" |
199 | 201 | indicesWithSize, err := client.GetIndicesWithFields(pattern, "index,ss", "ss:asc") |
200 | 202 | if err != nil { |
201 | 203 | logger.Warn(fmt.Sprintf("Failed to get indices with size for date date=%s error=%v, using unsorted list", dateKey, err)) |
202 | 204 | } else { |
203 | 205 | var sortedIndices []string |
204 | 206 | for _, idx := range indicesWithSize { |
| 207 | + if size, err := strconv.ParseInt(idx.Size, 10, 64); err == nil { |
| 208 | + indexSizes[idx.Index] = size |
| 209 | + } |
205 | 210 | if indicesSet[idx.Index] { |
206 | 211 | sortedIndices = append(sortedIndices, idx.Index) |
207 | 212 | delete(indicesSet, idx.Index) |
@@ -312,6 +317,17 @@ func runSnapshotsBackfill(cmd *cobra.Command, args []string) error { |
312 | 317 | Kind: "unknown", |
313 | 318 | }) |
314 | 319 | } |
| 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 | + }) |
315 | 331 | if len(snapshotGroups) == 0 && len(repoGroups) == 0 { |
316 | 332 | logger.Info(fmt.Sprintf("No snapshots to create for date date=%s", dateKey)) |
317 | 333 | continue |
@@ -382,6 +398,16 @@ func runSnapshotsBackfill(cmd *cobra.Command, args []string) error { |
382 | 398 | filteredPerRepo := map[string][]utils.SnapshotGroup{} |
383 | 399 | inProgressPerRepo := make([]string, 0) |
384 | 400 | 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 | + }) |
385 | 411 | existing, err := utils.GetSnapshotsIgnore404(client, repo, "*"+snapshotDate+"*") |
386 | 412 | if err != nil { |
387 | 413 | existing = nil |
@@ -598,6 +624,16 @@ func runSnapshotsBackfill(cmd *cobra.Command, args []string) error { |
598 | 624 | perRepo[repo] = append(perRepo[repo], g) |
599 | 625 | } |
600 | 626 | 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 | + }) |
601 | 637 | existing, err := utils.GetSnapshotsIgnore404(client, repo, "*"+snapshotDate+"*") |
602 | 638 | if err != nil { |
603 | 639 | logger.Error(fmt.Sprintf("Failed to get snapshots from repo repo=%s error=%v", repo, err)) |
|
0 commit comments