Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Bugfix: avoid overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Sep 28, 2022
1 parent 4bc1431 commit 2841c90
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions synapse/storage/databases/main/event_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,20 @@

logger = logging.getLogger(__name__)

BACKFILL_EVENT_BACKOFF_UPPER_BOUND_SECONDS: int = int(
datetime.timedelta(days=7).total_seconds()
)
BACKFILL_EVENT_EXPONENTIAL_BACKOFF_MAXIMUM_DOUBLING_STEPS = 8
BACKFILL_EVENT_EXPONENTIAL_BACKOFF_STEP_SECONDS: int = int(
datetime.timedelta(hours=1).total_seconds()
)

# The longest backoff period is then:
# _LONGEST_BACKOFF_PERIOD_SECONDS = (
# (1 << BACKFILL_EVENT_EXPONENTIAL_BACKOFF_MAXIMUM_DOUBLING_STEPS)
# * BACKFILL_EVENT_EXPONENTIAL_BACKOFF_STEP_SECONDS,
# )
# which is 2 ** 8 hours = 256 hours ≈ 10 days. This number needs to fit
# in a 32 bit signed int, or else Postgres will error.
# assert _LONGEST_BACKOFF_PERIOD_SECONDS < ((2 ** 31) - 1)
# (We could use a bigint, but bigint overflows still cause errors. This
# at least avoids CASTing in the queries below.)

# All the info we need while iterating the DAG while backfilling
@attr.s(frozen=True, slots=True, auto_attribs=True)
Expand Down Expand Up @@ -803,7 +810,10 @@ def get_backfill_points_in_room_txn(
*/
AND (
failed_backfill_attempt_info.event_id IS NULL
OR ? /* current_time */ >= failed_backfill_attempt_info.last_attempt_ts + {least_function}((1 << failed_backfill_attempt_info.num_attempts) * ? /* step */, ? /* upper bound */)
OR ? /* current_time */ >= failed_backfill_attempt_info.last_attempt_ts + (
(1 << {least_function}(failed_backfill_attempt_info.num_attempts, ? /* max doubling steps */))
* ? /* step */
)
)
/**
* Sort from highest to the lowest depth. Then tie-break on
Expand All @@ -819,8 +829,8 @@ def get_backfill_points_in_room_txn(
room_id,
False,
self._clock.time_msec(),
BACKFILL_EVENT_EXPONENTIAL_BACKOFF_MAXIMUM_DOUBLING_STEPS,
1000 * BACKFILL_EVENT_EXPONENTIAL_BACKOFF_STEP_SECONDS,
1000 * BACKFILL_EVENT_BACKOFF_UPPER_BOUND_SECONDS,
),
)

Expand Down Expand Up @@ -888,7 +898,10 @@ def get_insertion_event_backward_extremities_in_room_txn(
*/
AND (
failed_backfill_attempt_info.event_id IS NULL
OR ? /* current_time */ >= failed_backfill_attempt_info.last_attempt_ts + {least_function}((1 << failed_backfill_attempt_info.num_attempts) * ? /* step */, ? /* upper bound */)
OR ? /* current_time */ >= failed_backfill_attempt_info.last_attempt_ts + (
(1 << {least_function}(failed_backfill_attempt_info.num_attempts, ? /* max doubling steps */))
* ? /* step */
)
)
/**
* Sort from highest to the lowest depth. Then tie-break on
Expand All @@ -904,8 +917,8 @@ def get_insertion_event_backward_extremities_in_room_txn(
(
room_id,
self._clock.time_msec(),
BACKFILL_EVENT_EXPONENTIAL_BACKOFF_MAXIMUM_DOUBLING_STEPS,
1000 * BACKFILL_EVENT_EXPONENTIAL_BACKOFF_STEP_SECONDS,
1000 * BACKFILL_EVENT_BACKOFF_UPPER_BOUND_SECONDS,
),
)
return cast(List[Tuple[str, int]], txn.fetchall())
Expand Down

0 comments on commit 2841c90

Please sign in to comment.