mirror of
https://github.com/grafana/grafana.git
synced 2024-12-01 21:19:28 -06:00
b476ae62fb
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. These new transactions are off by default, guarded by the feature toggle "alertingBigTransactions" 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.
75 lines
2.9 KiB
Go
75 lines
2.9 KiB
Go
package migrator
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestUpsertMultiple(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
keyCols []string
|
|
updateCols []string
|
|
count int
|
|
expectedErr bool
|
|
expectedPostgresQuery string
|
|
expectedMySQLQuery string
|
|
expectedSQLiteQuery string
|
|
}{
|
|
{
|
|
"upsert one",
|
|
[]string{"key1", "key2"},
|
|
[]string{"key1", "key2", "val1", "val2"},
|
|
1,
|
|
false,
|
|
"INSERT INTO test_table (\"key1\", \"key2\", \"val1\", \"val2\") VALUES ($1, $2, $3, $4) ON CONFLICT (\"key1\", \"key2\") DO UPDATE SET \"key1\"=EXCLUDED.\"key1\", \"key2\"=EXCLUDED.\"key2\", \"val1\"=EXCLUDED.\"val1\", \"val2\"=EXCLUDED.\"val2\";",
|
|
"INSERT INTO test_table (`key1`, `key2`, `val1`, `val2`) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE `key1`=VALUES(`key1`), `key2`=VALUES(`key2`), `val1`=VALUES(`val1`), `val2`=VALUES(`val2`)",
|
|
"INSERT INTO test_table (`key1`, `key2`, `val1`, `val2`) VALUES (?, ?, ?, ?) ON CONFLICT(`key1`, `key2`) DO UPDATE SET `key1`=excluded.`key1`, `key2`=excluded.`key2`, `val1`=excluded.`val1`, `val2`=excluded.`val2`",
|
|
},
|
|
{
|
|
"upsert two",
|
|
[]string{"key1", "key2"},
|
|
[]string{"key1", "key2", "val1", "val2"},
|
|
2,
|
|
false,
|
|
"INSERT INTO test_table (\"key1\", \"key2\", \"val1\", \"val2\") VALUES ($1, $2, $3, $4), ($5, $6, $7, $8) ON CONFLICT (\"key1\", \"key2\") DO UPDATE SET \"key1\"=EXCLUDED.\"key1\", \"key2\"=EXCLUDED.\"key2\", \"val1\"=EXCLUDED.\"val1\", \"val2\"=EXCLUDED.\"val2\";",
|
|
"INSERT INTO test_table (`key1`, `key2`, `val1`, `val2`) VALUES (?, ?, ?, ?), (?, ?, ?, ?) ON DUPLICATE KEY UPDATE `key1`=VALUES(`key1`), `key2`=VALUES(`key2`), `val1`=VALUES(`val1`), `val2`=VALUES(`val2`)",
|
|
"INSERT INTO test_table (`key1`, `key2`, `val1`, `val2`) VALUES (?, ?, ?, ?), (?, ?, ?, ?) ON CONFLICT(`key1`, `key2`) DO UPDATE SET `key1`=excluded.`key1`, `key2`=excluded.`key2`, `val1`=excluded.`val1`, `val2`=excluded.`val2`",
|
|
},
|
|
{
|
|
"count error",
|
|
[]string{"key1", "key2"},
|
|
[]string{"key1", "key2", "val1", "val2"},
|
|
0,
|
|
true,
|
|
"",
|
|
"",
|
|
"",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var db Dialect
|
|
db = &PostgresDialect{}
|
|
q, err := db.UpsertMultipleSQL("test_table", tc.keyCols, tc.updateCols, tc.count)
|
|
|
|
require.True(t, (err != nil) == tc.expectedErr)
|
|
require.Equal(t, tc.expectedPostgresQuery, q, "Postgres query incorrect")
|
|
|
|
db = &MySQLDialect{}
|
|
q, err = db.UpsertMultipleSQL("test_table", tc.keyCols, tc.updateCols, tc.count)
|
|
|
|
require.True(t, (err != nil) == tc.expectedErr)
|
|
require.Equal(t, tc.expectedMySQLQuery, q, "MySQL query incorrect")
|
|
|
|
db = &SQLite3{}
|
|
q, err = db.UpsertMultipleSQL("test_table", tc.keyCols, tc.updateCols, tc.count)
|
|
|
|
require.True(t, (err != nil) == tc.expectedErr)
|
|
require.Equal(t, tc.expectedSQLiteQuery, q, "SQLite query incorrect")
|
|
})
|
|
}
|
|
}
|