grafana/pkg/services/ngalert/state
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
..
cache_test.go Alerting: Update state manager to accept reserved labels (#52189) 2022-07-14 15:59:59 -04:00
cache.go Alerting: Delete state from the database on reset (#53919) 2022-08-25 14:12:22 -04:00
manager_private_test.go Alerting: Write and Delete multiple alert instances. (#54072) 2022-09-02 11:17:20 +08:00
manager_test.go Alerting: Write and Delete multiple alert instances. (#54072) 2022-09-02 11:17:20 +08:00
manager.go Alerting: Write and Delete multiple alert instances. (#54072) 2022-09-02 11:17:20 +08:00
state_test.go Alerting: Add a general screenshot service and alerting-specific image service. (#49293) 2022-05-22 22:33:49 +08:00
state.go Alerting: State manager to use InstanceStore (#53852) 2022-08-18 09:40:33 -04:00
template_test.go Chore: Remove context.TODO (#43458) 2021-12-28 10:26:18 +01:00
template.go Chore: Remove context.TODO (#43458) 2021-12-28 10:26:18 +01:00