From 79f1a7a4fdf09479e7b37a8c5f428d6bd246bbe9 Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Wed, 16 Nov 2022 19:29:33 +0100 Subject: [PATCH] Database: Adds support for enable/disable SQLite Write-Ahead Logging (WAL) via configuration (#58268) Adds support for enable/disable SQLite Write-Ahead Logging (WAL) via configuration. Enables SQLite WAL for E2E tests. --- conf/defaults.ini | 5 ++++- conf/sample.ini | 3 +++ docs/sources/setup-grafana/configure-grafana/_index.md | 4 ++++ pkg/services/sqlstore/sqlstore.go | 7 +++++++ scripts/grafana-server/custom.ini | 4 ++++ 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/conf/defaults.ini b/conf/defaults.ini index 8e00ce19af8..023d23a53cb 100644 --- a/conf/defaults.ini +++ b/conf/defaults.ini @@ -134,6 +134,9 @@ path = grafana.db # For "sqlite3" only. cache mode setting used for connecting to the database cache_mode = private +# For "sqlite3" only. Enable/disable Write-Ahead Logging, https://sqlite.org/wal.html. Default is false. +wal = false + # For "mysql" only if migrationLocking feature toggle is set. How many seconds to wait before failing to lock the database for the migrations, default is 0. locking_attempt_timeout_sec = 0 @@ -1147,7 +1150,7 @@ renderer_token = - # which this setting can help protect against by only allowing a certain amount of concurrent requests. concurrent_render_request_limit = 30 # Determines the lifetime of the render key used by the image renderer to access and render Grafana. -# This setting should be expressed as a duration. Examples: 10s (seconds), 5m (minutes), 2h (hours). +# This setting should be expressed as a duration. Examples: 10s (seconds), 5m (minutes), 2h (hours). # Default is 5m. This should be more than enough for most deployments. # Change the value only if image rendering is failing and you see `Failed to get the render key from cache` in Grafana logs. render_key_lifetime = 5m diff --git a/conf/sample.ini b/conf/sample.ini index 6272b628544..aacf119127c 100644 --- a/conf/sample.ini +++ b/conf/sample.ini @@ -136,6 +136,9 @@ # For "sqlite3" only. cache mode setting used for connecting to the database. (private, shared) ;cache_mode = private +# For "sqlite3" only. Enable/disable Write-Ahead Logging, https://sqlite.org/wal.html. Default is false. +;wal = false + # For "mysql" only if migrationLocking feature toggle is set. How many seconds to wait before failing to lock the database for the migrations, default is 0. ;locking_attempt_timeout_sec = 0 diff --git a/docs/sources/setup-grafana/configure-grafana/_index.md b/docs/sources/setup-grafana/configure-grafana/_index.md index d85f3324b27..bf938890362 100644 --- a/docs/sources/setup-grafana/configure-grafana/_index.md +++ b/docs/sources/setup-grafana/configure-grafana/_index.md @@ -380,6 +380,10 @@ will be stored. For "sqlite3" only. [Shared cache](https://www.sqlite.org/sharedcache.html) setting used for connecting to the database. (private, shared) Defaults to `private`. +### wal + +For "sqlite3" only. Setting to enable/disable [Write-Ahead Logging](https://sqlite.org/wal.html). The default value is `false` (disabled). + ### query_retries This setting applies to `sqlite` only and controls the number of times the system retries a query when the database is locked. The default value is `0` (disabled). diff --git a/pkg/services/sqlstore/sqlstore.go b/pkg/services/sqlstore/sqlstore.go index 1e1bb85eb1c..411c30d038b 100644 --- a/pkg/services/sqlstore/sqlstore.go +++ b/pkg/services/sqlstore/sqlstore.go @@ -325,6 +325,11 @@ func (ss *SQLStore) buildConnectionString() (string, error) { } cnnstr = fmt.Sprintf("file:%s?cache=%s&mode=rwc", ss.dbCfg.Path, ss.dbCfg.CacheMode) + + if ss.dbCfg.WALEnabled { + cnnstr += "&_journal_mode=WAL" + } + cnnstr += ss.buildExtraConnectionString('&') default: return "", fmt.Errorf("unknown database type: %s", ss.dbCfg.Type) @@ -453,6 +458,7 @@ func (ss *SQLStore) readConfig() error { ss.dbCfg.IsolationLevel = sec.Key("isolation_level").String() ss.dbCfg.CacheMode = sec.Key("cache_mode").MustString("private") + ss.dbCfg.WALEnabled = sec.Key("wal").MustBool(false) ss.dbCfg.SkipMigrations = sec.Key("skip_migrations").MustBool() ss.dbCfg.MigrationLockAttemptTimeout = sec.Key("locking_attempt_timeout_sec").MustInt() @@ -677,6 +683,7 @@ type DatabaseConfig struct { MaxIdleConn int ConnMaxLifetime int CacheMode string + WALEnabled bool UrlQueryParams map[string][]string SkipMigrations bool MigrationLockAttemptTimeout int diff --git a/scripts/grafana-server/custom.ini b/scripts/grafana-server/custom.ini index 1d2c1bd3fa0..5256269baa3 100644 --- a/scripts/grafana-server/custom.ini +++ b/scripts/grafana-server/custom.ini @@ -1,2 +1,6 @@ [feature_toggles] enable = publicDashboards + +[database] +type=sqlite3 +wal=true