@@ -2,7 +2,9 @@ package states
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
7
+ "path"
6
8
"strings"
7
9
"time"
8
10
@@ -15,9 +17,22 @@ const (
15
17
metaPath = `meta`
16
18
)
17
19
18
- func pingEtcd (ctx context.Context , cli clientv3.KV ) error {
19
- _ , err := cli .Get (ctx , "ping" )
20
- return err
20
+ var (
21
+ // ErrNotMilvsuRootPath sample error for non-valid root path.
22
+ ErrNotMilvsuRootPath = errors .New ("not a Milvus RootPath" )
23
+ )
24
+
25
+ func pingEtcd (ctx context.Context , cli clientv3.KV , rootPath string , metaPath string ) error {
26
+ key := path .Join (rootPath , metaPath , "session/id" )
27
+ resp , err := cli .Get (ctx , key )
28
+ if err != nil {
29
+ return err
30
+ }
31
+
32
+ if len (resp .Kvs ) == 0 {
33
+ return fmt .Errorf ("\" %s\" %w" , rootPath , ErrNotMilvsuRootPath )
34
+ }
35
+ return nil
21
36
}
22
37
23
38
// getConnectCommand returns the command for connect etcd.
@@ -58,8 +73,13 @@ func getConnectCommand(state State) *cobra.Command {
58
73
// ping etcd
59
74
ctx , cancel := context .WithTimeout (context .Background (), time .Second * 10 )
60
75
defer cancel ()
61
- err = pingEtcd (ctx , etcdCli )
76
+ err = pingEtcd (ctx , etcdCli , rootPath , metaPath )
62
77
if err != nil {
78
+ if errors .Is (err , ErrNotMilvsuRootPath ) {
79
+ etcdCli .Close ()
80
+ fmt .Printf ("Connection established, but %s, please check your config or use Dry mode\n " , err .Error ())
81
+ return nil
82
+ }
63
83
fmt .Println ("cannot connect to etcd with addr:" , etcdAddr , err .Error ())
64
84
return nil
65
85
}
0 commit comments