2021-05-18 19:07:27 +08:00
|
|
|
package sessionutil
|
2021-05-18 11:39:21 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-05-21 19:28:52 +08:00
|
|
|
"path"
|
2021-05-18 11:39:21 +08:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/coreos/etcd/mvcc/mvccpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/retry"
|
|
|
|
"go.etcd.io/etcd/clientv3"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-05-25 20:15:46 +08:00
|
|
|
const (
|
2021-05-26 20:14:30 +08:00
|
|
|
DefaultServiceRoot = "session/"
|
2021-05-25 20:15:46 +08:00
|
|
|
DefaultIDKey = "id"
|
|
|
|
DefaultRetryTimes = 30
|
|
|
|
DefaultTTL = 10
|
|
|
|
)
|
|
|
|
|
|
|
|
type SessionEventType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
SessionNoneEvent SessionEventType = iota
|
|
|
|
SessionAddEvent
|
|
|
|
SessionDelEvent
|
|
|
|
)
|
2021-05-18 11:39:21 +08:00
|
|
|
|
|
|
|
// Session is a struct to store service's session, including ServerID, ServerName,
|
|
|
|
// Address.
|
2021-06-23 17:14:10 +08:00
|
|
|
// Exclusive indicates that this server can only start one.
|
2021-05-18 11:39:21 +08:00
|
|
|
type Session struct {
|
2021-05-21 19:28:52 +08:00
|
|
|
ctx context.Context
|
|
|
|
ServerID int64 `json:"ServerID,omitempty"`
|
|
|
|
ServerName string `json:"ServerName,omitempty"`
|
|
|
|
Address string `json:"Address,omitempty"`
|
|
|
|
Exclusive bool `json:"Exclusive,omitempty"`
|
2021-05-18 11:39:21 +08:00
|
|
|
|
2021-05-26 20:14:30 +08:00
|
|
|
etcdCli *clientv3.Client
|
|
|
|
leaseID clientv3.LeaseID
|
|
|
|
cancel context.CancelFunc
|
|
|
|
metaRoot string
|
2021-05-19 18:36:05 +08:00
|
|
|
}
|
|
|
|
|
2021-05-24 11:53:07 +08:00
|
|
|
// NewSession is a helper to build Session object.
|
2021-06-23 17:14:10 +08:00
|
|
|
// ServerID, ServerName, Address, Exclusive will be assigned after Init().
|
|
|
|
// metaRoot is a path in etcd to save session information.
|
|
|
|
// etcdEndpoints is to init etcdCli when NewSession
|
2021-06-11 22:04:41 +08:00
|
|
|
func NewSession(ctx context.Context, metaRoot string, etcdEndpoints []string) *Session {
|
2021-05-21 19:28:52 +08:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
session := &Session{
|
2021-05-26 20:14:30 +08:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
metaRoot: metaRoot,
|
2021-05-19 18:36:05 +08:00
|
|
|
}
|
|
|
|
|
2021-05-21 19:28:52 +08:00
|
|
|
connectEtcdFn := func() error {
|
2021-06-26 14:14:11 +08:00
|
|
|
log.Debug("Session try to connect to etcd")
|
2021-06-11 22:04:41 +08:00
|
|
|
etcdCli, err := clientv3.New(clientv3.Config{Endpoints: etcdEndpoints, DialTimeout: 5 * time.Second})
|
2021-05-21 19:28:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-30 14:12:15 +08:00
|
|
|
ctx2, cancel2 := context.WithTimeout(session.ctx, 5*time.Second)
|
|
|
|
defer cancel2()
|
|
|
|
if _, err = etcdCli.Get(ctx2, "health"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-21 19:28:52 +08:00
|
|
|
session.etcdCli = etcdCli
|
2021-05-19 18:36:05 +08:00
|
|
|
return nil
|
|
|
|
}
|
2021-06-23 09:24:10 +08:00
|
|
|
err := retry.Do(ctx, connectEtcdFn, retry.Attempts(300))
|
2021-05-21 19:28:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
2021-05-19 18:36:05 +08:00
|
|
|
}
|
2021-06-26 14:14:11 +08:00
|
|
|
log.Debug("Sessiont connect to etcd success")
|
2021-05-21 19:28:52 +08:00
|
|
|
return session
|
2021-05-19 18:36:05 +08:00
|
|
|
}
|
|
|
|
|
2021-06-23 17:14:10 +08:00
|
|
|
// Init will initialize base struct of the Session, including ServerName, ServerID,
|
|
|
|
// Address, Exclusive. ServerID is obtained in getServerID.
|
|
|
|
// Finally it will process keepAliveResponse to keep alive with etcd.
|
2021-05-24 11:53:07 +08:00
|
|
|
func (s *Session) Init(serverName, address string, exclusive bool) <-chan bool {
|
2021-05-24 09:44:49 +08:00
|
|
|
s.ServerName = serverName
|
|
|
|
s.Address = address
|
|
|
|
s.Exclusive = exclusive
|
2021-05-21 19:28:52 +08:00
|
|
|
s.checkIDExist()
|
|
|
|
serverID, err := s.getServerID()
|
2021-05-19 18:36:05 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-05-21 19:28:52 +08:00
|
|
|
s.ServerID = serverID
|
|
|
|
ch, err := s.registerService()
|
2021-05-19 18:36:05 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-05-24 11:53:07 +08:00
|
|
|
return s.processKeepAliveResponse(ch)
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
|
|
|
|
2021-05-21 19:28:52 +08:00
|
|
|
func (s *Session) getServerID() (int64, error) {
|
2021-05-24 09:44:49 +08:00
|
|
|
return s.getServerIDWithKey(DefaultIDKey, DefaultRetryTimes)
|
2021-05-19 18:36:05 +08:00
|
|
|
}
|
|
|
|
|
2021-05-21 19:28:52 +08:00
|
|
|
func (s *Session) checkIDExist() {
|
2021-06-26 18:04:12 +08:00
|
|
|
log.Debug("Session checkIDExist Begin")
|
2021-05-21 19:28:52 +08:00
|
|
|
s.etcdCli.Txn(s.ctx).If(
|
|
|
|
clientv3.Compare(
|
2021-05-26 20:14:30 +08:00
|
|
|
clientv3.Version(path.Join(s.metaRoot, DefaultServiceRoot, DefaultIDKey)),
|
2021-05-21 19:28:52 +08:00
|
|
|
"=",
|
|
|
|
0)).
|
2021-05-26 20:14:30 +08:00
|
|
|
Then(clientv3.OpPut(path.Join(s.metaRoot, DefaultServiceRoot, DefaultIDKey), "1")).Commit()
|
2021-06-26 18:04:12 +08:00
|
|
|
log.Debug("Session checkIDExist End")
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
|
|
|
|
2021-06-23 09:24:10 +08:00
|
|
|
func (s *Session) getServerIDWithKey(key string, retryTimes uint) (int64, error) {
|
2021-06-26 14:14:11 +08:00
|
|
|
for {
|
2021-06-26 18:04:12 +08:00
|
|
|
log.Debug("Session try to get serverID")
|
2021-05-26 20:14:30 +08:00
|
|
|
getResp, err := s.etcdCli.Get(s.ctx, path.Join(s.metaRoot, DefaultServiceRoot, key))
|
2021-05-18 11:39:21 +08:00
|
|
|
if err != nil {
|
2021-06-26 14:14:11 +08:00
|
|
|
log.Debug("Session get etcd key error", zap.String("key", key), zap.Error(err))
|
|
|
|
return -1, err
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
2021-05-21 19:28:52 +08:00
|
|
|
if getResp.Count <= 0 {
|
2021-06-26 14:14:11 +08:00
|
|
|
log.Debug("Session there is no value", zap.String("key", key))
|
|
|
|
continue
|
2021-05-21 19:28:52 +08:00
|
|
|
}
|
|
|
|
value := string(getResp.Kvs[0].Value)
|
2021-05-18 11:39:21 +08:00
|
|
|
valueInt, err := strconv.ParseInt(value, 10, 64)
|
|
|
|
if err != nil {
|
2021-06-26 14:14:11 +08:00
|
|
|
log.Debug("Session ParseInt error", zap.String("value", value), zap.Error(err))
|
|
|
|
continue
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
2021-05-21 19:28:52 +08:00
|
|
|
txnResp, err := s.etcdCli.Txn(s.ctx).If(
|
|
|
|
clientv3.Compare(
|
2021-06-26 14:14:11 +08:00
|
|
|
clientv3.Value(path.Join(s.metaRoot, DefaultServiceRoot, key)),
|
2021-05-21 19:28:52 +08:00
|
|
|
"=",
|
|
|
|
value)).
|
2021-06-26 14:14:11 +08:00
|
|
|
Then(clientv3.OpPut(path.Join(s.metaRoot, DefaultServiceRoot, key), strconv.FormatInt(valueInt+1, 10))).Commit()
|
2021-05-18 11:39:21 +08:00
|
|
|
if err != nil {
|
2021-06-26 14:14:11 +08:00
|
|
|
log.Debug("Session Txn failed", zap.String("key", key), zap.Error(err))
|
|
|
|
return -1, err
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
2021-05-21 19:28:52 +08:00
|
|
|
|
|
|
|
if !txnResp.Succeeded {
|
2021-06-26 14:14:11 +08:00
|
|
|
log.Debug("Session Txn unsuccess", zap.String("key", key))
|
|
|
|
continue
|
2021-05-21 19:28:52 +08:00
|
|
|
}
|
2021-06-26 18:04:12 +08:00
|
|
|
log.Debug("Session get serverID success")
|
2021-06-26 14:14:11 +08:00
|
|
|
return valueInt, nil
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 11:53:07 +08:00
|
|
|
// registerService registers the service to etcd so that other services
|
2021-05-18 11:39:21 +08:00
|
|
|
// can find that the service is online and issue subsequent operations
|
|
|
|
// RegisterService will save a key-value in etcd
|
|
|
|
// key: metaRootPath + "/services" + "/ServerName-ServerID"
|
|
|
|
// value: json format
|
|
|
|
// {
|
2021-05-24 11:53:07 +08:00
|
|
|
// ServerID int64 `json:"ServerID,omitempty"`
|
|
|
|
// ServerName string `json:"ServerName,omitempty"`
|
|
|
|
// Address string `json:"Address,omitempty"`
|
|
|
|
// Exclusive bool `json:"Exclusive,omitempty"`
|
2021-05-18 11:39:21 +08:00
|
|
|
// }
|
2021-05-18 19:07:27 +08:00
|
|
|
// Exclusive means whether this service can exist two at the same time, if so,
|
|
|
|
// it is false. Otherwise, set it to true.
|
2021-05-21 19:28:52 +08:00
|
|
|
func (s *Session) registerService() (<-chan *clientv3.LeaseKeepAliveResponse, error) {
|
|
|
|
var ch <-chan *clientv3.LeaseKeepAliveResponse
|
|
|
|
registerFn := func() error {
|
2021-05-24 09:44:49 +08:00
|
|
|
resp, err := s.etcdCli.Grant(s.ctx, DefaultTTL)
|
2021-05-21 19:28:52 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error("register service", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.leaseID = resp.ID
|
2021-05-18 11:39:21 +08:00
|
|
|
|
2021-05-21 19:28:52 +08:00
|
|
|
sessionJSON, err := json.Marshal(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-18 11:39:21 +08:00
|
|
|
|
2021-05-21 19:28:52 +08:00
|
|
|
key := s.ServerName
|
|
|
|
if !s.Exclusive {
|
|
|
|
key = key + "-" + strconv.FormatInt(s.ServerID, 10)
|
|
|
|
}
|
|
|
|
txnResp, err := s.etcdCli.Txn(s.ctx).If(
|
|
|
|
clientv3.Compare(
|
2021-05-26 20:14:30 +08:00
|
|
|
clientv3.Version(path.Join(s.metaRoot, DefaultServiceRoot, key)),
|
2021-05-21 19:28:52 +08:00
|
|
|
"=",
|
|
|
|
0)).
|
2021-05-26 20:14:30 +08:00
|
|
|
Then(clientv3.OpPut(path.Join(s.metaRoot, DefaultServiceRoot, key), string(sessionJSON), clientv3.WithLease(resp.ID))).Commit()
|
2021-05-21 19:28:52 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("compare and swap error %s\n. maybe the key has registered", err)
|
|
|
|
return err
|
|
|
|
}
|
2021-05-18 11:39:21 +08:00
|
|
|
|
2021-05-21 19:28:52 +08:00
|
|
|
if !txnResp.Succeeded {
|
|
|
|
return fmt.Errorf("function CompareAndSwap error for compare is false for key: %s", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
ch, err = s.etcdCli.KeepAlive(s.ctx, resp.ID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("keep alive error %s\n", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-23 09:24:10 +08:00
|
|
|
err := retry.Do(s.ctx, registerFn, retry.Attempts(DefaultRetryTimes), retry.Sleep(500*time.Millisecond))
|
2021-05-18 11:39:21 +08:00
|
|
|
if err != nil {
|
2021-05-24 16:31:06 +08:00
|
|
|
return nil, err
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
2021-05-24 11:53:07 +08:00
|
|
|
// processKeepAliveResponse processes the response of etcd keepAlive interface
|
2021-05-18 11:39:21 +08:00
|
|
|
// If keepAlive fails for unexpected error, it will send a signal to the channel.
|
2021-05-21 19:28:52 +08:00
|
|
|
func (s *Session) processKeepAliveResponse(ch <-chan *clientv3.LeaseKeepAliveResponse) (failChannel <-chan bool) {
|
|
|
|
failCh := make(chan bool)
|
2021-05-18 11:39:21 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2021-05-21 19:28:52 +08:00
|
|
|
case <-s.ctx.Done():
|
2021-05-18 11:39:21 +08:00
|
|
|
log.Error("keep alive", zap.Error(errors.New("context done")))
|
|
|
|
return
|
|
|
|
case resp, ok := <-ch:
|
|
|
|
if !ok {
|
2021-05-24 11:53:07 +08:00
|
|
|
close(failCh)
|
2021-05-24 16:31:06 +08:00
|
|
|
return
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
2021-05-19 18:36:05 +08:00
|
|
|
if resp == nil {
|
2021-05-24 11:53:07 +08:00
|
|
|
close(failCh)
|
2021-05-24 16:31:06 +08:00
|
|
|
return
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
2021-06-15 18:53:55 +08:00
|
|
|
//failCh <- true
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2021-05-21 19:28:52 +08:00
|
|
|
return failCh
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
|
|
|
|
2021-05-21 19:28:52 +08:00
|
|
|
// GetSessions will get all sessions registered in etcd.
|
2021-06-23 17:14:10 +08:00
|
|
|
// Revision is returned for WatchServices to prevent key events from being missed.
|
2021-05-25 20:15:46 +08:00
|
|
|
func (s *Session) GetSessions(prefix string) (map[string]*Session, int64, error) {
|
2021-05-21 19:28:52 +08:00
|
|
|
res := make(map[string]*Session)
|
2021-05-26 20:14:30 +08:00
|
|
|
key := path.Join(s.metaRoot, DefaultServiceRoot, prefix)
|
2021-05-21 19:28:52 +08:00
|
|
|
resp, err := s.etcdCli.Get(s.ctx, key, clientv3.WithPrefix(),
|
|
|
|
clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend))
|
2021-05-18 11:39:21 +08:00
|
|
|
if err != nil {
|
2021-05-25 20:15:46 +08:00
|
|
|
return nil, 0, err
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
2021-06-15 10:19:38 +08:00
|
|
|
log.Debug("SessionUtil GetSessions", zap.Any("prefix", prefix), zap.Any("resp", resp))
|
2021-05-21 19:28:52 +08:00
|
|
|
for _, kv := range resp.Kvs {
|
2021-05-18 11:39:21 +08:00
|
|
|
session := &Session{}
|
2021-06-15 10:19:38 +08:00
|
|
|
err = json.Unmarshal(kv.Value, session)
|
2021-05-18 11:39:21 +08:00
|
|
|
if err != nil {
|
2021-05-25 20:15:46 +08:00
|
|
|
return nil, 0, err
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
2021-05-25 20:15:46 +08:00
|
|
|
_, mapKey := path.Split(string(kv.Key))
|
|
|
|
res[mapKey] = session
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
2021-05-25 20:15:46 +08:00
|
|
|
return res, resp.Header.Revision, nil
|
2021-05-19 18:36:05 +08:00
|
|
|
}
|
|
|
|
|
2021-06-23 17:14:10 +08:00
|
|
|
// SessionEvent indicates the changes of other servers.
|
|
|
|
// if a server is up, EventType is SessAddEvent.
|
|
|
|
// if a server is down, EventType is SessDelEvent.
|
|
|
|
// Session Saves the changed server's information.
|
|
|
|
type SessionEvent struct {
|
|
|
|
EventType SessionEventType
|
|
|
|
Session *Session
|
|
|
|
}
|
|
|
|
|
|
|
|
// WatchServices watch the service's up and down in etcd, and send event to
|
|
|
|
// eventChannel.
|
|
|
|
// prefix is a parameter to know which service to watch and can be obtained in
|
|
|
|
// typeutil.type.go.
|
|
|
|
// revision is a etcd reversion to prevent missing key events and can be obtained
|
|
|
|
// in GetSessions.
|
|
|
|
// If a server up, a event will be add to channel with eventType SessionAddType.
|
|
|
|
// If a server down, a event will be add to channel with eventType SessionDelType.
|
2021-05-25 20:15:46 +08:00
|
|
|
func (s *Session) WatchServices(prefix string, revision int64) (eventChannel <-chan *SessionEvent) {
|
|
|
|
eventCh := make(chan *SessionEvent, 100)
|
2021-05-26 20:14:30 +08:00
|
|
|
rch := s.etcdCli.Watch(s.ctx, path.Join(s.metaRoot, DefaultServiceRoot, prefix), clientv3.WithPrefix(), clientv3.WithPrevKV(), clientv3.WithRev(revision))
|
2021-05-18 11:39:21 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2021-05-21 19:28:52 +08:00
|
|
|
case <-s.ctx.Done():
|
2021-05-18 11:39:21 +08:00
|
|
|
return
|
|
|
|
case wresp, ok := <-rch:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, ev := range wresp.Events {
|
2021-05-25 20:15:46 +08:00
|
|
|
session := &Session{}
|
|
|
|
var eventType SessionEventType
|
2021-05-18 11:39:21 +08:00
|
|
|
switch ev.Type {
|
|
|
|
case mvccpb.PUT:
|
|
|
|
log.Debug("watch services",
|
2021-05-19 18:36:05 +08:00
|
|
|
zap.Any("add kv", ev.Kv))
|
|
|
|
err := json.Unmarshal([]byte(ev.Kv.Value), session)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("watch services", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
2021-05-25 20:15:46 +08:00
|
|
|
eventType = SessionAddEvent
|
2021-05-18 11:39:21 +08:00
|
|
|
case mvccpb.DELETE:
|
|
|
|
log.Debug("watch services",
|
2021-05-21 19:28:52 +08:00
|
|
|
zap.Any("delete kv", ev.PrevKv))
|
|
|
|
err := json.Unmarshal([]byte(ev.PrevKv.Value), session)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("watch services", zap.Error(err))
|
|
|
|
continue
|
2021-05-20 15:07:25 +08:00
|
|
|
}
|
2021-05-25 20:15:46 +08:00
|
|
|
eventType = SessionDelEvent
|
|
|
|
}
|
|
|
|
eventCh <- &SessionEvent{
|
|
|
|
EventType: eventType,
|
|
|
|
Session: session,
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2021-05-25 20:15:46 +08:00
|
|
|
return eventCh
|
2021-05-19 18:36:05 +08:00
|
|
|
}
|