2022-08-01 10:04:33 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you 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 config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
2023-02-26 11:31:49 +08:00
|
|
|
"github.com/cockroachdb/errors"
|
2022-08-01 10:04:33 +08:00
|
|
|
"github.com/spf13/cast"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.uber.org/zap"
|
2023-04-06 19:14:32 +08:00
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
2022-08-01 10:04:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type FileSource struct {
|
|
|
|
sync.RWMutex
|
2023-03-07 18:23:51 +08:00
|
|
|
files []string
|
2022-08-01 10:04:33 +08:00
|
|
|
configs map[string]string
|
2022-11-17 18:59:09 +08:00
|
|
|
|
2023-03-23 19:27:58 +08:00
|
|
|
configRefresher *refresher
|
2022-08-01 10:04:33 +08:00
|
|
|
}
|
|
|
|
|
2022-11-17 18:59:09 +08:00
|
|
|
func NewFileSource(fileInfo *FileInfo) *FileSource {
|
|
|
|
fs := &FileSource{
|
2023-03-07 18:23:51 +08:00
|
|
|
files: fileInfo.Files,
|
2022-11-17 18:59:09 +08:00
|
|
|
configs: make(map[string]string),
|
|
|
|
}
|
|
|
|
fs.configRefresher = newRefresher(fileInfo.RefreshInterval, fs.loadFromFile)
|
2022-08-01 10:04:33 +08:00
|
|
|
return fs
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfigurationByKey implements ConfigSource
|
|
|
|
func (fs *FileSource) GetConfigurationByKey(key string) (string, error) {
|
2022-11-17 18:59:09 +08:00
|
|
|
fs.RLock()
|
2022-08-01 10:04:33 +08:00
|
|
|
v, ok := fs.configs[key]
|
2022-11-17 18:59:09 +08:00
|
|
|
fs.RUnlock()
|
2022-08-01 10:04:33 +08:00
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("key not found: %s", key)
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfigurations implements ConfigSource
|
|
|
|
func (fs *FileSource) GetConfigurations() (map[string]string, error) {
|
|
|
|
configMap := make(map[string]string)
|
2022-11-17 18:59:09 +08:00
|
|
|
|
|
|
|
err := fs.loadFromFile()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-07 18:01:19 +08:00
|
|
|
fs.configRefresher.start(fs.GetSourceName())
|
2022-11-17 18:59:09 +08:00
|
|
|
|
|
|
|
fs.RLock()
|
2022-08-01 10:04:33 +08:00
|
|
|
for k, v := range fs.configs {
|
|
|
|
configMap[k] = v
|
|
|
|
}
|
2022-11-17 18:59:09 +08:00
|
|
|
fs.RUnlock()
|
2022-08-01 10:04:33 +08:00
|
|
|
return configMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPriority implements ConfigSource
|
|
|
|
func (fs *FileSource) GetPriority() int {
|
|
|
|
return LowPriority
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSourceName implements ConfigSource
|
|
|
|
func (fs *FileSource) GetSourceName() string {
|
|
|
|
return "FileSource"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FileSource) Close() {
|
2022-11-17 18:59:09 +08:00
|
|
|
fs.configRefresher.stop()
|
2022-08-01 10:04:33 +08:00
|
|
|
}
|
|
|
|
|
2022-11-17 18:59:09 +08:00
|
|
|
func (fs *FileSource) SetEventHandler(eh EventHandler) {
|
2023-08-24 09:18:24 +08:00
|
|
|
fs.RWMutex.Lock()
|
|
|
|
defer fs.RWMutex.Unlock()
|
2022-11-17 18:59:09 +08:00
|
|
|
fs.configRefresher.eh = eh
|
|
|
|
}
|
2023-04-06 19:14:32 +08:00
|
|
|
|
2023-05-06 17:34:39 +08:00
|
|
|
func (fs *FileSource) UpdateOptions(opts Options) {
|
|
|
|
if opts.FileInfo == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.Lock()
|
|
|
|
defer fs.Unlock()
|
|
|
|
fs.files = opts.FileInfo.Files
|
|
|
|
}
|
|
|
|
|
2022-08-01 10:04:33 +08:00
|
|
|
func (fs *FileSource) loadFromFile() error {
|
|
|
|
yamlReader := viper.New()
|
2023-03-07 18:23:51 +08:00
|
|
|
newConfig := make(map[string]string)
|
2023-05-06 17:34:39 +08:00
|
|
|
var configFiles []string
|
|
|
|
|
|
|
|
fs.RLock()
|
|
|
|
configFiles = fs.files
|
|
|
|
fs.RUnlock()
|
|
|
|
|
|
|
|
for _, configFile := range configFiles {
|
2023-03-07 18:23:51 +08:00
|
|
|
if _, err := os.Stat(configFile); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-08-01 10:04:33 +08:00
|
|
|
|
2023-03-07 18:23:51 +08:00
|
|
|
yamlReader.SetConfigFile(configFile)
|
|
|
|
if err := yamlReader.ReadInConfig(); err != nil {
|
|
|
|
return errors.Wrap(err, "Read config failed: "+configFile)
|
|
|
|
}
|
2022-08-01 10:04:33 +08:00
|
|
|
|
2023-03-07 18:23:51 +08:00
|
|
|
for _, key := range yamlReader.AllKeys() {
|
|
|
|
val := yamlReader.Get(key)
|
|
|
|
str, err := cast.ToStringE(val)
|
|
|
|
if err != nil {
|
|
|
|
switch val := val.(type) {
|
|
|
|
case []any:
|
|
|
|
str = str[:0]
|
|
|
|
for _, v := range val {
|
|
|
|
ss, err := cast.ToStringE(v)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("cast to string failed", zap.Any("value", v))
|
|
|
|
}
|
|
|
|
if str == "" {
|
|
|
|
str = ss
|
|
|
|
} else {
|
|
|
|
str = str + "," + ss
|
|
|
|
}
|
2022-08-01 10:04:33 +08:00
|
|
|
}
|
|
|
|
|
2023-03-07 18:23:51 +08:00
|
|
|
default:
|
|
|
|
log.Warn("val is not a slice", zap.Any("value", val))
|
|
|
|
continue
|
|
|
|
}
|
2022-08-01 10:04:33 +08:00
|
|
|
}
|
2023-03-07 18:23:51 +08:00
|
|
|
newConfig[key] = str
|
|
|
|
newConfig[formatKey(key)] = str
|
2022-08-01 10:04:33 +08:00
|
|
|
}
|
2022-11-17 18:59:09 +08:00
|
|
|
}
|
2023-05-06 17:34:39 +08:00
|
|
|
|
2022-11-17 18:59:09 +08:00
|
|
|
fs.Lock()
|
|
|
|
defer fs.Unlock()
|
|
|
|
err := fs.configRefresher.fireEvents(fs.GetSourceName(), fs.configs, newConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-08-01 10:04:33 +08:00
|
|
|
}
|
2022-11-17 18:59:09 +08:00
|
|
|
fs.configs = newConfig
|
2022-08-01 10:04:33 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|