DDLWorker.cpp 23.2 KB
Newer Older
1
#include <Interpreters/DDLWorker.h>
2 3

#include <Parsers/ASTAlterQuery.h>
4
#include <Parsers/ASTQueryWithOnCluster.h>
5 6 7
#include <Parsers/ParserQuery.h>
#include <Parsers/parseQuery.h>
#include <Parsers/queryToString.h>
8

9 10 11 12 13
#include <IO/WriteHelpers.h>
#include <IO/ReadHelpers.h>
#include <IO/Operators.h>
#include <IO/ReadBufferFromString.h>

14
#include <Storages/IStorage.h>
15
#include <DataStreams/OneBlockInputStream.h>
16

17
#include <Interpreters/executeQuery.h>
18
#include <Interpreters/Cluster.h>
19

20
#include <Common/getFQDNOrHostName.h>
21 22
#include <Common/setThreadName.h>
#include <Common/Stopwatch.h>
23 24 25 26 27 28 29

#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypeArray.h>
#include <Columns/ColumnsNumber.h>
#include <Columns/ColumnString.h>
#include <Columns/ColumnArray.h>
30 31

#include <zkutil/ZooKeeper.h>
32
#include <zkutil/Lock.h>
33 34
#include <Poco/Timestamp.h>

35 36
#include <experimental/optional>

37 38 39 40 41 42 43 44

namespace DB
{

namespace ErrorCodes
{
    extern const int UNKNOWN_ELEMENT_IN_CONFIG;
    extern const int INVALID_CONFIG_PARAMETER;
45
    extern const int UNKNOWN_FORMAT_VERSION;
46 47
    extern const int INCONSISTENT_TABLE_ACCROSS_SHARDS;
    extern const int INCONSISTENT_CLUSTER_DEFINITION;
48
    extern const int TIMEOUT_EXCEEDED;
49
    extern const int UNFINISHED;
50 51 52
}


53 54 55 56
struct DDLLogEntry
{
    String query;
    Strings hosts;
57
    String initiator; // optional
58

59
    static constexpr int CURRENT_VERSION = '1';
60 61 62 63 64 65 66

    String toString()
    {
        String res;
        {
            WriteBufferFromString wb(res);

67
            wb << "version: " << CURRENT_VERSION << "\n";
68 69 70
            wb << "query: " << query << "\n";
            wb << "hosts: " << hosts << "\n";
            wb << "initiator: " << initiator << "\n";
71 72 73 74 75 76 77 78 79
        }

        return res;
    }

