Skip to content

Commit

Permalink
Remove a lot useless code (#118)
Browse files Browse the repository at this point in the history
* Add sync schema on read

* Simplify schema syncer interface and adjust mock stuff

* Rename default schema version setting

* Compensate last commit

* Remove curl library

* Remove curl from builder image

* Remove useless codes, init schema syncer based on pd config

* Minor fix to schema debug

* Fix alter tmt and pass tests

* Fix build fail

* Add lock for mock schema syncer

* Fix schema sync service init context

* Adjust schema tests

* Not sync if no schema change detected

* Adjust txn mock tests

* Fix default value bug

* Rename some tests

* Remove sync schema test

* Remove a lot useless code
  • Loading branch information
zanmato1984 authored and hanfei1991 committed Jul 20, 2019
1 parent 4dad596 commit 1a6a1c4
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 226 deletions.
2 changes: 1 addition & 1 deletion dbms/src/Debug/MockSchemaSyncer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ void MockSchemaSyncer::syncTable(Context & context, MockTiDB::TablePtr table)
for (auto part_def : table_info.partition.definitions)
{
auto part_table_info = table_info.producePartitionTableInfo(part_def.id);
createTable(*part_table_info, context);
createTable(part_table_info, context);
}
}
};
Expand Down
35 changes: 18 additions & 17 deletions dbms/src/Storages/Transaction/SchemaBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include <Interpreters/InterpreterDropQuery.h>
#include <Parsers/ASTCreateQuery.h>
#include <Parsers/ASTDropQuery.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ParserCreateQuery.h>
#include <Parsers/ParserDropQuery.h>
#include <Parsers/parseQuery.h>
#include <Parsers/ASTLiteral.h>
#include <Storages/MutableSupport.h>
#include <Storages/Transaction/SchemaBuilder.h>
#include <Storages/Transaction/TMTContext.h>
Expand Down Expand Up @@ -37,7 +37,8 @@ inline AlterCommands detectSchemaChanges(Logger * log, const TiDB::TableInfo & t
command.type = AlterCommand::ADD_COLUMN;
command.column_name = column_info.name;
command.data_type = getDataTypeByColumnInfo(column_info);
if (!column_info.origin_default_value.isEmpty()) {
if (!column_info.origin_default_value.isEmpty())
{
LOG_DEBUG(log, "add default value for column: " + column_info.name);
command.default_expression = ASTPtr(new ASTLiteral(column_info.defaultValueToField()));
}
Expand Down Expand Up @@ -92,14 +93,13 @@ void SchemaBuilder::applyAlterTableImpl(TiDB::TableInfoPtr table_info, const Str
for (auto part_def : table_info->partition.definitions)
{
auto new_table_info = table_info->producePartitionTableInfo(part_def.id);
storage->alterForTMT(commands, *new_table_info, db_name, context);
storage->alterForTMT(commands, new_table_info, db_name, context);
}
}
}

void SchemaBuilder::applyAlterTable(TiDB::DBInfoPtr dbInfo, Int64 table_id)
{

auto table_info = getter.getTableInfo(dbInfo->id, table_id);
auto & tmt_context = context.getTMTContext();
auto storage = static_cast<StorageMergeTree *>(tmt_context.getStorages().get(table_id).get());
Expand Down Expand Up @@ -279,16 +279,16 @@ String createTableStmt(const DBInfo & db_info, const TableInfo & table_info)
return stmt;
}

void SchemaBuilder::applyCreatePhysicalTableImpl(TiDB::DBInfoPtr db_info, TiDB::TableInfoPtr table_info)
void SchemaBuilder::applyCreatePhysicalTableImpl(const TiDB::DBInfo & db_info, const TiDB::TableInfo & table_info)
{
String stmt = createTableStmt(*db_info, *table_info);
String stmt = createTableStmt(db_info, table_info);

ParserCreateQuery parser;
ASTPtr ast = parseQuery(parser, stmt.data(), stmt.data() + stmt.size(), "from syncSchema " + table_info->name, 0);
ASTPtr ast = parseQuery(parser, stmt.data(), stmt.data() + stmt.size(), "from syncSchema " + table_info.name, 0);

ASTCreateQuery * ast_create_query = typeid_cast<ASTCreateQuery *>(ast.get());
ast_create_query->attach = true;
ast_create_query->database = db_info->name;
ast_create_query->database = db_info.name;

InterpreterCreateQuery interpreter(ast, context);
interpreter.setInternal(true);
Expand All @@ -305,17 +305,17 @@ void SchemaBuilder::applyCreateTable(TiDB::DBInfoPtr db_info, Int64 table_id)
// this table is dropped.
return;
}
applyCreateTableImpl(db_info, table_info);
applyCreateTableImpl(*db_info, *table_info);
}

