fix(unified-storage): check resource_api cfg for isHA (#102283)

This commit is contained in:
Jean-Philippe Quéméner
2025-03-18 12:33:27 +01:00
committed by GitHub
parent 321a886b8b
commit 08994304d8
2 changed files with 110 additions and 61 deletions
+9 -2
View File
@@ -48,7 +48,8 @@ func NewResourceServer(db infraDB.DB, cfg *setting.Cfg,
return nil, err return nil, err
} }
isHA := isHighAvailabilityEnabled(cfg.SectionWithEnvOverrides("database")) isHA := isHighAvailabilityEnabled(cfg.SectionWithEnvOverrides("database"),
cfg.SectionWithEnvOverrides("resource_api"))
withPruner := features.IsEnabledGlobally(featuremgmt.FlagUnifiedStorageHistoryPruner) withPruner := features.IsEnabledGlobally(featuremgmt.FlagUnifiedStorageHistoryPruner)
store, err := NewBackend(BackendOptions{ store, err := NewBackend(BackendOptions{
@@ -79,7 +80,13 @@ func NewResourceServer(db infraDB.DB, cfg *setting.Cfg,
// isHighAvailabilityEnabled determines if high availability mode should // isHighAvailabilityEnabled determines if high availability mode should
// be enabled based on database configuration. High availability is enabled // be enabled based on database configuration. High availability is enabled
// by default except for SQLite databases. // by default except for SQLite databases.
func isHighAvailabilityEnabled(dbCfg *setting.DynamicSection) bool { func isHighAvailabilityEnabled(dbCfg, resourceAPICfg *setting.DynamicSection) bool {
// If the resource API is using a non-SQLite database, we assume it's in HA mode.
resourceDBType := resourceAPICfg.Key("db_type").String()
if resourceDBType != "" && resourceDBType != migrator.SQLite {
return true
}
// Check in the config if HA is enabled - by default we always assume a HA setup. // Check in the config if HA is enabled - by default we always assume a HA setup.
isHA := dbCfg.Key("high_availability").MustBool(true) isHA := dbCfg.Key("high_availability").MustBool(true)
+101 -59
View File
@@ -1,7 +1,6 @@
package sql package sql
import ( import (
"strconv"
"testing" "testing"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator" "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
@@ -11,90 +10,133 @@ import (
func TestIsHighAvailabilityEnabled(t *testing.T) { func TestIsHighAvailabilityEnabled(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
dbType string cfg *setting.Cfg
haConfigValue *bool isHA bool
isHA bool
}{ }{
{ {
name: "SQLite should never have HA enabled", name: "SQLite should never have HA enabled",
dbType: migrator.SQLite, cfg: func() *setting.Cfg {
haConfigValue: boolPtr(true), cfg := setting.NewCfg()
isHA: false, dbSection := cfg.SectionWithEnvOverrides("database")
dbSection.Key("type").SetValue(migrator.SQLite)
dbSection.Key("high_availability").SetValue("true")
return cfg
}(),
isHA: false,
}, },
{ {
name: "MySQL with HA enabled in config should default to true", name: "MySQL with HA enabled in config should default to true",
dbType: migrator.MySQL, cfg: func() *setting.Cfg {
haConfigValue: boolPtr(true), cfg := setting.NewCfg()
isHA: true, dbSection := cfg.SectionWithEnvOverrides("database")
dbSection.Key("type").SetValue(migrator.MySQL)
dbSection.Key("high_availability").SetValue("true")
return cfg
}(),
isHA: true,
}, },
{ {
name: "MySQL with HA disabled in config should default to false", name: "MySQL with HA disabled in config should default to false",
dbType: migrator.MySQL, cfg: func() *setting.Cfg {
haConfigValue: boolPtr(false), cfg := setting.NewCfg()
isHA: false, dbSection := cfg.SectionWithEnvOverrides("database")
dbSection.Key("type").SetValue(migrator.MySQL)
dbSection.Key("high_availability").SetValue("false")
return cfg
}(),
isHA: false,
}, },
{ {
name: "MySQL with no HA config should default to true", name: "MySQL with no HA config should default to true",
dbType: migrator.MySQL, cfg: func() *setting.Cfg {
haConfigValue: nil, cfg := setting.NewCfg()
isHA: true, dbSection := cfg.SectionWithEnvOverrides("database")
dbSection.Key("type").SetValue(migrator.MySQL)
return cfg
}(),
isHA: true,
}, },
{ {
name: "Postgres with HA enabled in config should default to true", name: "Postgres with HA enabled in config should default to true",
dbType: migrator.Postgres, cfg: func() *setting.Cfg {
haConfigValue: boolPtr(true), cfg := setting.NewCfg()
isHA: true, dbSection := cfg.SectionWithEnvOverrides("database")
dbSection.Key("type").SetValue(migrator.Postgres)
dbSection.Key("high_availability").SetValue("true")
return cfg
}(),
isHA: true,
}, },
{ {
name: "Postgres with HA disabled in config should default to false", name: "Postgres with HA disabled in config should default to false",
dbType: migrator.Postgres, cfg: func() *setting.Cfg {
haConfigValue: boolPtr(false), cfg := setting.NewCfg()
isHA: false, dbSection := cfg.SectionWithEnvOverrides("database")
dbSection.Key("type").SetValue(migrator.Postgres)
dbSection.Key("high_availability").SetValue("false")
return cfg
}(),
isHA: false,
}, },
{ {
name: "Postgres with no HA config should default to true", name: "Postgres with no HA config should default to true",
dbType: migrator.Postgres, cfg: func() *setting.Cfg {
haConfigValue: nil, cfg := setting.NewCfg()
isHA: true, dbSection := cfg.SectionWithEnvOverrides("database")
dbSection.Key("type").SetValue(migrator.Postgres)
return cfg
}(),
isHA: true,
}, },
{ {
name: "No database type set should default to true", name: "No database type set should default to true",
dbType: "", cfg: func() *setting.Cfg {
haConfigValue: nil, cfg := setting.NewCfg()
isHA: true, _ = cfg.SectionWithEnvOverrides("database")
return cfg
}(),
isHA: true,
}, },
{ {
name: "No database type set with HA enabled in config should default to true", name: "No database type set with HA enabled in config should default to true",
dbType: "", cfg: func() *setting.Cfg {
haConfigValue: boolPtr(true), cfg := setting.NewCfg()
isHA: true, dbSection := cfg.SectionWithEnvOverrides("database")
dbSection.Key("high_availability").SetValue("true")
return cfg
}(),
isHA: true,
}, },
{ {
name: "No database type set with HA disabled in config should default to false", name: "No database type set with HA disabled in config should default to false",
dbType: "", cfg: func() *setting.Cfg {
haConfigValue: boolPtr(false), cfg := setting.NewCfg()
isHA: false, dbSection := cfg.SectionWithEnvOverrides("database")
dbSection.Key("high_availability").SetValue("false")
return cfg
}(),
isHA: false,
},
{
name: "Resource API with non-SQLite database type should default to true",
cfg: func() *setting.Cfg {
cfg := setting.NewCfg()
dbSection := cfg.SectionWithEnvOverrides("database")
dbSection.Key("type").SetValue(migrator.SQLite)
resourceAPISection := cfg.SectionWithEnvOverrides("resource_api")
resourceAPISection.Key("db_type").SetValue(migrator.Postgres)
return cfg
}(),
isHA: true,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
cfg := setting.NewCfg().SectionWithEnvOverrides("database") result := isHighAvailabilityEnabled(tt.cfg.SectionWithEnvOverrides("database"),
if tt.dbType != "" { tt.cfg.SectionWithEnvOverrides("resource_api"))
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) require.Equal(t, tt.isHA, result)
}) })
} }
} }
func boolPtr(b bool) *bool {
return &b
}