From 8c0492f0ba7c2077b168cfc51f5277dbe1d2cdbf Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Thu, 2 Jun 2022 18:24:28 +0800 Subject: [PATCH] clear old range before apply snapshot (#4668) (#4720) close pingcap/tiflash#4414 --- dbms/src/Storages/Transaction/ApplySnapshot.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/dbms/src/Storages/Transaction/ApplySnapshot.cpp b/dbms/src/Storages/Transaction/ApplySnapshot.cpp index b2748134a0f..5d4f5edd504 100644 --- a/dbms/src/Storages/Transaction/ApplySnapshot.cpp +++ b/dbms/src/Storages/Transaction/ApplySnapshot.cpp @@ -151,10 +151,22 @@ void KVStore::onSnapshot(const RegionPtrWithBlock & new_region_wrap, RegionPtr o // Acquire `drop_lock` so that no other threads can drop the storage. `alter_lock` is not required. auto table_lock = storage->lockForShare(getThreadName()); auto dm_storage = std::dynamic_pointer_cast(storage); - auto key_range = DM::RowKeyRange::fromRegionRange( + auto new_key_range = DM::RowKeyRange::fromRegionRange( new_region_wrap->getRange(), table_id, storage->isCommonHandle(), storage->getRowKeyColumnSize()); + if (old_region) + { + auto old_key_range = DM::RowKeyRange::fromRegionRange( + old_region->getRange(), table_id, storage->isCommonHandle(), storage->getRowKeyColumnSize()); + if (old_key_range != new_key_range) + { + LOG_INFO(log, + "clear region " << region_id << " old range " << old_key_range.toDebugString() + << " before apply snapshot of new range " << new_key_range.toDebugString()); + dm_storage->deleteRange(old_key_range, context.getSettingsRef()); + } + } // Call `deleteRange` to delete data for range - dm_storage->deleteRange(key_range, context.getSettingsRef()); + dm_storage->deleteRange(new_key_range, context.getSettingsRef()); } catch (DB::Exception & e) {