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 #6690

Closed
Closed
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
14 changes: 8 additions & 6 deletions scripts/load_uptime_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
query = """
INSERT INTO default.uptime_monitor_checks_local (
organization_id, project_id, environment, uptime_subscription_id, uptime_check_id,
scheduled_check_time, timestamp, duration, region_id, check_status,
scheduled_check_time, timestamp, duration_ms, region_slug, check_status,
check_status_reason, http_status_code, trace_id, retention_days
) FORMAT JSONEachRow
"""

total_records = 0

for project_id in range(1, 2):
for project_id in range(2, 100):
project_data = []
for minute in range(24 * 60 * 90): # 24 hours * 60 minutes * 90 days
timestamp = base_time + datetime.timedelta(minutes=minute)
Expand All @@ -36,10 +36,12 @@
"environment": "production",
"uptime_subscription_id": random.randint(1, 3) * project_id,
"uptime_check_id": str(uuid.uuid4()),
"scheduled_check_time": scheduled_time.strftime("%Y-%m-%d %H:%M:%S"),
"timestamp": timestamp.strftime("%Y-%m-%d %H:%M:%S"),
"duration": random.randint(1, 1000),
"region_id": random.randint(1, 3),
"scheduled_check_time": scheduled_time.strftime("%Y-%m-%d %H:%M:%S.%f")[
:-3
],
"timestamp": timestamp.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3],
"duration_ms": random.randint(1, 1000),
"region_slug": f"region-{random.randint(1, 3)}",
"check_status": check_status,
"check_status_reason": "Timeout error"
if check_status == "failure"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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.UPTIME_MONITOR_CHECKS
phacops marked this conversation as resolved.
Show resolved Hide resolved
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(nullable=True, 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(
primary_key="(organization_id, project_id, toDateTime(timestamp), uptime_check_id, trace_id)",
order_by="(organization_id, project_id, toDateTime(timestamp), uptime_check_id, trace_id)",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there only 1 check per trace? You might something more specific here otherwise the ReplacingMergeTree will merge 2 rows with the same sort key.

The engine differs from MergeTree in that it removes duplicate entries with the same sorting key value (ORDER BY table section, not PRIMARY KEY).

https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/replacingmergetree

If you're not going to update values or if you don't care about duplicates, you could just use a MergeTree.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only one check per trace. duplicates would not be ideal

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