8000 Fix macros for autodiscovery clusters by ianton-ru · Pull Request #757 · Altinity/ClickHouse · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Interpreters/ClusterDiscovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ class ClusterDiscovery::Flags
ClusterDiscovery::ClusterDiscovery(
const Poco::Util::AbstractConfiguration & config,
ContextPtr context_,
MultiVersion<Macros>::Version macros_,
const String & config_prefix)
: context(Context::createCopy(context_))
, current_node_name(toString(ServerUUID::get()))
, log(getLogger("ClusterDiscovery"))
, macros(macros_)
{
LOG_DEBUG(log, "Cluster discovery is enabled");

Expand Down Expand Up @@ -726,7 +728,8 @@ bool ClusterDiscovery::runMainThread(std::function<void()> up_to_date_callback)
ClusterPtr ClusterDiscovery::getCluster(const String & cluster_name) const
{
std::lock_guard lock(mutex);
auto it = cluster_impls.find(cluster_name);
auto expanded_cluster_name = macros->expand(cluster_name);
auto it = cluster_impls.find(expanded_cluster_name);
if (it == cluster_impls.end())
return nullptr;
return it->second;
Expand Down
3 changes: 3 additions & 0 deletions src/Interpreters/ClusterDiscovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ClusterDiscovery
ClusterDiscovery(
const Poco::Util::AbstractConfiguration & config,
ContextPtr context_,
MultiVersion<Macros>::Version macros_,
const String & config_prefix = "remote_servers");

void start();
Expand Down Expand Up @@ -204,6 +205,8 @@ class ClusterDiscovery
};

std::shared_ptr<std::vector<std::shared_ptr<MulticlusterDiscovery>>> multicluster_discovery_paths;

MultiVersion<Macros>::Version macros;
};

}
2 changes: 1 addition & 1 deletion src/Interpreters/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4533,7 +4533,7 @@ void Context::setClustersConfig(const ConfigurationPtr & config, bool enable_dis
std::lock_guard lock(shared->clusters_mutex);
if (ConfigHelper::getBool(*config, "allow_experimental_cluster_discovery") && enable_discovery && !shared->cluster_discovery)
{
shared->cluster_discovery = std::make_unique<ClusterDiscovery>(*config, getGlobalContext());
shared->cluster_discovery = std::make_unique<ClusterDiscovery>(*config, getGlobalContext(), getMacros());
}

/// Do not update clusters if this part of config wasn't changed.
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/test_cluster_discovery/config/macros.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<clickhouse>
<macros>
<autocluster>test_auto_cluster</autocluster>
</macros>
</clickhouse>
10 changes: 9 additions & 1 deletion tests/integration/test_cluster_discovery/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
nodes = {
node_name: cluster.add_instance(
node_name,
main_configs=[shard_config, "config/config_discovery_path.xml"],
main_configs=[shard_config, "config/config_discovery_path.xml", "config/macros.xml"],
stay_alive=True,
with_zookeeper=True,
)
Expand Down Expand Up @@ -122,3 +122,11 @@ def test_cluster_discovery_startup_and_stop(start_cluster):

# cleanup
nodes["node0"].query("DROP TABLE tbl ON CLUSTER 'test_auto_cluster' SYNC")


def test_cluster_discovery_macros(start_cluster):
# check macros
res = nodes["node_observer"].query(
"SELECT sum(number) FROM clusterAllReplicas('{autocluster}', system.numbers) WHERE number=1"
)
assert res.strip() == "5"
0