MCPcopy Create free account
hub / github.com/ByConity/ByConity / doResyncTable

Method doResyncTable

src/Databases/MySQL/MaterializedMySQLSyncThreadManager.cpp:1459–1551  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1457}
1458
1459void MaterializedMySQLSyncThreadManager::doResyncTable(const String & table_name)
1460{
1461 String tmp_table_suffix = "_CHTMP";
1462 String query_prefix = "EXTERNAL DDL FROM MySQL(" + backQuoteIfNeed(storage_id.database_name) + ", "
1463 + backQuoteIfNeed(materialized_mysql_ptr->getMysqlDatabaseInfo().mysql_database_name) + ") ";
1464 Stopwatch timer;
1465 LOG_DEBUG(log, "Start to resync table {}", table_name);
1466 if (auto materialize_mysql_log = getContext()->getCloudMaterializedMySQLLog())
1467 {
1468 auto current_log = MaterializedMySQL::createMaterializeMySQLLog(MaterializedMySQLLogElement::SYNC_MANAGER, MaterializedMySQLLogElement::RESYNC_TABLE, storage_id.database_name);
1469 current_log.resync_table = table_name;
1470 materialize_mysql_log->add(current_log);
1471 }
1472
1473 /// Check table existence
1474 auto connection = mysql_component->pool.get();
1475 if (!checkTableExistence(connection, materialized_mysql_ptr->getMysqlDatabaseInfo().mysql_database_name, table_name, getContext()->getSettingsRef()))
1476 throw Exception(ErrorCodes::UNKNOWN_TABLE, "Resync table {} doesn't exist in mysql.", table_name);
1477
1478 String full_mysql_table_name = backQuoteIfNeed(materialized_mysql_ptr->getMysqlDatabaseInfo().mysql_database_name) + "." + backQuoteIfNeed(table_name);
1479 String tmp_table_name = table_name + tmp_table_suffix;
1480 String full_mysql_tmp_table_name = backQuoteIfNeed(materialized_mysql_ptr->getMysqlDatabaseInfo().mysql_database_name) + "." + backQuoteIfNeed(tmp_table_name);
1481 Block show_create_table_header{
1482 {std::make_shared<DataTypeString>(), "Table"},
1483 {std::make_shared<DataTypeString>(), "Create Table"},
1484 };
1485
1486 StreamSettings mysql_input_stream_settings(getContext()->getSettingsRef(), false, true);
1487 MySQLBlockInputStream show_create_table(
1488 connection, "SHOW CREATE TABLE " + full_mysql_table_name, show_create_table_header, mysql_input_stream_settings);
1489
1490 Block create_query_block = show_create_table.read();
1491
1492 if (!create_query_block || create_query_block.rows() != 1)
1493 throw Exception("LOGICAL ERROR mysql show create return more rows.", ErrorCodes::LOGICAL_ERROR);
1494
1495 String create_query = create_query_block.getByName("Create Table").column->getDataAt(0).toString();
1496 LOG_DEBUG(log, "The original create table query of resync table {} is {}", table_name, create_query);
1497 create_query = create_query.replace(create_query.find(table_name), table_name.length(), tmp_table_name);
1498 LOG_DEBUG(log, "The temp create table query of resync temp table {} is {}", tmp_table_name, create_query);
1499
1500 auto query_context = MaterializedMySQL::createQueryContext(getContext());
1501 {
1502 String comment = "Materialize MySQL resync table step 1: execute MySQL DDL for dump data to tmp table";
1503 tryToExecuteQueryWithTxn(query_prefix + " DROP TABLE IF EXISTS " + full_mysql_tmp_table_name, query_context, storage_id.database_name, comment);
1504 tryToExecuteQueryWithTxn(query_prefix + " " + create_query, query_context, storage_id.database_name, comment);
1505
1506 auto query_context_inter = MaterializedMySQL::createQueryContext(query_context);
1507 auto txn = createTransactionForMySQL(query_context);
1508 SCOPE_EXIT({
1509 if (txn)
1510 query_context->getCnchTransactionCoordinator().finishTransaction(txn);
1511 });
1512
1513 query_context_inter->setCurrentTransaction(txn);
1514
1515 auto out = std::make_shared<CountingBlockOutputStream>(getTableOutput(storage_id.database_name, tmp_table_name, query_context_inter));
1516 StreamSettings inner_mysql_input_stream_settings(getContext()->getSettingsRef());

Callers

nothing calls this directly

Calls 15

backQuoteIfNeedFunction · 0.85
checkTableExistenceFunction · 0.85
createQueryContextFunction · 0.85
tryToExecuteQueryWithTxnFunction · 0.85
formatReadableQuantityFunction · 0.85
getMysqlDatabaseInfoMethod · 0.80
getByNameMethod · 0.80
replaceMethod · 0.80

Tested by

no test coverage detected