Don't use time.time() or timezone.utcnow() for duration calculations#12353
Conversation
|
(This will likely fail tests at first. I likely messed up some calculations somewhere.) |
|
Should we add note about this approach in https://github.com/apache/airflow/blob/master/CONTRIBUTING.rst#coding-style-and-best-practices ? |
|
The Workflow run is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*. |
77f5299 to
0f5a3a7
Compare
|
Master was failing, I have fixed it and rebased this PR |
Done in a7642459d |
|
One failure |
a764245 to
e03c1c3
Compare
|
That failure was an actual bug -- I had broken rescheduled sensors. Yay for tests! |
|
The PR needs to run all tests because it modifies core of Airflow! Please rebase it to latest master or ask committer to re-run it! |
b9c63a1 to
91d9d67
Compare
|
Hopefully got the tests sorted now (just one last instance of mocking |
91d9d67 to
6eeac20
Compare
|
Hmmmm, even with rebasing Kube tests are failing. I wonder what I broke. |
6eeac20 to
f960ac7
Compare
|
The Workflow run is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*. |
|
The Workflow run is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*. |
|
The Workflow run is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*. |
|
The Workflow run is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*. |
f960ac7 to
f05dd39
Compare
|
The Workflow run is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*. |
b445dee to
a650be6
Compare
`time.time() - start`, or `timezone.utcnow() - start_dttm` will work fine in 99% of cases, but it has one fatal flaw: They both operate on system time, and that can go backwards. While this might be surprising, it can happen -- usually due to clocks being adjusted. And while it is might seem rare, for long running processes it is more common than we might expect. Most of these durations are harmless to get wrong (just being logs) it is better to be safe than sorry. Also the `utcnow()` style I have replaced will be much lighter weight - creating a date time object is a comparatively expensive operation, and computing a diff between two even more so, _especially_ when compared to just subtracting two floats. To make the "common" case easier of wanting to compute a duration for a block, I have made `Stats.timer()` return an object that has a `duration` field.
a650be6 to
5225354
Compare
|
YAY GREEN FINALLY! |
|
@ashb what exactly was the problem with the tests? |
|
When statsd was actually enabled that line was failing, but we don't test with statsd on anywhere outside of the kube tests (thankfully?) -- but to get the error we had to download the kube logs artifact and trawl through the logs until I found the actual error. |
time.time() - start, ortimezone.utcnow() - start_dttmwill workfine in 99% of cases, but it has one fatal flaw:
They both operate on system time, and that can go backwards.
While this might be surprising, it can happen -- usually due to clocks
being adjusted.
And while it is might seem rare, for long running processes it is more
common than we might expect. Most of these durations are harmless to get
wrong (just being logs) it is better to be safe than sorry.
Also the
utcnow()style I have replaced will be much lighter weight -creating a date time object is a comparatively expensive operation, and
computing a diff between two even more so, especially when compared to
just subtracting two floats.
To make the "common" case easier of wanting to compute a duration for a
block, I have made
Stats.timer()return an object that has adurationfield.^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code change, Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in UPDATING.md.