-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support for coordinator schemas #28031
Changes from all commits
158c430
22110bd
b2e050f
a3a126d
7d63488
265bae4
ac7bf42
a0c7feb
5263982
294f3b1
6595959
37267ed
32673da
8246bce
36f3789
18f64e8
e7867dd
71439c5
bd654ba
21d1fde
8584672
6ba1e5f
ed24ba3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: shard number '02' may conflict with existing shards if not carefully coordinated across the cluster |
||
<replica>coord</replica> | ||
<hostClusterType>online</hostClusterType> | ||
<hostClusterRole>coordinator</hostClusterRole> | ||
</macros> | ||
</clickhouse> |
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> |
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> |
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: No error handling for failed migrations across nodes - could leave cluster in inconsistent state |
||
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()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: consider deduplicating volume mounts between clickhouse and clickhouse-coordinator services using YAML anchors