From d10d35436f8cee2fb38fbcd67f8ad1d695b950a0 Mon Sep 17 00:00:00 2001 From: JaySon-Huang Date: Tue, 17 Dec 2024 19:08:51 +0800 Subject: [PATCH 1/8] Try remove RegionPtrWithBlock Signed-off-by: JaySon-Huang --- .../dbgKVStore/dbgFuncMockRaftSnapshot.cpp | 18 +++++++--- dbms/src/Debug/dbgKVStore/dbgFuncRegion.cpp | 4 +-- dbms/src/Debug/dbgNaturalDag.cpp | 2 +- dbms/src/Debug/dbgTools.cpp | 2 +- .../KVStore/Decode/PartitionStreams.cpp | 26 +++++---------- .../KVStore/Decode/PartitionStreams.h | 3 +- .../Storages/KVStore/Decode/RegionTable.cpp | 2 +- .../src/Storages/KVStore/Decode/RegionTable.h | 7 ++-- .../KVStore/MultiRaft/ApplySnapshot.cpp | 33 +++---------------- .../Storages/KVStore/tests/gtest_spill.cpp | 4 +-- dbms/src/Storages/StorageDeltaMerge.cpp | 2 +- dbms/src/TiDB/Schema/TiDB.cpp | 10 +++--- dbms/src/TiDB/Schema/TiDB.h | 2 ++ tests/delta-merge-test/raft/snapshot.test | 22 ------------- 14 files changed, 46 insertions(+), 91 deletions(-) diff --git a/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp b/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp index baefef60223..58163314908 100644 --- a/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp +++ b/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp @@ -172,7 +172,7 @@ void MockRaftCommand::dbgFuncRegionSnapshotWithData(Context & context, const AST // Mock to apply a snapshot with data in `region` auto & tmt = context.getTMTContext(); - context.getTMTContext().getKVStore()->checkAndApplyPreHandledSnapshot(region, tmt); + context.getTMTContext().getKVStore()->checkAndApplyPreHandledSnapshot(region, tmt); output(fmt::format("put region #{}, range{} to table #{} with {} records", region_id, range_string, table_id, cnt)); } @@ -488,12 +488,13 @@ void MockRaftCommand::dbgFuncIngestSST(Context & context, const ASTs & args, DBG struct GlobalRegionMap { using Key = std::string; - using BlockVal = std::pair; - std::unordered_map regions_block; + // using BlockVal = std::pair; + // std::unordered_map regions_block; using SnapPath = std::pair>; std::unordered_map regions_snap_files; std::mutex mutex; +#if 0 void insertRegionCache(const Key & name, BlockVal && val) { auto _ = std::lock_guard(mutex); @@ -511,6 +512,7 @@ struct GlobalRegionMap return ret; } } +#endif void insertRegionSnap(const Key & name, SnapPath && val) { @@ -533,6 +535,7 @@ struct GlobalRegionMap static GlobalRegionMap GLOBAL_REGION_MAP; +#if 0 /// Mock to pre-decode snapshot to block then apply /// Pre-decode region data into block cache and remove committed data from `region` @@ -635,6 +638,7 @@ RegionPtrWithBlock::CachePtr GenRegionPreDecodeBlockData(const RegionPtr & regio return std::make_unique(std::move(res_block), schema_version, std::move(*data_list_read)); } +#endif void MockRaftCommand::dbgFuncRegionSnapshotPreHandleBlock( Context & context, @@ -645,6 +649,7 @@ void MockRaftCommand::dbgFuncRegionSnapshotPreHandleBlock( auto region = GenDbgRegionSnapshotWithData(context, args); const auto region_name = "__snap_" + std::to_string(region->id()); fmt_buf.fmtAppend("pre-handle {} snapshot with data {}", region->toString(false), region->dataInfo()); +#if 0 auto & tmt = context.getTMTContext(); auto block_cache = GenRegionPreDecodeBlockData(region, tmt.getContext()); fmt_buf.append(", pre-decode block cache"); @@ -655,10 +660,11 @@ void MockRaftCommand::dbgFuncRegionSnapshotPreHandleBlock( block_cache->block.columns(), block_cache->block.bytes()); GLOBAL_REGION_MAP.insertRegionCache(region_name, {region, std::move(block_cache)}); +#endif output(fmt_buf.toString()); } -void MockRaftCommand::dbgFuncRegionSnapshotApplyBlock(Context & context, const ASTs & args, DBGInvoker::Printer output) +void MockRaftCommand::dbgFuncRegionSnapshotApplyBlock(Context & /*context*/, const ASTs & args, DBGInvoker::Printer output) { if (args.size() != 1) { @@ -666,13 +672,15 @@ void MockRaftCommand::dbgFuncRegionSnapshotApplyBlock(Context & context, const A } auto region_id = static_cast(safeGet(typeid_cast(*args.front()).value)); +#if 0 auto [region, block_cache] = GLOBAL_REGION_MAP.popRegionCache("__snap_" + std::to_string(region_id)); auto & tmt = context.getTMTContext(); context.getTMTContext().getKVStore()->checkAndApplyPreHandledSnapshot( {region, std::move(block_cache)}, tmt); +#endif - output(fmt::format("success apply {} with block cache", region->id())); + output(fmt::format("success apply {} with block cache", region_id)); } diff --git a/dbms/src/Debug/dbgKVStore/dbgFuncRegion.cpp b/dbms/src/Debug/dbgKVStore/dbgFuncRegion.cpp index 92fe3ee678e..41421d5bdc5 100644 --- a/dbms/src/Debug/dbgKVStore/dbgFuncRegion.cpp +++ b/dbms/src/Debug/dbgKVStore/dbgFuncRegion.cpp @@ -81,7 +81,7 @@ void dbgFuncPutRegion(Context & context, const ASTs & args, DBGInvoker::Printer TMTContext & tmt = context.getTMTContext(); RegionPtr region = RegionBench::createRegion(table_info, region_id, start_keys, end_keys); - tmt.getKVStore()->onSnapshot(region, nullptr, 0, tmt); + tmt.getKVStore()->onSnapshot(region, nullptr, 0, tmt); output(fmt::format( "put region #{}, range{} to table #{} with kvstore.onSnapshot", @@ -96,7 +96,7 @@ void dbgFuncPutRegion(Context & context, const ASTs & args, DBGInvoker::Printer TMTContext & tmt = context.getTMTContext(); RegionPtr region = RegionBench::createRegion(table_id, region_id, start, end); - tmt.getKVStore()->onSnapshot(region, nullptr, 0, tmt); + tmt.getKVStore()->onSnapshot(region, nullptr, 0, tmt); output(fmt::format( "put region #{}, range[{}, {}) to table #{} with kvstore.onSnapshot", diff --git a/dbms/src/Debug/dbgNaturalDag.cpp b/dbms/src/Debug/dbgNaturalDag.cpp index e208f1e76cf..33d6aeb32ae 100644 --- a/dbms/src/Debug/dbgNaturalDag.cpp +++ b/dbms/src/Debug/dbgNaturalDag.cpp @@ -232,7 +232,7 @@ void NaturalDag::buildTables(Context & context) auto raft_index = RAFT_INIT_LOG_INDEX; region_meta.setApplied(raft_index, RAFT_INIT_LOG_TERM); RegionPtr region_ptr = RegionBench::makeRegion(std::move(region_meta)); - tmt.getKVStore()->onSnapshot(region_ptr, nullptr, 0, tmt); + tmt.getKVStore()->onSnapshot(region_ptr, nullptr, 0, tmt); auto & pairs = region.pairs; for (auto & pair : pairs) diff --git a/dbms/src/Debug/dbgTools.cpp b/dbms/src/Debug/dbgTools.cpp index f896ab6346d..8edf28d732d 100644 --- a/dbms/src/Debug/dbgTools.cpp +++ b/dbms/src/Debug/dbgTools.cpp @@ -581,7 +581,7 @@ void concurrentBatchInsert( Regions regions = createRegions(table_info.id, concurrent_num, key_num_each_region, handle_begin, curr_max_region_id + 1); for (const RegionPtr & region : regions) - debug_kvstore.onSnapshot(region, nullptr, 0, tmt); + debug_kvstore.onSnapshot(region, nullptr, 0, tmt); std::list threads; for (Int64 i = 0; i < concurrent_num; i++, handle_begin += key_num_each_region) diff --git a/dbms/src/Storages/KVStore/Decode/PartitionStreams.cpp b/dbms/src/Storages/KVStore/Decode/PartitionStreams.cpp index 0e2e7076cdf..44b225d45b4 100644 --- a/dbms/src/Storages/KVStore/Decode/PartitionStreams.cpp +++ b/dbms/src/Storages/KVStore/Decode/PartitionStreams.cpp @@ -106,7 +106,7 @@ static void inline writeCommittedBlockDataIntoStorage( template static inline bool atomicReadWrite( AtomicReadWriteCtx & rw_ctx, - const RegionPtrWithBlock & region, + const RegionPtr & region, ReadList & data_list_read, bool force_decode) { @@ -147,9 +147,7 @@ static inline bool atomicReadWrite( should_handle_version_col = false; } - // Currently, RegionPtrWithBlock with a not-null CachePtr is only used in debug functions - // to apply a pre-decoded snapshot. So it will not take place here. - // In short, we always decode here because there is no pre-decode cache. + // Decode `data_list_read` according to the schema snapshot into `Block` { LOG_TRACE( rw_ctx.log, @@ -169,6 +167,7 @@ static inline bool atomicReadWrite( GET_METRIC(tiflash_raft_write_data_to_storage_duration_seconds, type_decode) .Observe(rw_ctx.region_decode_cost / 1000.0); } + if constexpr (std::is_same_v) { RUNTIME_CHECK(block_ptr != nullptr); @@ -198,12 +197,12 @@ static inline bool atomicReadWrite( template DM::WriteResult writeRegionDataToStorage( Context & context, - const RegionPtrWithBlock & region, + const RegionPtr & region, RegionUncommittedDataList & data_list_read, const LoggerPtr & log); template DM::WriteResult writeRegionDataToStorage( Context & context, - const RegionPtrWithBlock & region, + const RegionPtr & region, RegionDataReadInfoList & data_list_read, const LoggerPtr & log); @@ -212,7 +211,7 @@ template DM::WriteResult writeRegionDataToStorage( template DM::WriteResult writeRegionDataToStorage( Context & context, - const RegionPtrWithBlock & region, + const RegionPtr & region, ReadList & data_list_read, const LoggerPtr & log) { @@ -431,21 +430,12 @@ static inline void reportUpstreamLatency(const RegionDataReadInfoList & data_lis DM::WriteResult RegionTable::writeCommittedByRegion( Context & context, - const RegionPtrWithBlock & region, + const RegionPtr & region, RegionDataReadInfoList & data_list_to_remove, const LoggerPtr & log, bool lock_region) { - std::optional maybe_data_list_read = std::nullopt; - if (region.pre_decode_cache) - { - // If schema version changed, use the kv data to rebuild block cache - maybe_data_list_read = std::move(region.pre_decode_cache->data_list_read); - } - else - { - maybe_data_list_read = ReadRegionCommitCache(region, lock_region); - } + std::optional maybe_data_list_read = ReadRegionCommitCache(region, lock_region); if (!maybe_data_list_read.has_value()) return std::nullopt; diff --git a/dbms/src/Storages/KVStore/Decode/PartitionStreams.h b/dbms/src/Storages/KVStore/Decode/PartitionStreams.h index ac3badbe57c..874c0fa3790 100644 --- a/dbms/src/Storages/KVStore/Decode/PartitionStreams.h +++ b/dbms/src/Storages/KVStore/Decode/PartitionStreams.h @@ -27,7 +27,6 @@ class Region; using RegionPtr = std::shared_ptr; class StorageDeltaMerge; class TMTContext; -struct RegionPtrWithBlock; std::optional ReadRegionCommitCache(const RegionPtr & region, bool lock_region); void RemoveRegionCommitCache( @@ -48,7 +47,7 @@ Block GenRegionBlockDataWithSchema( template DM::WriteResult writeRegionDataToStorage( Context & context, - const RegionPtrWithBlock & region, + const RegionPtr& region, ReadList & data_list_read, const LoggerPtr & log); diff --git a/dbms/src/Storages/KVStore/Decode/RegionTable.cpp b/dbms/src/Storages/KVStore/Decode/RegionTable.cpp index a13d55cd3d0..4425bd932c0 100644 --- a/dbms/src/Storages/KVStore/Decode/RegionTable.cpp +++ b/dbms/src/Storages/KVStore/Decode/RegionTable.cpp @@ -250,7 +250,7 @@ void RegionTable::removeRegion(const RegionID region_id, bool remove_data, const } } -RegionDataReadInfoList RegionTable::tryWriteBlockByRegion(const RegionPtrWithBlock & region) +RegionDataReadInfoList RegionTable::tryWriteBlockByRegion(const RegionPtr & region) { const RegionID region_id = region->id(); diff --git a/dbms/src/Storages/KVStore/Decode/RegionTable.h b/dbms/src/Storages/KVStore/Decode/RegionTable.h index ea80410f5cb..2dc252455dc 100644 --- a/dbms/src/Storages/KVStore/Decode/RegionTable.h +++ b/dbms/src/Storages/KVStore/Decode/RegionTable.h @@ -120,7 +120,7 @@ class RegionTable : private boost::noncopyable // Protects writeBlockByRegionAndFlush and ensures it's executed by only one thread at the same time. // Only one thread can do this at the same time. // The original name for this function is tryFlushRegion. - RegionDataReadInfoList tryWriteBlockByRegion(const RegionPtrWithBlock & region); + RegionDataReadInfoList tryWriteBlockByRegion(const RegionPtr& region); void handleInternalRegionsByTable( KeyspaceID keyspace_id, @@ -136,7 +136,7 @@ class RegionTable : private boost::noncopyable /// Note that table schema must be keep unchanged throughout the process of read then write, we take good care of the lock. static DM::WriteResult writeCommittedByRegion( Context & context, - const RegionPtrWithBlock & region, + const RegionPtr & region, RegionDataReadInfoList & data_list_to_remove, const LoggerPtr & log, bool lock_region = true); @@ -231,6 +231,7 @@ struct RegionPreDecodeBlockData } }; +#if 0 // A wrap of RegionPtr, could try to use its block cache while writing region data to storage. struct RegionPtrWithBlock { @@ -260,7 +261,7 @@ struct RegionPtrWithBlock , pre_decode_cache(std::move(cache)) {} }; - +#endif // A wrap of RegionPtr, with snapshot files directory waitting to be ingested struct RegionPtrWithSnapshotFiles diff --git a/dbms/src/Storages/KVStore/MultiRaft/ApplySnapshot.cpp b/dbms/src/Storages/KVStore/MultiRaft/ApplySnapshot.cpp index e2fdbf31364..3908fe23a49 100644 --- a/dbms/src/Storages/KVStore/MultiRaft/ApplySnapshot.cpp +++ b/dbms/src/Storages/KVStore/MultiRaft/ApplySnapshot.cpp @@ -28,6 +28,7 @@ #include #include +#include namespace DB { @@ -195,6 +196,10 @@ void KVStore::onSnapshot( UInt64 old_region_index, TMTContext & tmt) { + static_assert( + std::is_same_v + || std::is_same_v); + RegionID region_id = new_region_wrap->id(); // 1. Try to clean stale data. @@ -259,14 +264,6 @@ void KVStore::onSnapshot( context.getSettingsRef()); maybeUpdateRU(dm_storage, keyspace_id, ingested_bytes); } - else - { - // It is only for debug usage now. - static_assert(std::is_same_v); - // Call `deleteRange` to delete data for range - dm_storage->deleteRange(new_key_range, context.getSettingsRef()); - // We don't flushCache here, but flush as a whole in stage 2 in `tryFlushRegionCacheInStorage`. - } } catch (DB::Exception & e) { @@ -283,24 +280,6 @@ void KVStore::onSnapshot( auto & region_table = tmt.getRegionTable(); // extend region to make sure data won't be removed. region_table.extendRegionRange(region_id, *range); - // For `RegionPtrWithBlock`, try to flush data into storage first. - if constexpr (std::is_same_v) - { - try - { - auto tmp = region_table.tryWriteBlockByRegion(new_region_wrap); - { - std::lock_guard lock(bg_gc_region_data_mutex); - bg_gc_region_data.push_back(std::move(tmp)); - } - tryFlushRegionCacheInStorage(tmt, *new_region_wrap, log); - } - catch (...) - { - tryLogCurrentException(__PRETTY_FUNCTION__); - } - } - // For `RegionPtrWithSnapshotFiles`, don't need to flush cache. } // Register the new Region. @@ -403,11 +382,9 @@ template void KVStore::applyPreHandledSnapshot( const RegionPtrWithSnapshotFiles &, TMTContext &); -template void KVStore::checkAndApplyPreHandledSnapshot(const RegionPtrWithBlock &, TMTContext &); template void KVStore::checkAndApplyPreHandledSnapshot( const RegionPtrWithSnapshotFiles &, TMTContext &); -template void KVStore::onSnapshot(const RegionPtrWithBlock &, RegionPtr, UInt64, TMTContext &); template void KVStore::onSnapshot( const RegionPtrWithSnapshotFiles &, RegionPtr, diff --git a/dbms/src/Storages/KVStore/tests/gtest_spill.cpp b/dbms/src/Storages/KVStore/tests/gtest_spill.cpp index bf7874e9da4..e4533c43583 100644 --- a/dbms/src/Storages/KVStore/tests/gtest_spill.cpp +++ b/dbms/src/Storages/KVStore/tests/gtest_spill.cpp @@ -134,11 +134,11 @@ try KVStore & kvs = getKVS(); proxy_instance->bootstrapWithRegion(kvs, ctx.getTMTContext(), 1, std::nullopt); auto region = kvs.getRegion(1); - RegionPtrWithBlock region_with_cache = RegionPtrWithBlock(region); + RegionPtrWithSnapshotFiles region_with_cache = RegionPtrWithSnapshotFiles(region); // TODO(Spill) spill logic EXPECT_THROW(writeRegionDataToStorage(ctx, region_with_cache, data_list_read, log), Exception); } } CATCH -} // namespace DB::tests \ No newline at end of file +} // namespace DB::tests diff --git a/dbms/src/Storages/StorageDeltaMerge.cpp b/dbms/src/Storages/StorageDeltaMerge.cpp index c61613adc0c..7a50b6e0c3e 100644 --- a/dbms/src/Storages/StorageDeltaMerge.cpp +++ b/dbms/src/Storages/StorageDeltaMerge.cpp @@ -1292,7 +1292,7 @@ inline OptionTableInfoConstRef getTableInfoForCreateStatement( /// If TableInfo from TiDB is empty, for example, create DM table for test, /// we refine TableInfo from store's table column, so that we can restore column id next time - table_info_from_store.schema_version = DEFAULT_UNSPECIFIED_SCHEMA_VERSION; + // table_info_from_store.schema_version = DEFAULT_UNSPECIFIED_SCHEMA_VERSION; for (const auto & column_define : store_table_columns) { if (hidden_columns.has(column_define.name)) diff --git a/dbms/src/TiDB/Schema/TiDB.cpp b/dbms/src/TiDB/Schema/TiDB.cpp index 3e7133d5945..00be2f4aace 100644 --- a/dbms/src/TiDB/Schema/TiDB.cpp +++ b/dbms/src/TiDB/Schema/TiDB.cpp @@ -1005,7 +1005,7 @@ try } } - json->set("schema_version", schema_version); + // json->set("schema_version", schema_version); json->set("tiflash_replica", replica_info.getJSONObject()); @@ -1085,10 +1085,10 @@ try if (!partition_obj.isNull()) partition.deserialize(partition_obj); } - if (obj->has("schema_version")) - { - schema_version = obj->getValue("schema_version"); - } + // if (obj->has("schema_version")) + // { + // schema_version = obj->getValue("schema_version"); + // } if (obj->has("view") && !obj->getObject("view").isNull()) { is_view = true; diff --git a/dbms/src/TiDB/Schema/TiDB.h b/dbms/src/TiDB/Schema/TiDB.h index 30d19740daa..3a5763ebca0 100644 --- a/dbms/src/TiDB/Schema/TiDB.h +++ b/dbms/src/TiDB/Schema/TiDB.h @@ -307,8 +307,10 @@ struct TableInfo bool is_view = false; // If the table is sequence, we should ignore it. bool is_sequence = false; +#if 0 Int64 schema_version = DEFAULT_UNSPECIFIED_SCHEMA_VERSION; // TODO(hyy):can be removed after removing RegionPtrWithBlock +#endif // The TiFlash replica info persisted by TiDB TiFlashReplicaInfo replica_info; diff --git a/tests/delta-merge-test/raft/snapshot.test b/tests/delta-merge-test/raft/snapshot.test index 621898f4c43..0d3e0e412d3 100644 --- a/tests/delta-merge-test/raft/snapshot.test +++ b/tests/delta-merge-test/raft/snapshot.test @@ -65,28 +65,6 @@ │ 11 │ 2 │ └───────┴─────────────┘ -=> DBGInvoke __enable_schema_sync_service('false') -=> DBGInvoke region_snapshot_pre_handle_block(default, test_dm, 5, 1000, 2000, 1002, 30, 0, 110, 1003, 31, 0, 130) -┌─region_snapshot_pre_handle_block(default, test, 5, 1000, 2000, 1002, 30, 0, 110, 1003, 31, 0, 130)──────────────────────────────────────────────────┐ -│ pre-handle [region_id=5] snapshot with data [write 2 ], pre-decode block cache { schema_version: ?, data_list size: 2, block row: 2 col: 4 bytes: 50 } │ -└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ - -=> DBGInvoke __add_column_to_tidb_table(default, test_dm, 'col_2 Nullable(Int64) default 23333') -=> DBGInvoke __refresh_mapped_table_schema(default, test_dm) -=> DBGInvoke __region_snapshot_apply_block(5) -=> DBGInvoke query_mapped('select * from \$d.\$t order by col_1', default, test_dm) -┌─col_1─┬─col_2─┬─_tidb_rowid─┐ -│ 11 │ 23333 │ 2 │ -│ 13 │ 23333 │ 1 │ -│ 110 │ 23333 │ 1002 │ -│ 130 │ 23333 │ 1003 │ -└───────┴───────┴─────────────┘ - -=> DBGInvoke region_snapshot_pre_handle_block(default, test_dm, 5, 1000, 2000, 1002, 34, 0, 110, 888) -┌─region_snapshot_pre_handle_block(default, test_dm, 5, 1000, 2000, 1002, 30, 0, 110, 888)────────────────────────────────────────────────────────────┐ -│ pre-handle [region_id=5] snapshot with data [write 1 ], pre-decode block cache { schema_version: ?, data_list size: 1, block row: 1 col: 5 bytes: 34 } │ -└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ - ## clean up => DBGInvoke __drop_tidb_table(default, test_dm) => DBGInvoke __refresh_schemas() From 22b021363c7f02d7b92988135a6d984214846e9d Mon Sep 17 00:00:00 2001 From: JaySon-Huang Date: Tue, 17 Dec 2024 19:23:16 +0800 Subject: [PATCH 2/8] Format files Signed-off-by: JaySon-Huang --- dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp | 5 ++++- dbms/src/Storages/KVStore/Decode/PartitionStreams.h | 2 +- dbms/src/Storages/KVStore/Decode/RegionTable.h | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp b/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp index 58163314908..f484d036881 100644 --- a/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp +++ b/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp @@ -664,7 +664,10 @@ void MockRaftCommand::dbgFuncRegionSnapshotPreHandleBlock( output(fmt_buf.toString()); } -void MockRaftCommand::dbgFuncRegionSnapshotApplyBlock(Context & /*context*/, const ASTs & args, DBGInvoker::Printer output) +void MockRaftCommand::dbgFuncRegionSnapshotApplyBlock( + Context & /*context*/, + const ASTs & args, + DBGInvoker::Printer output) { if (args.size() != 1) { diff --git a/dbms/src/Storages/KVStore/Decode/PartitionStreams.h b/dbms/src/Storages/KVStore/Decode/PartitionStreams.h index 874c0fa3790..0fa9c30be53 100644 --- a/dbms/src/Storages/KVStore/Decode/PartitionStreams.h +++ b/dbms/src/Storages/KVStore/Decode/PartitionStreams.h @@ -47,7 +47,7 @@ Block GenRegionBlockDataWithSchema( template DM::WriteResult writeRegionDataToStorage( Context & context, - const RegionPtr& region, + const RegionPtr & region, ReadList & data_list_read, const LoggerPtr & log); diff --git a/dbms/src/Storages/KVStore/Decode/RegionTable.h b/dbms/src/Storages/KVStore/Decode/RegionTable.h index 2dc252455dc..1555ee97ccc 100644 --- a/dbms/src/Storages/KVStore/Decode/RegionTable.h +++ b/dbms/src/Storages/KVStore/Decode/RegionTable.h @@ -120,7 +120,7 @@ class RegionTable : private boost::noncopyable // Protects writeBlockByRegionAndFlush and ensures it's executed by only one thread at the same time. // Only one thread can do this at the same time. // The original name for this function is tryFlushRegion. - RegionDataReadInfoList tryWriteBlockByRegion(const RegionPtr& region); + RegionDataReadInfoList tryWriteBlockByRegion(const RegionPtr & region); void handleInternalRegionsByTable( KeyspaceID keyspace_id, From b09619496d8b7896eb30345402101f80e2c60e1d Mon Sep 17 00:00:00 2001 From: JaySon-Huang Date: Wed, 18 Dec 2024 11:38:51 +0800 Subject: [PATCH 3/8] Fix ut Signed-off-by: JaySon-Huang --- .../TiDB/Schema/tests/gtest_table_info.cpp | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/dbms/src/TiDB/Schema/tests/gtest_table_info.cpp b/dbms/src/TiDB/Schema/tests/gtest_table_info.cpp index 74b52ff57e4..f93f7635c4d 100644 --- a/dbms/src/TiDB/Schema/tests/gtest_table_info.cpp +++ b/dbms/src/TiDB/Schema/tests/gtest_table_info.cpp @@ -308,65 +308,65 @@ try 1145, // 0, R"json({"id":1939,"db_name":{"O":"customer","L":"customer"},"charset":"utf8mb4","collate":"utf8mb4_bin","state":5})json", // - R"json({"id":1145,"name":{"O":"customerdebt","L":"customerdebt"},"cols":[{"id":1,"name":{"O":"id","L":"id"},"offset":0,"type":{"Tp":8,"Flag":515,"Flen":20,"Decimal":0},"state":5}],"state":5,"pk_is_handle":true,"schema_version":-1,"partition":null})json", // - R"stmt(CREATE TABLE `db_1939`.`t_1145`(`id` Int64) Engine = DeltaMerge((`id`), '{"cols":[{"id":1,"name":{"L":"id","O":"id"},"offset":0,"state":5,"type":{"Decimal":0,"Flag":515,"Flen":20,"Tp":8}}],"id":1145,"index_info":[],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"customerdebt","O":"customerdebt"},"pk_is_handle":true,"schema_version":-1,"state":5,"tiflash_replica":{"Count":0},"update_timestamp":0}', 0))stmt", // + R"json({"id":1145,"name":{"O":"customerdebt","L":"customerdebt"},"cols":[{"id":1,"name":{"O":"id","L":"id"},"offset":0,"type":{"Tp":8,"Flag":515,"Flen":20,"Decimal":0},"state":5}],"state":5,"pk_is_handle":true,"partition":null})json", // + R"stmt(CREATE TABLE `db_1939`.`t_1145`(`id` Int64) Engine = DeltaMerge((`id`), '{"cols":[{"id":1,"name":{"L":"id","O":"id"},"offset":0,"state":5,"type":{"Decimal":0,"Flag":515,"Flen":20,"Tp":8}}],"id":1145,"index_info":[],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"customerdebt","O":"customerdebt"},"pk_is_handle":true,"state":5,"tiflash_replica":{"Count":0},"update_timestamp":0}', 0))stmt", // }, StmtCase{ 2049, // 0, R"json({"id":1939,"db_name":{"O":"customer","L":"customer"},"charset":"utf8mb4","collate":"utf8mb4_bin","state":5})json", // - R"json({"id":2049,"name":{"O":"customerdebt","L":"customerdebt"},"cols":[{"id":1,"name":{"O":"id","L":"id"},"offset":0,"type":{"Tp":8,"Flag":515,"Flen":20,"Decimal":0},"state":5}],"state":5,"pk_is_handle":true,"schema_version":-1,"update_timestamp":404545295996944390,"partition":null})json", // - R"stmt(CREATE TABLE `db_1939`.`t_2049`(`id` Int64) Engine = DeltaMerge((`id`), '{"cols":[{"id":1,"name":{"L":"id","O":"id"},"offset":0,"state":5,"type":{"Decimal":0,"Flag":515,"Flen":20,"Tp":8}}],"id":2049,"index_info":[],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"customerdebt","O":"customerdebt"},"pk_is_handle":true,"schema_version":-1,"state":5,"tiflash_replica":{"Count":0},"update_timestamp":404545295996944390}', 0))stmt", // + R"json({"id":2049,"name":{"O":"customerdebt","L":"customerdebt"},"cols":[{"id":1,"name":{"O":"id","L":"id"},"offset":0,"type":{"Tp":8,"Flag":515,"Flen":20,"Decimal":0},"state":5}],"state":5,"pk_is_handle":true,"update_timestamp":404545295996944390,"partition":null})json", // + R"stmt(CREATE TABLE `db_1939`.`t_2049`(`id` Int64) Engine = DeltaMerge((`id`), '{"cols":[{"id":1,"name":{"L":"id","O":"id"},"offset":0,"state":5,"type":{"Decimal":0,"Flag":515,"Flen":20,"Tp":8}}],"id":2049,"index_info":[],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"customerdebt","O":"customerdebt"},"pk_is_handle":true,"state":5,"tiflash_replica":{"Count":0},"update_timestamp":404545295996944390}', 0))stmt", // }, StmtCase{ 31, // 0, R"json({"id":1,"db_name":{"O":"db1","L":"db1"},"charset":"utf8mb4","collate":"utf8mb4_bin","state":5})json", // - R"json({"id":31,"name":{"O":"simple_t","L":"simple_t"},"charset":"","collate":"","cols":[{"id":1,"name":{"O":"i","L":"i"},"offset":0,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":3,"Flag":0,"Flen":11,"Decimal":0,"Charset":"binary","Collate":"binary","Elems":null},"state":5}],"index_info":null,"fk_info":null,"state":5,"pk_is_handle":false,"schema_version":-1,"auto_inc_id":0,"max_col_id":1,"max_idx_id":0,"update_timestamp":404545295996944390,"ShardRowIDBits":0,"partition":null})json", // - R"stmt(CREATE TABLE `db_1`.`t_31`(`i` Nullable(Int32), `_tidb_rowid` Int64) Engine = DeltaMerge((`_tidb_rowid`), '{"cols":[{"id":1,"name":{"L":"i","O":"i"},"offset":0,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":0,"Flen":11,"Tp":3}}],"id":31,"index_info":[],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"simple_t","O":"simple_t"},"pk_is_handle":false,"schema_version":-1,"state":5,"tiflash_replica":{"Count":0},"update_timestamp":404545295996944390}', 0))stmt", // + R"json({"id":31,"name":{"O":"simple_t","L":"simple_t"},"charset":"","collate":"","cols":[{"id":1,"name":{"O":"i","L":"i"},"offset":0,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":3,"Flag":0,"Flen":11,"Decimal":0,"Charset":"binary","Collate":"binary","Elems":null},"state":5}],"index_info":null,"fk_info":null,"state":5,"pk_is_handle":false,"auto_inc_id":0,"max_col_id":1,"max_idx_id":0,"update_timestamp":404545295996944390,"ShardRowIDBits":0,"partition":null})json", // + R"stmt(CREATE TABLE `db_1`.`t_31`(`i` Nullable(Int32), `_tidb_rowid` Int64) Engine = DeltaMerge((`_tidb_rowid`), '{"cols":[{"id":1,"name":{"L":"i","O":"i"},"offset":0,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":0,"Flen":11,"Tp":3}}],"id":31,"index_info":[],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"simple_t","O":"simple_t"},"pk_is_handle":false,"state":5,"tiflash_replica":{"Count":0},"update_timestamp":404545295996944390}', 0))stmt", // }, StmtCase{ 33, // 0, R"json({"id":2,"db_name":{"O":"db2","L":"db2"},"charset":"utf8mb4","collate":"utf8mb4_bin","state":5})json", // - R"json({"id":33,"name":{"O":"pk_t","L":"pk_t"},"charset":"","collate":"","cols":[{"id":1,"name":{"O":"i","L":"i"},"offset":0,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":3,"Flag":3,"Flen":11,"Decimal":0,"Charset":"binary","Collate":"binary","Elems":null},"state":5}],"index_info":null,"fk_info":null,"state":5,"pk_is_handle":true,"schema_version":-1,"auto_inc_id":0,"max_col_id":1,"max_idx_id":0,"update_timestamp":404545312978108418,"ShardRowIDBits":0,"partition":null})json", // - R"stmt(CREATE TABLE `db_2`.`t_33`(`i` Int32) Engine = DeltaMerge((`i`), '{"cols":[{"id":1,"name":{"L":"i","O":"i"},"offset":0,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":3,"Flen":11,"Tp":3}}],"id":33,"index_info":[],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"pk_t","O":"pk_t"},"pk_is_handle":true,"schema_version":-1,"state":5,"tiflash_replica":{"Count":0},"update_timestamp":404545312978108418}', 0))stmt", // + R"json({"id":33,"name":{"O":"pk_t","L":"pk_t"},"charset":"","collate":"","cols":[{"id":1,"name":{"O":"i","L":"i"},"offset":0,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":3,"Flag":3,"Flen":11,"Decimal":0,"Charset":"binary","Collate":"binary","Elems":null},"state":5}],"index_info":null,"fk_info":null,"state":5,"pk_is_handle":true,"auto_inc_id":0,"max_col_id":1,"max_idx_id":0,"update_timestamp":404545312978108418,"ShardRowIDBits":0,"partition":null})json", // + R"stmt(CREATE TABLE `db_2`.`t_33`(`i` Int32) Engine = DeltaMerge((`i`), '{"cols":[{"id":1,"name":{"L":"i","O":"i"},"offset":0,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":3,"Flen":11,"Tp":3}}],"id":33,"index_info":[],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"pk_t","O":"pk_t"},"pk_is_handle":true,"state":5,"tiflash_replica":{"Count":0},"update_timestamp":404545312978108418}', 0))stmt", // }, StmtCase{ 35, // 0, R"json({"id":1,"db_name":{"O":"db1","L":"db1"},"charset":"utf8mb4","collate":"utf8mb4_bin","state":5})json", // - R"json({"id":35,"name":{"O":"not_null_t","L":"not_null_t"},"charset":"","collate":"","cols":[{"id":1,"name":{"O":"i","L":"i"},"offset":0,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":3,"Flag":4097,"Flen":11,"Decimal":0,"Charset":"binary","Collate":"binary","Elems":null},"state":5}],"index_info":null,"fk_info":null,"state":5,"pk_is_handle":false,"schema_version":-1,"auto_inc_id":0,"max_col_id":1,"max_idx_id":0,"update_timestamp":404545324922961926,"ShardRowIDBits":0,"partition":null})json", // - R"stmt(CREATE TABLE `db_1`.`t_35`(`i` Int32, `_tidb_rowid` Int64) Engine = DeltaMerge((`_tidb_rowid`), '{"cols":[{"id":1,"name":{"L":"i","O":"i"},"offset":0,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":4097,"Flen":11,"Tp":3}}],"id":35,"index_info":[],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"not_null_t","O":"not_null_t"},"pk_is_handle":false,"schema_version":-1,"state":5,"tiflash_replica":{"Count":0},"update_timestamp":404545324922961926}', 0))stmt", // + R"json({"id":35,"name":{"O":"not_null_t","L":"not_null_t"},"charset":"","collate":"","cols":[{"id":1,"name":{"O":"i","L":"i"},"offset":0,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":3,"Flag":4097,"Flen":11,"Decimal":0,"Charset":"binary","Collate":"binary","Elems":null},"state":5}],"index_info":null,"fk_info":null,"state":5,"pk_is_handle":false,"auto_inc_id":0,"max_col_id":1,"max_idx_id":0,"update_timestamp":404545324922961926,"ShardRowIDBits":0,"partition":null})json", // + R"stmt(CREATE TABLE `db_1`.`t_35`(`i` Int32, `_tidb_rowid` Int64) Engine = DeltaMerge((`_tidb_rowid`), '{"cols":[{"id":1,"name":{"L":"i","O":"i"},"offset":0,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":4097,"Flen":11,"Tp":3}}],"id":35,"index_info":[],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"not_null_t","O":"not_null_t"},"pk_is_handle":false,"state":5,"tiflash_replica":{"Count":0},"update_timestamp":404545324922961926}', 0))stmt", // }, StmtCase{ 37, // 0, R"json({"id":2,"db_name":{"O":"db2","L":"db2"},"charset":"utf8mb4","collate":"utf8mb4_bin","state":5})json", - R"json({"id":37,"name":{"O":"mytable","L":"mytable"},"charset":"","collate":"","cols":[{"id":1,"name":{"O":"mycol","L":"mycol"},"offset":0,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":15,"Flag":4099,"Flen":256,"Decimal":0,"Charset":"utf8","Collate":"utf8_bin","Elems":null},"state":5}],"index_info":[{"id":1,"idx_name":{"O":"PRIMARY","L":"primary"},"tbl_name":{"O":"","L":""},"idx_cols":[{"name":{"O":"mycol","L":"mycol"},"offset":0,"length":-1}],"is_unique":true,"is_primary":true,"state":5,"index_type":1}],"fk_info":null,"state":5,"pk_is_handle":true,"schema_version":-1,"auto_inc_id":0,"max_col_id":1,"max_idx_id":1,"update_timestamp":404566455285710853,"ShardRowIDBits":0,"partition":null})json", // + R"json({"id":37,"name":{"O":"mytable","L":"mytable"},"charset":"","collate":"","cols":[{"id":1,"name":{"O":"mycol","L":"mycol"},"offset":0,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":15,"Flag":4099,"Flen":256,"Decimal":0,"Charset":"utf8","Collate":"utf8_bin","Elems":null},"state":5}],"index_info":[{"id":1,"idx_name":{"O":"PRIMARY","L":"primary"},"tbl_name":{"O":"","L":""},"idx_cols":[{"name":{"O":"mycol","L":"mycol"},"offset":0,"length":-1}],"is_unique":true,"is_primary":true,"state":5,"index_type":1}],"fk_info":null,"state":5,"pk_is_handle":true,"auto_inc_id":0,"max_col_id":1,"max_idx_id":1,"update_timestamp":404566455285710853,"ShardRowIDBits":0,"partition":null})json", // // The primary index is kept - replace_string_name(R"stmt(CREATE TABLE `db_2`.`t_37`(`mycol` {StringName}) Engine = DeltaMerge((`mycol`), '{"cols":[{"id":1,"name":{"L":"mycol","O":"mycol"},"offset":0,"state":5,"type":{"Charset":"utf8","Collate":"utf8_bin","Decimal":0,"Flag":4099,"Flen":256,"Tp":15}}],"id":37,"index_info":[{"id":1,"idx_cols":[{"length":-1,"name":{"L":"mycol","O":"mycol"},"offset":0}],"idx_name":{"L":"primary","O":"primary"},"index_type":1,"is_global":false,"is_invisible":false,"is_primary":true,"is_unique":true,"state":5}],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"mytable","O":"mytable"},"pk_is_handle":true,"schema_version":-1,"state":5,"tiflash_replica":{"Count":0},"update_timestamp":404566455285710853}', 0))stmt"), // + replace_string_name(R"stmt(CREATE TABLE `db_2`.`t_37`(`mycol` {StringName}) Engine = DeltaMerge((`mycol`), '{"cols":[{"id":1,"name":{"L":"mycol","O":"mycol"},"offset":0,"state":5,"type":{"Charset":"utf8","Collate":"utf8_bin","Decimal":0,"Flag":4099,"Flen":256,"Tp":15}}],"id":37,"index_info":[{"id":1,"idx_cols":[{"length":-1,"name":{"L":"mycol","O":"mycol"},"offset":0}],"idx_name":{"L":"primary","O":"primary"},"index_type":1,"is_global":false,"is_invisible":false,"is_primary":true,"is_unique":true,"state":5}],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"mytable","O":"mytable"},"pk_is_handle":true,"state":5,"tiflash_replica":{"Count":0},"update_timestamp":404566455285710853}', 0))stmt"), // }, StmtCase{ 32, // 0, R"json({"id":1,"db_name":{"O":"test","L":"test"},"charset":"utf8mb4","collate":"utf8mb4_bin","state":5})json", // - R"json({"id":31,"name":{"O":"range_part_t","L":"range_part_t"},"charset":"utf8mb4","collate":"utf8mb4_bin","cols":[{"id":1,"name":{"O":"i","L":"i"},"offset":0,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":3,"Flag":0,"Flen":11,"Decimal":0,"Charset":"binary","Collate":"binary","Elems":null},"state":5,"version":0}],"index_info":null,"fk_info":null,"state":5,"pk_is_handle":false,"schema_version":-1,"auto_inc_id":0,"max_col_id":1,"max_idx_id":0,"update_timestamp":407445773801488390,"ShardRowIDBits":0,"partition":{"type":1,"expr":"`i`","columns":null,"enable":true,"definitions":[{"id":32,"name":{"O":"p0","L":"p0"},"less_than":["0"]},{"id":33,"name":{"O":"p1","L":"p1"},"less_than":["100"]}],"num":0},"compression":"","version":1})json", // - R"stmt(CREATE TABLE `db_1`.`t_32`(`i` Nullable(Int32), `_tidb_rowid` Int64) Engine = DeltaMerge((`_tidb_rowid`), '{"belonging_table_id":31,"cols":[{"id":1,"name":{"L":"i","O":"i"},"offset":0,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":0,"Flen":11,"Tp":3}}],"id":32,"index_info":[],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"range_part_t_32","O":"range_part_t_32"},"pk_is_handle":false,"schema_version":-1,"state":5,"tiflash_replica":{"Count":0},"update_timestamp":407445773801488390}', 0))stmt", // + R"json({"id":31,"name":{"O":"range_part_t","L":"range_part_t"},"charset":"utf8mb4","collate":"utf8mb4_bin","cols":[{"id":1,"name":{"O":"i","L":"i"},"offset":0,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":3,"Flag":0,"Flen":11,"Decimal":0,"Charset":"binary","Collate":"binary","Elems":null},"state":5,"version":0}],"index_info":null,"fk_info":null,"state":5,"pk_is_handle":false,"auto_inc_id":0,"max_col_id":1,"max_idx_id":0,"update_timestamp":407445773801488390,"ShardRowIDBits":0,"partition":{"type":1,"expr":"`i`","columns":null,"enable":true,"definitions":[{"id":32,"name":{"O":"p0","L":"p0"},"less_than":["0"]},{"id":33,"name":{"O":"p1","L":"p1"},"less_than":["100"]}],"num":0},"compression":"","version":1})json", // + R"stmt(CREATE TABLE `db_1`.`t_32`(`i` Nullable(Int32), `_tidb_rowid` Int64) Engine = DeltaMerge((`_tidb_rowid`), '{"belonging_table_id":31,"cols":[{"id":1,"name":{"L":"i","O":"i"},"offset":0,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":0,"Flen":11,"Tp":3}}],"id":32,"index_info":[],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"range_part_t_32","O":"range_part_t_32"},"pk_is_handle":false,"state":5,"tiflash_replica":{"Count":0},"update_timestamp":407445773801488390}', 0))stmt", // }, StmtCase{ 32, // 1700815239, R"json({"id":1,"db_name":{"O":"test","L":"test"},"charset":"utf8mb4","collate":"utf8mb4_bin","state":5})json", // - R"json({"id":31,"name":{"O":"range_part_t","L":"range_part_t"},"charset":"utf8mb4","collate":"utf8mb4_bin","cols":[{"id":1,"name":{"O":"i","L":"i"},"offset":0,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":3,"Flag":0,"Flen":11,"Decimal":0,"Charset":"binary","Collate":"binary","Elems":null},"state":5,"version":0}],"index_info":null,"fk_info":null,"state":5,"pk_is_handle":false,"schema_version":-1,"auto_inc_id":0,"max_col_id":1,"max_idx_id":0,"update_timestamp":407445773801488390,"ShardRowIDBits":0,"partition":{"type":1,"expr":"`i`","columns":null,"enable":true,"definitions":[{"id":32,"name":{"O":"p0","L":"p0"},"less_than":["0"]},{"id":33,"name":{"O":"p1","L":"p1"},"less_than":["100"]}],"num":0},"compression":"","version":1})json", // - R"stmt(CREATE TABLE `db_1`.`t_32`(`i` Nullable(Int32), `_tidb_rowid` Int64) Engine = DeltaMerge((`_tidb_rowid`), '{"belonging_table_id":31,"cols":[{"id":1,"name":{"L":"i","O":"i"},"offset":0,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":0,"Flen":11,"Tp":3}}],"id":32,"index_info":[],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"range_part_t_32","O":"range_part_t_32"},"pk_is_handle":false,"schema_version":-1,"state":5,"tiflash_replica":{"Count":0},"update_timestamp":407445773801488390}', 1700815239))stmt", // + R"json({"id":31,"name":{"O":"range_part_t","L":"range_part_t"},"charset":"utf8mb4","collate":"utf8mb4_bin","cols":[{"id":1,"name":{"O":"i","L":"i"},"offset":0,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":3,"Flag":0,"Flen":11,"Decimal":0,"Charset":"binary","Collate":"binary","Elems":null},"state":5,"version":0}],"index_info":null,"fk_info":null,"state":5,"pk_is_handle":false,"auto_inc_id":0,"max_col_id":1,"max_idx_id":0,"update_timestamp":407445773801488390,"ShardRowIDBits":0,"partition":{"type":1,"expr":"`i`","columns":null,"enable":true,"definitions":[{"id":32,"name":{"O":"p0","L":"p0"},"less_than":["0"]},{"id":33,"name":{"O":"p1","L":"p1"},"less_than":["100"]}],"num":0},"compression":"","version":1})json", // + R"stmt(CREATE TABLE `db_1`.`t_32`(`i` Nullable(Int32), `_tidb_rowid` Int64) Engine = DeltaMerge((`_tidb_rowid`), '{"belonging_table_id":31,"cols":[{"id":1,"name":{"L":"i","O":"i"},"offset":0,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":0,"Flen":11,"Tp":3}}],"id":32,"index_info":[],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"range_part_t_32","O":"range_part_t_32"},"pk_is_handle":false,"state":5,"tiflash_replica":{"Count":0},"update_timestamp":407445773801488390}', 1700815239))stmt", // }, StmtCase{ 546, // 0, R"json({"id":2,"db_name":{"O":"test","L":"test"},"charset":"utf8mb4","collate":"utf8mb4_bin","state":5})json", // R"json({"id":546,"name":{"O":"tcfc7825f","L":"tcfc7825f"},"charset":"utf8mb4","collate":"utf8mb4_general_ci","cols":[{"id":1,"name":{"O":"col_86","L":"col_86"},"offset":0,"default_is_expr":false,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":252,"Flag":128,"Flen":65535,"Decimal":0,"Charset":"binary","Collate":"binary","ElemsIsBinaryLit":null,"Array":false},"state":5,"comment":"","hidden":false,"change_state_info":null,"version":2},{"id":2,"name":{"O":"col_87","L":"col_87"},"offset":1,"default":"1994-05-0600:00:00","default_is_expr":false,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":12,"Flag":129,"Flen":19,"Decimal":0,"Charset":"binary","Collate":"binary","ElemsIsBinaryLit":null,"Array":false},"state":5,"comment":"","hidden":false,"change_state_info":null,"version":2},{"id":3,"name":{"O":"col_88","L":"col_88"},"offset":2,"default_is_expr":false,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":16,"Flag":32,"Flen":42,"Decimal":0,"Charset":"binary","Collate":"binary","ElemsIsBinaryLit":null,"Array":false},"state":5,"comment":"","hidden":false,"change_state_info":null,"version":2},{"id":4,"name":{"O":"col_89","L":"col_89"},"offset":3,"default":"\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000","default_is_expr":false,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":254,"Flag":129,"Flen":21,"Decimal":0,"Charset":"binary","Collate":"binary","ElemsIsBinaryLit":null,"Array":false},"state":5,"comment":"","hidden":false,"change_state_info":null,"version":2},{"id":5,"name":{"O":"col_90","L":"col_90"},"offset":4,"default_is_expr":false,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":1,"Flag":4129,"Flen":3,"Decimal":0,"Charset":"binary","Collate":"binary","ElemsIsBinaryLit":null,"Array":false},"state":5,"comment":"","hidden":false,"change_state_info":null,"version":2},{"id":6,"name":{"O":"col_91","L":"col_91"},"offset":5,"default":"\u0007\u0007","default_bit":"Bwc=","default_is_expr":false,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":16,"Flag":32,"Flen":12,"Decimal":0,"Charset":"binary","Collate":"binary","ElemsIsBinaryLit":null,"Array":false},"state":5,"comment":"","hidden":false,"change_state_info":null,"version":2},{"id":7,"name":{"O":"col_92","L":"col_92"},"offset":6,"default":"kY~6to6H4ut*QAPrj@\u0026","default_is_expr":false,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":15,"Flag":129,"Flen":343,"Decimal":0,"Charset":"binary","Collate":"binary","ElemsIsBinaryLit":null,"Array":false},"state":5,"comment":"","hidden":false,"change_state_info":null,"version":2},{"id":8,"name":{"O":"col_93","L":"col_93"},"offset":7,"default_is_expr":false,"generated_expr_string":"","generated_stored":false,"dependences":null,"type":{"Tp":245,"Flag":128,"Flen":4294967295,"Decimal":0,"Charset":"binary","Collate":"binary","ElemsIsBinaryLit":null,"Array":false},"state":5,"comment":"","hidden":false,"change_state_info":null,"version":2}],"index_info":null,"constraint_info":null,"fk_info":null,"state":5,"pk_is_handle":false,"is_common_handle":false,"common_handle_version":0,"comment":"","auto_inc_id":0,"auto_id_cache":0,"auto_rand_id":0,"max_col_id":8,"max_idx_id":0,"max_fk_id":0,"max_cst_id":0,"update_timestamp":452653255976550448,"ShardRowIDBits":0,"max_shard_row_id_bits":0,"auto_random_bits":0,"auto_random_range_bits":0,"pre_split_regions":0,"compression":"","view":null,"sequence":null,"Lock":null,"version":5,"tiflash_replica":{"Count":1,"LocationLabels":[],"Available":false,"AvailablePartitionIDs":null},"is_columnar":false,"temp_table_type":0,"cache_table_status":0,"policy_ref_info":null,"stats_options":null,"exchange_partition_info":null,"ttl_info":null,"revision":1})json", // - replace_string_name(R"stmt(CREATE TABLE `db_2`.`t_546`(`col_86` Nullable({StringName}), `col_87` MyDateTime(0), `col_88` Nullable(UInt64), `col_89` {StringName}, `col_90` UInt8, `col_91` Nullable(UInt64), `col_92` {StringName}, `col_93` Nullable({StringName}), `_tidb_rowid` Int64) Engine = DeltaMerge((`_tidb_rowid`), '{"cols":[{"id":1,"name":{"L":"col_86","O":"col_86"},"offset":0,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":128,"Flen":65535,"Tp":252}},{"default":"1994-05-0600:00:00","id":2,"name":{"L":"col_87","O":"col_87"},"offset":1,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":129,"Flen":19,"Tp":12}},{"id":3,"name":{"L":"col_88","O":"col_88"},"offset":2,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":32,"Flen":42,"Tp":16}},{"default":"\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000","id":4,"name":{"L":"col_89","O":"col_89"},"offset":3,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":129,"Flen":21,"Tp":254}},{"id":5,"name":{"L":"col_90","O":"col_90"},"offset":4,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":4129,"Flen":3,"Tp":1}},{"default":"\\u0007\\u0007","default_bit":"Bwc=","id":6,"name":{"L":"col_91","O":"col_91"},"offset":5,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":32,"Flen":12,"Tp":16}},{"default":"kY~6to6H4ut*QAPrj@&","id":7,"name":{"L":"col_92","O":"col_92"},"offset":6,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":129,"Flen":343,"Tp":15}},{"id":8,"name":{"L":"col_93","O":"col_93"},"offset":7,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":128,"Flen":-1,"Tp":245}}],"id":546,"index_info":[],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"tcfc7825f","O":"tcfc7825f"},"pk_is_handle":false,"schema_version":-1,"state":5,"tiflash_replica":{"Available":false,"Count":1},"update_timestamp":452653255976550448}', 0))stmt"), // + replace_string_name(R"stmt(CREATE TABLE `db_2`.`t_546`(`col_86` Nullable({StringName}), `col_87` MyDateTime(0), `col_88` Nullable(UInt64), `col_89` {StringName}, `col_90` UInt8, `col_91` Nullable(UInt64), `col_92` {StringName}, `col_93` Nullable({StringName}), `_tidb_rowid` Int64) Engine = DeltaMerge((`_tidb_rowid`), '{"cols":[{"id":1,"name":{"L":"col_86","O":"col_86"},"offset":0,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":128,"Flen":65535,"Tp":252}},{"default":"1994-05-0600:00:00","id":2,"name":{"L":"col_87","O":"col_87"},"offset":1,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":129,"Flen":19,"Tp":12}},{"id":3,"name":{"L":"col_88","O":"col_88"},"offset":2,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":32,"Flen":42,"Tp":16}},{"default":"\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000","id":4,"name":{"L":"col_89","O":"col_89"},"offset":3,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":129,"Flen":21,"Tp":254}},{"id":5,"name":{"L":"col_90","O":"col_90"},"offset":4,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":4129,"Flen":3,"Tp":1}},{"default":"\\u0007\\u0007","default_bit":"Bwc=","id":6,"name":{"L":"col_91","O":"col_91"},"offset":5,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":32,"Flen":12,"Tp":16}},{"default":"kY~6to6H4ut*QAPrj@&","id":7,"name":{"L":"col_92","O":"col_92"},"offset":6,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":129,"Flen":343,"Tp":15}},{"id":8,"name":{"L":"col_93","O":"col_93"},"offset":7,"state":5,"type":{"Charset":"binary","Collate":"binary","Decimal":0,"Flag":128,"Flen":-1,"Tp":245}}],"id":546,"index_info":[],"is_common_handle":false,"keyspace_id":4294967295,"name":{"L":"tcfc7825f","O":"tcfc7825f"},"pk_is_handle":false,"state":5,"tiflash_replica":{"Available":false,"Count":1},"update_timestamp":452653255976550448}', 0))stmt"), // }, }; // clang-format on From 84318881db2529d9cd0709a767f3bd1dc1e0a8c9 Mon Sep 17 00:00:00 2001 From: JaySon-Huang Date: Wed, 18 Dec 2024 16:42:01 +0800 Subject: [PATCH 4/8] Fix mock tests Signed-off-by: JaySon-Huang --- .../Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp b/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp index f484d036881..2d8ad4f8ac5 100644 --- a/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp +++ b/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp @@ -46,6 +46,8 @@ #include #include +#include "Common/Exception.h" + namespace DB { namespace FailPoints @@ -87,9 +89,10 @@ RegionPtr GenDbgRegionSnapshotWithData(Context & context, const ASTs & args) // Get start key and end key form multiple column if it is clustered_index. std::vector start_keys; std::vector end_keys; + const auto & pk_idx_cols = table_info.getPrimaryIndexInfo().idx_cols; for (size_t i = 0; i < handle_column_size; i++) { - auto & column_info = table_info.columns[table_info.getPrimaryIndexInfo().idx_cols[i].offset]; + auto & column_info = table_info.columns[pk_idx_cols[i].offset]; auto start_field = RegionBench::convertField(column_info, typeid_cast(*args[3 + i]).value); TiDB::DatumBumpy start_datum = TiDB::DatumBumpy(start_field, column_info.tp); @@ -108,8 +111,7 @@ RegionPtr GenDbgRegionSnapshotWithData(Context & context, const ASTs & args) const size_t len = table->table_info.columns.size() + 3; - if ((args_end - args_begin) % len) - throw Exception("Number of insert values and columns do not match.", ErrorCodes::LOGICAL_ERROR); + RUNTIME_CHECK_MSG((((args_end - args_begin) % len) == 0), "Number of insert values and columns do not match."); // Parse row values for (auto it = args_begin; it != args_end; it += len) @@ -172,7 +174,12 @@ void MockRaftCommand::dbgFuncRegionSnapshotWithData(Context & context, const AST // Mock to apply a snapshot with data in `region` auto & tmt = context.getTMTContext(); - context.getTMTContext().getKVStore()->checkAndApplyPreHandledSnapshot(region, tmt); + tmt.getKVStore()->checkAndApplyPreHandledSnapshot(region, tmt); + // Decode the committed rows into Block and flush to the IStorage layer + if (auto region_applied = tmt.getKVStore()->getRegion(region_id); region_applied) + { + tmt.getRegionTable().tryWriteBlockByRegion(region_applied); + } output(fmt::format("put region #{}, range{} to table #{} with {} records", region_id, range_string, table_id, cnt)); } From 2b3699a7b616b08852be2a65249f5113efa34b00 Mon Sep 17 00:00:00 2001 From: JaySon-Huang Date: Wed, 18 Dec 2024 17:26:37 +0800 Subject: [PATCH 5/8] Cleanup codes Signed-off-by: JaySon-Huang --- dbms/src/Debug/DBGInvoker.cpp | 2 - .../Debug/dbgKVStore/dbgFuncMockRaftCommand.h | 10 - .../dbgKVStore/dbgFuncMockRaftSnapshot.cpp | 177 +----------------- .../src/Storages/KVStore/Decode/RegionTable.h | 62 ------ dbms/src/Storages/StorageDeltaMerge.cpp | 1 - dbms/src/TiDB/Schema/TiDB.cpp | 6 - dbms/src/TiDB/Schema/TiDB.h | 4 - 7 files changed, 1 insertion(+), 261 deletions(-) diff --git a/dbms/src/Debug/DBGInvoker.cpp b/dbms/src/Debug/DBGInvoker.cpp index fd0a38455bb..12410e0770c 100644 --- a/dbms/src/Debug/DBGInvoker.cpp +++ b/dbms/src/Debug/DBGInvoker.cpp @@ -101,8 +101,6 @@ DBGInvoker::DBGInvoker() regSchemalessFunc("region_snapshot", MockRaftCommand::dbgFuncRegionSnapshot); regSchemalessFunc("region_snapshot_data", MockRaftCommand::dbgFuncRegionSnapshotWithData); - regSchemalessFunc("region_snapshot_pre_handle_block", /**/ MockRaftCommand::dbgFuncRegionSnapshotPreHandleBlock); - regSchemalessFunc("region_snapshot_apply_block", /* */ MockRaftCommand::dbgFuncRegionSnapshotApplyBlock); regSchemalessFunc("region_snapshot_pre_handle_file", /* */ MockRaftCommand::dbgFuncRegionSnapshotPreHandleDTFiles); regSchemalessFunc( "region_snapshot_pre_handle_file_pks", diff --git a/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftCommand.h b/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftCommand.h index c4240dc089a..ee184335938 100644 --- a/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftCommand.h +++ b/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftCommand.h @@ -62,16 +62,6 @@ struct MockRaftCommand // ./storage-client.sh "DBGInvoke region_ingest_sst(database_name, table_name, region_id, start, end)" static void dbgFuncIngestSST(Context & context, const ASTs & args, DBGInvoker::Printer output); - // Apply snapshot for a region. (pre-handle) - // Usage: - // ./storages-client.sh "DBGInvoke region_snapshot_pre_handle_block(database_name, table_name, region_id, start, end, handle_id1, tso1, del1, r1_c1, r1_c2, ..., handle_id2, tso2, del2, r2_c1, r2_c2, ... )" - static void dbgFuncRegionSnapshotPreHandleBlock(Context & context, const ASTs & args, DBGInvoker::Printer output); - - // Apply snapshot for a region. (apply a pre-handle snapshot) - // Usage: - // ./storages-client.sh "DBGInvoke region_snapshot_apply_block(region_id)" - static void dbgFuncRegionSnapshotApplyBlock(Context & context, const ASTs & args, DBGInvoker::Printer output); - // Simulate a region pre-handle snapshot data to DTFiles // Usage: // ./storage-client.sh "DBGInvoke region_snapshot_pre_handle_file(database_name, table_name, region_id, start, end, schema_string, pk_name[, test-fields=1, cfs="write,default"])" diff --git a/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp b/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp index 2d8ad4f8ac5..03a9df82e13 100644 --- a/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp +++ b/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include #include @@ -46,8 +47,6 @@ #include #include -#include "Common/Exception.h" - namespace DB { namespace FailPoints @@ -495,32 +494,10 @@ void MockRaftCommand::dbgFuncIngestSST(Context & context, const ASTs & args, DBG struct GlobalRegionMap { using Key = std::string; - // using BlockVal = std::pair; - // std::unordered_map regions_block; using SnapPath = std::pair>; std::unordered_map regions_snap_files; std::mutex mutex; -#if 0 - void insertRegionCache(const Key & name, BlockVal && val) - { - auto _ = std::lock_guard(mutex); - regions_block[name] = std::move(val); - } - BlockVal popRegionCache(const Key & name) - { - auto _ = std::lock_guard(mutex); - if (auto it = regions_block.find(name); it == regions_block.end()) - throw Exception(std::string(__PRETTY_FUNCTION__) + " ... " + name); - else - { - auto ret = std::move(it->second); - regions_block.erase(it); - return ret; - } - } -#endif - void insertRegionSnap(const Key & name, SnapPath && val) { auto _ = std::lock_guard(mutex); @@ -542,158 +519,6 @@ struct GlobalRegionMap static GlobalRegionMap GLOBAL_REGION_MAP; -#if 0 -/// Mock to pre-decode snapshot to block then apply - -/// Pre-decode region data into block cache and remove committed data from `region` -RegionPtrWithBlock::CachePtr GenRegionPreDecodeBlockData(const RegionPtr & region, Context & context) -{ - auto keyspace_id = region->getKeyspaceID(); - const auto & tmt = context.getTMTContext(); - { - Timestamp gc_safe_point = 0; - if (auto pd_client = tmt.getPDClient(); !pd_client->isMock()) - { - gc_safe_point = PDClientHelper::getGCSafePointWithRetry( - pd_client, - keyspace_id, - false, - context.getSettingsRef().safe_point_update_interval_seconds); - } - /** - * In 5.0.1, feature `compaction filter` is enabled by default. Under such feature tikv will do gc in write & default cf individually. - * If some rows were updated and add tiflash replica, tiflash store may receive region snapshot with unmatched data in write & default cf sst files. - */ - region->tryCompactionFilter(gc_safe_point); - } - std::optional data_list_read = std::nullopt; - try - { - data_list_read = ReadRegionCommitCache(region, true); - if (!data_list_read) - return nullptr; - } - catch (const Exception & e) - { - if (e.code() == ErrorCodes::ILLFORMAT_RAFT_ROW) - { - // br or lighting may write illegal data into tikv, skip pre-decode and ingest sst later. - LOG_WARNING( - Logger::get(__PRETTY_FUNCTION__), - "Got error while reading region committed cache: {}. Skip pre-decode and keep original cache.", - e.displayText()); - // set data_list_read and let apply snapshot process use empty block - data_list_read = RegionDataReadInfoList(); - } - else - throw; - } - - TableID table_id = region->getMappedTableID(); - Int64 schema_version = DEFAULT_UNSPECIFIED_SCHEMA_VERSION; - Block res_block; - - const auto atomic_decode = [&](bool force_decode) -> bool { - Stopwatch watch; - auto storage = tmt.getStorages().get(keyspace_id, table_id); - if (storage == nullptr || storage->isTombstone()) - { - if (!force_decode) // Need to update. - return false; - if (storage == nullptr) // Table must have just been GC-ed. - return true; - } - - /// Get a structure read lock throughout decode, during which schema must not change. - TableStructureLockHolder lock; - try - { - lock = storage->lockStructureForShare(getThreadNameAndID()); - } - catch (DB::Exception & e) - { - // If the storage is physical dropped (but not removed from `ManagedStorages`) when we want to decode snapshot, consider the decode done. - if (e.code() == ErrorCodes::TABLE_IS_DROPPED) - return true; - else - throw; - } - - DecodingStorageSchemaSnapshotConstPtr decoding_schema_snapshot - = storage->getSchemaSnapshotAndBlockForDecoding(lock, false, true).first; - res_block = createBlockSortByColumnID(decoding_schema_snapshot); - auto reader = RegionBlockReader(decoding_schema_snapshot); - return reader.read(res_block, *data_list_read, force_decode); - }; - - /// In TiFlash, the actions between applying raft log and schema changes are not strictly synchronized. - /// There could be a chance that some raft logs come after a table gets tombstoned. Take care of it when - /// decoding data. Check the test case for more details. - FAIL_POINT_PAUSE(FailPoints::pause_before_apply_raft_snapshot); - - if (!atomic_decode(false)) - { - tmt.getSchemaSyncerManager()->syncSchemas(context, keyspace_id); - - if (!atomic_decode(true)) - throw Exception( - "Pre-decode " + region->toString() + " cache to table " + std::to_string(table_id) + " block failed", - ErrorCodes::LOGICAL_ERROR); - } - - RemoveRegionCommitCache(region, *data_list_read); - - return std::make_unique(std::move(res_block), schema_version, std::move(*data_list_read)); -} -#endif - -void MockRaftCommand::dbgFuncRegionSnapshotPreHandleBlock( - Context & context, - const ASTs & args, - DBGInvoker::Printer output) -{ - FmtBuffer fmt_buf; - auto region = GenDbgRegionSnapshotWithData(context, args); - const auto region_name = "__snap_" + std::to_string(region->id()); - fmt_buf.fmtAppend("pre-handle {} snapshot with data {}", region->toString(false), region->dataInfo()); -#if 0 - auto & tmt = context.getTMTContext(); - auto block_cache = GenRegionPreDecodeBlockData(region, tmt.getContext()); - fmt_buf.append(", pre-decode block cache"); - fmt_buf.fmtAppend( - " {{ schema_version: ?, data_list size: {}, block row: {} col: {} bytes: {} }}", - block_cache->data_list_read.size(), - block_cache->block.rows(), - block_cache->block.columns(), - block_cache->block.bytes()); - GLOBAL_REGION_MAP.insertRegionCache(region_name, {region, std::move(block_cache)}); -#endif - output(fmt_buf.toString()); -} - -void MockRaftCommand::dbgFuncRegionSnapshotApplyBlock( - Context & /*context*/, - const ASTs & args, - DBGInvoker::Printer output) -{ - if (args.size() != 1) - { - throw Exception("Args not matched, should be: region-id", ErrorCodes::BAD_ARGUMENTS); - } - - auto region_id = static_cast(safeGet(typeid_cast(*args.front()).value)); -#if 0 - auto [region, block_cache] = GLOBAL_REGION_MAP.popRegionCache("__snap_" + std::to_string(region_id)); - auto & tmt = context.getTMTContext(); - context.getTMTContext().getKVStore()->checkAndApplyPreHandledSnapshot( - {region, std::move(block_cache)}, - tmt); -#endif - - output(fmt::format("success apply {} with block cache", region_id)); -} - - /// Mock to pre-decode snapshot to DTFile(s) then apply // Simulate a region pre-handle snapshot data to DTFiles diff --git a/dbms/src/Storages/KVStore/Decode/RegionTable.h b/dbms/src/Storages/KVStore/Decode/RegionTable.h index 1555ee97ccc..c3fd605ca18 100644 --- a/dbms/src/Storages/KVStore/Decode/RegionTable.h +++ b/dbms/src/Storages/KVStore/Decode/RegionTable.h @@ -27,18 +27,11 @@ #include #include -#include #include #include -#include #include #include -namespace TiDB -{ -struct TableInfo; -}; - namespace DB { struct MockRaftCommand; @@ -208,61 +201,6 @@ class RegionTable : private boost::noncopyable }; -// Block cache of region data with schema version. -struct RegionPreDecodeBlockData -{ - Block block; - Int64 schema_version; - RegionDataReadInfoList data_list_read; // if schema version changed, use kv data to rebuild block cache - - RegionPreDecodeBlockData(Block && block_, Int64 schema_version_, RegionDataReadInfoList && data_list_read_) - : block(std::move(block_)) - , schema_version(schema_version_) - , data_list_read(std::move(data_list_read_)) - {} - DISALLOW_COPY(RegionPreDecodeBlockData); - void toString(std::stringstream & ss) const - { - ss << " {"; - ss << " schema_version: " << schema_version; - ss << ", data_list size: " << data_list_read.size(); - ss << ", block row: " << block.rows() << " col: " << block.columns() << " bytes: " << block.bytes(); - ss << " }"; - } -}; - -#if 0 -// A wrap of RegionPtr, could try to use its block cache while writing region data to storage. -struct RegionPtrWithBlock -{ - using Base = RegionPtr; - using CachePtr = std::unique_ptr; - - RegionPtrWithBlock(const Base & base_) - : base(base_) - , pre_decode_cache(nullptr) - {} - - /// to be compatible with usage as RegionPtr. - Base::element_type * operator->() const { return base.operator->(); } - const Base::element_type & operator*() const { return base.operator*(); } - - /// make it could be cast into RegionPtr implicitly. - operator const Base &() const { return base; } - - const Base & base; - CachePtr pre_decode_cache; - -private: - friend struct MockRaftCommand; - /// Can accept const ref of RegionPtr without cache - RegionPtrWithBlock(const Base & base_, CachePtr cache) - : base(base_) - , pre_decode_cache(std::move(cache)) - {} -}; -#endif - // A wrap of RegionPtr, with snapshot files directory waitting to be ingested struct RegionPtrWithSnapshotFiles { diff --git a/dbms/src/Storages/StorageDeltaMerge.cpp b/dbms/src/Storages/StorageDeltaMerge.cpp index 7a50b6e0c3e..df7ceb171a5 100644 --- a/dbms/src/Storages/StorageDeltaMerge.cpp +++ b/dbms/src/Storages/StorageDeltaMerge.cpp @@ -1292,7 +1292,6 @@ inline OptionTableInfoConstRef getTableInfoForCreateStatement( /// If TableInfo from TiDB is empty, for example, create DM table for test, /// we refine TableInfo from store's table column, so that we can restore column id next time - // table_info_from_store.schema_version = DEFAULT_UNSPECIFIED_SCHEMA_VERSION; for (const auto & column_define : store_table_columns) { if (hidden_columns.has(column_define.name)) diff --git a/dbms/src/TiDB/Schema/TiDB.cpp b/dbms/src/TiDB/Schema/TiDB.cpp index 00be2f4aace..2a5dd792044 100644 --- a/dbms/src/TiDB/Schema/TiDB.cpp +++ b/dbms/src/TiDB/Schema/TiDB.cpp @@ -1005,8 +1005,6 @@ try } } - // json->set("schema_version", schema_version); - json->set("tiflash_replica", replica_info.getJSONObject()); std::stringstream buf; @@ -1085,10 +1083,6 @@ try if (!partition_obj.isNull()) partition.deserialize(partition_obj); } - // if (obj->has("schema_version")) - // { - // schema_version = obj->getValue("schema_version"); - // } if (obj->has("view") && !obj->getObject("view").isNull()) { is_view = true; diff --git a/dbms/src/TiDB/Schema/TiDB.h b/dbms/src/TiDB/Schema/TiDB.h index 3a5763ebca0..09dda11ac69 100644 --- a/dbms/src/TiDB/Schema/TiDB.h +++ b/dbms/src/TiDB/Schema/TiDB.h @@ -307,10 +307,6 @@ struct TableInfo bool is_view = false; // If the table is sequence, we should ignore it. bool is_sequence = false; -#if 0 - Int64 schema_version - = DEFAULT_UNSPECIFIED_SCHEMA_VERSION; // TODO(hyy):can be removed after removing RegionPtrWithBlock -#endif // The TiFlash replica info persisted by TiDB TiFlashReplicaInfo replica_info; From ee203d868d90273d7fb6af48ced8bb124c21bf94 Mon Sep 17 00:00:00 2001 From: JaySon-Huang Date: Wed, 18 Dec 2024 18:15:18 +0800 Subject: [PATCH 6/8] Cleanup Signed-off-by: JaySon-Huang --- .../dbgKVStore/dbgFuncMockRaftSnapshot.cpp | 2 +- .../src/Storages/KVStore/Decode/RegionTable.h | 1 - .../KVStore/MultiRaft/ApplySnapshot.cpp | 30 +++++++++---------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp b/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp index 03a9df82e13..8860fd885c7 100644 --- a/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp +++ b/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp @@ -171,7 +171,7 @@ void MockRaftCommand::dbgFuncRegionSnapshotWithData(Context & context, const AST auto table_id = region->getMappedTableID(); auto cnt = region->writeCFCount(); - // Mock to apply a snapshot with data in `region` + // Mock to apply a snapshot with committed rows in `region` auto & tmt = context.getTMTContext(); tmt.getKVStore()->checkAndApplyPreHandledSnapshot(region, tmt); // Decode the committed rows into Block and flush to the IStorage layer diff --git a/dbms/src/Storages/KVStore/Decode/RegionTable.h b/dbms/src/Storages/KVStore/Decode/RegionTable.h index c3fd605ca18..d0eaf4f80ec 100644 --- a/dbms/src/Storages/KVStore/Decode/RegionTable.h +++ b/dbms/src/Storages/KVStore/Decode/RegionTable.h @@ -46,7 +46,6 @@ class Block; struct MockTiDBTable; class RegionRangeKeys; class RegionTaskLock; -struct RegionPtrWithBlock; struct RegionPtrWithSnapshotFiles; class RegionScanFilter; using RegionScanFilterPtr = std::shared_ptr; diff --git a/dbms/src/Storages/KVStore/MultiRaft/ApplySnapshot.cpp b/dbms/src/Storages/KVStore/MultiRaft/ApplySnapshot.cpp index 3908fe23a49..d01c3aab43e 100644 --- a/dbms/src/Storages/KVStore/MultiRaft/ApplySnapshot.cpp +++ b/dbms/src/Storages/KVStore/MultiRaft/ApplySnapshot.cpp @@ -46,7 +46,7 @@ extern const int TABLE_IS_DROPPED; template void KVStore::checkAndApplyPreHandledSnapshot(const RegionPtrWrap & new_region, TMTContext & tmt) { - auto region_id = new_region->id(); + const auto region_id = new_region->id(); auto old_region = getRegion(region_id); UInt64 old_applied_index = 0; @@ -75,18 +75,16 @@ void KVStore::checkAndApplyPreHandledSnapshot(const RegionPtrWrap & new_region, return; } - { - LOG_INFO(log, "{} set state to `Applying`", old_region->toString()); - // Set original region state to `Applying` and any read request toward this region should be rejected because - // engine may delete data unsafely. - auto region_lock = region_manager.genRegionTaskLock(old_region->id()); - old_region->setStateApplying(); - // It is not worthy to call `tryWriteBlockByRegion` and `tryFlushRegionCacheInStorage` here, - // even if the written data is useful, it could be overwritten later in `onSnapshot`. - // Note that we must persistRegion. This is to ensure even if a restart happens before - // the apply snapshot is finished, TiFlash can correctly reject the read index requests - persistRegion(*old_region, region_lock, PersistRegionReason::ApplySnapshotPrevRegion, ""); - } + LOG_INFO(log, "{} set state to `Applying`", old_region->toString()); + // Set original region state to `Applying` and any read request toward this region should be rejected because + // engine may delete data unsafely. + auto region_lock = region_manager.genRegionTaskLock(old_region->id()); + old_region->setStateApplying(); + // It is not worthy to call `tryWriteBlockByRegion` and `tryFlushRegionCacheInStorage` here, + // even if the written data is useful, it could be overwritten later in `onSnapshot`. + // Note that we must persistRegion. This is to ensure even if a restart happens before + // the apply snapshot is finished, TiFlash can correctly reject the read index requests + persistRegion(*old_region, region_lock, PersistRegionReason::ApplySnapshotPrevRegion, ""); } { @@ -153,14 +151,14 @@ void KVStore::checkAndApplyPreHandledSnapshot(const RegionPtrWrap & new_region, } } } + // NOTE Do NOT move it to prehandle stage! // Otherwise a fap snapshot may be cleaned when prehandling after restarted. - if (tmt.getContext().getSharedContextDisagg()->isDisaggregatedStorageMode()) + if constexpr (!std::is_same_v) { - if constexpr (!std::is_same_v) + if (tmt.getContext().getSharedContextDisagg()->isDisaggregatedStorageMode()) { auto fap_ctx = tmt.getContext().getSharedContextDisagg()->fap_context; - auto region_id = new_region->id(); // Everytime we meet a regular snapshot, we try to clean obsolete fap ingest info. fap_ctx->resolveFapSnapshotState(tmt, proxy_helper, region_id, true); } From 14e63c784c67466e243a2530fdc72729fb14a9f2 Mon Sep 17 00:00:00 2001 From: JaySon-Huang Date: Wed, 18 Dec 2024 18:25:22 +0800 Subject: [PATCH 7/8] Add comments Signed-off-by: JaySon-Huang --- dbms/src/Storages/KVStore/KVStore.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/dbms/src/Storages/KVStore/KVStore.h b/dbms/src/Storages/KVStore/KVStore.h index b7875714832..d18d2848613 100644 --- a/dbms/src/Storages/KVStore/KVStore.h +++ b/dbms/src/Storages/KVStore/KVStore.h @@ -289,10 +289,15 @@ class KVStore final : private boost::noncopyable DM::FileConvertJobType, TMTContext & tmt); + // Note that if there are committed rows in `new_region`, those rows will be left to + // the in-memory `RegionData` but not flushed into the IStorage layer after calling + // `checkAndApplyPreHandledSnapshot`. + // `PreHandle` should take care of the committed rows and only leave uncommitted + // key-values in the `new_region`. template - void checkAndApplyPreHandledSnapshot(const RegionPtrWrap &, TMTContext & tmt); + void checkAndApplyPreHandledSnapshot(const RegionPtrWrap & new_region, TMTContext & tmt); template - void onSnapshot(const RegionPtrWrap &, RegionPtr old_region, UInt64 old_region_index, TMTContext & tmt); + void onSnapshot(const RegionPtrWrap & new_region, RegionPtr old_region, UInt64 old_region_index, TMTContext & tmt); RegionPtr handleIngestSSTByDTFile( const RegionPtr & region, From 9461ba8b8cfa7b64cff4fd5f861a362015be17f4 Mon Sep 17 00:00:00 2001 From: JaySon Date: Thu, 19 Dec 2024 10:38:02 +0800 Subject: [PATCH 8/8] Apply suggestions from code review --- dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp b/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp index 8860fd885c7..2c1aec5d265 100644 --- a/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp +++ b/dbms/src/Debug/dbgKVStore/dbgFuncMockRaftSnapshot.cpp @@ -174,7 +174,8 @@ void MockRaftCommand::dbgFuncRegionSnapshotWithData(Context & context, const AST // Mock to apply a snapshot with committed rows in `region` auto & tmt = context.getTMTContext(); tmt.getKVStore()->checkAndApplyPreHandledSnapshot(region, tmt); - // Decode the committed rows into Block and flush to the IStorage layer + // Decode the committed rows into Block and flush to the IStorage layer. + // This dose not ensure the atomic of "apply snapshot". But we only use it for writing tests now. if (auto region_applied = tmt.getKVStore()->getRegion(region_id); region_applied) { tmt.getRegionTable().tryWriteBlockByRegion(region_applied);