void SchemaBuilder::applyCreateTableImpl(TiDB::DBInfoPtr db_info, TiDB::TableInfoPtr table_info)
void SchemaBuilder::applyCreateTableImpl(const TiDB::DBInfo & db_info, const TiDB::TableInfo & table_info)
{
if (table_info->is_partition_table)
if (table_info.is_partition_table)
{
// create partition table.
for (auto part_def : table_info->partition.definitions)
for (auto part_def : table_info.partition.definitions)
{
auto new_table_info = table_info->producePartitionTableInfo(part_def.id);
auto new_table_info = table_info.producePartitionTableInfo(part_def.id);
applyCreatePhysicalTableImpl(db_info, new_table_info);
}
}
Expand Down Expand Up @@ -347,7 +347,7 @@ void SchemaBuilder::applyDropTable(TiDB::DBInfoPtr dbInfo, Int64 table_id)
for (auto part_def : table_info.partition.definitions)
{
auto new_table_info = table_info.producePartitionTableInfo(part_def.id);
applyDropTableImpl(database_name, new_table_info->name);
applyDropTableImpl(database_name, new_table_info.name);
}
}
// and drop logic table.
Expand All @@ -370,9 +370,11 @@ void SchemaBuilder::updateDB(TiDB::DBInfoPtr db_info)
table_ids.insert(table->id);

auto storage_map = tmt_context.getStorages().getAllStorage();
for (auto it = storage_map.begin(); it != storage_map.end(); it++) {
for (auto it = storage_map.begin(); it != storage_map.end(); it++)
{
auto storage = it->second;
if(storage->getDatabaseName() == db_info->name && table_ids.count(storage->getTableInfo().id) == 0) {
if (storage->getDatabaseName() == db_info->name && table_ids.count(storage->getTableInfo().id) == 0)
{
// Drop Table
applyDropTableImpl(db_info->name, storage->getTableName());
LOG_DEBUG(log, "Table " + db_info->name + "." + storage->getTableName() + " is dropped during schema all schemas");
Expand All @@ -391,7 +393,6 @@ void SchemaBuilder::updateDB(TiDB::DBInfoPtr db_info)
applyAlterTableImpl(table, db_info->name, storage);
}
}

}

// end namespace
Expand Down
6 changes: 2 additions & 4 deletions dbms/src/Storages/Transaction/SchemaBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ struct SchemaBuilder

void applyCreateSchemaImpl(TiDB::DBInfoPtr db_info);


void applyCreateTable(TiDB::DBInfoPtr dbInfo, Int64 table_id);

void applyDropTable(TiDB::DBInfoPtr dbInfo, Int64 table_id);
Expand All @@ -46,12 +45,11 @@ struct SchemaBuilder

//void applyDropPartition(TiDB::DBInfoPtr dbInfo, Int64 table_id);

void applyCreatePhysicalTableImpl(TiDB::DBInfoPtr db_info, TiDB::TableInfoPtr table_info);
void applyCreatePhysicalTableImpl(const TiDB::DBInfo & db_info, const TiDB::TableInfo & table_info);

void applyCreateTableImpl(TiDB::DBInfoPtr db_info, TiDB::TableInfoPtr table_info);
void applyCreateTableImpl(const TiDB::DBInfo & db_info, const TiDB::TableInfo & table_info);

void applyDropTableImpl(const String &, const String &);
};
bool applyCreateSchemaImpl(TiDB::DBInfoPtr db_info);

} // namespace DB
119 changes: 0 additions & 119 deletions dbms/src/Storages/Transaction/TiDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,128 +2,9 @@
#include <Storages/MutableSupport.h>
#include <Storages/Transaction/TiDB.h>

