Skip to content

Commit

Permalink
feat: optimize scheduler dynconfig (#480)
Browse files Browse the repository at this point in the history
* feat: optimize scheduler dynconfig

Signed-off-by: Gaius <gaius.qi@gmail.com>
  • Loading branch information
gaius-qi committed Jul 20, 2021
1 parent 2bbcd97 commit 8efafd8
Show file tree
Hide file tree
Showing 20 changed files with 271 additions and 168 deletions.
9 changes: 9 additions & 0 deletions cmd/dependency/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ func SetupQuitSignalHandler(handler func()) {
}()
}

func GetConfigPath(name string) string {
cfgFile := viper.GetString("config")
if cfgFile != "" {
return cfgFile
}

return filepath.Join(dfpath.DefaultConfigDir, fmt.Sprintf("%s.yaml", name))
}

// initConfig reads in config file and ENV variables if set.
func initConfig(useConfigFile bool, name string, config interface{}) {
// Use config file and read once.
Expand Down
23 changes: 23 additions & 0 deletions internal/dfpath/cache_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2020 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dfpath

import (
"path/filepath"
)

var DefaultCacheDir = filepath.Join(WorkHome, "cache")
19 changes: 19 additions & 0 deletions internal/dfpath/cache_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2020 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dfpath

var DefaultCacheDir = "/var/cache/dragonfly"
4 changes: 4 additions & 0 deletions internal/dfpath/dfpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func init() {
panic(err)
}

if err := fileutils.MkdirAll(DefaultCacheDir); err != nil {
panic(err)
}

if err := fileutils.MkdirAll(LogDir); err != nil {
panic(err)
}
Expand Down
27 changes: 4 additions & 23 deletions internal/dynconfig/dynconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const (
)

type strategy interface {
Unmarshal(rawVal interface{}, opts ...DecoderConfigOption) error
Unmarshal(rawVal interface{}) error
}

type Dynconfig struct {
Expand All @@ -56,10 +56,6 @@ type Option func(d *Dynconfig) error
// WithManagerClient set the manager client
func WithManagerClient(c ManagerClient) Option {
return func(d *Dynconfig) error {
if d.sourceType != ManagerSourceType {
return errors.New("the source type must be ManagerSourceType")
}

d.managerClient = c
return nil
}
Expand All @@ -68,10 +64,6 @@ func WithManagerClient(c ManagerClient) Option {
// WithLocalConfigPath set the file path
func WithLocalConfigPath(p string) Option {
return func(d *Dynconfig) error {
if d.sourceType != LocalSourceType {
return errors.New("the source type must be LocalSourceType")
}

d.localConfigPath = p
return nil
}
Expand All @@ -80,10 +72,6 @@ func WithLocalConfigPath(p string) Option {
// WithCachePath set the cache file path
func WithCachePath(p string) Option {
return func(d *Dynconfig) error {
if d.sourceType != ManagerSourceType {
return errors.New("the source type must be ManagerSourceType")
}

d.cachePath = p
return nil
}
Expand All @@ -92,10 +80,6 @@ func WithCachePath(p string) Option {
// WithExpireTime set the expire time for cache
func WithExpireTime(e time.Duration) Option {
return func(d *Dynconfig) error {
if d.sourceType != ManagerSourceType {
return errors.New("the source type must be ManagerSourceType")
}

d.expire = e
return nil
}
Expand Down Expand Up @@ -168,8 +152,8 @@ func (d *Dynconfig) validate() error {

// Unmarshal unmarshals the config into a Struct. Make sure that the tags
// on the fields of the structure are properly set.
func (d *Dynconfig) Unmarshal(rawVal interface{}, opts ...DecoderConfigOption) error {
return d.strategy.Unmarshal(rawVal, opts...)
func (d *Dynconfig) Unmarshal(rawVal interface{}) error {
return d.strategy.Unmarshal(rawVal)
}

// A DecoderConfigOption can be passed to dynconfig Unmarshal to configure
Expand All @@ -178,7 +162,7 @@ type DecoderConfigOption func(*mapstructure.DecoderConfig)

// defaultDecoderConfig returns default mapstructure.DecoderConfig with support
// of time.Duration values & string slices
func defaultDecoderConfig(output interface{}, opts ...DecoderConfigOption) *mapstructure.DecoderConfig {
func defaultDecoderConfig(output interface{}) *mapstructure.DecoderConfig {
c := &mapstructure.DecoderConfig{
Metadata: nil,
Result: output,
Expand All @@ -188,9 +172,6 @@ func defaultDecoderConfig(output interface{}, opts ...DecoderConfigOption) *maps
mapstructure.StringToSliceHookFunc(","),
),
}
for _, opt := range opts {
opt(c)
}
return c
}

Expand Down
10 changes: 5 additions & 5 deletions internal/dynconfig/dynconfig_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package dynconfig

import (
"encoding/json"
"errors"
"io/ioutil"

"gopkg.in/yaml.v3"
)

type dynconfigLocal struct {
Expand All @@ -37,11 +37,11 @@ func newDynconfigLocal(path string) (*dynconfigLocal, error) {

// Unmarshal unmarshals the config into a Struct. Make sure that the tags
// on the fields of the structure are properly set.
func (d *dynconfigLocal) Unmarshal(rawVal interface{}, opts ...DecoderConfigOption) error {
func (d *dynconfigLocal) Unmarshal(rawVal interface{}) error {
b, err := ioutil.ReadFile(d.filepath)
if err != nil {
return errors.New("can't find the local config data")
return err
}

return json.Unmarshal(b, rawVal)
return yaml.Unmarshal(b, rawVal)
}
4 changes: 2 additions & 2 deletions internal/dynconfig/dynconfig_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func (d *dynconfigManager) get() (interface{}, error) {

// Unmarshal unmarshals the config into a Struct. Make sure that the tags
// on the fields of the structure are properly set.
func (d *dynconfigManager) Unmarshal(rawVal interface{}, opts ...DecoderConfigOption) error {
func (d *dynconfigManager) Unmarshal(rawVal interface{}) error {
dynconfig, err := d.get()
if err != nil {
return errors.New("can't find the cached data")
}

return decode(dynconfig, defaultDecoderConfig(rawVal, opts...))
return decode(dynconfig, defaultDecoderConfig(rawVal))
}

// Load dynamic config from manager
Expand Down
26 changes: 9 additions & 17 deletions scheduler/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,19 @@ func New() *Config {
}

func (c *Config) Validate() error {
if c.Dynconfig.Type == dc.LocalSourceType && c.Dynconfig.Path == "" {
return errors.New("dynconfig is LocalSourceType type requires parameter path")
if c.Dynconfig.CDNDirPath == "" {
if c.Dynconfig.Type == dc.LocalSourceType && c.Dynconfig.Data == nil {
return errors.New("dynconfig is LocalSourceType type requires parameter data")
}
}

if c.Dynconfig.Type == dc.ManagerSourceType {
if c.Dynconfig.ExpireTime == 0 {
return errors.New("dynconfig is ManagerSourceType type requires parameter expireTime")
}

if c.Dynconfig.CachePath == "" {
return errors.New("dynconfig is ManagerSourceType type requires parameter cachePath")
}

if c.Dynconfig.Addr == "" {
return errors.New("dynconfig is ManagerSourceType type requires parameter addr")
if c.Manager.Addr == "" {
return errors.New("dynconfig is ManagerSourceType type requires parameter manager addr")
}
}

Expand Down Expand Up @@ -93,17 +91,11 @@ type DynconfigOptions struct {
// ExpireTime is expire time for manager cache.
ExpireTime time.Duration `yaml:"expireTime" mapstructure:"expireTime"`

// Addr is dynconfig source address.
Addr string `yaml:"addr" mapstructure:"addr"`

// Path is dynconfig filepath.
Path string `yaml:"path" mapstructure:"path"`

// CachePath is cache filepath.
CachePath string `yaml:"cachePath" mapstructure:"cachePath"`

// CDNDirPath is cdn dir.
CDNDirPath string `yaml:"cdnDirPath" mapstructure:"cdnDirPath"`

// Data is dynconfig local data.
Data *DynconfigData `yaml:"data" mapstructure:"data"`
}

type SchedulerConfig struct {
Expand Down
2 changes: 0 additions & 2 deletions scheduler/config/config_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ var config = Config{
Dynconfig: &DynconfigOptions{
Type: dc.LocalSourceType,
ExpireTime: 30000 * 1000 * 1000,
Path: SchedulerDynconfigPath,
CachePath: SchedulerDynconfigCachePath,
},
Server: ServerConfig{
IP: iputils.HostIP,
Expand Down
2 changes: 0 additions & 2 deletions scheduler/config/config_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ var config = Config{
Dynconfig: &DynconfigOptions{
Type: dc.LocalSourceType,
ExpireTime: 30000 * 1000 * 1000,
Path: SchedulerDynconfigPath,
CachePath: SchedulerDynconfigCachePath,
},
Server: ServerConfig{
IP: iputils.HostIP,
Expand Down
3 changes: 0 additions & 3 deletions scheduler/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ func TestSchedulerConfig_Load(t *testing.T) {
config := &Config{
Dynconfig: &DynconfigOptions{
Type: dc.LocalSourceType,
Path: "foo",
CachePath: "bar",
ExpireTime: 1000,
Addr: "127.0.0.1:8002",
CDNDirPath: "tmp",
},
Scheduler: SchedulerConfig{
Expand Down

0 comments on commit 8efafd8

Please sign in to comment.