Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
JaySon-Huang committed Nov 23, 2023
1 parent 0f1cb0e commit 1fa824a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 54 deletions.
13 changes: 0 additions & 13 deletions dbms/src/TiDB/Schema/DatabaseInfoCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,6 @@ class DatabaseInfoCache
return it->second;
}

template <typename NameMapper>
TiDB::DBInfoPtr getDBInfoByMappedName(const String & mapped_database_name) const
{
std::shared_lock lock(mtx_databases);

auto it = std::find_if(databases.begin(), databases.end(), [&](const auto & pair) {
return NameMapper().mapDatabaseName(*pair.second) == mapped_database_name;
});
if (it == databases.end())
return nullptr;
return it->second;
}

void addDatabaseInfo(const TiDB::DBInfoPtr & db_info)
{
std::unique_lock lock(mtx_databases);
Expand Down
35 changes: 27 additions & 8 deletions dbms/src/TiDB/Schema/SchemaNameMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ struct SchemaNameMapper
{
virtual ~SchemaNameMapper() = default;

static constexpr auto DATABASE_PREFIX = "db_";
static constexpr auto TABLE_PREFIX = "t_";
static constexpr std::string_view DATABASE_PREFIX = "db_";
static constexpr std::string_view TABLE_PREFIX = "t_";
static constexpr std::string_view KEYSPACE_PREFIX = "ks_";


static KeyspaceID getMappedNameKeyspaceID(const String & name)
{
auto keyspace_prefix_len = KEYSPACE_PREFIX.length();
static constexpr auto keyspace_prefix_len = KEYSPACE_PREFIX.length();
auto pos = name.find(KEYSPACE_PREFIX);
if (pos == String::npos)
return NullspaceID;
Expand All @@ -41,20 +41,39 @@ struct SchemaNameMapper
return std::stoull(name.substr(keyspace_prefix_len, pos - keyspace_prefix_len));
}

static std::optional<DatabaseID> tryGetDatabaseID(const String & name)
{
auto pos = name.find(DATABASE_PREFIX);
if (pos == String::npos || name.length() <= pos + DATABASE_PREFIX.length())
return std::nullopt;
try
{
return std::stoull(name.substr(pos + DATABASE_PREFIX.length()));
}
catch (std::invalid_argument & e)
{
return std::nullopt;
}
catch (std::out_of_range & e)
{
return std::nullopt;
}
}

static String map2Keyspace(KeyspaceID keyspace_id, const String & name)
{
return keyspace_id == NullspaceID ? name : KEYSPACE_PREFIX.data() + std::to_string(keyspace_id) + "_" + name;
return keyspace_id == NullspaceID ? name : fmt::format("{}{}_{}", KEYSPACE_PREFIX, keyspace_id, name);
}

virtual String mapDatabaseName(DatabaseID db_id, KeyspaceID keyspace_id) const
{
auto db_name = DATABASE_PREFIX + std::to_string(db_id);
auto db_name = fmt::format("{}{}", DATABASE_PREFIX, db_id);
return map2Keyspace(keyspace_id, db_name);
}

virtual String mapDatabaseName(const TiDB::DBInfo & db_info) const
{
auto db_name = DATABASE_PREFIX + std::to_string(db_info.id);
auto db_name = fmt::format("{}{}", DATABASE_PREFIX, db_info.id);
return map2Keyspace(db_info.keyspace_id, db_name);
}

Expand All @@ -64,7 +83,7 @@ struct SchemaNameMapper
}
virtual String mapTableName(const TiDB::TableInfo & table_info) const
{
auto table_name = TABLE_PREFIX + std::to_string(table_info.id);
auto table_name = fmt::format("{}{}", TABLE_PREFIX, table_info.id);
return map2Keyspace(table_info.keyspace_id, table_name);
}
virtual String displayTableName(const TiDB::TableInfo & table_info) const
Expand All @@ -90,7 +109,7 @@ struct SchemaNameMapper
virtual String debugCanonicalName(const TiDB::TableInfo & table_info, DatabaseID db_id, KeyspaceID keyspace_id)
const
{
auto db_name = DATABASE_PREFIX + std::to_string(db_id);
auto db_name = fmt::format("{}{}", DATABASE_PREFIX, db_id);
return map2Keyspace(keyspace_id, db_name) + "." + debugTableName(table_info);
}
};
Expand Down
29 changes: 15 additions & 14 deletions dbms/src/TiDB/Schema/SchemaSyncService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ bool SchemaSyncService::gc(Timestamp gc_safepoint, KeyspaceID keyspace_id)
}
}