    void parse(const String & data)
    {
        ReadBufferFromString rb(data);

80 81 82
        int version;
        rb >> "version: " >> version >> "\n";

83 84 85
        if (version != CURRENT_VERSION)
            throw Exception("Unknown DDLLogEntry format version: " + version, ErrorCodes::UNKNOWN_FORMAT_VERSION);

86 87
        rb >> "query: " >> query >> "\n";
        rb >> "hosts: " >> hosts >> "\n";
88 89 90 91 92

        if (!rb.eof())
            rb >> "initiator: " >> initiator >> "\n";
        else
            initiator.clear();
93 94 95 96

        assertEOF(rb);
    }
};
97 98


99 100
using ShardAndHostNum = std::experimental::optional<std::pair<size_t, size_t>>;
static ShardAndHostNum tryGetShardAndHostNum(const Cluster::AddressesWithFailover & cluster, const String & host_name, UInt16 port)
101 102 103 104 105 106 107
{
    for (size_t shard_num = 0; shard_num < cluster.size(); ++shard_num)
    {
        for (size_t host_num = 0; host_num < cluster[shard_num].size(); ++host_num)
        {
            const Cluster::Address & address = cluster[shard_num][host_num];
            if (address.host_name == host_name && address.port == port)
108
                return std::make_pair(shard_num, host_num);
109 110 111
        }
    }

112
    return {};
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
}


static bool isSupportedAlterType(int type)
{
    static const std::unordered_set<int> supported_alter_types{
        ASTAlterQuery::ADD_COLUMN,
        ASTAlterQuery::DROP_COLUMN,
        ASTAlterQuery::MODIFY_COLUMN,
        ASTAlterQuery::MODIFY_PRIMARY_KEY,
        ASTAlterQuery::DROP_PARTITION
    };

    return supported_alter_types.count(type);
}


130 131 132
DDLWorker::DDLWorker(const std::string & zk_root_dir, Context & context_)
    : context(context_), stop_flag(false)
{
133 134 135
    queue_dir = zk_root_dir;
    if (queue_dir.back() == '/')
        queue_dir.resize(queue_dir.size() - 1);
136

137 138
    host_name = getFQDNOrHostName();
    port = context.getTCPPort();
139
    host_id = Cluster::Address::toString(host_name, port);
140

141 142
    event_queue_updated = std::make_shared<Poco::Event>();

143 144 145
    thread = std::thread(&DDLWorker::run, this);
}

146

147 148 149
DDLWorker::~DDLWorker()
{
    stop_flag = true;
150
    event_queue_updated->set();
151 152 153
    thread.join();
}

154

155 156
void DDLWorker::processTasks()
{
157
    LOG_DEBUG(log, "Processing tasks");
158

159
    Strings queue_nodes = zookeeper->getChildren(queue_dir, nullptr, event_queue_updated);
160
    if (queue_nodes.empty())
161 162
        return;

163
    bool server_startup = last_processed_node_name.empty();
164

165 166 167 168
    std::sort(queue_nodes.begin(), queue_nodes.end());
    auto begin_node = server_startup
        ? queue_nodes.begin()
        : std::upper_bound(queue_nodes.begin(), queue_nodes.end(), last_processed_node_name);
169

170
    for (auto it = begin_node; it != queue_nodes.end(); ++it)
171
    {
172 173
        String node_data, node_name = *it, node_path = queue_dir + "/" + node_name;

174 175 176 177 178
        if (!zookeeper->tryGet(node_path, node_data))
        {
            /// It is Ok that node could be deleted just now. It means that there are no current host in node's host list.
            continue;
        }
179 180 181 182

        DDLLogEntry node;
        node.parse(node_data);

183
        bool host_in_hostlist = std::find(node.hosts.cbegin(), node.hosts.cend(), host_id) != node.hosts.cend();
184
        bool already_processed = zookeeper->exists(node_path + "/finished/" + host_id);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

        if (!server_startup && already_processed)
        {
            throw Exception(
                "Server expects that DDL node " + node_name + " should be processed, but it was already processed according to ZK",
                ErrorCodes::LOGICAL_ERROR);
        }

        if (host_in_hostlist && !already_processed)
        {
            try
            {
                processTask(node, node_name);
            }
            catch (...)
            {
201
                tryLogCurrentException(log, "An error occurred while processing node " + node_name + " (" + node.query + ")");
202 203 204
                throw;
            }
        }
205 206 207 208
        else
        {
            LOG_DEBUG(log, "Node " << node_name << " (" << node.query << ") will not be processed");
        }
209 210

        last_processed_node_name = node_name;
211
    }
212 213
}

214

215
static bool tryExecuteQuery(const String & query, Context & context, ExecutionStatus & status, Logger * log = nullptr)
216 217 218 219 220 221 222
{
    try
    {
        executeQuery(query, context);
    }
    catch (...)
    {
223 224
        status = ExecutionStatus::fromCurrentException();

225 226 227 228 229 230
        if (log)
            tryLogCurrentException(log, "Query " + query + " wasn't finished successfully");

        return false;
    }

231
    status = ExecutionStatus(0);
232 233 234 235 236 237 238
    if (log)
        LOG_DEBUG(log, "Executed query: " << query);

    return true;
}


239
void DDLWorker::processTask(const DDLLogEntry & node, const std::string & node_name)
240
{
241
    LOG_DEBUG(log, "Processing node " << node_name << " (" << node.query << ")");
242

243
    String node_path = queue_dir + "/" + node_name;
244 245
    createStatusDirs(node_path);

246
    bool should_not_execute = current_node == node_name && current_node_was_executed;
247

248 249 250 251
    if (!should_not_execute)
    {
        current_node = node_name;
        current_node_was_executed = false;
252

253
        zookeeper->create(node_path + "/active/" + host_id, "", zkutil::CreateMode::Ephemeral);
254

255 256 257 258 259 260 261 262 263
        try
        {
            ASTPtr query_ast;
            {
                ParserQuery parser_query;
                String description;
                IParser::Pos begin = &node.query.front();
                query_ast = parseQuery(parser_query, begin, begin + node.query.size(), description);
            }
264

265 266 267 268 269 270 271
            const ASTQueryWithOnCluster * query;
            if (!query_ast || !(query = dynamic_cast<const ASTQueryWithOnCluster *>(query_ast.get())))
                throw Exception("Recieved unsupported DDL query", ErrorCodes::NOT_IMPLEMENTED);

            String cluster_name = query->cluster;
            auto cluster = context.getCluster(cluster_name);

272 273
            auto shard_host_num = tryGetShardAndHostNum(cluster->getShardsWithFailoverAddresses(), host_name, port);
            if (!shard_host_num)
274 275 276 277
            {
                throw Exception("Cannot find own address (" + host_id + ") in cluster " + cluster_name + " configuration",
                                ErrorCodes::INCONSISTENT_CLUSTER_DEFINITION);
            }
278

279 280 281
            size_t shard_num = shard_host_num->first;
            size_t host_num = shard_host_num->second;

282 283 284
            const auto & host_address = cluster->getShardsWithFailoverAddresses().at(shard_num).at(host_num);
            ASTPtr rewritten_ast = query->getRewrittenASTWithoutOnCluster(host_address.default_database);
            String rewritten_query = queryToString(rewritten_ast);
285

286
            LOG_DEBUG(log, "Executing query: " << rewritten_query);
287

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
            if (auto query_alter = dynamic_cast<const ASTAlterQuery *>(rewritten_ast.get()))
            {
                processTaskAlter(query_alter, rewritten_query, cluster, shard_num, node_path);
            }
            else
            {
                tryExecuteQuery(rewritten_query, context, current_node_execution_status, log);
            }
        }
        catch (const zkutil::KeeperException & e)
        {
            throw;
        }
        catch (...)
        {
            current_node_execution_status = ExecutionStatus::fromCurrentException("An error occured during query preparation");
        }

        /// We need to distinguish ZK errors occured before and after query executing
        current_node_was_executed = true;
308 309
    }

310 311 312 313 314
    /// Delete active flag and create finish flag
    zkutil::Ops ops;
    ops.emplace_back(std::make_unique<zkutil::Op::Remove>(node_path + "/active/" + host_id, -1));
    ops.emplace_back(std::make_unique<zkutil::Op::Create>(node_path + "/finished/" + host_id,
        current_node_execution_status.serializeText(), zookeeper->getDefaultACL(), zkutil::CreateMode::Persistent));
315

316 317 318 319
    int code = zookeeper->tryMultiWithRetries(ops);
    if (code != ZOK && code != ZNONODE)
        throw zkutil::KeeperException("Cannot commit executed node " + node_name, code);
}
320 321


322 323 324 325 326 327 328 329 330
void DDLWorker::processTaskAlter(
    const ASTAlterQuery * query_alter,
    const String & rewritten_query,
    const std::shared_ptr<Cluster> & cluster,
    ssize_t shard_num,
    const String & node_path)
{
    String database = query_alter->database.empty() ? context.getCurrentDatabase() : query_alter->database;
    StoragePtr storage = context.getTable(database, query_alter->table);
331

332 333
    bool execute_once_on_replica = storage->supportsReplication();
    bool execute_on_leader_replica = false;
334

335
    for (const auto & param : query_alter->parameters)
336
    {
337 338
        if (!isSupportedAlterType(param.type))
            throw Exception("Unsupported type of ALTER query", ErrorCodes::NOT_IMPLEMENTED);
339

340 341 342
        if (execute_once_on_replica)
            execute_on_leader_replica |= param.type == ASTAlterQuery::DROP_PARTITION;
    }
343

344 345
    const auto & shard_info = cluster->getShardsInfo().at(shard_num);
    bool config_is_replicated_shard = shard_info.hasInternalReplication();
346

347 348 349 350 351 352 353 354 355 356
    if (execute_once_on_replica && !config_is_replicated_shard)
    {
        throw Exception("Table " + query_alter->table + " is replicated, but shard #" + toString(shard_num + 1) +
            " isn't replicated according to its cluster definition", ErrorCodes::INCONSISTENT_CLUSTER_DEFINITION);
    }
    else if (!execute_once_on_replica && config_is_replicated_shard)
    {
        throw Exception("Table " + query_alter->table + " isn't replicated, but shard #" + toString(shard_num + 1) +
            " replicated according to its cluster definition", ErrorCodes::INCONSISTENT_CLUSTER_DEFINITION);
    }
357

358 359
    if (execute_once_on_replica)
    {
360 361 362 363
        /// The following code may perform ALTER twice if
        ///  current secver aquires lock, executes replicated alter,
        ///  losts zookeeper connection and doesn't have time to create /executed node, second server executes replicated alter again
        /// To avoid this problem alter() method of replicated tables should be changed and takes into account ddl query id tag.
364 365
        if (!context.getSettingsRef().distributed_ddl_allow_replicated_alter)
            throw Exception("Distributed DDL alters don't work properly yet", ErrorCodes::NOT_IMPLEMENTED);
366

367 368 369 370
        Strings replica_names;
        for (const auto & address : cluster->getShardsWithFailoverAddresses().at(shard_num))
            replica_names.emplace_back(address.toString());
        std::sort(replica_names.begin(), replica_names.end());
371

372 373 374
        String shard_dir_name;
        for (auto it = replica_names.begin(); it != replica_names.end(); ++it)
            shard_dir_name += *it + (std::next(it) != replica_names.end() ? "," : "");
375

376 377 378
        String shard_path = node_path + "/shards/" + shard_dir_name;
        String is_executed_path = shard_path + "/executed";
        zookeeper->createAncestors(shard_path + "/");
379

380 381 382 383
        bool alter_executed_by_replica = false;
        {
            auto zookeeper_holder = std::make_shared<zkutil::ZooKeeperHolder>();
            zookeeper_holder->initFromInstance(zookeeper);
384

385 386 387 388
            zkutil::Lock lock(zookeeper_holder, shard_path, "lock", host_id);
            std::mt19937 rng(std::hash<String>{}(host_id) + reinterpret_cast<intptr_t>(&rng));

            for (int num_tries = 0; num_tries < 10; ++num_tries)
389
            {
390 391 392 393 394
                if (zookeeper->exists(is_executed_path))
                {
                    alter_executed_by_replica = true;
                    break;
                }
395

396
                if (lock.tryLock())
397
                {
398
                    tryExecuteQuery(rewritten_query, context, current_node_execution_status, log);
399

400
                    if (execute_on_leader_replica && current_node_execution_status.code == ErrorCodes::NOT_IMPLEMENTED)
401
                    {
402
                        /// TODO: it is ok to recieve exception "host is not leader"
403 404
                    }

405 406 407 408
                    zookeeper->create(is_executed_path, host_id, zkutil::CreateMode::Persistent);
                    lock.unlock();
                    alter_executed_by_replica = true;
                    break;
409 410
                }

411
                std::this_thread::sleep_for(std::chrono::duration<double>(std::uniform_real_distribution<double>(0, 1)(rng)));
412 413
            }
        }
414 415 416

        if (!alter_executed_by_replica)
            current_node_execution_status = ExecutionStatus(ErrorCodes::NOT_IMPLEMENTED, "Cannot enqueue replicated DDL query");
417 418 419
    }
    else
    {
420
        tryExecuteQuery(rewritten_query, context, current_node_execution_status, log);
421 422 423 424
    }
}


425 426 427 428 429 430 431
void DDLWorker::cleanupQueue(const Strings * node_names_to_check)
{
    /// Both ZK and Poco use Unix epoch
    size_t current_time_seconds = Poco::Timestamp().epochTime();
    constexpr size_t zookeeper_time_resolution = 1000;

    // Too early to check
432
    if (last_cleanup_time_seconds && current_time_seconds < last_cleanup_time_seconds + cleanup_min_period_seconds)
433 434 435 436
        return;

    last_cleanup_time_seconds = current_time_seconds;

437 438
    LOG_DEBUG(log, "Cleaning queue");

439 440 441 442
    String data;
    zkutil::Stat stat;
    DDLLogEntry node;

443
    Strings node_names_fetched = node_names_to_check ? Strings{} : zookeeper->getChildren(queue_dir);
444 445 446 447 448 449
    const Strings & node_names = (node_names_to_check) ? *node_names_to_check : node_names_fetched;

    for (const String & node_name : node_names)
    {
        try
        {
450
            String node_path = queue_dir + "/" + node_name;
451 452 453
            if (!zookeeper->tryGet(node_path, data, &stat))
                continue;

454
            /// TODO: Add shared lock to avoid rare race counditions.
455 456 457 458 459 460 461 462 463 464

            size_t zookeeper_time_seconds = stat.mtime / zookeeper_time_resolution;
            if (zookeeper_time_seconds + node_max_lifetime_seconds < current_time_seconds)
            {
                size_t lifetime_seconds = current_time_seconds - zookeeper_time_seconds;
                LOG_INFO(log, "Lifetime of node " << node_name << " (" << lifetime_seconds << " sec.) is expired, deleting it");
                zookeeper->removeRecursive(node_path);
                continue;
            }

465
            Strings finished_nodes = zookeeper->getChildren(node_path + "/finished");
466 467
            node.parse(data);

468
            if (finished_nodes.size() >= node.hosts.size())
469 470 471 472 473 474 475 476 477 478 479 480
            {
                LOG_INFO(log, "Node " << node_name << " had been executed by each host, deleting it");
                zookeeper->removeRecursive(node_path);
            }
        }
        catch (...)
        {
            tryLogCurrentException(log, "An error occured while checking and cleaning node " + node_name + " from queue");
        }
    }
}

481

482 483
/// Try to create unexisting "status" dirs for a node
void DDLWorker::createStatusDirs(const std::string & node_path)
484
{
485
    zkutil::Ops ops;
486
    auto acl = zookeeper->getDefaultACL();
487
    ops.emplace_back(std::make_unique<zkutil::Op::Create>(node_path + "/active", "", acl, zkutil::CreateMode::Persistent));
488
    ops.emplace_back(std::make_unique<zkutil::Op::Create>(node_path + "/finished", "", acl, zkutil::CreateMode::Persistent));
489

490 491 492 493
    int code = zookeeper->tryMulti(ops);
    if (code != ZOK && code != ZNODEEXISTS)
        throw zkutil::KeeperException(code);
}
494 495


496
String DDLWorker::enqueueQuery(DDLLogEntry & entry)
497
{
498
    if (entry.hosts.empty())
499
        return {};
500

501
    String query_path_prefix = queue_dir + "/query-";
502
    zookeeper->createAncestors(query_path_prefix);
503

504 505
    String node_path = zookeeper->create(query_path_prefix, entry.toString(), zkutil::CreateMode::PersistentSequential);
    createStatusDirs(node_path);
506 507

    return node_path;
508 509
}

510

511 512
void DDLWorker::run()
{
513
    setThreadName("DDLWorker");
514 515
    LOG_DEBUG(log, "Started DDLWorker thread");

516 517 518
    zookeeper = context.getZooKeeper();
    zookeeper->createAncestors(queue_dir + "/");

519 520 521 522
    while (!stop_flag)
    {
        try
        {
523
            processTasks();
524

525 526 527 528 529
            LOG_DEBUG(log, "Waiting watch");
            event_queue_updated->wait();

            if (stop_flag)
                break;
530 531

            cleanupQueue();
532
        }
533 534 535 536 537
        catch (zkutil::KeeperException &)
        {
            LOG_DEBUG(log, "Recovering ZooKeeper session");
            zookeeper = context.getZooKeeper();
        }
538 539 540
        catch (...)
        {
            tryLogCurrentException(log);
541
            throw;
542
        }
543 544 545
    }
}

546

547
class DDLQueryStatusInputSream : public IProfilingBlockInputStream
548
{
549
public:
550

551
    DDLQueryStatusInputSream(const String & zk_node_path, Context & context, size_t num_hosts)
552
    : node_path(zk_node_path), context(context), watch(CLOCK_MONOTONIC_COARSE)
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    {
        sample = Block{
            {std::make_shared<DataTypeString>(),    "host"},
            {std::make_shared<DataTypeUInt64>(),    "status"},
            {std::make_shared<DataTypeString>(),    "error"},
            {std::make_shared<DataTypeUInt64>(),    "num_hosts_remaining"},
            {std::make_shared<DataTypeUInt64>(),    "num_hosts_active"},
        };

        setTotalRowsApprox(num_hosts);
    }

    String getName() const override
    {
        return "DDLQueryStatusInputSream";
    }

    String getID() const override
    {
        return "DDLQueryStatusInputSream(" + node_path + ")";
    }

575 576
    static constexpr size_t timeout_seconds = 120;

577 578 579 580 581 582 583 584 585 586 587 588 589 590
    Block readImpl() override
    {
        Block res;
        if (num_hosts_finished >= total_rows_approx)
            return res;

        auto zookeeper = context.getZooKeeper();
        size_t try_number = 0;

        while(res.rows() == 0)
        {
            if (is_cancelled)
                return res;

591 592
            auto elapsed_seconds = watch.elapsedSeconds();
            if (elapsed_seconds > timeout_seconds)
593
                throw Exception("Watching query is executing too long (" + toString(std::round(elapsed_seconds)) + " sec.)", ErrorCodes::TIMEOUT_EXCEEDED);
594

595
            if (num_hosts_finished != 0 || try_number != 0)
596
                std::this_thread::sleep_for(std::chrono::milliseconds(50 * std::min(20LU, try_number + 1)));
597

598
            /// TODO: add shared lock
599
            if (!zookeeper->exists(node_path))
600 601 602 603 604
            {
                throw Exception("Cannot provide query execution status. The query's node " + node_path
                                + " had been deleted by cleaner since it was finished (or its lifetime is expired)",
                                ErrorCodes::UNFINISHED);
            }
605

606
            Strings new_hosts = getNewAndUpdate(finished_hosts_set, getChildrenAllowNoNode(zookeeper, node_path + "/finished"));
607 608 609 610
            ++try_number;
            if (new_hosts.empty())
                continue;

611 612
            Strings cur_active_hosts = getChildrenAllowNoNode(zookeeper, node_path + "/active");

613
            res = sample.cloneEmpty();
614
            for (const String & host : new_hosts)
615
            {
616 617 618 619 620 621 622 623 624 625
                ExecutionStatus status(1, "Cannot obtain error message");
                {
                    String status_data;
                    if (zookeeper->tryGet(node_path + "/finished/" + host, status_data))
                        status.deserializeText(status_data);
                }

                res.getByName("host").column->insert(host);
                res.getByName("status").column->insert(static_cast<UInt64>(status.code));
                res.getByName("error").column->insert(status.message);
626
                res.getByName("num_hosts_remaining").column->insert(total_rows_approx - (++num_hosts_finished));
627
                res.getByName("num_hosts_active").column->insert(cur_active_hosts.size());
628 629
            }
        }
630

631 632 633
        return res;
    }

634 635 636 637 638 639 640 641 642 643
    static Strings getChildrenAllowNoNode(const std::shared_ptr<zkutil::ZooKeeper> & zookeeper, const String & node_path)
    {
        Strings res;
        int code = zookeeper->tryGetChildren(node_path, res);
        if (code != ZOK && code != ZNONODE)
            throw zkutil::KeeperException(code, node_path);
        return res;
    }

    static Strings getNewAndUpdate(NameSet & prev, const Strings & cur_list)
644 645 646 647 648
    {
        Strings diff;
        for (const String & elem : cur_list)
        {
            if (!prev.count(elem))
649
            {
650
                diff.emplace_back(elem);
651 652
                prev.emplace(elem);
            }
653 654 655 656 657 658 659 660 661 662 663 664 665
        }

        return diff;
    }

    ~DDLQueryStatusInputSream() override = default;

    Block sample;

private:
    String node_path;
    Context & context;

666 667 668
    Stopwatch watch;

    NameSet finished_hosts_set;
669
    size_t num_hosts_finished = 0;
670
};
671 672


673
BlockIO executeDDLQueryOnCluster(const ASTPtr & query_ptr, Context & context)
674
{
675 676 677 678 679 680
    const auto query = dynamic_cast<const ASTQueryWithOnCluster *>(query_ptr.get());
    if (!query)
    {
        throw Exception("Distributed execution is not supported for such DDL queries",
                        ErrorCodes::NOT_IMPLEMENTED);
    }
681

682 683 684 685 686 687 688 689 690 691 692 693
    auto query_alter = dynamic_cast<const ASTAlterQuery *>(query_ptr.get());
    if (query_alter)
    {
        for (const auto & param : query_alter->parameters)
        {
            if (!isSupportedAlterType(param.type))
                throw Exception("Unsupported type of ALTER query", ErrorCodes::NOT_IMPLEMENTED);
        }
    }

    ClusterPtr cluster = context.getCluster(query->cluster);
    DDLWorker & ddl_worker = context.getDDLWorker();
694

695
    DDLLogEntry entry;
696
    entry.query = queryToString(query_ptr);
697 698
    entry.initiator = ddl_worker.getHostName();

699
    Cluster::AddressesWithFailover shards = cluster->getShardsWithFailoverAddresses();
700
    for (const auto & shard : shards)
701
    {
702
        for (const auto & addr : shard)
703
            entry.hosts.emplace_back(addr.toString());
704
    }
705

706
    String node_path = ddl_worker.enqueueQuery(entry);
707 708

    BlockIO io;
709 710 711 712 713 714
    if (node_path.empty())
        return io;

    auto stream = std::make_shared<DDLQueryStatusInputSream>(node_path, context, entry.hosts.size());
    io.in_sample = stream->sample.cloneEmpty();
    io.in = std::move(stream);
715 716 717 718
    return io;
}


719
}