Skip to content
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(uptime): add initial table migration #6696

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from typing import List, Sequence

from snuba.clickhouse.columns import UUID, Column, String, UInt
from snuba.clusters.storage_sets import StorageSetKey
from snuba.migrations import migration, operations, table_engines
from snuba.migrations.columns import MigrationModifiers as Modifiers
from snuba.migrations.operations import OperationTarget, SqlOperation
from snuba.utils.schemas import DateTime64

storage_set = StorageSetKey.EVENTS_ANALYTICS_PLATFORM
table_prefix = "uptime_monitor_checks"
local_table_name = f"{table_prefix}_local"
dist_table_name = f"{table_prefix}_dist"

## what about all the fancy codecs? do we need those?
columns: List[Column[Modifiers]] = [
Column("organization_id", UInt(64)),
Column("project_id", UInt(64)),
Column("environment", String(Modifiers(nullable=True, low_cardinality=True))),
Column("uptime_subscription_id", UUID()),
Column("uptime_check_id", UUID()),
Column("scheduled_check_time", DateTime64(3)), # millisecond precision
Column("timestamp", DateTime64(3)), # millisecond precision
Column("duration_ms", UInt(64)),
Column("region_slug", String(Modifiers(low_cardinality=True))),
Column("check_status", String(Modifiers(low_cardinality=True))),
Column("check_status_reason", String(Modifiers(low_cardinality=True))),
Column("http_status_code", UInt(16)),
Column("trace_id", UUID()),
Column("retention_days", UInt(16)),
]


class Migration(migration.ClickhouseNodeMigration):
blocking = False

def forwards_ops(self) -> Sequence[SqlOperation]:
return [
operations.CreateTable(
storage_set=storage_set,
table_name=local_table_name,
columns=columns,
engine=table_engines.ReplacingMergeTree(
order_by="(organization_id, project_id, toDateTime(timestamp), trace_id, uptime_check_id)",
partition_by="(retention_days, toMonday(timestamp))",
settings={"index_granularity": "8192"},
storage_set=storage_set,
ttl="toDateTime(timestamp) + toIntervalDay(retention_days)",
),
target=OperationTarget.LOCAL,
),
operations.CreateTable(
storage_set=storage_set,
table_name=dist_table_name,
columns=columns,
engine=table_engines.Distributed(
local_table_name=local_table_name,
sharding_key="cityHash64(reinterpretAsUInt128(trace_id))",
),
target=OperationTarget.DISTRIBUTED,
),
]

def backwards_ops(self) -> Sequence[SqlOperation]:
return [
operations.DropTable(
storage_set=storage_set,
table_name=dist_table_name,
target=OperationTarget.DISTRIBUTED,
),
operations.DropTable(
storage_set=storage_set,
table_name=local_table_name,
target=OperationTarget.LOCAL,
),
]
Loading