Commit Graph

32 Commits

Author SHA1 Message Date
Phil Porada 0fc9de63ee
SA: Enforce microsecond granularity for long_query_time and max_statement_time (#7224)
In MariaDB, `long_query_time`[1] and `max_statement_time`[2] have up to
microsecond granularity (6 digits to the right of the decimal).

Fixes an issue detected by proxysql in staging.
```
MySQL_Session.cpp:6567:handler___status_WAITING_CLIENT_DATA___STATE_SLEEP___MYSQL_COM_QUERY_qpo(): [ERROR] Unable to parse query. If correct, report it as a bug: SET long_query_time=3.9200000000000004
```

1. https://mariadb.com/kb/en/server-system-variables/#long_query_time
2. https://mariadb.com/kb/en/server-system-variables/#max_statement_time

---------

Co-authored-by: Aaron Gable <aaron@letsencrypt.org>
2023-12-21 13:06:30 -05:00
Jacob Hoffman-Andrews 7d66d67054
It's borpin' time! (#6982)
This change replaces [gorp] with [borp].

The changes consist of a mass renaming of the import and comments / doc
fixups, plus modifications of many call sites to provide a
context.Context everywhere, since gorp newly requires this (this was one
of the motivating factors for the borp fork).

This also refactors `github.com/letsencrypt/boulder/db.WrappedMap` and
`github.com/letsencrypt/boulder/db.Transaction` to not embed their
underlying gorp/borp objects, but to have them as plain fields. This
ensures that we can only call methods on them that are specifically
implemented in `github.com/letsencrypt/boulder/db`, so we don't miss
wrapping any. This required introducing a `NewWrappedMap` method along
with accessors `SQLDb()` and `BorpDB()` to get at the internal fields
during metrics and logging setup.

Fixes #6944
2023-07-17 14:38:29 -07:00
Jacob Hoffman-Andrews 824417f6c0
sa: refactor db initialization (#6930)
Previously, we had three chained calls initializing a database:

 - InitWrappedDb calls NewDbMap
 - NewDbMap calls NewDbMapFromConfig

Since all three are exporetd, this left me wondering when to call one vs
the others.

It turns out that NewDbMap is only called from tests, so I renamed it to
DBMapForTest to make that clear.

NewDbMapFromConfig is only called internally to the SA, so I made it
unexported it as newDbMapFromMysqlConfig.

Also, I copied the ParseDSN call into InitWrappedDb, so it doesn't need
to call DBMapForTest. Now InitWrappedDb and DBMapForTest both
independently call newDbMapFromMysqlConfig.

I also noticed that InitDBMetrics was only called internally so I
unexported it.
2023-06-13 10:15:40 -07:00
Samantha e72a8f9cac
docker: Update proxysql container to match production (#6914) 2023-05-31 11:31:10 -04:00
Phil Porada 939a14544c
SA: Check MariaDB system variables at startup (#6791)
Adds a new function to the `//sa` to ensure that a MariaDB config passed
in via SA `setDefault` or via DSN perform the following validations:
1. Correct quoting for strings and string enums to prevent future
problems such as PR #6683 from occurring.
2. Each system variable we care to use is scoped as SESSION, rather than
strictly GLOBAL.
3. Detect system variables passed in that are not in a curated list of
variables we care about.
4. Validate that values for booleans, floats, integers, and strings at
least pass basic a regex.

This change is in a bit of a weird place. The ideal place for this
change would be `go-sql-driver/mysql`, but since that driver handles the
general case of MySQL-compatible connections to the database, we're
implementing this validation in Boulder instead. We're confident about
the specific versions of MariaDB running in staging/prod and that the
database vendor won't change underneath us, which is why I decided to
take this approach. However, this change will bind us tighter to MariaDB
than MySQL due to the specific variables we're checking. An up-to-date
list of MariaDB system variables can be found
[here.](https://mariadb.com/kb/en/server-system-variables/)

Fixes https://github.com/letsencrypt/boulder/issues/6687.
2023-04-18 11:02:33 -04:00
Samantha 511f5b79f1
test: Add ProxySQL to our Docker development stack (#6754)
Add an upstream ProxySQL container to our docker-compose. Configure
ProxySQL to manage database connections for our unit and integration
tests.

Fixes #5873
2023-03-29 18:41:24 -04:00
Jacob Hoffman-Andrews 8fd5861c1f
sa: quote sql_mode (#6683)
When sql_mode is set as part of a multi-variable SET command (which
happens in go-sql-driver/mysql 1.6.0+), ProxySQL can mis-parse parts of
the SET command that come after it. For instance, if we run:

SET sql_mode=STRICT_ALL_TABLES,log_queries_not_using_indexes=ON;

Then ProxySQL would mis-parse that and pass along to its upstream:

SET sql_mode=STRICT_ALL_TABLES,log_queries_not_using_indexes;

Adding quotes around sql_mode (a string-valued variables) causes
ProxySQL to parse this correctly.
2023-02-22 16:30:04 -05:00
Samantha 76b2ec0702
SA: Standardize methods which use COUNT queries (#6505)
- Replace `-1` in return values with `0`. No callers were depending on
`-1`.
- Replace `count(` with `COUNT(` for the sake of readability.
- Replace `COUNT(1)` with `COUNT(*)` (https://mariadb.com/kb/en/count).
Both
  versions provide identical outputs but let's standardize on the docs.

Fixes #6494
2022-11-14 18:10:32 -08:00
Jacob Hoffman-Andrews ee1afbb988
SA: Enable overriding max_statement_time in DSN (#6492)
Also sql_mode and long_query_time.
2022-11-07 15:42:58 -08:00
Samantha e0510056cc
Enhancements to SQL driver tuning via JSON config (#5235)
Historically the only database/sql driver setting exposed via JSON
config was maxDBConns. This change adds support for maxIdleConns,
connMaxLifetime, connMaxIdleTime, and renames maxDBConns to
maxOpenConns. The addition of these settings will give our SRE team a
convenient method for tuning the reuse/closure of database connections.

A new struct, DBSettings, has been added to SA. The struct, and each of
it's fields has been commented.

All new fields have been plumbed through to the relevant Boulder
components and exported as Prometheus metrics. Tests have been
added/modified to ensure that the fields are being set. There should be
no loss in coverage

Deployability concerns for the migration from maxDBConns to maxOpenConns
have been addressed with the temporary addition of the helper method
cmd.DBConfig.GetMaxOpenConns(). This method can be removed once
test/config is defaulted to using maxOpenConns. Relevant sections of the
code have TODOs added that link back to an newly opened issue.

Fixes #5199
2021-01-25 15:34:55 -08:00
Jacob Hoffman-Andrews 56d581613c
Update test/config. (#4923)
This copies over a number of features flags and other settings from
test/config-next that have been applied in prod.

Also, remove the config-next gate on various tests.
2020-07-01 17:59:14 -07:00
Daniel McCarney fc15f2f4cd
SA: add unit test for auto_increment schemas. (#4586)
* SA: add unit test for auto_increment schemas.

`TestAutoIncrementSchema` uses a root user connection to the
`information_schema` MariaDB database to try and find table columns from
the Boulder schemas that are both `auto_increment` and not `int64`.

* SA: rename _db-next RemoveOCSPResponses.sql migration.

Based on the order that we apply migrations the
`RemoveOCSPResponses.sql` migration with its old prefix
(`20181101105733`) was never being applied. That in turn caused the new
`TestAutoIncrementSchema` unit test to fail because the old
`ocspResponses` table has an `id` field that is `auto_increment` but
`sized `int(11)`.

Renaming the migration with a newer prefix solves the problem. The
`ocspResponses` table ends up dropped when `config-next` is used.
Afterwards the `TestAutoIncrementSchema` unit test passes again.
2019-12-02 11:33:36 -05:00
Roland Bracewell Shoemaker a44f346f88 SA: remove old authorization tables from the schema (#4538) 2019-11-11 16:17:16 -05:00
Jacob Hoffman-Andrews 9fda3fb77d Switch to DSNs (#4044)
* Switch to DSNs

We used to use "mysql+tcp://" URLs but we don't need those anymore,
and there aren't any more of them in prod.

* Fix test.
2019-02-11 10:46:07 -08:00
Jeff Hodges b88750ede0 remove prefixdb and replace with mysql driver params (#2871)
This uses the mysql driver library's capability to use `SET` to set the system
variables that prefixdb previously was.

Unfortunately, the library doesn't sort the params when making the string, so we
have to do a little munging to TestNewDbMap.

Ran it in a checkout of the repo since godeps now doesn't include the test files (which is great!).

```
MYSQL_TEST_ADDR=127.0.0.1:3306 go test .
ok  	github.com/go-sql-driver/mysql	46.099s
```
2017-07-17 16:40:56 -07:00
Jacob Hoffman-Andrews d47d3c5066 Recycle pending authorizations (#2797)
If the feature flag "ReusePendingAuthz" is enabled, a request to create a new authorization object from an account that already has a pending authorization object for the same identifier will return the already-existing authorization object. This should make it less common for people to get stuck in the "too many pending authorizations" state, and reduce DB storage growth.

Fixes #2768
2017-06-19 13:35:36 -04:00
Jacob Hoffman-Andrews dcbe7e0895 Increase readTimeout in TestTimeouts. (#2671)
Should decrease flakiness in this test. Fixes #2564
2017-04-09 15:32:02 -04:00
Jacob Hoffman-Andrews 5f1919b40f Use max_statement_time for timeouts. (#2263)
This means that we get a more useful log message for slow queries, and don't
need to close the MySQL connection. It also means that the query is actually
killed on the MySQL side, rather than just timing out and returning on the
client side.

We set the max_statement_time to 95% of the `readTimeout`.

Also set the `long_query_time` to 80% of the `readTimeout`, so that queries that are
close to timing out will be logged by MySQL's slow query logging.
2016-10-20 10:29:29 -04:00
Jacob Hoffman-Andrews 19da2be7ef Set STRICT_ALL_TABLES when setting up DB (#1890)
Fixes #1202.
2016-06-06 13:48:16 -07:00
Roland Bracewell Shoemaker 54573b36ba Remove all stray copyright headers and appends the initial line to LICENSE.txt (#1853) 2016-05-31 12:32:04 -07:00
Jacob Hoffman-Andrews b3bc3d8e41 Add a MaxDBConns config parameter. (#1793) 2016-05-09 14:21:15 -07:00
Igor Bukanov 7b14242079 always use boulder-mysql, not localhost, for mysql
This followup for #1639 to always use the boulder-mysql, not localhost, when connecting to mysql database when testing.
2016-04-07 10:19:24 +02:00
Kane York 21700ffec5 Improve mocks.Mailer to check To: line 2016-03-14 17:08:44 -07:00
Kane York f440380330 sa: use mysql.Config to set flags on connect strings
This also exposes an interface to open a connection using a
mysql.Config, so we can start changing config files to use those.

Part of #1505
2016-02-29 15:38:43 -08:00
Jeff Hodges 5e97aa23c6 use MariaDB in the unit tests
And delete the uses of sqlite3
2015-08-14 17:13:15 -07:00
Roland Shoemaker df179a1dde Review fixes 2015-07-29 12:17:04 -07:00
Roland Shoemaker 3a5f96c9e1 Remove debug statement 2015-07-28 10:33:26 -07:00
Roland Shoemaker 33a79c3910 Pull out previous parseTime check 2015-07-27 17:57:14 -07:00
Jeff Hodges 79d9b52ad0 rename dbName to dbConnect
The configuration is actually a connection string, not a database name,
and it was a bit confusing.
2015-07-10 16:17:27 -07:00
Richard Barnes 9bd9ed08da Change from adding the parameter to enforcing it 2015-06-23 23:18:45 -07:00
J.C. Jones ef0e7fd3f6 New tests
- Write a database.go test
 - Start writing a type-converter.go test
2015-06-11 17:29:38 -05:00
J.C. Jones af0f8446eb Issue #202, Periodic OCSP Signer Tool
- Move dbMap construction and type converter into individual files in the sa package.
- Add DB configuration for the OCSP tool to the boulder config:
  - left to the user if they want to use different boulder-config.json files
    for different purposes.
- Added updater to Makefile
- Fix trailing ',' in the Boulder config, add more panic logging
- Ignore .pem files produced by the integration test
- Change RPC to use per-instance named reply-to queues.
- Finish OCSP Updater logic
- Rework RPC for OCSP to use a transfer object (due to serialization problems of x509.Certificate)
2015-05-27 22:01:29 -07:00