2020-11-16 21:10:43 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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 paramtable
|
|
|
|
|
|
|
|
import (
|
2020-11-21 15:06:46 +08:00
|
|
|
"os"
|
2020-11-16 21:10:43 +08:00
|
|
|
"path"
|
|
|
|
"runtime"
|
2020-12-08 14:41:04 +08:00
|
|
|
"strconv"
|
2020-11-16 21:10:43 +08:00
|
|
|
"strings"
|
2022-08-01 10:04:33 +08:00
|
|
|
"time"
|
2020-11-16 21:10:43 +08:00
|
|
|
|
2022-08-01 10:04:33 +08:00
|
|
|
config "github.com/milvus-io/milvus/internal/config"
|
2021-09-11 14:40:01 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
2022-11-30 18:23:15 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/etcd"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
2022-06-16 17:28:11 +08:00
|
|
|
"go.uber.org/zap"
|
2020-11-16 21:10:43 +08:00
|
|
|
)
|
|
|
|
|
2021-10-26 22:35:05 +08:00
|
|
|
// UniqueID is type alias of typeutil.UniqueID
|
2020-12-08 14:41:04 +08:00
|
|
|
type UniqueID = typeutil.UniqueID
|
|
|
|
|
2022-03-02 18:51:55 +08:00
|
|
|
const (
|
2022-05-03 08:39:49 +08:00
|
|
|
DefaultMilvusYaml = "milvus.yaml"
|
|
|
|
DefaultEasyloggingYaml = "easylogging.yaml"
|
2022-03-02 18:51:55 +08:00
|
|
|
DefaultMinioHost = "localhost"
|
|
|
|
DefaultMinioPort = "9000"
|
|
|
|
DefaultMinioAccessKey = "minioadmin"
|
|
|
|
DefaultMinioSecretAccessKey = "minioadmin"
|
|
|
|
DefaultMinioUseSSL = "false"
|
|
|
|
DefaultMinioBucketName = "a-bucket"
|
2022-06-02 19:42:03 +08:00
|
|
|
DefaultMinioUseIAM = "false"
|
2022-11-01 11:07:35 +08:00
|
|
|
DefaultMinioCloudProvider = "aws"
|
2022-06-02 19:42:03 +08:00
|
|
|
DefaultMinioIAMEndpoint = ""
|
2022-03-02 18:51:55 +08:00
|
|
|
DefaultEtcdEndpoints = "localhost:2379"
|
2022-09-23 09:50:52 +08:00
|
|
|
|
|
|
|
DefaultLogFormat = "text"
|
|
|
|
DefaultLogLevelForBase = "debug"
|
|
|
|
DefaultRootPath = ""
|
2022-03-02 18:51:55 +08:00
|
|
|
)
|
2021-11-10 14:27:37 +08:00
|
|
|
|
2022-12-09 14:31:21 +08:00
|
|
|
//Const of Global Config List
|
|
|
|
func globalConfigPrefixs() []string {
|
|
|
|
return []string{"metastore.", "localStorage.", "etcd.", "mysql.", "minio.", "pulsar.", "kafka.", "rocksmq.", "log.", "grpc.", "common.", "quotaAndLimits."}
|
|
|
|
}
|
|
|
|
|
2022-05-03 08:39:49 +08:00
|
|
|
var defaultYaml = DefaultMilvusYaml
|
2022-03-10 10:35:59 +08:00
|
|
|
|
2022-01-05 13:56:05 +08:00
|
|
|
// BaseTable the basics of paramtable
|
2020-11-16 21:10:43 +08:00
|
|
|
type BaseTable struct {
|
2022-12-16 15:59:23 +08:00
|
|
|
mgr *config.Manager
|
2022-08-01 10:04:33 +08:00
|
|
|
|
2021-09-11 14:40:01 +08:00
|
|
|
configDir string
|
2022-12-16 15:59:23 +08:00
|
|
|
YamlFile string
|
2021-09-26 21:18:11 +08:00
|
|
|
|
2022-11-08 14:33:03 +08:00
|
|
|
Log log.Config
|
2020-11-16 21:10:43 +08:00
|
|
|
}
|
|
|
|
|
2022-10-12 11:37:23 +08:00
|
|
|
// NewBaseTableFromYamlOnly only used in migration tool.
|
|
|
|
// Maybe we shouldn't limit the configDir internally.
|
|
|
|
func NewBaseTableFromYamlOnly(yaml string) *BaseTable {
|
2022-11-17 18:59:09 +08:00
|
|
|
mgr, _ := config.Init(config.WithFilesSource(&config.FileInfo{
|
|
|
|
Filepath: yaml,
|
|
|
|
RefreshInterval: 10 * time.Second,
|
|
|
|
}))
|
2022-10-12 11:37:23 +08:00
|
|
|
gp := &BaseTable{mgr: mgr, YamlFile: yaml}
|
|
|
|
return gp
|
|
|
|
}
|
|
|
|
|
2022-03-10 10:35:59 +08:00
|
|
|
// Init initializes the param table.
|
2022-12-16 15:59:23 +08:00
|
|
|
// if refreshInterval greater than 0 will auto refresh config from source
|
|
|
|
func (gp *BaseTable) Init(refreshInterval int) {
|
2022-08-01 10:04:33 +08:00
|
|
|
formatter := func(key string) string {
|
|
|
|
ret := strings.ToLower(key)
|
|
|
|
ret = strings.TrimPrefix(ret, "milvus.")
|
|
|
|
ret = strings.ReplaceAll(ret, "/", "")
|
|
|
|
ret = strings.ReplaceAll(ret, "_", "")
|
|
|
|
ret = strings.ReplaceAll(ret, ".", "")
|
|
|
|
return ret
|
|
|
|
}
|
2022-09-23 09:50:52 +08:00
|
|
|
if gp.YamlFile == "" {
|
2022-12-16 15:59:23 +08:00
|
|
|
gp.YamlFile = DefaultMilvusYaml
|
2022-09-23 09:50:52 +08:00
|
|
|
}
|
2022-08-02 21:26:33 +08:00
|
|
|
var err error
|
2022-08-01 10:04:33 +08:00
|
|
|
gp.mgr, err = config.Init(config.WithEnvSource(formatter))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-12-16 15:59:23 +08:00
|
|
|
gp.initConfigsFromLocal(refreshInterval)
|
|
|
|
gp.initConfigsFromRemote(refreshInterval)
|
|
|
|
gp.InitLogCfg()
|
|
|
|
}
|
2022-08-01 10:04:33 +08:00
|
|
|
|
2022-12-16 15:59:23 +08:00
|
|
|
func (gp *BaseTable) initConfigsFromLocal(refreshInterval int) {
|
2021-09-26 19:46:17 +08:00
|
|
|
gp.configDir = gp.initConfPath()
|
2022-09-23 09:50:52 +08:00
|
|
|
configFilePath := gp.configDir + "/" + gp.YamlFile
|
2022-12-16 15:59:23 +08:00
|
|
|
err := gp.mgr.AddSource(config.NewFileSource(&config.FileInfo{
|
|
|
|
Filepath: configFilePath,
|
|
|
|
RefreshInterval: time.Duration(refreshInterval) * time.Second,
|
|
|
|
}))
|
2022-08-01 10:04:33 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("init baseTable with file failed", zap.String("configFile", configFilePath), zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
2022-08-02 21:26:33 +08:00
|
|
|
}
|
2022-08-01 10:04:33 +08:00
|
|
|
|
2022-12-16 15:59:23 +08:00
|
|
|
func (gp *BaseTable) initConfigsFromRemote(refreshInterval int) {
|
2022-11-30 18:23:15 +08:00
|
|
|
etcdConfig := EtcdConfig{}
|
|
|
|
etcdConfig.Init(gp)
|
2022-12-07 18:01:19 +08:00
|
|
|
etcdConfig.Endpoints.PanicIfEmpty = false
|
|
|
|
etcdConfig.RootPath.PanicIfEmpty = false
|
2022-11-30 18:23:15 +08:00
|
|
|
if etcdConfig.Endpoints.GetValue() == "" {
|
2022-08-01 10:04:33 +08:00
|
|
|
return
|
|
|
|
}
|
2022-11-30 18:23:15 +08:00
|
|
|
if etcdConfig.UseEmbedEtcd.GetAsBool() && !etcd.HasServer() {
|
2022-08-01 10:04:33 +08:00
|
|
|
return
|
|
|
|
}
|
2022-11-30 18:23:15 +08:00
|
|
|
info := &config.EtcdInfo{
|
|
|
|
UseEmbed: etcdConfig.UseEmbedEtcd.GetAsBool(),
|
|
|
|
UseSSL: etcdConfig.EtcdUseSSL.GetAsBool(),
|
|
|
|
Endpoints: etcdConfig.Endpoints.GetAsStrings(),
|
|
|
|
CertFile: etcdConfig.EtcdTLSCert.GetValue(),
|
|
|
|
KeyFile: etcdConfig.EtcdTLSKey.GetValue(),
|
|
|
|
CaCertFile: etcdConfig.EtcdTLSCACert.GetValue(),
|
|
|
|
MinVersion: etcdConfig.EtcdTLSMinVersion.GetValue(),
|
|
|
|
KeyPrefix: etcdConfig.RootPath.GetValue(),
|
2022-12-16 15:59:23 +08:00
|
|
|
RefreshInterval: time.Duration(refreshInterval) * time.Second,
|
2022-11-30 18:23:15 +08:00
|
|
|
}
|
2022-08-02 21:26:33 +08:00
|
|
|
|
2022-12-07 18:01:19 +08:00
|
|
|
s, err := config.NewEtcdSource(info)
|
2022-08-01 10:04:33 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Info("init with etcd failed", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
2022-12-07 18:01:19 +08:00
|
|
|
gp.mgr.AddSource(s)
|
|
|
|
s.SetEventHandler(gp.mgr)
|
2020-12-31 11:20:03 +08:00
|
|
|
}
|
|
|
|
|
2022-01-06 13:49:27 +08:00
|
|
|
// GetConfigDir returns the config directory
|
2021-09-11 14:40:01 +08:00
|
|
|
func (gp *BaseTable) GetConfigDir() string {
|
|
|
|
return gp.configDir
|
|
|
|
}
|
|
|
|
|
2021-09-26 19:46:17 +08:00
|
|
|
func (gp *BaseTable) initConfPath() string {
|
|
|
|
// check if user set conf dir through env
|
2022-08-01 10:04:33 +08:00
|
|
|
configDir, err := gp.mgr.GetConfig("MILVUSCONF")
|
|
|
|
if err != nil {
|
2021-09-26 19:46:17 +08:00
|
|
|
runPath, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-09-26 17:36:54 +08:00
|
|
|
configDir = runPath + "/configs"
|
2021-09-26 19:46:17 +08:00
|
|
|
if _, err := os.Stat(configDir); err != nil {
|
|
|
|
_, fpath, _, _ := runtime.Caller(0)
|
2022-08-23 13:24:53 +08:00
|
|
|
configDir = path.Dir(fpath) + "/../../../configs"
|
2021-09-26 19:46:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return configDir
|
|
|
|
}
|
|
|
|
|
2021-12-03 17:09:43 +08:00
|
|
|
// Load loads an object with @key.
|
2020-11-16 21:10:43 +08:00
|
|
|
func (gp *BaseTable) Load(key string) (string, error) {
|
2022-08-01 10:04:33 +08:00
|
|
|
return gp.mgr.GetConfig(key)
|
2020-11-16 21:10:43 +08:00
|
|
|
}
|
|
|
|
|
2022-12-26 19:11:30 +08:00
|
|
|
func (gp *BaseTable) Get(key string) string {
|
|
|
|
return gp.GetWithDefault(key, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWithDefault loads an object with @key. If the object does not exist, @defaultValue will be returned.
|
|
|
|
func (gp *BaseTable) GetWithDefault(key, defaultValue string) string {
|
2022-08-01 10:04:33 +08:00
|
|
|
str, err := gp.mgr.GetConfig(key)
|
|
|
|
if err != nil {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
return str
|
2021-08-24 09:45:51 +08:00
|
|
|
}
|
|
|
|
|
2022-12-07 18:01:19 +08:00
|
|
|
// Remove Config by key
|
2020-11-16 21:10:43 +08:00
|
|
|
func (gp *BaseTable) Remove(key string) error {
|
2022-08-01 10:04:33 +08:00
|
|
|
gp.mgr.DeleteConfig(key)
|
|
|
|
return nil
|
2020-11-16 21:10:43 +08:00
|
|
|
}
|
|
|
|
|
2022-12-07 18:01:19 +08:00
|
|
|
// Update Config
|
2020-11-16 21:10:43 +08:00
|
|
|
func (gp *BaseTable) Save(key, value string) error {
|
2022-08-01 10:04:33 +08:00
|
|
|
gp.mgr.SetConfig(key, value)
|
|
|
|
return nil
|
2020-11-16 21:10:43 +08:00
|
|
|
}
|
2020-12-08 14:41:04 +08:00
|
|
|
|
2022-12-07 18:01:19 +08:00
|
|
|
// Reset Config to default value
|
|
|
|
func (gp *BaseTable) Reset(key string) error {
|
|
|
|
gp.mgr.ResetConfig(key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-04 10:47:27 +08:00
|
|
|
// InitLogCfg init log of the base table
|
2021-10-01 08:52:50 +08:00
|
|
|
func (gp *BaseTable) InitLogCfg() {
|
2021-09-26 21:18:11 +08:00
|
|
|
gp.Log = log.Config{}
|
2022-12-26 19:11:30 +08:00
|
|
|
format := gp.GetWithDefault("log.format", DefaultLogFormat)
|
2021-09-26 21:18:11 +08:00
|
|
|
gp.Log.Format = format
|
2022-12-26 19:11:30 +08:00
|
|
|
level := gp.GetWithDefault("log.level", DefaultLogLevelForBase)
|
2021-09-26 21:18:11 +08:00
|
|
|
gp.Log.Level = level
|
2022-12-26 19:11:30 +08:00
|
|
|
gp.Log.File.MaxSize, _ = strconv.Atoi(gp.GetWithDefault("log.file.maxSize", "300"))
|
|
|
|
gp.Log.File.MaxBackups, _ = strconv.Atoi(gp.GetWithDefault("log.file.maxBackups", "10"))
|
|
|
|
gp.Log.File.MaxDays, _ = strconv.Atoi(gp.GetWithDefault("log.file.maxAge", "20"))
|
|
|
|
gp.Log.File.RootPath = gp.GetWithDefault("log.file.rootPath", DefaultRootPath)
|
2021-10-01 08:52:50 +08:00
|
|
|
|
2022-11-08 14:33:03 +08:00
|
|
|
grpclog, err := gp.Load("grpc.log.level")
|
|
|
|
if err != nil {
|
|
|
|
gp.Log.GrpcLevel = DefaultLogLevel
|
2021-09-26 21:18:11 +08:00
|
|
|
} else {
|
2022-11-08 14:33:03 +08:00
|
|
|
gp.Log.GrpcLevel = strings.ToUpper(grpclog)
|
2021-09-26 21:18:11 +08:00
|
|
|
}
|
2021-10-01 08:52:50 +08:00
|
|
|
|
2021-09-26 21:18:11 +08:00
|
|
|
}
|