Skip to content

Commit

Permalink
feat: add database config and move redis to it (#2338)
Browse files Browse the repository at this point in the history
Signed-off-by: XZ <[email protected]>
  • Loading branch information
fcgxz2003 authored May 11, 2023
1 parent 2260614 commit 2e3db55
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 123 deletions.
96 changes: 68 additions & 28 deletions scheduler/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type Config struct {
// Server configuration.
Server ServerConfig `yaml:"server" mapstructure:"server"`

// Database configuration.
Database DatabaseConfig `yaml:"database" mapstructure:"database"`

// Dynconfig configuration.
DynConfig DynConfig `yaml:"dynConfig" mapstructure:"dynConfig"`

Expand Down Expand Up @@ -112,6 +115,11 @@ type ServerConfig struct {
DataDir string `yaml:"dataDir" mapstructure:"dataDir"`
}

type DatabaseConfig struct {
// Redis configuration.
Redis RedisConfig `yaml:"redis" mapstructure:"redis"`
}

type SchedulerConfig struct {
// Algorithm is scheduling algorithm used by the scheduler.
Algorithm string `yaml:"algorithm" mapstructure:"algorithm"`
Expand Down Expand Up @@ -208,7 +216,7 @@ type JobConfig struct {
// Number of workers in local queue.
LocalWorkerNum uint `yaml:"localWorkerNum" mapstructure:"localWorkerNum"`

// Redis configuration.
// DEPRECATED: Please use the `database.redis` field instead.
Redis RedisConfig `yaml:"redis" mapstructure:"redis"`
}

Expand Down Expand Up @@ -248,6 +256,9 @@ type RedisConfig struct {

// BackendDB is backend database name.
BackendDB int `yaml:"backendDB" mapstructure:"backendDB"`

// NetworkTopologyDB is network topology database name.
NetworkTopologyDB int `yaml:"networkTopologyDB" mapstructure:"networkTopologyDB"`
}

type MetricsConfig struct {
Expand Down Expand Up @@ -302,9 +313,6 @@ type NetworkTopologyConfig struct {
// Enable network topology service, including probe, network topology collection and synchronization service.
Enable bool `yaml:"enable" mapstructure:"enable"`

// SyncInterval is the interval of synchronizing network topology between schedulers.
SyncInterval time.Duration `mapstructure:"syncInterval" yaml:"syncInterval"`

// CollectInterval is the interval of collecting network topology.
CollectInterval time.Duration `mapstructure:"collectInterval" yaml:"collectInterval"`

Expand Down Expand Up @@ -342,6 +350,13 @@ func New() *Config {
AdvertisePort: DefaultServerAdvertisePort,
Host: fqdn.FQDNHostname,
},
Database: DatabaseConfig{
Redis: RedisConfig{
BrokerDB: DefaultRedisBrokerDB,
BackendDB: DefaultRedisBackendDB,
NetworkTopologyDB: DefaultNetworkTopologyDB,
},
},
Scheduler: SchedulerConfig{
Algorithm: DefaultSchedulerAlgorithm,
BackToSourceCount: DefaultSchedulerBackToSourceCount,
Expand Down Expand Up @@ -375,10 +390,6 @@ func New() *Config {
GlobalWorkerNum: DefaultJobGlobalWorkerNum,
SchedulerWorkerNum: DefaultJobSchedulerWorkerNum,
LocalWorkerNum: DefaultJobLocalWorkerNum,
Redis: RedisConfig{
BrokerDB: DefaultJobRedisBrokerDB,
BackendDB: DefaultJobRedisBackendDB,
},
},
Storage: StorageConfig{
MaxSize: DefaultStorageMaxSize,
Expand All @@ -405,7 +416,6 @@ func New() *Config {
},
NetworkTopology: NetworkTopologyConfig{
Enable: true,
SyncInterval: DefaultNetworkTopologySyncInterval,
CollectInterval: DefaultNetworkTopologyCollectInterval,
Probe: ProbeConfig{
QueueLength: DefaultProbeQueueLength,
Expand Down Expand Up @@ -443,6 +453,22 @@ func (cfg *Config) Validate() error {
return errors.New("server requires parameter host")
}

if len(cfg.Database.Redis.Addrs) == 0 {
return errors.New("redis requires parameter addrs")
}

if cfg.Database.Redis.BrokerDB < 0 {
return errors.New("redis requires parameter brokerDB")
}

if cfg.Database.Redis.BackendDB < 0 {
return errors.New("redis requires parameter backendDB")
}

if cfg.Database.Redis.NetworkTopologyDB < 0 {
return errors.New("redis requires parameter networkTopologyDB")
}

if cfg.Scheduler.Algorithm == "" {
return errors.New("scheduler requires parameter algorithm")
}
Expand Down Expand Up @@ -515,18 +541,6 @@ func (cfg *Config) Validate() error {
if cfg.Job.LocalWorkerNum == 0 {
return errors.New("job requires parameter localWorkerNum")
}

if len(cfg.Job.Redis.Addrs) == 0 {
return errors.New("job requires parameter addrs")
}

if cfg.Job.Redis.BrokerDB < 0 {
return errors.New("job requires parameter redis brokerDB")
}

if cfg.Job.Redis.BackendDB < 0 {
return errors.New("job requires parameter redis backendDB")
}
}

if cfg.Storage.MaxSize <= 0 {
Expand Down Expand Up @@ -569,10 +583,6 @@ func (cfg *Config) Validate() error {
}
}

if cfg.NetworkTopology.SyncInterval <= 0 {
return errors.New("networkTopology requires parameter syncInterval")
}

if cfg.NetworkTopology.CollectInterval <= 0 {
return errors.New("networkTopology requires parameter collectInterval")
}
Expand Down Expand Up @@ -613,9 +623,39 @@ func (cfg *Config) Convert() error {
cfg.Scheduler.RetryBackToSourceLimit = cfg.Scheduler.RetryBackSourceLimit
}

// TODO Compatible with deprecated fields host and port.
if len(cfg.Job.Redis.Addrs) == 0 && cfg.Job.Redis.Host != "" && cfg.Job.Redis.Port > 0 {
cfg.Job.Redis.Addrs = []string{fmt.Sprintf("%s:%d", cfg.Job.Redis.Host, cfg.Job.Redis.Port)}
// TODO Compatible with deprecated fields address of redis of job.
if len(cfg.Database.Redis.Addrs) == 0 && len(cfg.Job.Redis.Addrs) != 0 {
cfg.Database.Redis.Addrs = cfg.Job.Redis.Addrs
}

// TODO Compatible with deprecated fields host and port of redis of job.
if len(cfg.Database.Redis.Addrs) == 0 && len(cfg.Job.Redis.Addrs) == 0 && cfg.Job.Redis.Host != "" && cfg.Job.Redis.Port > 0 {
cfg.Database.Redis.Addrs = []string{fmt.Sprintf("%s:%d", cfg.Job.Redis.Host, cfg.Job.Redis.Port)}
}

// TODO Compatible with deprecated fields master name of redis of job.
if cfg.Database.Redis.MasterName == "" && cfg.Job.Redis.MasterName != "" {
cfg.Database.Redis.MasterName = cfg.Job.Redis.MasterName
}

// TODO Compatible with deprecated fields user name of redis of job.
if cfg.Database.Redis.Username == "" && cfg.Job.Redis.Username != "" {
cfg.Database.Redis.Username = cfg.Job.Redis.Username
}

// TODO Compatible with deprecated fields password of redis of job.
if cfg.Database.Redis.Password == "" && cfg.Job.Redis.Password != "" {
cfg.Database.Redis.Password = cfg.Job.Redis.Password
}

// TODO Compatible with deprecated fields broker database of redis of job.
if cfg.Database.Redis.BrokerDB == 0 && cfg.Job.Redis.BrokerDB != 0 {
cfg.Database.Redis.BrokerDB = cfg.Job.Redis.BrokerDB
}

// TODO Compatible with deprecated fields backend database of redis of job.
if cfg.Database.Redis.BackendDB == 0 && cfg.Job.Redis.BackendDB != 0 {
cfg.Database.Redis.BackendDB = cfg.Job.Redis.BackendDB
}

// TODO Compatible with deprecated fields ip.
Expand Down
Loading

0 comments on commit 2e3db55

Please sign in to comment.