未验证 提交 2e37fca2 编写于 作者: N neza2017 提交者: GitHub

add log in session (#6136)

Signed-off-by: Nyefu.chen <yefu.chen@zilliz.com>
上级 42be531f
...@@ -60,6 +60,7 @@ func NewSession(ctx context.Context, metaRoot string, etcdEndpoints []string) *S ...@@ -60,6 +60,7 @@ func NewSession(ctx context.Context, metaRoot string, etcdEndpoints []string) *S
} }
connectEtcdFn := func() error { connectEtcdFn := func() error {
log.Debug("Session try to connect to etcd")
etcdCli, err := clientv3.New(clientv3.Config{Endpoints: etcdEndpoints, DialTimeout: 5 * time.Second}) etcdCli, err := clientv3.New(clientv3.Config{Endpoints: etcdEndpoints, DialTimeout: 5 * time.Second})
if err != nil { if err != nil {
return err return err
...@@ -71,6 +72,7 @@ func NewSession(ctx context.Context, metaRoot string, etcdEndpoints []string) *S ...@@ -71,6 +72,7 @@ func NewSession(ctx context.Context, metaRoot string, etcdEndpoints []string) *S
if err != nil { if err != nil {
return nil return nil
} }
log.Debug("Sessiont connect to etcd success")
return session return session
} }
...@@ -109,40 +111,40 @@ func (s *Session) checkIDExist() { ...@@ -109,40 +111,40 @@ func (s *Session) checkIDExist() {
} }
func (s *Session) getServerIDWithKey(key string, retryTimes uint) (int64, error) { func (s *Session) getServerIDWithKey(key string, retryTimes uint) (int64, error) {
res := int64(0) for {
getServerIDWithKeyFn := func() error { log.Debug("Session try to get servdeID")
getResp, err := s.etcdCli.Get(s.ctx, path.Join(s.metaRoot, DefaultServiceRoot, key)) getResp, err := s.etcdCli.Get(s.ctx, path.Join(s.metaRoot, DefaultServiceRoot, key))
if err != nil { if err != nil {
return nil log.Debug("Session get etcd key error", zap.String("key", key), zap.Error(err))
return -1, err
} }
if getResp.Count <= 0 { if getResp.Count <= 0 {
return fmt.Errorf("there is no value on key = %s", key) log.Debug("Session there is no value", zap.String("key", key))
continue
} }
value := string(getResp.Kvs[0].Value) value := string(getResp.Kvs[0].Value)
valueInt, err := strconv.ParseInt(value, 10, 64) valueInt, err := strconv.ParseInt(value, 10, 64)
if err != nil { if err != nil {
log.Debug("session", zap.Error(err)) log.Debug("Session ParseInt error", zap.String("value", value), zap.Error(err))
return err continue
} }
txnResp, err := s.etcdCli.Txn(s.ctx).If( txnResp, err := s.etcdCli.Txn(s.ctx).If(
clientv3.Compare( clientv3.Compare(
clientv3.Value(path.Join(s.metaRoot, DefaultServiceRoot, DefaultIDKey)), clientv3.Value(path.Join(s.metaRoot, DefaultServiceRoot, key)),
"=", "=",
value)). value)).
Then(clientv3.OpPut(path.Join(s.metaRoot, DefaultServiceRoot, DefaultIDKey), strconv.FormatInt(valueInt+1, 10))).Commit() Then(clientv3.OpPut(path.Join(s.metaRoot, DefaultServiceRoot, key), strconv.FormatInt(valueInt+1, 10))).Commit()
if err != nil { if err != nil {
return err log.Debug("Session Txn failed", zap.String("key", key), zap.Error(err))
return -1, err
} }
if !txnResp.Succeeded { if !txnResp.Succeeded {
return fmt.Errorf("function CompareAndSwap error for compare is false for key: %s", key) log.Debug("Session Txn unsuccess", zap.String("key", key))
continue
} }
res = valueInt return valueInt, nil
return nil
} }
err := retry.Do(s.ctx, getServerIDWithKeyFn, retry.Attempts(retryTimes), retry.Sleep(500*time.Millisecond))
return res, err
} }
// registerService registers the service to etcd so that other services // registerService registers the service to etcd so that other services
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册