Skip to content

Commit

Permalink
feat: add support for coordinator schemas (#28031)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Daesgar and github-actions[bot] authored Jan 31, 2025
1 parent f7563e3 commit 9b30f8d
Show file tree
Hide file tree
Showing 20 changed files with 292 additions and 73 deletions.
6 changes: 3 additions & 3 deletions .flox/env/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ fi
if [[ -t 0 ]]; then # The block below only runs when in an interactive shell
# Add required entries to /etc/hosts if not present
if ! grep -q "127.0.0.1 kafka clickhouse" /etc/hosts; then
if ! grep -q "127.0.0.1 kafka clickhouse clickhouse-coordinator" /etc/hosts; then
echo
echo "🚨 Amending /etc/hosts to map hostnames 'kafka' and 'clickhouse' to 127.0.0.1..."
echo "127.0.0.1 kafka clickhouse" | sudo tee -a /etc/hosts 1> /dev/null
echo "🚨 Amending /etc/hosts to map hostnames 'kafka', 'clickhouse' and 'clickhouse-coordinator' to 127.0.0.1..."
echo "127.0.0.1 kafka clickhouse clickhouse-coordinator" | sudo tee -a /etc/hosts 1> /dev/null
echo "✅ /etc/hosts amended"
fi
Expand Down
1 change: 1 addition & 0 deletions docker-compose.dev-full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ services:
- ./posthog/idl:/idl
- ./docker/clickhouse/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
- ./docker/clickhouse/config.xml:/etc/clickhouse-server/config.xml
- ./docker/clickhouse/config.d/default.xml:/etc/clickhouse-server/config.d/default.xml
- ./docker/clickhouse/users-dev.xml:/etc/clickhouse-server/users.xml
- ./docker/clickhouse/user_defined_function.xml:/etc/clickhouse-server/user_defined_function.xml
- ./posthog/user_scripts:/var/lib/clickhouse/user_scripts
Expand Down
20 changes: 19 additions & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ services:
ports:
- '5555:5555'

clickhouse:
clickhouse: &clickhouse
extends:
file: docker-compose.base.yml
service: clickhouse
Expand All @@ -69,6 +69,7 @@ services:
- ./posthog/idl:/idl
- ./docker/clickhouse/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
- ./docker/clickhouse/config.xml:/etc/clickhouse-server/config.xml
- ./docker/clickhouse/config.d/worker.xml:/etc/clickhouse-server/config.d/worker.xml
- ./docker/clickhouse/users-dev.xml:/etc/clickhouse-server/users.xml
- ./docker/clickhouse/user_defined_function.xml:/etc/clickhouse-server/user_defined_function.xml
- ./posthog/user_scripts:/var/lib/clickhouse/user_scripts
Expand All @@ -78,6 +79,23 @@ services:
- kafka
- zookeeper

clickhouse-coordinator:
hostname: clickhouse-coordinator
<<: *clickhouse
volumes:
# this new entrypoint file is to fix a bug detailed here https://github.com/ClickHouse/ClickHouse/pull/59991
# revert this when we upgrade clickhouse
- ./docker/clickhouse/entrypoint.sh:/entrypoint.sh
- ./posthog/idl:/idl
- ./docker/clickhouse/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
- ./docker/clickhouse/config.xml:/etc/clickhouse-server/config.xml
- ./docker/clickhouse/config.d/coordinator.xml:/etc/clickhouse-server/config.d/coordinator.xml
- ./docker/clickhouse/users-dev.xml:/etc/clickhouse-server/users.xml
- ./docker/clickhouse/user_defined_function.xml:/etc/clickhouse-server/user_defined_function.xml
- ./posthog/user_scripts:/var/lib/clickhouse/user_scripts
ports:
- '9001:9001'

zookeeper:
extends:
file: docker-compose.base.yml
Expand Down
1 change: 1 addition & 0 deletions docker-compose.hobby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ services:
- ./posthog/posthog/idl:/idl
- ./posthog/docker/clickhouse/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
- ./posthog/docker/clickhouse/config.xml:/etc/clickhouse-server/config.xml
- ./posthog/docker/clickhouse/config.d/default.xml:/etc/clickhouse-server/config.d/default.xml
- ./posthog/docker/clickhouse/users.xml:/etc/clickhouse-server/users.xml
- clickhouse-data:/var/lib/clickhouse
zookeeper:
Expand Down
42 changes: 42 additions & 0 deletions docker/clickhouse/config.d/coordinator.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<clickhouse>
<tcp_port>9001</tcp_port>
<remote_servers>
<posthog>
<shard>
<replica>
<host>clickhouse</host>
<port>9000</port>
</replica>
</shard>
</posthog>
<posthog_single_shard>
<shard>
<replica>
<host>clickhouse</host>
<port>9000</port>
</replica>
</shard>
</posthog_single_shard>
<posthog_migrations>
<shard>
<replica>
<host>clickhouse</host>
<port>9000</port>
</replica>
</shard>
<shard>
<replica>
<host>clickhouse-coordinator</host>
<port>9001</port>
</replica>
</shard>
</posthog_migrations>
</remote_servers>

<macros>
<shard>02</shard>
<replica>coord</replica>
<hostClusterType>online</hostClusterType>
<hostClusterRole>coordinator</hostClusterRole>
</macros>
</clickhouse>
37 changes: 37 additions & 0 deletions docker/clickhouse/config.d/default.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<clickhouse>
<tcp_port>9000</tcp_port>

<remote_servers>
<posthog>
<shard>
<replica>
<host>localhost</host>
<port>9000</port>
</replica>
</shard>
</posthog>
<posthog_single_shard>
<shard>
<replica>
<host>localhost</host>
<port>9000</port>
</replica>
</shard>
</posthog_single_shard>
<posthog_migrations>
<shard>
<replica>
<host>localhost</host>
<port>9000</port>
</replica>
</shard>
</posthog_migrations>
</remote_servers>

<macros>
<shard>01</shard>
<replica>ch1</replica>
<hostClusterType>online</hostClusterType>
<hostClusterRole>worker</hostClusterRole>
</macros>
</clickhouse>
42 changes: 42 additions & 0 deletions docker/clickhouse/config.d/worker.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<clickhouse>
<tcp_port>9000</tcp_port>
<remote_servers>
<posthog>
<shard>
<replica>
<host>clickhouse</host>
<port>9000</port>
</replica>
</shard>
</posthog>
<posthog_single_shard>
<shard>
<replica>
<host>clickhouse</host>
<port>9000</port>
</replica>
</shard>
</posthog_single_shard>
<posthog_migrations>
<shard>
<replica>
<host>clickhouse</host>
<port>9000</port>
</replica>
</shard>
<shard>
<replica>
<host>clickhouse-coordinator</host>
<port>9001</port>
</replica>
</shard>
</posthog_migrations>
</remote_servers>

<macros>
<shard>01</shard>
<replica>ch1</replica>
<hostClusterType>online</hostClusterType>
<hostClusterRole>worker</hostClusterRole>
</macros>
</clickhouse>
27 changes: 1 addition & 26 deletions docker/clickhouse/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
<errorlog>/var/log/clickhouse-server/clickhouse-server.err.log</errorlog>
<size>1000M</size>
<count>10</count>
<console>1</console>
</logger>

<http_port>8123</http_port>
<tcp_port>9000</tcp_port>
<mysql_port>9004</mysql_port>
<postgresql_port>9005</postgresql_port>
<https_port>8443</https_port>
Expand Down Expand Up @@ -162,25 +162,6 @@
<!-- Reallocate memory for machine code ("text") using huge pages. Highly experimental. -->
<remap_executable>false</remap_executable>

<remote_servers>
<posthog>
<shard>
<replica>
<host>localhost</host>
<port>9000</port>
</replica>
</shard>
</posthog>
<posthog_single_shard>
<shard>
<replica>
<host>localhost</host>
<port>9000</port>
</replica>
</shard>
</posthog_single_shard>
</remote_servers>

<remote_url_allow_hosts>
<host_regexp>.*</host_regexp>
</remote_url_allow_hosts>
Expand All @@ -192,12 +173,6 @@
</node>
</zookeeper>

<macros>
<shard>01</shard>
<replica>ch1</replica>
</macros>


<!-- Reloading interval for embedded dictionaries, in seconds. Default: 3600. -->
<builtin_dictionaries_reload_interval>3600</builtin_dictionaries_reload_interval>

Expand Down
6 changes: 6 additions & 0 deletions posthog/clickhouse/client/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class Workload(Enum):
OFFLINE = "OFFLINE"


class NodeRole(Enum):
ALL = "ALL"
COORDINATOR = "COORDINATOR"
WORKER = "WORKER"


_default_workload = Workload.ONLINE


Expand Down
29 changes: 17 additions & 12 deletions posthog/clickhouse/client/migration_tools.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
from typing import Union
from collections.abc import Callable
import logging

from infi.clickhouse_orm import migrations

from posthog.clickhouse.client.execute import sync_execute
from posthog.clickhouse.client.connection import NodeRole
from posthog.clickhouse.cluster import get_cluster
from posthog.settings.data_stores import CLICKHOUSE_MIGRATIONS_CLUSTER

logger = logging.getLogger("migrations")

def run_sql_with_exceptions(sql: Union[str, Callable[[], str]], settings=None):

def run_sql_with_exceptions(sql: str, settings=None, node_role: NodeRole = NodeRole.WORKER):
"""
migrations.RunSQL does not raise exceptions, so we need to wrap it in a function that does.
node_role is set to WORKER by default to keep compatibility with the old migrations.
"""

if settings is None:
settings = {}
cluster = get_cluster(client_settings=settings, cluster=CLICKHOUSE_MIGRATIONS_CLUSTER)

def run_sql(database):
nonlocal sql
if callable(sql):
sql = sql()
sync_execute(sql, settings=settings)
def run_migration():
if node_role == NodeRole.ALL:
logger.info(" Running migration on coordinators and workers")
return cluster.map_all_hosts(lambda client: client.execute(sql)).result()
else:
logger.info(f" Running migration on {node_role.value.lower()}s")
return cluster.map_hosts_by_role(lambda client: client.execute(sql), node_role=node_role).result()

return migrations.RunPython(run_sql)
return migrations.RunPython(lambda _: run_migration())
Loading

0 comments on commit 9b30f8d

Please sign in to comment.