namespace JsonSer
{

using DB::WriteBuffer;

template <typename T = bool>
void serValue(WriteBuffer & buf, const bool & b)
{
writeString(b ? "true" : "false", buf);
}

template <typename T>
typename std::enable_if_t<std::is_integral<T>::value || std::is_enum<T>::value> serValue(WriteBuffer & buf, T i)
{
writeIntText(static_cast<Int64>(i), buf);
}

template <typename T>
typename std::enable_if_t<std::is_floating_point<T>::value> serValue(WriteBuffer & buf, T f)
{
writeFloadText(f, buf);
}

// String that has been already encoded as JSON.
struct JsonString : public std::string
{
};

template <typename T = JsonString>
void serValue(WriteBuffer & buf, const JsonString & qs)
{
writeString(qs, buf);
}

template <typename T = std::string>
void serValue(WriteBuffer & buf, const std::string & s)
{
writeJSONString(s, buf);
}

template <typename T>
void serValue(WriteBuffer & buf, const std::vector<T> & v)
{
writeString("[", buf);
bool first = true;
for (auto & e : v)
{
first = first ? false : (writeString(",", buf), false);
serValue(buf, e);
}
writeString("]", buf);
}

template <typename T = std::function<void(WriteBuffer &)>>
void serValue(WriteBuffer & buf, const std::function<void(WriteBuffer &)> & s)
{
s(buf);
}

template <typename T>
std::function<void(WriteBuffer &)> Nullable(const T & value, bool is_null)
{
return [value, is_null](WriteBuffer & buf) { is_null ? writeString("null", buf) : serValue(buf, value); };
}

template <typename T>
struct Field
{
Field(std::string name_, T value_, bool skip_ = false) : name(std::move(name_)), value(std::move(value_)), skip(skip_) {}
std::string name;
T value;
bool skip;
};

template <typename T>
void serField(WriteBuffer & buf, const Field<T> & field)
{
writeJSONString(field.name, buf);
writeString(":", buf);
serValue(buf, field.value);
}

template <typename T>
void serFields(WriteBuffer & buf, const T & last)
{
if (!last.skip)
serField(buf, last);
}

template <typename T, typename... Rest>
void serFields(WriteBuffer & buf, const T & first, const Rest &... rest)
{
if (!first.skip)
{
serField(buf, first);
writeString(",", buf);
}
serFields(buf, rest...);
}

template <typename... T>
void serValue(WriteBuffer & buf, const T &... fields)
{
writeString("{", buf);
serFields(buf, fields...);
writeString("}", buf);
}

template <typename... T>
std::function<void(WriteBuffer &)> Struct(const T &... fields)
{
return [fields...](WriteBuffer & buf) { serValue(buf, fields...); };
}

} // namespace JsonSer

namespace TiDB
{

using DB::Field;
using DB::ReadBufferFromString;
using DB::WriteBuffer;
using DB::WriteBufferFromOwnString;

ColumnInfo::ColumnInfo(Poco::JSON::Object::Ptr json) { deserialize(json); }
Expand Down
39 changes: 5 additions & 34 deletions dbms/src/Storages/Transaction/TiDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ struct TableInfo

ColumnID getColumnID(const String & name) const;

TableInfoPtr producePartitionTableInfo(TableID table_or_partition_id) const
TableInfo producePartitionTableInfo(TableID table_or_partition_id) const
{
//
// Some sanity checks for partition table.
Expand All @@ -280,44 +280,15 @@ struct TableInfo
DB::ErrorCodes::LOGICAL_ERROR);

// This is a TiDB partition table, adjust the table ID by making it to physical table ID (partition ID).
TableInfoPtr new_table = std::make_shared<TableInfo>(*this);
new_table->belonging_table_id = id;
new_table->id = table_or_partition_id;
TableInfo new_table = *this;
new_table.belonging_table_id = id;
new_table.id = table_or_partition_id;

// Mangle the table name by appending partition name.
new_table->name += "_" + std::to_string(table_or_partition_id);
new_table.name += "_" + std::to_string(table_or_partition_id);

return new_table;
}

bool manglePartitionTableIfNeeded(TableID table_or_partition_id)
{
if (id == table_or_partition_id)
// Non-partition table.
return false;

// Some sanity checks for partition table.
if (unlikely(!(is_partition_table && partition.enable)))
throw Exception("Table ID " + std::to_string(id) + " seeing partition ID " + std::to_string(table_or_partition_id)
+ " but it's not a partition table",
DB::ErrorCodes::LOGICAL_ERROR);

if (unlikely(std::find_if(partition.definitions.begin(), partition.definitions.end(), [table_or_partition_id](const auto & d) {
return d.id == table_or_partition_id;
}) == partition.definitions.end()))
throw Exception(
"Couldn't find partition with ID " + std::to_string(table_or_partition_id) + " in table ID " + std::to_string(id),
DB::ErrorCodes::LOGICAL_ERROR);

// This is a TiDB partition table, adjust the table ID by making it to physical table ID (partition ID).
belonging_table_id = id;
id = table_or_partition_id;

// Mangle the table name by appending partition name.
name += "_" + std::to_string(table_or_partition_id);

return true;
}
};

using DBInfoPtr = std::shared_ptr<DBInfo>;
Expand Down
3 changes: 0 additions & 3 deletions dbms/src/Storages/Transaction/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
include_directories (${CMAKE_CURRENT_BINARY_DIR})

add_executable (sync_schema sync_schema.cpp)
target_link_libraries (sync_schema dbms)

add_executable (tikv_keyvalue tikv_keyvalue.cpp)
target_link_libraries (tikv_keyvalue dbms)

Expand Down
42 changes: 0 additions & 42 deletions dbms/src/Storages/Transaction/tests/sync_schema.cpp

This file was deleted.

Loading

0 comments on commit 1a6a1c4

Please sign in to comment.