Skip to content

Commit

Permalink
Database: Adds support for enable/disable SQLite Write-Ahead Logging …
Browse files Browse the repository at this point in the history
…(WAL) via configuration (grafana#58268)

Adds support for enable/disable SQLite Write-Ahead Logging (WAL) via configuration.
Enables SQLite WAL for E2E tests.
  • Loading branch information
marefr authored Nov 16, 2022
1 parent df27164 commit 79f1a7a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion conf/defaults.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions conf/sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions docs/sources/setup-grafana/configure-grafana/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
7 changes: 7 additions & 0 deletions pkg/services/sqlstore/sqlstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -677,6 +683,7 @@ type DatabaseConfig struct {
MaxIdleConn int
ConnMaxLifetime int
CacheMode string
WALEnabled bool
UrlQueryParams map[string][]string
SkipMigrations bool
MigrationLockAttemptTimeout int
Expand Down
4 changes: 4 additions & 0 deletions scripts/grafana-server/custom.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
[feature_toggles]
enable = publicDashboards

[database]
type=sqlite3
wal=true

0 comments on commit 79f1a7a

Please sign in to comment.