auto schema_sync_manager = tmt_context.getSchemaSyncerManager();

// Physically drop tables
bool succeeded = true;
for (auto & storage_ptr : storages_to_gc)
Expand All @@ -268,21 +270,18 @@ bool SchemaSyncService::gc(Timestamp gc_safepoint, KeyspaceID keyspace_id)
String table_name = storage->getTableName();
const auto & table_info = storage->getTableInfo();

tmt_context.getSchemaSyncerManager()->removeTableID(keyspace_id, table_info.id);

auto canonical_name = [&]() {
// DB info maintenance is parallel with GC logic so we can't always assume one specific DB info's existence, thus checking its validity.
auto db_info = tmt_context.getSchemaSyncerManager()->getDBInfoByMappedName(keyspace_id, database_name);
return db_info ? fmt::format(
"{}, database_id={} table_id={}",
SchemaNameMapper().debugCanonicalName(*db_info, table_info),
db_info->id,
table_info.id)
: fmt::format(
"({}).{}, table_id={}",
database_name,
SchemaNameMapper().debugTableName(table_info),
table_info.id);
auto database_id = SchemaNameMapper::tryGetDatabaseID(database_name);
if (!database_id.has_value())
{
return fmt::format("{}.{} table_id={}", database_name, table_name, table_info.id);
}
return fmt::format(
"{}.{} database_id={} table_id={}",
database_name,
table_name,
*database_id,
table_info.id);
}();
LOG_INFO(
keyspace_log,
Expand All @@ -301,6 +300,8 @@ bool SchemaSyncService::gc(Timestamp gc_safepoint, KeyspaceID keyspace_id)
InterpreterDropQuery drop_interpreter(ast_drop_query, context);
drop_interpreter.execute();
LOG_INFO(keyspace_log, "Physically dropped table {}", canonical_name);
// remove the id mapping after physically dropped
schema_sync_manager->removeTableID(keyspace_id, table_info.id);
++num_tables_removed;
}
catch (DB::Exception & e)
Expand Down
2 changes: 0 additions & 2 deletions dbms/src/TiDB/Schema/SchemaSyncer.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class SchemaSyncer

virtual TiDB::DBInfoPtr getDBInfoByName(const String & database_name) = 0;

virtual TiDB::DBInfoPtr getDBInfoByMappedName(const String & mapped_database_name) = 0;

virtual void removeTableID(TableID table_id) = 0;

virtual void dropAllSchema(Context & context) = 0;
Expand Down
12 changes: 0 additions & 12 deletions dbms/src/TiDB/Schema/TiDBSchemaManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,6 @@ class TiDBSchemaSyncerManager
return schema_syncer->getDBInfoByName(database_name);
}

TiDB::DBInfoPtr getDBInfoByMappedName(KeyspaceID keyspace_id, const String & mapped_database_name)
{
std::shared_lock<std::shared_mutex> read_lock(schema_syncers_mutex);
auto schema_syncer = getSchemaSyncer(keyspace_id);
if (schema_syncer == nullptr)
{
LOG_ERROR(log, "SchemaSyncer not found, keyspace={}", keyspace_id);
return nullptr;
}
return schema_syncer->getDBInfoByMappedName(mapped_database_name);
}

bool removeSchemaSyncer(KeyspaceID keyspace_id)
{
std::unique_lock<std::shared_mutex> lock(schema_syncers_mutex);
Expand Down
5 changes: 0 additions & 5 deletions dbms/src/TiDB/Schema/TiDBSchemaSyncer.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,6 @@ class TiDBSchemaSyncer : public SchemaSyncer
return databases.getDBInfoByName(database_name);
}

TiDB::DBInfoPtr getDBInfoByMappedName(const String & mapped_database_name) override
{
return databases.getDBInfoByMappedName<NameMapper>(mapped_database_name);
}

// clear all states.
// just for testing restart
void reset() override
Expand Down

0 comments on commit 1fa824a

Please sign in to comment.