Skip to content

Commit b9525ec

Browse files
authored
Add rootPath check for connect etcd command (#99)
Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
1 parent fcaa02b commit b9525ec

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

states/etcd/audit/audit_write.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ func (c *FileAuditKV) writeKeyValue(key, value string) {
3535
func (c *FileAuditKV) writeData(data []byte) {
3636
lb := make([]byte, 8)
3737
binary.LittleEndian.PutUint64(lb, uint64(len(data)))
38-
n, err := c.file.Write(lb)
39-
fmt.Println(n, err)
38+
_, err := c.file.Write(lb)
39+
if err != nil {
40+
fmt.Println("failed to write audit header", err.Error())
41+
return
42+
}
4043
if len(data) > 0 {
41-
n, err = c.file.Write(data)
42-
fmt.Println(n, err)
44+
_, err = c.file.Write(data)
45+
fmt.Println("failed to write audit log", err.Error())
4346
}
4447
}

states/etcd_connect.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package states
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
7+
"path"
68
"strings"
79
"time"
810

@@ -15,9 +17,22 @@ const (
1517
metaPath = `meta`
1618
)
1719

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
2136
}
2237

2338
// getConnectCommand returns the command for connect etcd.
@@ -58,8 +73,13 @@ func getConnectCommand(state State) *cobra.Command {
5873
// ping etcd
5974
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
6075
defer cancel()
61-
err = pingEtcd(ctx, etcdCli)
76+
err = pingEtcd(ctx, etcdCli, rootPath, metaPath)
6277
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+
}
6383
fmt.Println("cannot connect to etcd with addr:", etcdAddr, err.Error())
6484
return nil
6585
}

0 commit comments

Comments
 (0)