Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KVStore: Fix unreleased Region instance #9763

Merged
merged 7 commits into from
Jan 7, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
make region key ranges independant
Signed-off-by: Calvin Neo <calvinneo1995@gmail.com>
CalvinNeo committed Jan 7, 2025
commit ed63a99ca552e4e95309f65a47303f93f9d2c2e2
71 changes: 71 additions & 0 deletions dbms/src/Storages/KVStore/MultiRaft/RegionRangeKeys.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2025 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/MultiRaft/RegionRangeKeys.h>
#include <Storages/KVStore/TiKVHelpers/TiKVRecordFormat.h>

namespace DB
{
RegionRangeKeys::RegionRangeKeys(TiKVKey && start_key, TiKVKey && end_key)
: ori(RegionRangeKeys::makeComparableKeys(std::move(start_key), std::move(end_key)))
, raw(std::make_shared<DecodedTiKVKey>(
ori.first.key.empty() ? DecodedTiKVKey() : RecordKVFormat::decodeTiKVKey(ori.first.key)),
std::make_shared<DecodedTiKVKey>(
ori.second.key.empty() ? DecodedTiKVKey() : RecordKVFormat::decodeTiKVKey(ori.second.key)))
{
keyspace_id = raw.first->getKeyspaceID();
if (!computeMappedTableID(*raw.first, mapped_table_id) || ori.first.compare(ori.second) >= 0)
{
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Illegal region range, should not happen, start_key={} end_key={}",
ori.first.key.toDebugString(),
ori.second.key.toDebugString());
}
}

TableID RegionRangeKeys::getMappedTableID() const
{
return mapped_table_id;
}

KeyspaceID RegionRangeKeys::getKeyspaceID() const
{
return keyspace_id;
}

const std::pair<DecodedTiKVKeyPtr, DecodedTiKVKeyPtr> & RegionRangeKeys::rawKeys() const
{
return raw;
}

const RegionRangeKeys::RegionRange & RegionRangeKeys::comparableKeys() const
{
return ori;
}

RegionRangeKeys::RegionRange RegionRangeKeys::makeComparableKeys(TiKVKey && start_key, TiKVKey && end_key)
{
return std::make_pair(
TiKVRangeKey::makeTiKVRangeKey<true>(std::move(start_key)),
TiKVRangeKey::makeTiKVRangeKey<false>(std::move(end_key)));
}

RegionRangeKeys::RegionRange RegionRangeKeys::cloneRange(const RegionRange & from)
{
return std::make_pair(from.first.copy(), from.second.copy());
}


} // namespace DB
2 changes: 2 additions & 0 deletions dbms/src/Storages/KVStore/MultiRaft/RegionRangeKeys.h
Original file line number Diff line number Diff line change
@@ -88,4 +88,6 @@ class RegionRangeKeys : boost::noncopyable
KeyspaceID keyspace_id = NullspaceID;
};

bool computeMappedTableID(const DecodedTiKVKey & key, TableID & table_id);

} // namespace DB
49 changes: 0 additions & 49 deletions dbms/src/Storages/KVStore/MultiRaft/RegionState.cpp
Original file line number Diff line number Diff line change
@@ -136,38 +136,6 @@ bool computeMappedTableID(const DecodedTiKVKey & key, TableID & table_id)
return false;
}

RegionRangeKeys::RegionRangeKeys(TiKVKey && start_key, TiKVKey && end_key)
: ori(RegionRangeKeys::makeComparableKeys(std::move(start_key), std::move(end_key)))
, raw(std::make_shared<DecodedTiKVKey>(
ori.first.key.empty() ? DecodedTiKVKey() : RecordKVFormat::decodeTiKVKey(ori.first.key)),
std::make_shared<DecodedTiKVKey>(
ori.second.key.empty() ? DecodedTiKVKey() : RecordKVFormat::decodeTiKVKey(ori.second.key)))
{
keyspace_id = raw.first->getKeyspaceID();
if (!computeMappedTableID(*raw.first, mapped_table_id) || ori.first.compare(ori.second) >= 0)
{
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Illegal region range, should not happen, start_key={} end_key={}",
ori.first.key.toDebugString(),
ori.second.key.toDebugString());
}
}

TableID RegionRangeKeys::getMappedTableID() const
{
return mapped_table_id;
}

KeyspaceID RegionRangeKeys::getKeyspaceID() const
{
return keyspace_id;
}

const std::pair<DecodedTiKVKeyPtr, DecodedTiKVKeyPtr> & RegionRangeKeys::rawKeys() const
{
return raw;
}

HandleRange<HandleID> getHandleRangeByTable(
const std::pair<DecodedTiKVKeyPtr, DecodedTiKVKeyPtr> & rawKeys,
@@ -176,11 +144,6 @@ HandleRange<HandleID> getHandleRangeByTable(
return TiKVRange::getHandleRangeByTable(*rawKeys.first, *rawKeys.second, table_id);
}

const RegionRangeKeys::RegionRange & RegionRangeKeys::comparableKeys() const
{
return ori;
}

template <bool is_start>
TiKVRangeKey TiKVRangeKey::makeTiKVRangeKey(TiKVKey && key)
{
@@ -191,18 +154,6 @@ TiKVRangeKey TiKVRangeKey::makeTiKVRangeKey(TiKVKey && key)
template TiKVRangeKey TiKVRangeKey::makeTiKVRangeKey<true>(TiKVKey &&);
template TiKVRangeKey TiKVRangeKey::makeTiKVRangeKey<false>(TiKVKey &&);

RegionRangeKeys::RegionRange RegionRangeKeys::makeComparableKeys(TiKVKey && start_key, TiKVKey && end_key)
{
return std::make_pair(
TiKVRangeKey::makeTiKVRangeKey<true>(std::move(start_key)),
TiKVRangeKey::makeTiKVRangeKey<false>(std::move(end_key)));
}

RegionRangeKeys::RegionRange RegionRangeKeys::cloneRange(const RegionRange & from)
{
return std::make_pair(from.first.copy(), from.second.copy());
}

std::string TiKVRangeKey::toDebugString() const
{
if (this->state == MAX)
1 change: 0 additions & 1 deletion dbms/src/Storages/KVStore/MultiRaft/RegionState.h
Original file line number Diff line number Diff line change
@@ -66,6 +66,5 @@ class RegionState : private boost::noncopyable
HandleRange<HandleID> getHandleRangeByTable(
const std::pair<DecodedTiKVKeyPtr, DecodedTiKVKeyPtr> & rawKeys,
TableID table_id);
bool computeMappedTableID(const DecodedTiKVKey & key, TableID & table_id);

} // namespace DB
2 changes: 1 addition & 1 deletion dbms/src/Storages/KVStore/tests/gtest_kvstore.cpp
Original file line number Diff line number Diff line change
@@ -356,7 +356,7 @@ static void testRaftSplit(KVStore & kvs, TMTContext & tmt, std::unique_ptr<MockR
RegionID region_id2 = 7;
auto source_region = kvs.getRegion(region_id);
auto old_epoch = source_region->getMeta().getMetaRegion().region_epoch();
auto ori_source_range = source_region->getRange()->comparableKeys();
auto ori_source_range = RegionRangeKeys::cloneRange(source_region->getRange()->comparableKeys());
RegionRangeKeys::RegionRange new_source_range = RegionRangeKeys::makeComparableKeys( //
RecordKVFormat::genKey(table_id, 5),
RecordKVFormat::genKey(table_id, 10));