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 (
|
2022-05-03 08:39:49 +08:00
|
|
|
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
|
|
|
|
2023-07-21 18:50:59 +08:00
|
|
|
DefaultLogFormat = "text"
|
|
|
|
DefaultLogLevelForBase = "debug"
|
|
|
|
DefaultRootPath = ""
|
|
|
|
DefaultKnowhereThreadPoolNumRatioInBuild = 1
|
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 {
|
|
|
|
return []string{"metastore.", "localStorage.", "etcd.", "mysql.", "minio.", "pulsar.", "kafka.", "rocksmq.", "log.", "grpc.", "common.", "quotaAndLimits."}
|
|
|
|
}
|
|
|
|
|
2023-03-07 18:23:51 +08:00
|
|
|
var defaultYaml = []string{"milvus.yaml", "default.yaml", "user.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-02-09 22:06:32 +08:00
|
|
|
once sync.Once
|
|
|
|
mgr *config.Manager
|
2022-08-01 10:04:33 +08:00
|
|
|
|
2021-09-11 14:40:01 +08:00
|
|
|
configDir string
|
2023-03-07 18:23:51 +08:00
|
|
|
YamlFiles []string
|
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{
|
2023-03-07 18:23:51 +08:00
|
|
|
Files: []string{yaml},
|
2022-11-17 18:59:09 +08:00
|
|
|
RefreshInterval: 10 * time.Second,
|
|
|
|
}))
|
2023-03-07 18:23:51 +08:00
|
|
|
gp := &BaseTable{mgr: mgr, YamlFiles: []string{yaml}}
|
2022-10-12 11:37:23 +08:00
|
|
|
return gp
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:06:32 +08:00
|
|
|
// GlobalInitWithYaml initializes the param table with the given yaml.
|
|
|
|
// We will update the global DefaultYaml variable directly, once and for all.
|
|
|
|
// GlobalInitWithYaml shall be called at the very beginning before initiating the base table.
|
|
|
|
// GlobalInitWithYaml should be called only in standalone and embedded Milvus.
|
|
|
|
func (gp *BaseTable) GlobalInitWithYaml(yaml string) {
|
|
|
|
gp.once.Do(func() {
|
2023-03-07 18:23:51 +08:00
|
|
|
defaultYaml = []string{yaml}
|
2023-02-09 22:06:32 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-15 14:45:21 +08:00
|
|
|
func (gp *BaseTable) UpdateSourceOptions(opts ...config.Option) {
|
2023-05-06 17:34:39 +08:00
|
|
|
gp.mgr.UpdateSourceOptions(opts...)
|
|
|
|
}
|
|
|
|
|
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-01-19 14:53:44 +08:00
|
|
|
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
|
|
|
|
}
|
2023-03-07 18:23:51 +08:00
|
|
|
if len(gp.YamlFiles) == 0 {
|
|
|
|
gp.YamlFiles = defaultYaml
|
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)
|
|
|
|
}
|
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-12-16 15:59:23 +08:00
|
|
|
err := gp.mgr.AddSource(config.NewFileSource(&config.FileInfo{
|
2023-03-07 18:23:51 +08:00
|
|
|
Files: lo.Map(gp.YamlFiles, func(file string, _ int) string {
|
|
|
|
return path.Join(gp.configDir, file)
|
|
|
|
}),
|
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-03-07 18:23:51 +08:00
|
|
|
log.Warn("init baseTable with file failed", zap.Strings("configFile", gp.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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-23 15:33:45 +08:00
|
|
|
func (gp *BaseTable) FileConfigs() map[string]string {
|
|
|
|
return gp.mgr.FileConfigs()
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-03-13 14:29:53 +08:00
|
|
|
if gp.mgr == nil {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|