Repository: ClickHouse/ClickHouse
File: src/Databases/DatabaseReplicated.cpp (C++)

Code before:
    {
        table_name_to_metadata.clear();

        Coordination::Stat prev_metadata_path_stat;
        zookeeper->get(metadata_path, &prev_metadata_path_stat);

        LOG_DEBUG(log, "Trying to get consistent metadata snapshot for log pointer {}", max_log_ptr);

        Strings escaped_table_names;
        escaped_table_names = zookeeper->getChildren(metadata_path);
        if (filter_by_table_name)
            std::erase_if(escaped_table_names, [&](const String & table) { return !filter_by_table_name(unescapeForFileName(table)); });

        std::vector<String> paths_to_fetch;
        paths_to_fetch.reserve(escaped_table_names.size() + 1);

        for (const auto & table : escaped_table_names)
            paths_to_fetch.push_back(metadata_path + "/" + table);

        paths_to_fetch.push_back(zookeeper_path + "/max_log_ptr");

        auto table_metadata_and_version = zookeeper->tryGet(paths_to_fetch);

        Coordination::Stat current_metadata_path_stat;
        zookeeper->get(metadata_path, &current_metadata_path_stat);

        if (current_metadata_path_stat.ctime != prev_metadata_path_stat.ctime)
        {
            LOG_DEBUG(
                log,
                "Database zookeeper path was recreated. Created time before {}, current {}",
                prev_metadata_path_stat.ctime,
                current_metadata_path_stat.ctime);
            continue;
        }

        for (size_t i = 0; i < escaped_table_names.size(); ++i)
        {
            auto & res = table_metadata_and_version[i];
            if (res.error != Coordination::Error::ZOK)
                break;

            table_name_to_metadata.emplace(unescapeForFileName(escaped_table_names[i]), std::move(res.data));
        }

        auto current_max_log_ptr_idx = paths_to_fetch.size() - 1;
        auto current_max_log_ptr = table_metadata_and_version[current_max_log_ptr_idx];

        if (current_max_log_ptr.error != Coordination::Error::ZOK)
            Coordination::Exception::fromPath(current_max_log_ptr.error, zookeeper_path + "/max_log_ptr");


Review comment (bug):
Would `get` here throw exception if it returns no path maybe on ZNONODE or some connection failures? Better to use a  try catch block with retry?

Code after:
    while (++iteration <= max_retries)
    {
        table_name_to_metadata.clear();

        Coordination::Stat prev_metadata_path_stat;
        zookeeper->get(metadata_path, &prev_metadata_path_stat);

        LOG_DEBUG(log, "Trying to get consistent metadata snapshot for log pointer {}", max_log_ptr);

        Strings escaped_table_names;
        escaped_table_names = zookeeper->getChildren(metadata_path);
        if (filter_by_table_name)
            std::erase_if(escaped_table_names, [&](const String & table) { return !filter_by_table_name(unescapeForFileName(table)); });

        std::vector<String> paths_to_fetch;
        paths_to_fetch.reserve(escaped_table_names.size() + 1);

        for (const auto & table : escaped_table_names)
            paths_to_fetch.push_back(metadata_path + "/" + table);

        paths_to_fetch.push_back(zookeeper_path + "/max_log_ptr");

        auto table_metadata_and_version = zookeeper->tryGet(paths_to_fetch);

        Coordination::Stat current_metadata_path_stat;
        zookeeper->get(metadata_path, &current_metadata_path_stat);

        if (current_metadata_path_stat.czxid != prev_metadata_path_stat.czxid)
        {
            LOG_DEBUG(
                log,
                "Database zookeeper path was recreated. Created time before {}, current {}",
                prev_metadata_path_stat.czxid,
                current_metadata_path_stat.czxid);
            continue;
        }

        for (size_t i = 0; i < escaped_table_names.size(); ++i)
        {
            auto & res = table_metadata_and_version[i];
            if (res.error != Coordination::Error::ZOK)
                break;

            table_name_to_metadata.emplace(unescapeForFileName(escaped_table_names[i]), std::move(res.data));
        }

        auto current_max_log_ptr_idx = paths_to_fetch.size() - 1;
        auto current_max_log_ptr = table_metadata_and_version[current_max_log_ptr_idx];

        if (current_max_log_ptr.error != Coordination::Error::ZOK)
            Coordination::Exception::fromPath(current_max_log_ptr.error, zookeeper_path + "/max_log_ptr");