Skip to content

Commit

Permalink
check connections on consumer startup (#4497)
Browse files Browse the repository at this point in the history
* check connections on consumer startup

* make retries a setting

* allow only checking a set of clusters

* remove retries setting
  • Loading branch information
dbanda authored Jul 12, 2023
1 parent a1d52b0 commit cb039ac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 8 additions & 1 deletion snuba/cli/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
ProcessingParameters,
)
from snuba.consumers.consumer_config import resolve_consumer_config
from snuba.datasets.storages.factory import get_writable_storage_keys
from snuba.datasets.storages.factory import get_storage, get_writable_storage_keys
from snuba.datasets.storages.storage_key import StorageKey
from snuba.environment import setup_logging, setup_sentry
from snuba.migrations.connect import check_clickhouse_connections
from snuba.utils.metrics.wrapper import MetricsWrapper
from snuba.utils.streams.metrics_adapter import StreamMetricsAdapter

Expand Down Expand Up @@ -179,11 +180,17 @@ def consumer(

setup_logging(log_level)
setup_sentry()

logger.info("Consumer Starting")

storage_key = StorageKey(storage_name)
sentry_sdk.set_tag("storage", storage_name)

logger.info("Checking Clickhouse connections")
storage = get_storage(storage_key)
cluster = storage.get_cluster()
check_clickhouse_connections([cluster])

metrics_tags = {
"consumer_group": consumer_group,
"storage": storage_key.value,
Expand Down
7 changes: 5 additions & 2 deletions snuba/migrations/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from snuba.clusters.cluster import (
CLUSTERS,
ClickhouseClientSettings,
ClickhouseCluster,
ClickhouseNode,
UndefinedClickhouseCluster,
)
Expand All @@ -25,13 +26,15 @@
logger = structlog.get_logger().bind(module=__name__)


def check_clickhouse_connections() -> None:
def check_clickhouse_connections(
clusters: Sequence[ClickhouseCluster] = CLUSTERS,
) -> None:
"""
Ensure that we can establish a connection with every cluster.
"""
attempts = 0

for cluster in CLUSTERS:
for cluster in clusters:
clickhouse = cluster.get_query_connection(ClickhouseClientSettings.MIGRATE)

while True:
Expand Down

0 comments on commit cb039ac

Please sign in to comment.