PostHog / posthog

🦔 PostHog provides open-source product analytics, session recording, feature flagging and A/B testing that you can self-host.
https://posthog.com
Other
21.56k stars 1.29k forks source link

Cloud clickhouse schema is incorrectly sharded for events_dead_letter_queue and plugin_log_entries #7571

Closed macobo closed 7 months ago

macobo commented 2 years ago

Bug description

Some of our table schemas are seemingly set up wrong on Cloud:

This means that these tables are sharded and when we sync_execute a query these will return data from a random shard instead of all of them.

Metabase query

Expected behavior

I believe these tables should not be sharded, so the engine call should look something like others, e.g.

ReplicatedReplacingMergeTree('/clickhouse/prod/tables/noshard/X', '{replica}-{shard}', _timestamp) 

Environment

Only affects PostHog Cloud

Additional context

cc @yakkomajuri @guidoiaquinti @tiina303 @fuziontech - something for your backlog

Note that this is one of the cases where CLICKHOUSE_REPLICATION sets things up in the wrong way. Luckily only used on cloud :crossed_fingers:

Why this issue matters? We've run into issues where cloud set up tables are wrong multiple times already, costing hours (days?) of work and in the case of groups, it contributed to losing a weeks worth of data.

Other oddities

These I'm not sure of:

In other noshard cases the replica_key is {replica}-{shard} so is using {replica} even correct?

Thank you for your bug report – we love squashing them!

yakkomajuri commented 2 years ago

Yeah so there's 2 actionable points here:

  1. Change the default schema for the ReplicatedReplacingMergeTree when CLICKHOUSE_REPLICATION is set.
  2. Write a special/big/async migration to correct it - have been thinking about this
macobo commented 2 years ago

Change the default schema for the ReplicatedReplacingMergeTree when CLICKHOUSE_REPLICATION is set.

Handled under https://github.com/PostHog/posthog/issues/8652

Write a special/big/async migration to correct it - have been thinking about this

If we do this, note this only affects posthog cloud and we might get away with something simpler.

Note that TODAY plugin logs will return random data instead of complete logs due to this issue.

macobo commented 2 years ago

Note from the future. The correct schema here would be to actually be sharding these tables and use a Distributed engine table to query them. This avoids double-storing each event.

A similar architecture is described under https://posthog.com/handbook/engineering/databases/event-ingestion under sharded events ingestion.

Twixes commented 7 months ago

I don't think this is an issue anymore, but let me know otherwise @fuziontech