diff --git a/src/Storages/MergeTree/MergeTreeDataMergerMutator.cpp b/src/Storages/MergeTree/MergeTreeDataMergerMutator.cpp index 4b28dbf9949..a5d0db47929 100644 --- a/src/Storages/MergeTree/MergeTreeDataMergerMutator.cpp +++ b/src/Storages/MergeTree/MergeTreeDataMergerMutator.cpp @@ -359,6 +359,7 @@ bool MergeTreeDataMergerMutator::selectAllPartsToMergeWithinPartition( const String & partition_id, bool final, bool * is_single_merged_part, + const StorageMetadataPtr & metadata_snapshot, String * out_disable_reason) { MergeTreeData::DataPartsVector parts = selectAllPartsFromPartition(partition_id); @@ -374,8 +375,9 @@ bool MergeTreeDataMergerMutator::selectAllPartsToMergeWithinPartition( } /// If final, optimize_skip_merged_partitions is true and we have only one part in partition with level > 0 - /// than we don't select it to merge - if (final && data.getSettings()->optimize_skip_merged_partitions && parts.size() == 1 && parts[0]->info.level > 0) + /// than we don't select it to merge. But if there are some expired TTL then merge is needed + if (final && data.getSettings()->optimize_skip_merged_partitions && parts.size() == 1 && parts[0]->info.level > 0 && + (!metadata_snapshot->hasAnyTTL() || parts[0]->checkAllTTLCalculated(metadata_snapshot))) { *is_single_merged_part = true; return false; @@ -639,6 +641,8 @@ MergeTreeData::MutableDataPartPtr MergeTreeDataMergerMutator::mergePartsToTempor { static const String TMP_PREFIX = "tmp_merge_"; + + if (merges_blocker.isCancelled()) throw Exception("Cancelled merging parts", ErrorCodes::ABORTED); diff --git a/src/Storages/MergeTree/MergeTreeDataMergerMutator.h b/src/Storages/MergeTree/MergeTreeDataMergerMutator.h index 481fa24637b..a3f240f86de 100644 --- a/src/Storages/MergeTree/MergeTreeDataMergerMutator.h +++ b/src/Storages/MergeTree/MergeTreeDataMergerMutator.h @@ -99,6 +99,7 @@ public: const String & partition_id, bool final, bool * is_single_merged_part, + const StorageMetadataPtr & metadata_snapshot, String * out_disable_reason = nullptr); /** Merge the parts. diff --git a/src/Storages/StorageMergeTree.cpp b/src/Storages/StorageMergeTree.cpp index 574c65502f1..69558b13d9b 100644 --- a/src/Storages/StorageMergeTree.cpp +++ b/src/Storages/StorageMergeTree.cpp @@ -686,7 +686,7 @@ bool StorageMergeTree::merge( { UInt64 disk_space = getStoragePolicy()->getMaxUnreservedFreeSpace(); selected = merger_mutator.selectAllPartsToMergeWithinPartition( - future_part, disk_space, can_merge, partition_id, final, &is_single_merged_part, out_disable_reason); + future_part, disk_space, can_merge, partition_id, final, &is_single_merged_part, metadata_snapshot, out_disable_reason); /// If final - we will wait for currently processing merges to finish and continue. /// TODO Respect query settings for timeout diff --git a/src/Storages/StorageReplicatedMergeTree.cpp b/src/Storages/StorageReplicatedMergeTree.cpp index 19ce88ad0b8..09d36ddd4e4 100644 --- a/src/Storages/StorageReplicatedMergeTree.cpp +++ b/src/Storages/StorageReplicatedMergeTree.cpp @@ -3739,6 +3739,7 @@ bool StorageReplicatedMergeTree::optimize( }; const auto storage_settings_ptr = getSettings(); + auto metadata_snapshot = getInMemoryMetadataPtr(); if (!partition && final) { @@ -3766,7 +3767,7 @@ bool StorageReplicatedMergeTree::optimize( /// and setting optimize_skip_merged_partitions is true bool is_single_merged_part = false; bool selected = merger_mutator.selectAllPartsToMergeWithinPartition( - future_merged_part, disk_space, can_merge, partition_id, true, &is_single_merged_part, nullptr); + future_merged_part, disk_space, can_merge, partition_id, true, &is_single_merged_part, metadata_snapshot, nullptr); if (!selected) { @@ -3821,7 +3822,7 @@ bool StorageReplicatedMergeTree::optimize( UInt64 disk_space = getStoragePolicy()->getMaxUnreservedFreeSpace(); String partition_id = getPartitionIDFromQuery(partition, query_context); selected = merger_mutator.selectAllPartsToMergeWithinPartition( - future_merged_part, disk_space, can_merge, partition_id, final, &is_single_merged_part, &disable_reason); + future_merged_part, disk_space, can_merge, partition_id, final, &is_single_merged_part, metadata_snapshot, &disable_reason); } if (!selected)