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

[WIP]: Region safe ts #10007

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions dbms/src/Storages/KVStore/Decode/SafeTsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ struct SafeTsManager
static const SafeTS SafeTsDiffThreshold = 2 * 60 * 1000;
bool isSafeTSLag(RegionID region_id, SafeTS * leader_safe_ts, SafeTS * self_safe_ts);

struct SafeTsPair
{
const SafeTS leader_safe_ts;
const SafeTS self_safe_ts;
};
SafeTsPair get(RegionID region_id) const
{
std::shared_lock read_lock(rw_lock);
if (auto iter = safe_ts_map.find(region_id); iter != safe_ts_map.end())
{
return SafeTsPair{
.leader_safe_ts = (*iter->second).leader_safe_ts.load(),
.self_safe_ts = (*iter->second).self_safe_ts.load(),
};
}
return SafeTsPair{0, 0};
}

UInt64 getSelfSafeTS(RegionID region_id) const;

void remove(RegionID region_id)
Expand Down
10 changes: 0 additions & 10 deletions dbms/src/Storages/KVStore/Region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,6 @@ Region::~Region()
GET_METRIC(tiflash_raft_classes_count, type_region).Decrement();
}

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

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

void Region::setPeerState(raft_serverpb::PeerState state)
{
meta.setPeerState(state);
Expand Down
5 changes: 3 additions & 2 deletions dbms/src/Storages/KVStore/Region.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,9 @@ class Region : public std::enable_shared_from_this<Region>
RegionVersion version() const;
RegionVersion confVer() const;

TableID getMappedTableID() const;
KeyspaceID getKeyspaceID() const;
TableID getMappedTableID() const { return mapped_table_id; }
KeyspaceID getKeyspaceID() const { return keyspace_id; }
KeyspaceTableID getKeyspaceTableID() const { return KeyspaceTableID{keyspace_id, mapped_table_id}; }

/// get approx rows, bytes info about mem cache.
std::pair<size_t, size_t> getApproxMemCacheInfo() const;
Expand Down
104 changes: 104 additions & 0 deletions dbms/src/Storages/System/StorageSystemRegionInfos.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// 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 <Columns/ColumnString.h>
#include <DataStreams/OneBlockInputStream.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypesNumber.h>
#include <Databases/DatabaseTiFlash.h>
#include <Databases/IDatabase.h>
#include <Interpreters/Context.h>
#include <Storages/DeltaMerge/DeltaMergeStore.h>
#include <Storages/KVStore/KVStore.h>
#include <Storages/KVStore/TMTContext.h>
#include <Storages/KVStore/Types.h>
#include <Storages/MutableSupport.h>
#include <Storages/StorageDeltaMerge.h>
#include <Storages/System/StorageSystemRegionInfos.h>
#include <TiDB/Schema/SchemaNameMapper.h>

namespace DB
{

StorageSystemRegionInfos::StorageSystemRegionInfos(const std::string & name_)
: name(name_)
{
setColumns(ColumnsDescription({
{"region_id", std::make_shared<DataTypeUInt64>()},
{"keyspace_id", std::make_shared<DataTypeNullable>(std::make_shared<DataTypeUInt64>())},
{"table_id", std::make_shared<DataTypeInt64>()},
{"leader_safe_ts", std::make_shared<DataTypeUInt64>()},
{"self_safe_ts", std::make_shared<DataTypeUInt64>()},
}));
}

struct RegionInfoSummary
{
RegionID region_id;
KeyspaceTableID ks_table_id;
SafeTS self_safe_ts;
SafeTS leader_safe_ts;
};

BlockInputStreams StorageSystemRegionInfos::read(
const Names & column_names,
const SelectQueryInfo &,
const Context & context,
QueryProcessingStage::Enum & processed_stage,
const size_t /*max_block_size*/,
const unsigned /*num_streams*/)
{
check(column_names);
processed_stage = QueryProcessingStage::FetchColumns;

auto & tmt = context.getTMTContext();
auto kvstore = tmt.getKVStore();
std::map<RegionID, RegionInfoSummary> regions;
kvstore->traverseRegions([&regions](RegionID region_id, const RegionPtr & region) {
regions.emplace(
region_id,
RegionInfoSummary{
.region_id = region_id,
.ks_table_id = region->getKeyspaceTableID(),
});
});

auto & region_tbl = tmt.getRegionTable();
auto & safe_ts_mgr = region_tbl.safeTsMgr();
for (auto & [region_id, region_summary] : regions)
{
auto safe_ts_pair = safe_ts_mgr.get(region_id);
region_summary.leader_safe_ts = safe_ts_pair.leader_safe_ts;
region_summary.self_safe_ts = safe_ts_pair.self_safe_ts;
}

MutableColumns res_columns = getSampleBlock().cloneEmptyColumns();
for (const auto & [region_id, region_summary] : regions)
{
size_t j = 0;
res_columns[j++]->insert(region_id);
res_columns[j++]->insert(static_cast<UInt64>(region_summary.ks_table_id.first));
res_columns[j++]->insert(region_summary.ks_table_id.second);
res_columns[j++]->insert(region_summary.leader_safe_ts);
res_columns[j++]->insert(region_summary.self_safe_ts);
}

return BlockInputStreams(
1,
std::make_shared<OneBlockInputStream>(getSampleBlock().cloneWithColumns(std::move(res_columns))));
}


} // namespace DB
50 changes: 50 additions & 0 deletions dbms/src/Storages/System/StorageSystemRegionInfos.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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.

#pragma once

#include <Storages/IStorage.h>

#include <ext/shared_ptr_helper.h>


namespace DB
{
class Context;


class StorageSystemRegionInfos
: public ext::SharedPtrHelper<StorageSystemRegionInfos>
, public IStorage
{
public:
std::string getName() const override { return "SystemRegionInfos"; }
std::string getTableName() const override { return name; }

BlockInputStreams read(
const Names & column_names,
const SelectQueryInfo & query_info,
const Context & context,
QueryProcessingStage::Enum & processed_stage,
size_t max_block_size,
unsigned num_streams) override;

private:
const std::string name;

protected:
explicit StorageSystemRegionInfos(const std::string & name_);
};

} // namespace DB
2 changes: 2 additions & 0 deletions dbms/src/Storages/System/attachSystemTables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <Storages/System/StorageSystemNumbers.h>
#include <Storages/System/StorageSystemOne.h>
#include <Storages/System/StorageSystemProcesses.h>
#include <Storages/System/StorageSystemRegionInfos.h>
#include <Storages/System/StorageSystemSettings.h>
#include <Storages/System/StorageSystemTables.h>
#include <Storages/System/attachSystemTables.h>
Expand All @@ -42,6 +43,7 @@ void attachSystemTablesLocal(IDatabase & system_database)
system_database.attachTable("dt_tables", StorageSystemDTTables::create("dt_tables"));
system_database.attachTable("dt_segments", StorageSystemDTSegments::create("dt_segments"));
system_database.attachTable("dt_local_indexes", StorageSystemDTLocalIndexes::create("dt_local_indexes"));
system_database.attachTable("tiflash_regions", StorageSystemRegionInfos::create("tiflash_regions"));
system_database.attachTable("tables", StorageSystemTables::create("tables"));
system_database.attachTable("columns", StorageSystemColumns::create("columns"));
system_database.attachTable("functions", StorageSystemFunctions::create("functions"));
Expand Down