grafana/pkg/services/sqlstore/migrator
Joe Blubaugh 5e4fd94413
Alerting: Write and Delete multiple alert instances. (#54072)
Prior to this change, all alert instance writes and deletes happened
individually, in their own database transaction. This change batches up
writes or deletes for a given rule's evaluation loop into a single
transaction before applying it.

Before:
```
goos: darwin
goarch: arm64
pkg: github.com/grafana/grafana/pkg/services/ngalert/store
BenchmarkAlertInstanceOperations-8           398           2991381 ns/op         1133537 B/op      27703 allocs/op
--- BENCH: BenchmarkAlertInstanceOperations-8
    util.go:127: alert definition: {orgID: 1, UID: FovKXiRVzm} with title: "an alert definition FTvFXmRVkz" interval: 60 created
    util.go:127: alert definition: {orgID: 1, UID: foDFXmRVkm} with title: "an alert definition fovFXmRVkz" interval: 60 created
    util.go:127: alert definition: {orgID: 1, UID: VQvFuigVkm} with title: "an alert definition VwDKXmR4kz" interval: 60 created
PASS
ok      github.com/grafana/grafana/pkg/services/ngalert/store   1.619s
```

After:
```
goos: darwin
goarch: arm64
pkg: github.com/grafana/grafana/pkg/services/ngalert/store
BenchmarkAlertInstanceOperations-8          1440            816484 ns/op          352297 B/op       6529 allocs/op
--- BENCH: BenchmarkAlertInstanceOperations-8
    util.go:127: alert definition: {orgID: 1, UID: 302r_igVzm} with title: "an alert definition q0h9lmR4zz" interval: 60 created
    util.go:127: alert definition: {orgID: 1, UID: 71hrlmR4km} with title: "an alert definition nJ29_mR4zz" interval: 60 created
    util.go:127: alert definition: {orgID: 1, UID: Cahr_mR4zm} with title: "an alert definition ja2rlmg4zz" interval: 60 created
PASS
ok      github.com/grafana/grafana/pkg/services/ngalert/store   1.383s
```

So we cut time by about 75% and memory allocations by about 60% when
storing and deleting 100 instances.

This change also updates some of our tests so that they run successfully against postgreSQL - we were using random Int64s, but postgres integers, which our tables use, max out at 2^31-1
2022-09-02 11:17:20 +08:00
..
column.go move database-specific code into dialects (#11884) 2018-05-10 16:54:21 +02:00
conditions.go Chore: Fix SQL related Go variable naming (#28887) 2020-11-11 06:21:08 +01:00
dialect.go SQLStore: Support Upserting multiple rows. (#52228) 2022-07-19 16:42:48 +08:00
migrations.go SQLStore: Add deprecation comments for breaking migrations (#49740) 2022-06-03 17:42:08 +02:00
migrator.go Chore: Remove Wrapf (#50128) 2022-06-06 22:30:31 +02:00
mysql_dialect.go SQLStore: Support Upserting multiple rows. (#52228) 2022-07-19 16:42:48 +08:00
postgres_dialect.go Alerting: Write and Delete multiple alert instances. (#54072) 2022-09-02 11:17:20 +08:00
sqlite_dialect.go SQLStore: Support Upserting multiple rows. (#52228) 2022-07-19 16:42:48 +08:00
types.go Alerting: modify DB table, accessors and migration to restrict org access (#37414) 2021-08-12 16:04:09 +03:00
upsert_test.go Alerting: Write and Delete multiple alert instances. (#54072) 2022-09-02 11:17:20 +08:00