Skip to content

Commit

Permalink
fix possible wrong merging after ATTACH PART for Collapsing and Repla…
Browse files Browse the repository at this point in the history
…cing engines without version, look ClickHouse/ClickHouse#71009 for details

Signed-off-by: Slach <[email protected]>
  • Loading branch information
Slach committed Nov 13, 2024
1 parent 10c321f commit 40754f1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
3 changes: 1 addition & 2 deletions pkg/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"database/sql"
"errors"
"fmt"
"github.com/Altinity/clickhouse-backup/v2/pkg/filesystemhelper"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -772,7 +771,7 @@ func (ch *ClickHouse) AttachDataParts(table metadata.TableMetadata, dstTable Tab
}
for disk := range table.Parts {
// https://github.com/ClickHouse/ClickHouse/issues/71009
filesystemhelper.SortPartsByMinBlock(table.Parts[disk])
metadata.SortPartsByMinBlock(table.Parts[disk])
for _, part := range table.Parts[disk] {
if !strings.HasSuffix(part.Name, ".proj") {
query := fmt.Sprintf("ALTER TABLE `%s`.`%s` ATTACH PART '%s'", table.Database, table.Table, part.Name)
Expand Down
20 changes: 1 addition & 19 deletions pkg/filesystemhelper/filesystemhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"syscall"
Expand Down Expand Up @@ -300,26 +298,10 @@ func MoveShadowToBackup(shadowPath, backupPartsPath string, partitionsBackupMap
}
})
// https://github.com/ClickHouse/ClickHouse/issues/71009
SortPartsByMinBlock(parts)
metadata.SortPartsByMinBlock(parts)
return parts, size, err
}

// SortPartsByMinBlock need to avoid wrong restore for Replacing, Collapsing, https://github.com/ClickHouse/ClickHouse/issues/71009
func SortPartsByMinBlock(parts []metadata.Part) {
sort.Slice(parts, func(i, j int) bool {
namePartsI := strings.Split(parts[i].Name, "_")
namePartsJ := strings.Split(parts[j].Name, "_")
// partitions different
if namePartsI[0] != namePartsJ[0] {
return namePartsI[0] < namePartsJ[0]
}
// partition same, min block
minBlockI, _ := strconv.Atoi(namePartsI[1])
minBlockJ, _ := strconv.Atoi(namePartsJ[1])
return minBlockI < minBlockJ
})
}

func addRequiredPartIfNotExists(parts []metadata.Part, relativePath string, tableDiffFromRemote metadata.TableMetadata, disk clickhouse.Disk) ([]metadata.Part, bool, bool) {
isRequiredPartFound := false
exists := false
Expand Down
23 changes: 23 additions & 0 deletions pkg/metadata/part_metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package metadata

import (
"sort"
"strconv"
"strings"
)

// SortPartsByMinBlock need to avoid wrong restore for Replacing, Collapsing, https://github.com/ClickHouse/ClickHouse/issues/71009
func SortPartsByMinBlock(parts []Part) {
sort.Slice(parts, func(i, j int) bool {
namePartsI := strings.Split(parts[i].Name, "_")
namePartsJ := strings.Split(parts[j].Name, "_")
// partitions different
if namePartsI[0] != namePartsJ[0] {
return namePartsI[0] < namePartsJ[0]
}
// partition same, min block
minBlockI, _ := strconv.Atoi(namePartsI[1])
minBlockJ, _ := strconv.Atoi(namePartsJ[1])
return minBlockI < minBlockJ
})
}

0 comments on commit 40754f1

Please sign in to comment.