-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KVStore: Improve codes for read index worker (#8873)
ref #8864
- Loading branch information
Showing
15 changed files
with
1,283 additions
and
933 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <Storages/KVStore/Read/ReadIndexWorkerImpl.h> | ||
|
||
namespace DB | ||
{ | ||
AsyncNotifier::Status AsyncWaker::Notifier::blockedWaitUtil(const SteadyClock::time_point & time_point) | ||
{ | ||
// if flag from false to false, wait for notification. | ||
// if flag from true to false, do nothing. | ||
auto res = AsyncNotifier::Status::Normal; | ||
if (!is_awake->exchange(false, std::memory_order_acq_rel)) | ||
{ | ||
{ | ||
auto lock = genUniqueLock(); | ||
if (!is_awake->load(std::memory_order_acquire)) | ||
{ | ||
if (condVar().wait_until(lock, time_point) == std::cv_status::timeout) | ||
res = AsyncNotifier::Status::Timeout; | ||
} | ||
} | ||
is_awake->store(false, std::memory_order_release); | ||
} | ||
return res; | ||
} | ||
|
||
void AsyncWaker::Notifier::wake() NO_THREAD_SAFETY_ANALYSIS | ||
{ | ||
// if flag from false -> true, then wake up. | ||
// if flag from true -> true, do nothing. | ||
if (is_awake->load(std::memory_order_acquire)) | ||
return; | ||
if (!is_awake->exchange(true, std::memory_order_acq_rel)) | ||
{ | ||
// wake up notifier | ||
auto _ = genLockGuard(); | ||
condVar().notify_one(); | ||
} | ||
} | ||
|
||
void AsyncWaker::wake(RawVoidPtr notifier_) | ||
{ | ||
auto & notifier = *reinterpret_cast<AsyncNotifier *>(notifier_); | ||
notifier.wake(); | ||
} | ||
|
||
AsyncWaker::AsyncWaker(const TiFlashRaftProxyHelper & helper_) | ||
: AsyncWaker(helper_, new AsyncWaker::Notifier{}) | ||
{} | ||
|
||
AsyncWaker::AsyncWaker(const TiFlashRaftProxyHelper & helper_, AsyncNotifier * notifier_) | ||
: inner(helper_.makeAsyncWaker(AsyncWaker::wake, GenRawCppPtr(notifier_, RawCppPtrTypeImpl::WakerNotifier))) | ||
, notifier(*notifier_) | ||
{} | ||
|
||
AsyncNotifier::Status AsyncWaker::waitUtil(SteadyClock::time_point time_point) | ||
{ | ||
return notifier.blockedWaitUtil(time_point); | ||
} | ||
|
||
RawVoidPtr AsyncWaker::getRaw() const | ||
{ | ||
return inner.ptr; | ||
} | ||
} // namespace DB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <Storages/KVStore/Read/ReadIndexWorkerImpl.h> | ||
|
||
namespace DB | ||
{ | ||
BatchReadIndexRes TiFlashRaftProxyHelper::batchReadIndex( | ||
const std::vector<kvrpcpb::ReadIndexRequest> & req, | ||
uint64_t timeout_ms) const | ||
{ | ||
return batchReadIndex_v2(req, timeout_ms); | ||
} | ||
|
||
BatchReadIndexRes TiFlashRaftProxyHelper::batchReadIndex_v2( | ||
const std::vector<kvrpcpb::ReadIndexRequest> & req, | ||
uint64_t timeout_ms) const | ||
{ | ||
AsyncWaker waker(*this); | ||
BlockedReadIndexHelper helper{timeout_ms, waker}; | ||
|
||
std::queue<std::pair<RegionID, ReadIndexTask>> tasks; | ||
BatchReadIndexRes resps; | ||
resps.reserve(req.size()); | ||
|
||
for (const auto & r : req) | ||
{ | ||
if (auto task = makeReadIndexTask(r); !task) | ||
{ | ||
// The read index request is not sent successfully. | ||
GET_METRIC(tiflash_raft_learner_read_failures_count, type_request_error).Increment(); | ||
kvrpcpb::ReadIndexResponse res; | ||
res.mutable_region_error(); | ||
resps.emplace_back(std::move(res), r.context().region_id()); | ||
} | ||
else | ||
{ | ||
tasks.emplace(r.context().region_id(), std::move(*task)); | ||
} | ||
} | ||
|
||
{ | ||
// Block wait for all tasks are ready or timeout. | ||
kvrpcpb::ReadIndexResponse tmp; | ||
while (!tasks.empty()) | ||
{ | ||
auto & it = tasks.front(); | ||
if (pollReadIndexTask(it.second, tmp, helper.getWaker().getRaw())) | ||
{ | ||
resps.emplace_back(std::move(tmp), it.first); | ||
tmp.Clear(); | ||
tasks.pop(); | ||
} | ||
else | ||
{ | ||
if (helper.blockedWait() == AsyncNotifier::Status::Timeout) | ||
break; | ||
} | ||
} | ||
} | ||
{ | ||
// If meets timeout, which means some of the regions can not get response from leader, try to poll rest tasks | ||
while (!tasks.empty()) | ||
{ | ||
auto & it = tasks.front(); | ||
kvrpcpb::ReadIndexResponse tmp; | ||
if (pollReadIndexTask(it.second, tmp)) | ||
{ | ||
resps.emplace_back(std::move(tmp), it.first); | ||
} | ||
else | ||
{ | ||
tmp.mutable_region_error()->mutable_region_not_found(); | ||
resps.emplace_back(std::move(tmp), it.first); | ||
} | ||
tasks.pop(); | ||
} | ||
} | ||
|
||
return resps; | ||
} | ||
|
||
RawRustPtr TiFlashRaftProxyHelper::makeAsyncWaker(void (*wake_fn)(RawVoidPtr), RawCppPtr data) const | ||
{ | ||
return fn_make_async_waker(wake_fn, data); | ||
} | ||
|
||
std::optional<ReadIndexTask> TiFlashRaftProxyHelper::makeReadIndexTask(const kvrpcpb::ReadIndexRequest & req) const | ||
{ | ||
thread_local std::string buff_cache; | ||
req.SerializeToString(&buff_cache); | ||
auto req_view = strIntoView(&buff_cache); | ||
if (RawRustPtr ptr = fn_make_read_index_task(proxy_ptr, req_view); ptr.ptr) | ||
{ | ||
return ReadIndexTask{ptr}; | ||
} | ||
else | ||
{ | ||
return {}; | ||
} | ||
} | ||
|
||
bool TiFlashRaftProxyHelper::pollReadIndexTask( | ||
ReadIndexTask & task, | ||
kvrpcpb::ReadIndexResponse & resp, | ||
RawVoidPtr waker) const | ||
{ | ||
return fn_poll_read_index_task(proxy_ptr, task.ptr, &resp, waker); | ||
} | ||
|
||
TimerTask TiFlashRaftProxyHelper::makeTimerTask(uint64_t time_ms) const | ||
{ | ||
return TimerTask{fn_make_timer_task(time_ms)}; | ||
} | ||
|
||
bool TiFlashRaftProxyHelper::pollTimerTask(TimerTask & task, RawVoidPtr waker) const | ||
{ | ||
return fn_poll_timer_task(task.ptr, waker); | ||
} | ||
} // namespace DB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.