Skip to content

fix(downtime_schedules): rewrite past end on create path#626

Merged
riyazsh merged 3 commits into
mainfrom
riyaz/HAMR-392-downtime-schedule-past-fields
Jul 14, 2026
Merged

fix(downtime_schedules): rewrite past end on create path#626
riyazsh merged 3 commits into
mainfrom
riyaz/HAMR-392-downtime-schedule-past-fields

Conversation

@riyazsh

@riyazsh riyazsh commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Summary

pre_resource_action_hook already rewrites past schedule.start forward on the create path. This extends the create-path handling to schedule.end:

  • Past end → SkipResource. A downtime whose end time has already passed is a closed maintenance window with nothing left to silence. Replicating it either fails with "Downtime cannot be scheduled in the past" or invents a phantom window on the destination that never existed on the source.
  • Past start with future end → bump start, leave end as-is. The customer's intended end time is preserved; the window may shrink.
  • Both future → no-op (existing behavior).

Also

Create-path timestamp comparisons hardened to UTC-aware datetime so .timestamp() is correct on non-UTC hosts. Prior code compared naive utcnow() against tz-aware parse() results; correct on UTC hosts, drifted by TZ offset elsewhere.

Update path (backward-clamp of source to destination stored values) is intentionally out of scope; a follow-up will address the update-path case.

Test plan

  • 8 new unit tests covering: past-start + no end (bumped), past-start + future-end (start bumped, end preserved), past-end + past-start (skipped), past-end + future-start (skipped), both future (untouched), missing/null schedule (no-op), start-only no-end field (bumped, end field not injected), update-path untouched.
  • Full unit suite green apart from 6 pre-existing failures in test_custom_client_trust_env.py (unrelated).
  • Pre-merge review applied (two independent prompts, structural + checklist). Findings addressed across commits.

`pre_resource_action_hook` already rewrites past `schedule.start` forward
on create. This extends the same forward-rewrite to `schedule.end` so
one-off downtimes whose full window is in the past no longer 400 with
"Downtime cannot be scheduled in the past" at POST time.

Rewritten `end` is bumped to `max(now+60s, start+60s)` so the `end > start`
invariant is preserved when `start` is future and `end` is past.

Also hardens the create-path timestamp comparisons to UTC-aware `datetime`
so `.timestamp()` is correct on non-UTC hosts.

Update path (backward-clamp of source to destination stored values) is
intentionally out of scope for this PR.
@riyazsh riyazsh requested a review from a team as a code owner July 14, 2026 15:41
Pre-merge review found that when both `start` and `end` are past, the
prior implementation collapsed the customer's window to 60s. Preserve
the source duration when both fields need rewriting: shifted forward but
same length.

Tests now assert:
- upper-bounded rewrite window (catches units-of-hours-vs-seconds bugs)
- duration preservation for both-past case
- `schedule=None` early-return branch (in addition to `{}`)

@heyronhay heyronhay left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We should never move the end time for a downtime, so we shouldn't do this PR ... I think.

Prior revision shifted `end` forward to preserve source duration. That
invented a new customer-visible maintenance window on the destination
that never existed on the source. Better: past `end` means the downtime
has already closed; skip the resource entirely — an ended maintenance
has nothing left to silence.

For downtimes with past `start` but future `end`, `start` is bumped
forward and `end` is left as-is. The window may shrink but the customer's
intended end time is honored.

Tests updated to cover:
- Past start + future end → start rewritten, end preserved.
- Past end (any start) → SkipResource.
- Missing/null schedule fields → no-op, no crash.
@riyazsh

riyazsh commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

We should never move the end time for a downtime, so we shouldn't do this PR ... I think.

Thanks for the catch. Updated PR.

@riyazsh riyazsh requested a review from heyronhay July 14, 2026 17:15
@riyazsh riyazsh merged commit 10edf81 into main Jul 14, 2026
12 of 13 checks passed
@riyazsh riyazsh deleted the riyaz/HAMR-392-downtime-schedule-past-fields branch July 14, 2026 20:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants