Alarm v2 timestamps mix the application's clock with the database server's
The alarm v2 schema is written on two clocks.
The application writes some of it, always in UTC: the five alarm_state
columns and alarm_notification_outbox.available_at, through
LocalDateTime.now(ZoneOffset.UTC) in AlarmEvaluationService,
AlarmJobConfiguration, AlarmRuleService and AlarmRuleStamper, and
Clock.systemUTC() in AlarmNotificationDispatcher.
The database writes five more, on whatever clock the server runs:
| column | filled by |
|---|---|
alarm_template.updated_at |
DEFAULT CURRENT_TIMESTAMP / ON UPDATE CURRENT_TIMESTAMP |
alarm_template_item.updated_at |
same |
alarm_rule_v2.updated_at |
same, plus an explicit CURRENT_TIMESTAMP in updateRule |
alarm_notification_channel.updated_at |
same |
alarm_history_v2.created_at |
DEFAULT CURRENT_TIMESTAMP |
They are DATETIME, which carries no zone, so the stored value is whichever
wall clock wrote it. Where the server is not on UTC, one event is recorded in
the two sets of columns as two different wall-clock values.
Why it matters
AlarmHistoryV2Dao.deleteOlderThan(threshold, limit) compares created_at
against a threshold its caller supplies. It has no caller yet, so whoever
writes one has to know which clock that column is on. Reaching for the UTC
call the rest of the module uses puts the comparison off by the server's
offset, and the sign of that offset decides whether history is dropped early
or kept long past its retention.
The API gives a client no way to tell either. These reach it as
LocalDateTime with WRITE_DATES_AS_TIMESTAMPS disabled
(common.server.util.json.Jackson), i.e. an ISO string with no offset. A
browser parsing 2026-09-22T04:50:12 reads it as the viewer's local time,
which is correct only when the database server and the viewer happen to agree.
What to change
- Name the column in every statement that writes one of the five tables and
set it to
UTC_TIMESTAMP(). Thirteen statements across five mappers: template 3, template_item 4, rule 3, channel 2, history 1.AlarmHistoryV2Mapper.updateContextneeds nothing -- that table has noupdated_at-- andAlarmRuleV2Mapper.updateRulealready assigns the column, just on the wrong clock. - Decide what the DDL defaults are for. Left as they are, a statement that forgets the column silently falls back to the server clock, which is the same trap one level down. A test asserting that every write statement on these tables names the column would hold the line without an ALTER.
- Carry the offset in the API, by serializing these as an instant or another offset-bearing type, so a client does not have to assume. A client that parses what it is given then needs no change of its own.
Rollout on an existing installation
Where the server already runs on UTC this is a no-op. Elsewhere the existing rows stay on the old clock while new ones are UTC, so two things follow:
- the existing rows need a one-time conversion, bounded by the deployment instant so it does not also shift rows already written in UTC;
- anything comparing one of these columns against a threshold has to switch that expression in the same deployment rather than after it. In a window where the column is UTC and the threshold is not, such a comparison fires far outside its intended bound. Once every writer is explicit, such a comparison is better off taking its threshold from the application clock like the rest of the module, rather than keeping a query of its own.
Left alone
AlarmMessageFormatter stamps notifications with ZonedDateTime.now() and
formats them with a trailing z, so a notification carries local wall-clock
time and names its zone. That is what the person reading it wants.
alarm_state, alarm_notification_outbox and the evaluation sweep are UTC
end to end.
Source: pinpoint-apm/pinpoint