From 40754f16f37bab38866d2419720249721f75a008 Mon Sep 17 00:00:00 2001 From: Slach Date: Wed, 13 Nov 2024 21:02:03 +0400 Subject: [PATCH] fix possible wrong merging after ATTACH PART for Collapsing and Replacing engines without version, look https://github.com/ClickHouse/ClickHouse/issues/71009 for details Signed-off-by: Slach --- pkg/clickhouse/clickhouse.go | 3 +-- pkg/filesystemhelper/filesystemhelper.go | 20 +------------------- pkg/metadata/part_metadata.go | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+), 21 deletions(-) create mode 100644 pkg/metadata/part_metadata.go diff --git a/pkg/clickhouse/clickhouse.go b/pkg/clickhouse/clickhouse.go index 63f12eb3..5ad589ce 100644 --- a/pkg/clickhouse/clickhouse.go +++ b/pkg/clickhouse/clickhouse.go @@ -7,7 +7,6 @@ import ( "database/sql" "errors" "fmt" - "github.com/Altinity/clickhouse-backup/v2/pkg/filesystemhelper" "os" "path" "path/filepath" @@ -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) diff --git a/pkg/filesystemhelper/filesystemhelper.go b/pkg/filesystemhelper/filesystemhelper.go index 43b8a0e1..c136120a 100644 --- a/pkg/filesystemhelper/filesystemhelper.go +++ b/pkg/filesystemhelper/filesystemhelper.go @@ -7,8 +7,6 @@ import ( "os" "path" "path/filepath" - "sort" - "strconv" "strings" "sync" "syscall" @@ -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 diff --git a/pkg/metadata/part_metadata.go b/pkg/metadata/part_metadata.go new file mode 100644 index 00000000..9c30fbd3 --- /dev/null +++ b/pkg/metadata/part_metadata.go @@ -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 + }) +}