Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sa: truncate all timestamps to seconds #7519

Merged
merged 9 commits into from
Jun 12, 2024
Merged

sa: truncate all timestamps to seconds #7519

merged 9 commits into from
Jun 12, 2024

Conversation

jsha
Copy link
Contributor

@jsha jsha commented May 31, 2024

As described in #7075, go-sql-driver/mysql v1.5.0 truncates timestamps to
microseconds, while v1.6.0 and above does not. That means upon upgrading to
v1.6.0, timestamps are written to the database with a resolution of nanoseconds,
and SELECT statements also use a resolution of nanoseconds. We believe this is
the cause of performance problems we observed when upgrading to v1.6.0 and
above.

To fix that, apply rounding in the application code. Rather than just rounding
to microseconds, round to seconds since that is the resolution we care about.
Using seconds rather than microseconds may also allow some of our indexes to
grow more slowly over time.

Note: this omits truncating some timestamps in CRL shard calculations, since
truncating those resulted in test failures that I'll follow up on separately.

@jsha jsha requested a review from a team as a code owner May 31, 2024 21:06
@jsha jsha requested a review from pgporada May 31, 2024 21:06
jsha added 5 commits June 7, 2024 11:44
As described in #7075, go-sql-driver/mysql v1.5.0 truncates timestamps to
microseconds, while v1.6.0 and above does not. That means upon upgrading to
v1.6.0, timestamps are written to the database with a resolution of nanoseconds,
and SELECT statements also use a resolution of nanoseconds. We believe this is
the cause of performance problems we observed when upgrading to v1.6.0 and
above.

To fix that, apply rounding in the application code. Rather than just rounding
to microseconds, round to seconds since that is the resolution we care about.
Using seconds rather than microseconds may also allow some of our indexes to
grow more slowly over time.
@jsha jsha force-pushed the rounding-time branch from 69ff5b0 to 51b6cdd Compare June 7, 2024 18:53
@jsha
Copy link
Contributor Author

jsha commented Jun 7, 2024

Passing tests and ready for review @pgporada !

@pgporada pgporada requested review from a team and beautifulentropy and removed request for a team June 11, 2024 14:43
Copy link
Contributor

@aarongable aarongable left a comment

Choose a reason for hiding this comment

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

LGTM. I'd love to have a way to test that it stays this way. Maybe something that runs near the end of the integration tests and looks for any higher-precision timestamps, somehow?

@beautifulentropy
Copy link
Member

beautifulentropy commented Jun 11, 2024

LGTM. I'd love to have a way to test that it stays this way. Maybe something that runs near the end of the integration tests and looks for any higher-precision timestamps, somehow?

It's a little funky but we could add a trigger to each of our tables (for each of the datetime fields) that will actually reject datetimes with sub-second precision:

DELIMITER $$

CREATE TRIGGER check_timestamp_before_insert
BEFORE INSERT ON foo
FOR EACH ROW
BEGIN
    IF (MICROSECOND(NEW.barCreatedAt) > 0) THEN
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Fractional seconds are not allowed in foo';
    END IF;
END$$

DELIMITER ;

@pgporada
Copy link
Member

Going the trigger route is going to be fraught with peril because a trigger can only target a single row in a table so we'd need at least 25 separate triggers and then make sure to add/remove triggers as the schema changes. There's also a problem with delimiters in sql-migrate. No matter what I tried I could never get past an error setting the MESSAGE_TEXT = 'whatever' while attempting to create even one trigger just in plain MariaDB.

MariaDB [boulder_sa_test]> CREATE TRIGGER IF NOT EXISTS `checkTS_before_insert_in_expires` BEFORE INSERT ON `authz2` FOR EACH ROW BEGIN IF MICROSECOND(NEW.expires) > 0 THEN
    ->         SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'bad';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 2

@jsha
Copy link
Contributor Author

jsha commented Jun 12, 2024

The trigger idea is very cool! But in addition to Phil's point, I am not positive it will work since our schema sets the microseconds component of those columns to zero, so the DB is rounding them off.

However, I think I can add a setting for "nanoseconds component must be zero" in the arg conversion for borp, which would also get us what we want. I'll do that separately.

@jsha jsha merged commit 2b5b623 into main Jun 12, 2024
12 checks passed
@jsha jsha deleted the rounding-time branch June 12, 2024 22:00
jsha added a commit that referenced this pull request Jun 24, 2024
jsha added a commit that referenced this pull request Jun 27, 2024
This reverts commit 2b5b623.

Following up on #7556, after we made a more systematic change to use
borp's TypeConverter, we no longer need to manually truncate timestamps.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants