From 839a659fd8e27df7fcdd5f09b48f3a8c342b8c47 Mon Sep 17 00:00:00 2001 From: Zhigao Tong Date: Fri, 22 Apr 2022 12:52:04 +0800 Subject: [PATCH] Fix metadata corruption release-5.4 (#4496) close pingcap/tiflash#2576, close pingcap/tiflash#3435, close pingcap/tiflash#4437 --- contrib/tiflash-proxy | 2 +- dbms/src/Debug/dbgFuncRegion.cpp | 15 +++ dbms/src/Flash/DiagnosticsService.cpp | 1 - .../Storages/Transaction/ApplySnapshot.cpp | 24 +++-- dbms/src/Storages/Transaction/KVStore.cpp | 7 +- dbms/src/Storages/Transaction/KVStore.h | 1 + dbms/src/Storages/Transaction/ProxyFFI.cpp | 92 +++++++++++++++---- dbms/src/Storages/Transaction/ProxyFFI.h | 11 ++- tests/fullstack-test/sample.test | 13 ++- 9 files changed, 132 insertions(+), 34 deletions(-) diff --git a/contrib/tiflash-proxy b/contrib/tiflash-proxy index ed635da202e..aa46d3708d2 160000 --- a/contrib/tiflash-proxy +++ b/contrib/tiflash-proxy @@ -1 +1 @@ -Subproject commit ed635da202e21118750dfbdb69b5327d7181955c +Subproject commit aa46d3708d29330aa15bc731f1852f8dedb06728 diff --git a/dbms/src/Debug/dbgFuncRegion.cpp b/dbms/src/Debug/dbgFuncRegion.cpp index 2589b3eedb1..d8a20bfa0dc 100644 --- a/dbms/src/Debug/dbgFuncRegion.cpp +++ b/dbms/src/Debug/dbgFuncRegion.cpp @@ -270,6 +270,21 @@ void dbgFuncFindRegionByRange(Context & context, const ASTs & args, DBGInvoker:: fmt_buf.fmtAppend("{} ", region.second->id()); output(fmt_buf.toString()); } + else + { + if (!regions.empty()) + { + for (const auto & region : regions) + { + auto str = fmt::format( + "{}, local state: {}, proxy internal state: {}", + region.second->toString(), + region.second->getMetaRegion().ShortDebugString(), + kvstore->getProxyHelper()->getRegionLocalState(region.first).ShortDebugString()); + output(str); + } + } + } } } // namespace DB diff --git a/dbms/src/Flash/DiagnosticsService.cpp b/dbms/src/Flash/DiagnosticsService.cpp index 082806fedca..3680632fa2d 100644 --- a/dbms/src/Flash/DiagnosticsService.cpp +++ b/dbms/src/Flash/DiagnosticsService.cpp @@ -1185,5 +1185,4 @@ ::grpc::Status DiagnosticsService::search_log( return ::grpc::Status::OK; } - } // namespace DB diff --git a/dbms/src/Storages/Transaction/ApplySnapshot.cpp b/dbms/src/Storages/Transaction/ApplySnapshot.cpp index a6093467bb0..a27feeced59 100644 --- a/dbms/src/Storages/Transaction/ApplySnapshot.cpp +++ b/dbms/src/Storages/Transaction/ApplySnapshot.cpp @@ -76,14 +76,26 @@ void KVStore::checkAndApplySnapshot(const RegionPtrWrap & new_region, TMTContext { const auto & new_range = new_region->getRange(); - handleRegionsByRangeOverlap(new_range->comparableKeys(), [&](RegionMap region_map, const KVStoreTaskLock &) { - for (const auto & region : region_map) + handleRegionsByRangeOverlap(new_range->comparableKeys(), [&](RegionMap region_map, const KVStoreTaskLock & task_lock) { + for (const auto & overlapped_region : region_map) { - if (region.first != region_id) + if (overlapped_region.first != region_id) { - throw Exception(std::string(__PRETTY_FUNCTION__) + ": range of region " + std::to_string(region_id) - + " is overlapped with region " + std::to_string(region.first) + ", should not happen", - ErrorCodes::LOGICAL_ERROR); + auto state = getProxyHelper()->getRegionLocalState(overlapped_region.first); + if (state.state() != raft_serverpb::PeerState::Tombstone) + { + throw Exception(fmt::format( + "range of region {} is overlapped with {}, state: {}", + region_id, + overlapped_region.first, + state.ShortDebugString()), + ErrorCodes::LOGICAL_ERROR); + } + else + { + LOG_FMT_INFO(log, "range of region {} is overlapped with `Tombstone` region {}", region_id, overlapped_region.first); + handleDestroy(overlapped_region.first, tmt, task_lock); + } } } }); diff --git a/dbms/src/Storages/Transaction/KVStore.cpp b/dbms/src/Storages/Transaction/KVStore.cpp index 4b9bdcd2ffe..dab260ed5d8 100644 --- a/dbms/src/Storages/Transaction/KVStore.cpp +++ b/dbms/src/Storages/Transaction/KVStore.cpp @@ -266,7 +266,11 @@ EngineStoreApplyRes KVStore::handleWriteRaftCmd(const WriteCmdsView & cmds, UInt void KVStore::handleDestroy(UInt64 region_id, TMTContext & tmt) { - auto task_lock = genTaskLock(); + handleDestroy(region_id, tmt, genTaskLock()); +} + +void KVStore::handleDestroy(UInt64 region_id, TMTContext & tmt, const KVStoreTaskLock & task_lock) +{ const auto region = getRegion(region_id); if (region == nullptr) { @@ -599,6 +603,7 @@ void WaitCheckRegionReady(const TMTContext & tmt, const std::atomic_size_t & ter if (!need_retry) { // if region is able to get latest commit-index from TiKV, we should make it available only after it has caught up. + assert(resp.read_index() != 0); regions_to_check.emplace(region_id, resp.read_index()); remain_regions.erase(region_id); } diff --git a/dbms/src/Storages/Transaction/KVStore.h b/dbms/src/Storages/Transaction/KVStore.h index 37f2245d87c..715891d6747 100644 --- a/dbms/src/Storages/Transaction/KVStore.h +++ b/dbms/src/Storages/Transaction/KVStore.h @@ -180,6 +180,7 @@ class KVStore final : private boost::noncopyable TMTContext & tmt); void persistRegion(const Region & region, const RegionTaskLock & region_task_lock, const char * caller); + void handleDestroy(UInt64 region_id, TMTContext & tmt, const KVStoreTaskLock &); private: RegionManager region_manager; diff --git a/dbms/src/Storages/Transaction/ProxyFFI.cpp b/dbms/src/Storages/Transaction/ProxyFFI.cpp index a96a6ef26e7..7b6f78c2144 100644 --- a/dbms/src/Storages/Transaction/ProxyFFI.cpp +++ b/dbms/src/Storages/Transaction/ProxyFFI.cpp @@ -6,7 +6,16 @@ #include #include #include -#include +#include + +#define CHECK_PARSE_PB_BUFF_IMPL(n, a, b, c) \ + do \ + { \ + [[maybe_unused]] bool parse_res_##n = (a).ParseFromArray(b, static_cast(c)); \ + assert(parse_res_##n); \ + } while (false) +#define CHECK_PARSE_PB_BUFF_FWD(n, ...) CHECK_PARSE_PB_BUFF_IMPL(n, __VA_ARGS__) +#define CHECK_PARSE_PB_BUFF(...) CHECK_PARSE_PB_BUFF_FWD(__LINE__, __VA_ARGS__) namespace CurrentMetrics { @@ -83,9 +92,8 @@ EngineStoreApplyRes HandleAdminRaftCmd( { raft_cmdpb::AdminRequest request; raft_cmdpb::AdminResponse response; - request.ParseFromArray(req_buff.data, static_cast(req_buff.len)); - response.ParseFromArray(resp_buff.data, static_cast(resp_buff.len)); - + CHECK_PARSE_PB_BUFF(request, req_buff.data, req_buff.len); + CHECK_PARSE_PB_BUFF(response, resp_buff.data, resp_buff.len); auto & kvstore = server->tmt->getKVStore(); return kvstore->handleAdminRaftCmd( std::move(request), @@ -227,6 +235,13 @@ kvrpcpb::ReadIndexResponse TiFlashRaftProxyHelper::readIndex(const kvrpcpb::Read return std::move(res.at(0).first); } +void InsertBatchReadIndexResp(RawVoidPtr resp, BaseBuffView view, uint64_t region_id) +{ + kvrpcpb::ReadIndexResponse res; + CHECK_PARSE_PB_BUFF(res, view.data, view.len); + reinterpret_cast(resp)->emplace_back(std::move(res), region_id); +} + BatchReadIndexRes TiFlashRaftProxyHelper::batchReadIndex(const std::vector & req, uint64_t timeout_ms) const { std::vector req_strs; @@ -239,7 +254,7 @@ BatchReadIndexRes TiFlashRaftProxyHelper::batchReadIndex(const std::vector(region_buff.len)); + CHECK_PARSE_PB_BUFF(region, region_buff.data, region_buff.len); auto & tmt = *server->tmt; auto & kvstore = tmt.getKVStore(); auto new_region = kvstore->genRegionPtr(std::move(region), peer_id, index, term); + +#ifndef NDEBUG + { + auto & kvstore = server->tmt->getKVStore(); + auto state = kvstore->getProxyHelper()->getRegionLocalState(new_region->id()); + assert(state.state() == raft_serverpb::PeerState::Applying); + } +#endif + switch (kvstore->applyMethod()) { case TiDB::SnapshotApplyMethod::Block: @@ -394,24 +418,11 @@ const char * IntoEncryptionMethodName(EncryptionMethod method) return encryption_method_name[static_cast(method)]; } -void InsertBatchReadIndexResp(RawVoidPtr resp, BaseBuffView view, uint64_t region_id) -{ - kvrpcpb::ReadIndexResponse res; - res.ParseFromArray(view.data, view.len); - reinterpret_cast(resp)->emplace_back(std::move(res), region_id); -} - RawCppPtr GenRawCppPtr(RawVoidPtr ptr_, RawCppPtrTypeImpl type_) { return RawCppPtr{ptr_, static_cast(type_)}; } -void SetServerInfoResp(BaseBuffView view, RawVoidPtr ptr) -{ - using diagnosticspb::ServerInfoResponse; - reinterpret_cast(ptr)->ParseFromArray(view.data, view.len); -} - CppStrWithView GetConfig(EngineStoreServerWrap * server, [[maybe_unused]] uint8_t full) { std::string config_file_path; @@ -441,11 +452,52 @@ CppStrWithView GetConfig(EngineStoreServerWrap * server, [[maybe_unused]] uint8_ void SetStore(EngineStoreServerWrap * server, BaseBuffView buff) { metapb::Store store{}; - store.ParseFromArray(buff.data, buff.len); + CHECK_PARSE_PB_BUFF(store, buff.data, buff.len); assert(server); assert(server->tmt); assert(store.id() != 0); server->tmt->getKVStore()->setStore(std::move(store)); } +void SetPBMsByBytes(MsgPBType type, RawVoidPtr ptr, BaseBuffView view) +{ + switch (type) + { + case MsgPBType::ReadIndexResponse: + CHECK_PARSE_PB_BUFF(*reinterpret_cast(ptr), view.data, view.len); + break; + case MsgPBType::RegionLocalState: + CHECK_PARSE_PB_BUFF(*reinterpret_cast(ptr), view.data, view.len); + break; + case MsgPBType::ServerInfoResponse: + CHECK_PARSE_PB_BUFF(*reinterpret_cast(ptr), view.data, view.len); + break; + } +} + +raft_serverpb::RegionLocalState TiFlashRaftProxyHelper::getRegionLocalState(uint64_t region_id) const +{ + assert(this->fn_get_region_local_state); + + raft_serverpb::RegionLocalState state; + RawCppStringPtr error_msg_ptr{}; + SCOPE_EXIT({ + delete error_msg_ptr; + }); + auto res = this->fn_get_region_local_state(this->proxy_ptr, region_id, &state, &error_msg_ptr); + switch (res) + { + case KVGetStatus::Ok: + break; + case KVGetStatus::Error: + { + throw Exception(fmt::format("{} meet internal error: {}", __FUNCTION__, *error_msg_ptr), ErrorCodes::LOGICAL_ERROR); + } + case KVGetStatus::NotFound: + // make not found as `Tombstone` + state.set_state(raft_serverpb::PeerState::Tombstone); + break; + } + return state; +} } // namespace DB diff --git a/dbms/src/Storages/Transaction/ProxyFFI.h b/dbms/src/Storages/Transaction/ProxyFFI.h index 0ef48b443cb..8891b61b595 100644 --- a/dbms/src/Storages/Transaction/ProxyFFI.h +++ b/dbms/src/Storages/Transaction/ProxyFFI.h @@ -14,6 +14,10 @@ namespace kvrpcpb class ReadIndexResponse; class ReadIndexRequest; } // namespace kvrpcpb +namespace raft_serverpb +{ +class RegionLocalState; +} namespace DB { @@ -53,6 +57,7 @@ struct TiFlashRaftProxyHelper : RaftStoreProxyFFIHelper FileEncryptionInfo linkFile(const std::string &, const std::string &) const; kvrpcpb::ReadIndexResponse readIndex(const kvrpcpb::ReadIndexRequest &) const; BatchReadIndexRes batchReadIndex(const std::vector &, uint64_t) const; + raft_serverpb::RegionLocalState getRegionLocalState(uint64_t region_id) const; }; extern "C" { @@ -81,11 +86,10 @@ void ApplyPreHandledSnapshot(EngineStoreServerWrap * server, void * res, RawCppP HttpRequestRes HandleHttpRequest(EngineStoreServerWrap *, BaseBuffView path, BaseBuffView query, BaseBuffView body); uint8_t CheckHttpUriAvailable(BaseBuffView); void GcRawCppPtr(void * ptr, RawCppPtrType type); -void InsertBatchReadIndexResp(RawVoidPtr, BaseBuffView, uint64_t); -void SetServerInfoResp(BaseBuffView, RawVoidPtr); BaseBuffView strIntoView(const std::string * str_ptr); CppStrWithView GetConfig(EngineStoreServerWrap *, uint8_t full); void SetStore(EngineStoreServerWrap *, BaseBuffView); +void SetPBMsByBytes(MsgPBType type, RawVoidPtr ptr, BaseBuffView view); } inline EngineStoreServerHelper GetEngineStoreServerHelper( @@ -109,10 +113,9 @@ inline EngineStoreServerHelper GetEngineStoreServerHelper( .fn_handle_http_request = HandleHttpRequest, .fn_check_http_uri_available = CheckHttpUriAvailable, .fn_gc_raw_cpp_ptr = GcRawCppPtr, - .fn_insert_batch_read_index_resp = InsertBatchReadIndexResp, - .fn_set_server_info_resp = SetServerInfoResp, .fn_get_config = GetConfig, .fn_set_store = SetStore, + .fn_set_pb_msg_by_bytes = SetPBMsByBytes, }; } } // namespace DB diff --git a/tests/fullstack-test/sample.test b/tests/fullstack-test/sample.test index bff1d2f326d..2e575888816 100644 --- a/tests/fullstack-test/sample.test +++ b/tests/fullstack-test/sample.test @@ -1,10 +1,11 @@ mysql> drop table if exists test.t mysql> create table if not exists test.t(s varchar(256), i int) -mysql> alter table test.t set tiflash replica 1 location labels 'rack', 'host', 'abc'; mysql> delete from test.t mysql> insert into test.t values('Hello world', 666) +mysql> alter table test.t set tiflash replica 1 location labels 'rack', 'host', 'abc'; + func> wait_table test t mysql> set session tidb_isolation_read_engines='tiflash'; select s, i from test.t @@ -14,6 +15,16 @@ mysql> set session tidb_isolation_read_engines='tiflash'; select s, i from test. | Hello world | 666 | +-------------+------+ +mysql> insert into test.t values('test', -1) + +mysql> set session tidb_isolation_read_engines='tiflash'; select s, i from test.t ++-------------+------+ +| s | i | ++-------------+------+ +| Hello world | 666 | +| test | -1 | ++-------------+------+ + mysql> delete from test.t mysql> drop table if exists test.t