From 81784fef07cdeabaf26134547ec7afc4924ad7dc Mon Sep 17 00:00:00 2001 From: Onkar Deshpande Date: Mon, 16 Sep 2024 18:15:46 -0700 Subject: [PATCH] Revert "Specify UTC timezone argument in datetime.now() and fix unit tests (#6279)" This reverts commit 757753486e60b73de0b7a9e8ff2551c0c1a1409a. --- snuba/clickhouse/optimize/optimize.py | 4 ++-- snuba/clickhouse/optimize/optimize_scheduler.py | 10 +++++----- snuba/settings/settings_test.py | 2 +- tests/clickhouse/optimize/test_optimize_scheduler.py | 4 ++-- tests/clickhouse/optimize/test_optimize_tracker.py | 10 +++++----- tests/subscriptions/test_scheduler_consumer.py | 9 +++------ 6 files changed, 18 insertions(+), 21 deletions(-) diff --git a/snuba/clickhouse/optimize/optimize.py b/snuba/clickhouse/optimize/optimize.py index 6efb417a1d..35076d77a0 100644 --- a/snuba/clickhouse/optimize/optimize.py +++ b/snuba/clickhouse/optimize/optimize.py @@ -5,7 +5,7 @@ import time from collections import deque from concurrent.futures import Future, ThreadPoolExecutor -from datetime import datetime, timedelta, timezone +from datetime import datetime, timedelta from typing import Any, Mapping, Optional, Sequence import structlog @@ -347,7 +347,7 @@ def optimize_partitions( """ for partition in partitions: - if cutoff_time is not None and datetime.now(timezone.utc) > cutoff_time: + if cutoff_time is not None and datetime.now() > cutoff_time: logger.info( f"Optimize job is running past provided cutoff time" f" {cutoff_time}. Cancelling.", diff --git a/snuba/clickhouse/optimize/optimize_scheduler.py b/snuba/clickhouse/optimize/optimize_scheduler.py index 9c07326181..9e8c18b90a 100644 --- a/snuba/clickhouse/optimize/optimize_scheduler.py +++ b/snuba/clickhouse/optimize/optimize_scheduler.py @@ -1,6 +1,6 @@ import re from dataclasses import dataclass -from datetime import datetime, timedelta, timezone +from datetime import datetime, timedelta from typing import MutableSequence, Sequence from snuba import settings @@ -42,9 +42,9 @@ class OptimizeScheduler: def __init__(self, default_parallel_threads: int) -> None: self.__default_parallel_threads = default_parallel_threads - self.__last_midnight = ( - datetime.now(timezone.utc) + timedelta(minutes=10) - ).replace(hour=0, minute=0, second=0, microsecond=0) + self.__last_midnight = (datetime.now() + timedelta(minutes=10)).replace( + hour=0, minute=0, second=0, microsecond=0 + ) self.__parallel_start_time = self.__last_midnight + timedelta( hours=settings.PARALLEL_OPTIMIZE_JOB_START_TIME ) @@ -95,7 +95,7 @@ def get_next_schedule(self, partitions: Sequence[str]) -> OptimizationSchedule: reached. """ num_threads = get_num_threads(self.__default_parallel_threads) - current_time = datetime.now(timezone.utc) + current_time = datetime.now() if current_time >= self.__full_job_end_time: raise OptimizedSchedulerTimeout( f"Optimize job cutoff time exceeded " diff --git a/snuba/settings/settings_test.py b/snuba/settings/settings_test.py index 35acec35ae..c59bb3b37e 100644 --- a/snuba/settings/settings_test.py +++ b/snuba/settings/settings_test.py @@ -40,7 +40,7 @@ ENFORCE_RETENTION = True # Ignore optimize job cut off time for tests -OPTIMIZE_JOB_CUTOFF_TIME = 23 +OPTIMIZE_JOB_CUTOFF_TIME = 24 OPTIMIZE_PARALLEL_MAX_JITTER_MINUTES = 0 diff --git a/tests/clickhouse/optimize/test_optimize_scheduler.py b/tests/clickhouse/optimize/test_optimize_scheduler.py index 6375adf290..d68947cef3 100644 --- a/tests/clickhouse/optimize/test_optimize_scheduler.py +++ b/tests/clickhouse/optimize/test_optimize_scheduler.py @@ -1,4 +1,4 @@ -from datetime import datetime, timedelta, timezone +from datetime import datetime, timedelta from typing import Sequence import pytest @@ -132,7 +132,7 @@ def test_subdivide_partitions( ) -last_midnight = (datetime.now(timezone.utc) + timedelta(minutes=10)).replace( +last_midnight = (datetime.now() + timedelta(minutes=10)).replace( hour=0, minute=0, second=0, microsecond=0 ) diff --git a/tests/clickhouse/optimize/test_optimize_tracker.py b/tests/clickhouse/optimize/test_optimize_tracker.py index faf47cad2e..c5796dc19a 100644 --- a/tests/clickhouse/optimize/test_optimize_tracker.py +++ b/tests/clickhouse/optimize/test_optimize_tracker.py @@ -1,6 +1,6 @@ import time import uuid -from datetime import datetime, timedelta, timezone +from datetime import datetime, timedelta from typing import Optional, Set from unittest.mock import call, patch @@ -136,11 +136,11 @@ def write_error_message(writable_storage: WritableTableStorage, time: int) -> No for week in range(0, 4): write_error_message( writable_storage=storage, - time=int((datetime.now(timezone.utc) - timedelta(weeks=week)).timestamp()), + time=int((datetime.now() - timedelta(weeks=week)).timestamp()), ) write_error_message( writable_storage=storage, - time=int((datetime.now(timezone.utc) - timedelta(weeks=week)).timestamp()), + time=int((datetime.now() - timedelta(weeks=week)).timestamp()), ) partitions = optimize.get_partitions_to_optimize( @@ -227,11 +227,11 @@ def write_error_message(writable_storage: WritableTableStorage, time: int) -> No for week in range(0, 4): write_error_message( writable_storage=storage, - time=int((datetime.now(timezone.utc) - timedelta(weeks=week)).timestamp()), + time=int((datetime.now() - timedelta(weeks=week)).timestamp()), ) write_error_message( writable_storage=storage, - time=int((datetime.now(timezone.utc) - timedelta(weeks=week)).timestamp()), + time=int((datetime.now() - timedelta(weeks=week)).timestamp()), ) partitions = optimize.get_partitions_to_optimize( diff --git a/tests/subscriptions/test_scheduler_consumer.py b/tests/subscriptions/test_scheduler_consumer.py index 8432657a4c..ecb7ad21df 100644 --- a/tests/subscriptions/test_scheduler_consumer.py +++ b/tests/subscriptions/test_scheduler_consumer.py @@ -2,7 +2,7 @@ import logging import time import uuid -from datetime import datetime, timedelta, timezone +from datetime import datetime, timedelta from typing import Any, Mapping, Optional from unittest import mock @@ -148,10 +148,7 @@ def test_tick_time_shift() -> None: assert tick.time_shift(timedelta(hours=24).total_seconds()) == Tick( partition, offsets, - Interval( - datetime(1970, 1, 2, tzinfo=timezone.utc).timestamp(), - datetime(1970, 1, 3, tzinfo=timezone.utc).timestamp(), - ), + Interval(datetime(1970, 1, 2).timestamp(), datetime(1970, 1, 3).timestamp()), ) @@ -440,7 +437,7 @@ def test_invalid_commit_log_message(caplog: Any) -> None: "orig_message_ts", ) - now = datetime.now(timezone.utc) + now = datetime.now() def _assignment_callback(offsets: Mapping[Partition, int]) -> None: assert inner_consumer.tell() == {partition: 0}