fix(unified-storage): don't use in-process if db type is not set (#101254)

This commit is contained in:
Jean-Philippe Quéméner
2025-02-24 21:44:42 +01:00
committed by GitHub
parent e482c037bf
commit 9282e4cad4
2 changed files with 117 additions and 8 deletions
+17 -8
View File
@@ -45,14 +45,7 @@ func NewResourceServer(db infraDB.DB, cfg *setting.Cfg,
return nil, err
}
dbCfg := cfg.SectionWithEnvOverrides("database")
// Check in the config if HA is enabled by default we always assume a HA setup.
isHA := dbCfg.Key("high_availability").MustBool(true)
// SQLite is not possible to run in HA, so we set it to false.
databaseType := dbCfg.Key("type").MustString(migrator.SQLite)
if databaseType == migrator.SQLite {
isHA = false
}
isHA := isHighAvailabilityEnabled(cfg.SectionWithEnvOverrides("database"))
store, err := NewBackend(BackendOptions{DBProvider: eDB, Tracer: tracer, IsHA: isHA})
if err != nil {
@@ -70,3 +63,19 @@ func NewResourceServer(db infraDB.DB, cfg *setting.Cfg,
return rs, nil
}
// isHighAvailabilityEnabled determines if high availability mode should
// be enabled based on database configuration. High availability is enabled
// by default except for SQLite databases.
func isHighAvailabilityEnabled(dbCfg *setting.DynamicSection) bool {
// Check in the config if HA is enabled - by default we always assume a HA setup.
isHA := dbCfg.Key("high_availability").MustBool(true)
// SQLite is not possible to run in HA, so we force it to false.
databaseType := dbCfg.Key("type").String()
if databaseType == migrator.SQLite {
isHA = false
}
return isHA
}
+100
View File
@@ -0,0 +1,100 @@
package sql
import (
"strconv"
"testing"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/require"
)
func TestIsHighAvailabilityEnabled(t *testing.T) {
tests := []struct {
name string
dbType string
haConfigValue *bool
isHA bool
}{
{
name: "SQLite should never have HA enabled",
dbType: migrator.SQLite,
haConfigValue: boolPtr(true),
isHA: false,
},
{
name: "MySQL with HA enabled in config should default to true",
dbType: migrator.MySQL,
haConfigValue: boolPtr(true),
isHA: true,
},
{
name: "MySQL with HA disabled in config should default to false",
dbType: migrator.MySQL,
haConfigValue: boolPtr(false),
isHA: false,
},
{
name: "MySQL with no HA config should default to true",
dbType: migrator.MySQL,
haConfigValue: nil,
isHA: true,
},
{
name: "Postgres with HA enabled in config should default to true",
dbType: migrator.Postgres,
haConfigValue: boolPtr(true),
isHA: true,
},
{
name: "Postgres with HA disabled in config should default to false",
dbType: migrator.Postgres,
haConfigValue: boolPtr(false),
isHA: false,
},
{
name: "Postgres with no HA config should default to true",
dbType: migrator.Postgres,
haConfigValue: nil,
isHA: true,
},
{
name: "No database type set should default to true",
dbType: "",
haConfigValue: nil,
isHA: true,
},
{
name: "No database type set with HA enabled in config should default to true",
dbType: "",
haConfigValue: boolPtr(true),
isHA: true,
},
{
name: "No database type set with HA disabled in config should default to false",
dbType: "",
haConfigValue: boolPtr(false),
isHA: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := setting.NewCfg().SectionWithEnvOverrides("database")
if tt.dbType != "" {
cfg.Key("type").SetValue(tt.dbType)
}
if tt.haConfigValue != nil {
cfg.Key("high_availability").SetValue(strconv.FormatBool(*tt.haConfigValue))
}
result := isHighAvailabilityEnabled(cfg)
require.Equal(t, tt.isHA, result)
})
}
}
func boolPtr(b bool) *bool {
return &b
}