2021-12-17 14:19:36 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
2021-05-26 20:14:30 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-12-17 14:19:36 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-05-26 20:14:30 +08:00
|
|
|
//
|
2021-12-17 14:19:36 +08:00
|
|
|
// 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.
|
2021-05-26 20:14:30 +08:00
|
|
|
|
2021-06-18 21:30:08 +08:00
|
|
|
package rootcoord
|
2021-05-26 20:14:30 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
2021-06-01 11:04:31 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/metrics"
|
2021-05-26 20:14:30 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/sessionutil"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
2021-08-24 09:45:51 +08:00
|
|
|
"go.etcd.io/etcd/api/v3/mvccpb"
|
|
|
|
clientv3 "go.etcd.io/etcd/client/v3"
|
2021-05-26 20:14:30 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-09-22 23:55:53 +08:00
|
|
|
// proxyManager manages proxy connected to the rootcoord
|
2021-06-22 19:08:03 +08:00
|
|
|
type proxyManager struct {
|
2022-01-27 11:25:41 +08:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
lock sync.Mutex
|
|
|
|
etcdCli *clientv3.Client
|
|
|
|
initSessionsFunc []func([]*sessionutil.Session)
|
|
|
|
addSessionsFunc []func(*sessionutil.Session)
|
|
|
|
delSessionsFunc []func(*sessionutil.Session)
|
2021-05-26 20:14:30 +08:00
|
|
|
}
|
|
|
|
|
2021-09-22 23:55:53 +08:00
|
|
|
// newProxyManager helper function to create a proxyManager
|
|
|
|
// etcdEndpoints is the address list of etcd
|
|
|
|
// fns are the custom getSessions function list
|
2021-12-29 14:35:21 +08:00
|
|
|
func newProxyManager(ctx context.Context, client *clientv3.Client, fns ...func([]*sessionutil.Session)) *proxyManager {
|
2021-05-26 20:14:30 +08:00
|
|
|
ctx2, cancel2 := context.WithCancel(ctx)
|
2021-06-22 19:08:03 +08:00
|
|
|
p := &proxyManager{
|
2021-05-26 20:14:30 +08:00
|
|
|
ctx: ctx2,
|
|
|
|
cancel: cancel2,
|
|
|
|
lock: sync.Mutex{},
|
2021-12-29 14:35:21 +08:00
|
|
|
etcdCli: client,
|
2021-05-26 20:14:30 +08:00
|
|
|
}
|
2022-01-27 11:25:41 +08:00
|
|
|
p.initSessionsFunc = append(p.initSessionsFunc, fns...)
|
2021-12-29 14:35:21 +08:00
|
|
|
return p
|
2021-05-26 20:14:30 +08:00
|
|
|
}
|
|
|
|
|
2022-01-27 11:25:41 +08:00
|
|
|
// AddSessionFunc adds functions to addSessions function list
|
|
|
|
func (p *proxyManager) AddSessionFunc(fns ...func(*sessionutil.Session)) {
|
2021-05-26 20:14:30 +08:00
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
2022-01-27 11:25:41 +08:00
|
|
|
p.addSessionsFunc = append(p.addSessionsFunc, fns...)
|
2021-05-26 20:14:30 +08:00
|
|
|
}
|
|
|
|
|
2022-01-27 11:25:41 +08:00
|
|
|
// DelSessionFunc add functions to delSessions function list
|
|
|
|
func (p *proxyManager) DelSessionFunc(fns ...func(*sessionutil.Session)) {
|
2021-05-26 20:14:30 +08:00
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
2022-01-27 11:25:41 +08:00
|
|
|
p.delSessionsFunc = append(p.delSessionsFunc, fns...)
|
2021-05-26 20:14:30 +08:00
|
|
|
}
|
|
|
|
|
2021-09-22 23:55:53 +08:00
|
|
|
// WatchProxy starts a goroutine to watch proxy session changes on etcd
|
2021-06-22 19:08:03 +08:00
|
|
|
func (p *proxyManager) WatchProxy() error {
|
2021-11-29 17:33:41 +08:00
|
|
|
ctx, cancel := context.WithTimeout(p.ctx, RequestTimeout)
|
2021-05-26 20:14:30 +08:00
|
|
|
defer cancel()
|
2021-11-29 17:33:41 +08:00
|
|
|
|
|
|
|
sessions, rev, err := p.getSessionsOnEtcd(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-27 11:25:41 +08:00
|
|
|
log.Debug("succeed to init sessions on etcd", zap.Any("sessions", sessions), zap.Int64("revision", rev))
|
|
|
|
for _, f := range p.initSessionsFunc {
|
2021-11-29 17:33:41 +08:00
|
|
|
f(sessions)
|
|
|
|
}
|
|
|
|
|
|
|
|
eventCh := p.etcdCli.Watch(
|
|
|
|
p.ctx,
|
2022-02-07 10:09:45 +08:00
|
|
|
path.Join(Params.EtcdCfg.MetaRootPath, sessionutil.DefaultServiceRoot, typeutil.ProxyRole),
|
2021-05-26 20:14:30 +08:00
|
|
|
clientv3.WithPrefix(),
|
2021-11-29 17:33:41 +08:00
|
|
|
clientv3.WithCreatedNotify(),
|
|
|
|
clientv3.WithPrevKV(),
|
|
|
|
clientv3.WithRev(rev+1),
|
2021-05-26 20:14:30 +08:00
|
|
|
)
|
2021-11-29 17:33:41 +08:00
|
|
|
go p.startWatchEtcd(p.ctx, eventCh)
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-26 20:14:30 +08:00
|
|
|
|
2021-11-29 17:33:41 +08:00
|
|
|
func (p *proxyManager) startWatchEtcd(ctx context.Context, eventCh clientv3.WatchChan) {
|
|
|
|
log.Debug("start to watch etcd")
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Warn("stop watching etcd loop")
|
|
|
|
return
|
|
|
|
case event, ok := <-eventCh:
|
|
|
|
if !ok {
|
|
|
|
log.Warn("stop watching etcd loop due to closed etcd event channel")
|
|
|
|
return
|
2021-06-28 11:04:14 +08:00
|
|
|
}
|
2021-11-29 17:33:41 +08:00
|
|
|
if err := event.Err(); err != nil {
|
|
|
|
log.Error("received error event from etcd watcher", zap.Error(err))
|
2021-05-26 20:14:30 +08:00
|
|
|
return
|
2021-11-29 17:33:41 +08:00
|
|
|
}
|
|
|
|
for _, e := range event.Events {
|
|
|
|
var err error
|
|
|
|
switch e.Type {
|
|
|
|
case mvccpb.PUT:
|
|
|
|
err = p.handlePutEvent(e)
|
|
|
|
case mvccpb.DELETE:
|
|
|
|
err = p.handleDeleteEvent(e)
|
2021-05-26 20:14:30 +08:00
|
|
|
}
|
2021-11-29 17:33:41 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("failed to handle proxy event", zap.Any("event", e), zap.Error(err))
|
2021-05-26 20:14:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-29 17:33:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *proxyManager) handlePutEvent(e *clientv3.Event) error {
|
|
|
|
session, err := p.parseSession(e.Kv.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug("received proxy put event with session", zap.Any("session", session))
|
2022-01-27 11:25:41 +08:00
|
|
|
for _, f := range p.addSessionsFunc {
|
2021-11-29 17:33:41 +08:00
|
|
|
f(session)
|
|
|
|
}
|
2022-03-02 21:11:57 +08:00
|
|
|
metrics.RootCoordProxyLister.WithLabelValues(metricProxy(session.ServerID)).Inc()
|
2021-11-29 17:33:41 +08:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-26 20:14:30 +08:00
|
|
|
|
2021-11-29 17:33:41 +08:00
|
|
|
func (p *proxyManager) handleDeleteEvent(e *clientv3.Event) error {
|
|
|
|
session, err := p.parseSession(e.PrevKv.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug("received proxy delete event with session", zap.Any("session", session))
|
2022-01-27 11:25:41 +08:00
|
|
|
for _, f := range p.delSessionsFunc {
|
2021-11-29 17:33:41 +08:00
|
|
|
f(session)
|
|
|
|
}
|
2022-03-02 21:11:57 +08:00
|
|
|
metrics.RootCoordProxyLister.WithLabelValues(metricProxy(session.ServerID)).Dec()
|
2021-05-26 20:14:30 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-29 17:33:41 +08:00
|
|
|
func (p *proxyManager) parseSession(value []byte) (*sessionutil.Session, error) {
|
|
|
|
session := new(sessionutil.Session)
|
|
|
|
err := json.Unmarshal(value, session)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return session, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *proxyManager) getSessionsOnEtcd(ctx context.Context) ([]*sessionutil.Session, int64, error) {
|
|
|
|
resp, err := p.etcdCli.Get(
|
|
|
|
ctx,
|
2022-02-07 10:09:45 +08:00
|
|
|
path.Join(Params.EtcdCfg.MetaRootPath, sessionutil.DefaultServiceRoot, typeutil.ProxyRole),
|
2021-11-29 17:33:41 +08:00
|
|
|
clientv3.WithPrefix(),
|
|
|
|
clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, fmt.Errorf("proxy manager failed to watch proxy with error %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var sessions []*sessionutil.Session
|
|
|
|
for _, v := range resp.Kvs {
|
|
|
|
session, err := p.parseSession(v.Value)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("failed to unmarshal session", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sessions = append(sessions, session)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sessions, resp.Header.Revision, nil
|
|
|
|
}
|
|
|
|
|
2021-09-22 23:55:53 +08:00
|
|
|
// Stop stops the proxyManager
|
2021-06-22 19:08:03 +08:00
|
|
|
func (p *proxyManager) Stop() {
|
2021-05-26 20:14:30 +08:00
|
|
|
p.cancel()
|
|
|
|
}
|
2021-06-28 15:08:12 +08:00
|
|
|
|
2021-09-22 23:55:53 +08:00
|
|
|
// listProxyInEtcd helper function lists proxy in etcd
|
2021-06-28 15:08:12 +08:00
|
|
|
func listProxyInEtcd(ctx context.Context, cli *clientv3.Client) (map[int64]*sessionutil.Session, error) {
|
|
|
|
ctx2, cancel := context.WithTimeout(ctx, RequestTimeout)
|
|
|
|
defer cancel()
|
|
|
|
resp, err := cli.Get(
|
|
|
|
ctx2,
|
2022-02-07 10:09:45 +08:00
|
|
|
path.Join(Params.EtcdCfg.MetaRootPath, sessionutil.DefaultServiceRoot, typeutil.ProxyRole),
|
2021-06-28 15:08:12 +08:00
|
|
|
clientv3.WithPrefix(),
|
|
|
|
clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("list proxy failed, etcd error = %w", err)
|
|
|
|
}
|
|
|
|
sess := make(map[int64]*sessionutil.Session)
|
|
|
|
for _, v := range resp.Kvs {
|
|
|
|
var s sessionutil.Session
|
|
|
|
err := json.Unmarshal(v.Value, &s)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("unmarshal SvrSession failed", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sess[s.ServerID] = &s
|
|
|
|
}
|
|
|
|
return sess, nil
|
|
|
|
}
|