2021-06-18 15:20:08 +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 (
|
2021-08-24 09:45:51 +08:00
|
|
|
"os"
|
2021-12-21 15:53:51 +08:00
|
|
|
"path"
|
2021-08-24 09:45:51 +08:00
|
|
|
"strings"
|
2021-06-18 15:20:08 +08:00
|
|
|
"sync"
|
|
|
|
|
2021-09-03 19:35:47 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/metricsinfo"
|
2021-06-18 15:20:08 +08:00
|
|
|
)
|
|
|
|
|
2021-10-14 14:04:34 +08:00
|
|
|
// BaseParamTable is a derived struct of BaseTable. It achieves Composition by
|
|
|
|
// embedding BaseTable. It is used to quickly and easily access the system configuration.
|
2021-08-24 09:45:51 +08:00
|
|
|
type BaseParamTable struct {
|
2021-06-18 15:20:08 +08:00
|
|
|
BaseTable
|
2021-12-17 19:09:23 +08:00
|
|
|
once sync.Once
|
2021-06-18 15:20:08 +08:00
|
|
|
|
2021-08-24 09:45:51 +08:00
|
|
|
// --- ETCD ---
|
|
|
|
EtcdEndpoints []string
|
|
|
|
MetaRootPath string
|
|
|
|
KvRootPath string
|
|
|
|
|
|
|
|
// --- Embed ETCD ---
|
|
|
|
UseEmbedEtcd bool
|
|
|
|
EtcdConfigPath string
|
|
|
|
EtcdDataDir string
|
2021-06-18 15:20:08 +08:00
|
|
|
}
|
|
|
|
|
2021-10-14 14:04:34 +08:00
|
|
|
// Init is an override method of BaseTable's Init. It mainly calls the
|
|
|
|
// Init of BaseTable and do some other initialization.
|
2021-08-24 09:45:51 +08:00
|
|
|
func (p *BaseParamTable) Init() {
|
2021-12-17 19:09:23 +08:00
|
|
|
p.once.Do(func() {
|
2021-06-18 15:20:08 +08:00
|
|
|
p.BaseTable.Init()
|
2021-08-24 09:45:51 +08:00
|
|
|
p.LoadCfgToMemory()
|
2021-06-18 15:20:08 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-15 20:38:58 +08:00
|
|
|
// LoadCfgToMemory loads configurations from file into memory.
|
2021-08-24 09:45:51 +08:00
|
|
|
func (p *BaseParamTable) LoadCfgToMemory() {
|
|
|
|
p.initEtcdConf()
|
|
|
|
p.initMetaRootPath()
|
|
|
|
p.initKvRootPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BaseParamTable) initEtcdConf() {
|
|
|
|
p.initUseEmbedEtcd()
|
|
|
|
if p.UseEmbedEtcd {
|
|
|
|
p.initConfigPath()
|
|
|
|
p.initEtcdDataDir()
|
|
|
|
} else {
|
|
|
|
p.initEtcdEndpoints()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BaseParamTable) initUseEmbedEtcd() {
|
|
|
|
p.UseEmbedEtcd = p.ParseBool("etcd.use.embed", false)
|
2021-09-03 19:35:47 +08:00
|
|
|
if p.UseEmbedEtcd && (os.Getenv(metricsinfo.DeployModeEnvKey) != metricsinfo.StandaloneDeployMode) {
|
2021-08-24 09:45:51 +08:00
|
|
|
panic("embedded etcd can not be used under distributed mode")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BaseParamTable) initConfigPath() {
|
2021-10-28 23:16:40 +08:00
|
|
|
addr := p.LoadWithDefault("etcd.config.path", "")
|
2021-08-24 09:45:51 +08:00
|
|
|
p.EtcdConfigPath = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BaseParamTable) initEtcdDataDir() {
|
2021-10-28 23:16:40 +08:00
|
|
|
addr := p.LoadWithDefault("etcd.data.dir", "default.etcd")
|
2021-08-24 09:45:51 +08:00
|
|
|
p.EtcdDataDir = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BaseParamTable) initEtcdEndpoints() {
|
|
|
|
endpoints, err := p.Load("_EtcdEndpoints")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.EtcdEndpoints = strings.Split(endpoints, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BaseParamTable) initMetaRootPath() {
|
|
|
|
rootPath, err := p.Load("etcd.rootPath")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
subPath, err := p.Load("etcd.metaSubPath")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-12-21 15:53:51 +08:00
|
|
|
p.MetaRootPath = path.Join(rootPath, subPath)
|
2021-08-24 09:45:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BaseParamTable) initKvRootPath() {
|
|
|
|
rootPath, err := p.Load("etcd.rootPath")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
subPath, err := p.Load("etcd.kvSubPath")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-12-21 15:53:51 +08:00
|
|
|
p.KvRootPath = path.Join(rootPath, subPath)
|
2021-08-24 09:45:51 +08:00
|
|
|
}
|