Skip to content

Commit

Permalink
Fix metadata corruption release-5.4 (#4496)
Browse files Browse the repository at this point in the history
close #2576, close #3435, close #4437
  • Loading branch information
solotzg authored Apr 22, 2022
1 parent 95ccdb6 commit 839a659
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 34 deletions.
15 changes: 15 additions & 0 deletions dbms/src/Debug/dbgFuncRegion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion dbms/src/Flash/DiagnosticsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1185,5 +1185,4 @@ ::grpc::Status DiagnosticsService::search_log(

return ::grpc::Status::OK;
}

} // namespace DB
24 changes: 18 additions & 6 deletions dbms/src/Storages/Transaction/ApplySnapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
});
Expand Down
7 changes: 6 additions & 1 deletion dbms/src/Storages/Transaction/KVStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions dbms/src/Storages/Transaction/KVStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
92 changes: 72 additions & 20 deletions dbms/src/Storages/Transaction/ProxyFFI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
#include <Storages/Transaction/ProxyFFI.h>
#include <Storages/Transaction/Region.h>
#include <Storages/Transaction/TMTContext.h>
#include <diagnosticspb.pb.h>
#include <kvproto/diagnosticspb.pb.h>

#define CHECK_PARSE_PB_BUFF_IMPL(n, a, b, c) \
do \
{ \
[[maybe_unused]] bool parse_res_##n = (a).ParseFromArray(b, static_cast<int>(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
{
Expand Down Expand Up @@ -83,9 +92,8 @@ EngineStoreApplyRes HandleAdminRaftCmd(
{
raft_cmdpb::AdminRequest request;
raft_cmdpb::AdminResponse response;
request.ParseFromArray(req_buff.data, static_cast<int>(req_buff.len));
response.ParseFromArray(resp_buff.data, static_cast<int>(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),
Expand Down Expand Up @@ -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<BatchReadIndexRes *>(resp)->emplace_back(std::move(res), region_id);
}

BatchReadIndexRes TiFlashRaftProxyHelper::batchReadIndex(const std::vector<kvrpcpb::ReadIndexRequest> & req, uint64_t timeout_ms) const
{
std::vector<std::string> req_strs;
Expand All @@ -239,7 +254,7 @@ BatchReadIndexRes TiFlashRaftProxyHelper::batchReadIndex(const std::vector<kvrpc
auto outer_view = data.intoOuterView();
BatchReadIndexRes res;
res.reserve(req.size());
fn_handle_batch_read_index(proxy_ptr, outer_view, &res, timeout_ms);
fn_handle_batch_read_index(proxy_ptr, outer_view, &res, timeout_ms, InsertBatchReadIndexResp);
return res;
}

Expand Down Expand Up @@ -280,10 +295,19 @@ RawCppPtr PreHandleSnapshot(
try
{
metapb::Region region;
region.ParseFromArray(region_buff.data, static_cast<int>(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:
Expand Down Expand Up @@ -394,24 +418,11 @@ const char * IntoEncryptionMethodName(EncryptionMethod method)
return encryption_method_name[static_cast<uint8_t>(method)];
}

void InsertBatchReadIndexResp(RawVoidPtr resp, BaseBuffView view, uint64_t region_id)
{
kvrpcpb::ReadIndexResponse res;
res.ParseFromArray(view.data, view.len);
reinterpret_cast<BatchReadIndexRes *>(resp)->emplace_back(std::move(res), region_id);
}

RawCppPtr GenRawCppPtr(RawVoidPtr ptr_, RawCppPtrTypeImpl type_)
{
return RawCppPtr{ptr_, static_cast<RawCppPtrType>(type_)};
}

void SetServerInfoResp(BaseBuffView view, RawVoidPtr ptr)
{
using diagnosticspb::ServerInfoResponse;
reinterpret_cast<ServerInfoResponse *>(ptr)->ParseFromArray(view.data, view.len);
}

CppStrWithView GetConfig(EngineStoreServerWrap * server, [[maybe_unused]] uint8_t full)
{
std::string config_file_path;
Expand Down Expand Up @@ -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<kvrpcpb::ReadIndexResponse *>(ptr), view.data, view.len);
break;
case MsgPBType::RegionLocalState:
CHECK_PARSE_PB_BUFF(*reinterpret_cast<raft_serverpb::RegionLocalState *>(ptr), view.data, view.len);
break;
case MsgPBType::ServerInfoResponse:
CHECK_PARSE_PB_BUFF(*reinterpret_cast<diagnosticspb::ServerInfoResponse *>(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
11 changes: 7 additions & 4 deletions dbms/src/Storages/Transaction/ProxyFFI.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ namespace kvrpcpb
class ReadIndexResponse;
class ReadIndexRequest;
} // namespace kvrpcpb
namespace raft_serverpb
{
class RegionLocalState;
}

namespace DB
{
Expand Down Expand Up @@ -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<kvrpcpb::ReadIndexRequest> &, uint64_t) const;
raft_serverpb::RegionLocalState getRegionLocalState(uint64_t region_id) const;
};

extern "C" {
Expand Down Expand Up @@ -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(
Expand All @@ -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
13 changes: 12 additions & 1 deletion tests/fullstack-test/sample.test
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

0 comments on commit 839a659

Please sign in to comment.