mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-12-04 21:09:06 +08:00
6f75d02c65
/kind enhancement issue: #15711 Signed-off-by: Yuchen Gao <yuchen.gao@zilliz.com>
67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package etcd
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
"github.com/milvus-io/milvus/internal/util/paramtable"
|
|
clientv3 "go.etcd.io/etcd/client/v3"
|
|
"go.etcd.io/etcd/server/v3/embed"
|
|
"go.etcd.io/etcd/server/v3/etcdserver/api/v3client"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// EtcdServer is the singleton of embedded etcd server
|
|
var (
|
|
initOnce sync.Once
|
|
closeOnce sync.Once
|
|
etcdServer *embed.Etcd
|
|
)
|
|
|
|
// GetEmbedEtcdClient returns client of embed etcd server
|
|
func GetEmbedEtcdClient() (*clientv3.Client, error) {
|
|
client := v3client.New(etcdServer.Server)
|
|
return client, nil
|
|
}
|
|
|
|
// InitEtcdServer initializes embedded etcd server singleton.
|
|
func InitEtcdServer(etcdCfg *paramtable.EtcdConfig) error {
|
|
if etcdCfg.UseEmbedEtcd {
|
|
var initError error
|
|
initOnce.Do(func() {
|
|
path := etcdCfg.ConfigPath
|
|
var cfg *embed.Config
|
|
if len(path) > 0 {
|
|
cfgFromFile, err := embed.ConfigFromFile(path)
|
|
if err != nil {
|
|
initError = err
|
|
}
|
|
cfg = cfgFromFile
|
|
} else {
|
|
cfg = embed.NewConfig()
|
|
}
|
|
cfg.Dir = etcdCfg.DataDir
|
|
cfg.LogOutputs = []string{etcdCfg.EtcdLogPath}
|
|
cfg.LogLevel = etcdCfg.EtcdLogLevel
|
|
e, err := embed.StartEtcd(cfg)
|
|
if err != nil {
|
|
log.Error("failed to init embedded Etcd server", zap.Error(err))
|
|
initError = err
|
|
}
|
|
etcdServer = e
|
|
log.Info("finish init Etcd config", zap.String("path", path), zap.String("data", etcdCfg.DataDir))
|
|
})
|
|
return initError
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// StopEtcdServer stops embedded etcd server singleton.
|
|
func StopEtcdServer() {
|
|
if etcdServer != nil {
|
|
closeOnce.Do(func() {
|
|
etcdServer.Close()
|
|
})
|
|
}
|
|
}
|