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"
|
|
|
|
"strings"
|
2023-02-09 22:06:32 +08:00
|
|
|
"sync"
|
2022-08-01 10:04:33 +08:00
|
|
|
"time"
|
2020-11-16 21:10:43 +08:00
|
|
|
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/samber/lo"
|
2023-04-11 10:32:31 +08:00
|
|
|
"go.uber.org/zap"
|
2023-04-06 19:14:32 +08:00
|
|
|
|
|
|
|
config "github.com/milvus-io/milvus/pkg/config"
|
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/etcd"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/typeutil"
|
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 (
|
2023-07-27 19:49:02 +08:00
|
|
|
DefaultGlogConf = "glog.conf"
|
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
|
|
|
|
2023-07-21 18:50:59 +08:00
|
|
|
DefaultLogFormat = "text"
|
|
|
|
DefaultLogLevelForBase = "debug"
|
|
|
|
DefaultRootPath = ""
|
2023-09-04 17:25:48 +08:00
|
|
|
DefaultMinioLogLevel = "fatal"
|
2023-07-21 18:50:59 +08:00
|
|
|
DefaultKnowhereThreadPoolNumRatioInBuild = 1
|
2023-08-11 10:37:36 +08:00
|
|
|
DefaultMinioRegion = ""
|
|
|
|
DefaultMinioUseVirtualHost = "false"
|
2023-10-31 09:06:13 +08:00
|
|
|
DefaultMinioRequestTimeout = "10000"
|
2022-03-02 18:51:55 +08:00
|
|
|
)
|
2021-11-10 14:27:37 +08:00
|
|
|
|
2023-02-14 16:56:34 +08:00
|
|
|
// Const of Global Config List
|
2022-12-09 14:31:21 +08:00
|
|
|
func globalConfigPrefixs() []string {
|
2023-09-07 07:25:14 +08:00
|
|
|
return []string{"metastore", "localStorage", "etcd", "tikv", "minio", "pulsar", "kafka", "rocksmq", "log", "grpc", "common", "quotaAndLimits"}
|
2022-12-09 14:31:21 +08:00
|
|
|
}
|
|
|
|
|
2023-09-01 09:19:00 +08:00
|
|
|
var defaultYaml = []string{"milvus.yaml"}
|
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 {
|
2023-09-05 10:31:48 +08:00
|
|
|
once sync.Once
|
|
|
|
mgr *config.Manager
|
|
|
|
config *baseTableConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type baseTableConfig struct {
|
|
|
|
configDir string
|
|
|
|
refreshInterval int
|
|
|
|
skipRemote bool
|
|
|
|
skipEnv bool
|
|
|
|
yamlFiles []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*baseTableConfig)
|
2022-08-01 10:04:33 +08:00
|
|
|
|
2023-09-05 10:31:48 +08:00
|
|
|
func Files(files []string) Option {
|
|
|
|
return func(bt *baseTableConfig) {
|
|
|
|
bt.yamlFiles = files
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Interval(interval int) Option {
|
|
|
|
return func(bt *baseTableConfig) {
|
|
|
|
bt.refreshInterval = interval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SkipRemote(skip bool) Option {
|
|
|
|
return func(bt *baseTableConfig) {
|
|
|
|
bt.skipRemote = skip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func skipEnv(skip bool) Option {
|
|
|
|
return func(bt *baseTableConfig) {
|
|
|
|
bt.skipEnv = skip
|
|
|
|
}
|
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 {
|
2023-09-05 10:31:48 +08:00
|
|
|
return NewBaseTable(Files([]string{yaml}), SkipRemote(true), skipEnv(true))
|
2022-10-12 11:37:23 +08:00
|
|
|
}
|
|
|
|
|
2023-09-05 10:31:48 +08:00
|
|
|
func NewBaseTable(opts ...Option) *BaseTable {
|
|
|
|
defaultConfig := &baseTableConfig{
|
|
|
|
configDir: initConfPath(),
|
|
|
|
yamlFiles: defaultYaml,
|
|
|
|
refreshInterval: 5,
|
|
|
|
skipRemote: false,
|
|
|
|
skipEnv: false,
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(defaultConfig)
|
|
|
|
}
|
|
|
|
bt := &BaseTable{config: defaultConfig}
|
|
|
|
bt.init()
|
|
|
|
return bt
|
2023-05-06 17:34:39 +08:00
|
|
|
}
|
|
|
|
|
2023-01-19 14:53:44 +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
|
2023-09-05 10:31:48 +08:00
|
|
|
func (bt *BaseTable) init() {
|
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
|
|
|
|
}
|
2023-09-05 10:31:48 +08:00
|
|
|
bt.mgr, _ = config.Init()
|
|
|
|
if !bt.config.skipEnv {
|
|
|
|
err := bt.mgr.AddSource(config.NewEnvSource(formatter))
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("init baseTable with env failed", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
2022-09-23 09:50:52 +08:00
|
|
|
}
|
2023-09-05 10:31:48 +08:00
|
|
|
bt.initConfigsFromLocal()
|
|
|
|
if !bt.config.skipRemote {
|
|
|
|
bt.initConfigsFromRemote()
|
2022-08-01 10:04:33 +08:00
|
|
|
}
|
2022-12-16 15:59:23 +08:00
|
|
|
}
|
2022-08-01 10:04:33 +08:00
|
|
|
|
2023-09-05 10:31:48 +08:00
|
|
|
func (bt *BaseTable) initConfigsFromLocal() {
|
|
|
|
refreshInterval := bt.config.refreshInterval
|
|
|
|
err := bt.mgr.AddSource(config.NewFileSource(&config.FileInfo{
|
|
|
|
Files: lo.Map(bt.config.yamlFiles, func(file string, _ int) string {
|
|
|
|
return path.Join(bt.config.configDir, file)
|
2023-03-07 18:23:51 +08:00
|
|
|
}),
|
2022-12-16 15:59:23 +08:00
|
|
|
RefreshInterval: time.Duration(refreshInterval) * time.Second,
|
|
|
|
}))
|
2022-08-01 10:04:33 +08:00
|
|
|
if err != nil {
|
2023-09-05 10:31:48 +08:00
|
|
|
log.Warn("init baseTable with file failed", zap.Strings("configFile", bt.config.yamlFiles), zap.Error(err))
|
2022-08-01 10:04:33 +08:00
|
|
|
return
|
|
|
|
}
|
2022-08-02 21:26:33 +08:00
|
|
|
}
|
2022-08-01 10:04:33 +08:00
|
|
|
|
2023-09-05 10:31:48 +08:00
|
|
|
func (bt *BaseTable) initConfigsFromRemote() {
|
|
|
|
refreshInterval := bt.config.refreshInterval
|
2022-11-30 18:23:15 +08:00
|
|
|
etcdConfig := EtcdConfig{}
|
2023-09-05 10:31:48 +08:00
|
|
|
etcdConfig.Init(bt)
|
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
|
|
|
|
}
|
2023-09-05 10:31:48 +08:00
|
|
|
bt.mgr.AddSource(s)
|
|
|
|
s.SetEventHandler(bt.mgr)
|
2020-12-31 11:20:03 +08:00
|
|
|
}
|
|
|
|
|
2022-01-06 13:49:27 +08:00
|
|
|
// GetConfigDir returns the config directory
|
2023-09-05 10:31:48 +08:00
|
|
|
func (bt *BaseTable) GetConfigDir() string {
|
|
|
|
return bt.config.configDir
|
2021-09-11 14:40:01 +08:00
|
|
|
}
|
|
|
|
|
2023-09-05 10:31:48 +08:00
|
|
|
func initConfPath() string {
|
2021-09-26 19:46:17 +08:00
|
|
|
// check if user set conf dir through env
|
2023-09-05 10:31:48 +08:00
|
|
|
configDir := os.Getenv("MILVUSCONF")
|
|
|
|
if len(configDir) != 0 {
|
|
|
|
return configDir
|
|
|
|
}
|
|
|
|
runPath, err := os.Getwd()
|
2022-08-01 10:04:33 +08:00
|
|
|
if err != nil {
|
2023-09-05 10:31:48 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
configDir = runPath + "/configs"
|
|
|
|
if _, err := os.Stat(configDir); err != nil {
|
|
|
|
_, fpath, _, _ := runtime.Caller(0)
|
|
|
|
configDir = path.Dir(fpath) + "/../../../configs"
|
2021-09-26 19:46:17 +08:00
|
|
|
}
|
|
|
|
return configDir
|
|
|
|
}
|
|
|
|
|
2023-09-05 10:31:48 +08:00
|
|
|
func (bt *BaseTable) FileConfigs() map[string]string {
|
|
|
|
return bt.mgr.FileConfigs()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *BaseTable) UpdateSourceOptions(opts ...config.Option) {
|
|
|
|
bt.mgr.UpdateSourceOptions(opts...)
|
2023-02-23 15:33:45 +08:00
|
|
|
}
|
|
|
|
|
2021-12-03 17:09:43 +08:00
|
|
|
// Load loads an object with @key.
|
2023-09-05 10:31:48 +08:00
|
|
|
func (bt *BaseTable) Load(key string) (string, error) {
|
|
|
|
return bt.mgr.GetConfig(key)
|
2020-11-16 21:10:43 +08:00
|
|
|
}
|
|
|
|
|
2023-09-05 10:31:48 +08:00
|
|
|
func (bt *BaseTable) Get(key string) string {
|
|
|
|
return bt.GetWithDefault(key, "")
|
2022-12-26 19:11:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetWithDefault loads an object with @key. If the object does not exist, @defaultValue will be returned.
|
2023-09-05 10:31:48 +08:00
|
|
|
func (bt *BaseTable) GetWithDefault(key, defaultValue string) string {
|
|
|
|
if bt.mgr == nil {
|
2023-03-13 14:29:53 +08:00
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
2023-09-05 10:31:48 +08:00
|
|
|
str, err := bt.mgr.GetConfig(key)
|
2022-08-01 10:04:33 +08:00
|
|
|
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
|
2023-09-05 10:31:48 +08:00
|
|
|
func (bt *BaseTable) Remove(key string) error {
|
|
|
|
bt.mgr.DeleteConfig(key)
|
2022-08-01 10:04:33 +08:00
|
|
|
return nil
|
2020-11-16 21:10:43 +08:00
|
|
|
}
|
|
|
|
|
2022-12-07 18:01:19 +08:00
|
|
|
// Update Config
|
2023-09-05 10:31:48 +08:00
|
|
|
func (bt *BaseTable) Save(key, value string) error {
|
|
|
|
bt.mgr.SetConfig(key, value)
|
2022-08-01 10:04:33 +08:00
|
|
|
return nil
|
2020-11-16 21:10:43 +08:00
|
|
|
}
|
2020-12-08 14:41:04 +08:00
|
|
|
|
2023-11-28 15:32:31 +08:00
|
|
|
func (bt *BaseTable) SaveGroup(group map[string]string) error {
|
|
|
|
for key, value := range group {
|
|
|
|
bt.mgr.SetMapConfig(key, value)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-07 18:01:19 +08:00
|
|
|
// Reset Config to default value
|
2023-09-05 10:31:48 +08:00
|
|
|
func (bt *BaseTable) Reset(key string) error {
|
|
|
|
bt.mgr.ResetConfig(key)
|
2022-12-07 18:01:19 +08:00
|
|
|
return nil
|
|
|
|
}
|