client.go 8.0 KB
Newer Older
C
cai.zhang 已提交
1 2 3 4 5 6 7 8 9 10 11
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.

12
package grpcdatacoordclient
S
sunby 已提交
13 14 15

import (
	"context"
G
godchen 已提交
16
	"fmt"
S
sunby 已提交
17 18
	"time"

G
godchen 已提交
19 20
	grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
	grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
G
godchen 已提交
21
	grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
X
Xiangyu Wang 已提交
22 23 24
	"github.com/milvus-io/milvus/internal/log"
	"github.com/milvus-io/milvus/internal/proto/milvuspb"
	"github.com/milvus-io/milvus/internal/util/retry"
G
godchen 已提交
25
	"github.com/milvus-io/milvus/internal/util/sessionutil"
G
godchen 已提交
26
	"github.com/milvus-io/milvus/internal/util/trace"
G
godchen 已提交
27
	"github.com/milvus-io/milvus/internal/util/typeutil"
G
godchen 已提交
28
	"go.uber.org/zap"
S
sunby 已提交
29
	"google.golang.org/grpc"
S
sunby 已提交
30

X
Xiangyu Wang 已提交
31 32 33
	"github.com/milvus-io/milvus/internal/proto/commonpb"
	"github.com/milvus-io/milvus/internal/proto/datapb"
	"github.com/milvus-io/milvus/internal/proto/internalpb"
S
sunby 已提交
34 35 36
)

type Client struct {
G
godchen 已提交
37 38 39
	ctx    context.Context
	cancel context.CancelFunc

40
	grpcClient datapb.DataCoordClient
S
sunby 已提交
41
	conn       *grpc.ClientConn
G
godchen 已提交
42

G
godchen 已提交
43
	sess *sessionutil.Session
G
godchen 已提交
44
	addr string
G
godchen 已提交
45

G
godchen 已提交
46
	retryOptions []retry.Option
G
godchen 已提交
47 48
}

49
func getDataCoordAddress(sess *sessionutil.Session) (string, error) {
50
	key := typeutil.DataCoordRole
G
godchen 已提交
51 52
	msess, _, err := sess.GetSessions(key)
	if err != nil {
53
		log.Debug("DataCoordClient, getSessions failed", zap.Any("key", key), zap.Error(err))
G
godchen 已提交
54 55 56 57
		return "", err
	}
	ms, ok := msess[key]
	if !ok {
58 59
		log.Debug("DataCoordClient, not existed in msess ", zap.Any("key", key), zap.Any("len of msess", len(msess)))
		return "", fmt.Errorf("number of datacoord is incorrect, %d", len(msess))
G
godchen 已提交
60 61
	}
	return ms.Address, nil
S
sunby 已提交
62 63
}

G
godchen 已提交
64 65 66 67 68 69
func NewClient(ctx context.Context, metaRoot string, etcdEndpoints []string, retryOptions ...retry.Option) (*Client, error) {
	sess := sessionutil.NewSession(ctx, metaRoot, etcdEndpoints)
	if sess == nil {
		err := fmt.Errorf("new session error, maybe can not connect to etcd")
		log.Debug("DataCoordClient NewClient failed", zap.Error(err))
		return nil, err
S
sunby 已提交
70
	}
G
godchen 已提交
71 72 73 74 75 76 77
	ctx, cancel := context.WithCancel(ctx)
	return &Client{
		ctx:          ctx,
		cancel:       cancel,
		sess:         sess,
		retryOptions: retryOptions,
	}, nil
S
sunby 已提交
78 79 80
}

func (c *Client) Init() error {
G
godchen 已提交
81
	return c.connect()
G
godchen 已提交
82 83
}

G
godchen 已提交
84
func (c *Client) connect() error {
G
godchen 已提交
85
	var err error
86 87
	getDataCoordAddressFn := func() error {
		c.addr, err = getDataCoordAddress(c.sess)
G
godchen 已提交
88 89 90 91 92
		if err != nil {
			return err
		}
		return nil
	}
G
godchen 已提交
93
	err = retry.Do(c.ctx, getDataCoordAddressFn, c.retryOptions...)
G
godchen 已提交
94
	if err != nil {
95
		log.Debug("DataCoordClient try reconnect getDataCoordAddressFn failed", zap.Error(err))
G
godchen 已提交
96 97
		return err
	}
98
	connectGrpcFunc := func() error {
G
godchen 已提交
99
		opts := trace.GetInterceptorOpts()
100
		log.Debug("DataCoordClient try reconnect ", zap.String("address", c.addr))
G
godchen 已提交
101 102
		conn, err := grpc.DialContext(c.ctx, c.addr,
			grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(5*time.Second),
G
godchen 已提交
103
			grpc.WithUnaryInterceptor(
G
godchen 已提交
104 105 106 107
				grpc_middleware.ChainUnaryClient(
					grpc_retry.UnaryClientInterceptor(),
					grpc_opentracing.UnaryClientInterceptor(opts...),
				)),
G
godchen 已提交
108
			grpc.WithStreamInterceptor(
G
godchen 已提交
109 110 111 112 113
				grpc_middleware.ChainStreamClient(
					grpc_retry.StreamClientInterceptor(),
					grpc_opentracing.StreamClientInterceptor(opts...),
				)),
		)
114 115
		if err != nil {
			return err
S
sunby 已提交
116
		}
117 118
		c.conn = conn
		return nil
S
sunby 已提交
119
	}
120

G
godchen 已提交
121
	err = retry.Do(c.ctx, connectGrpcFunc, c.retryOptions...)
S
sunby 已提交
122
	if err != nil {
123
		log.Debug("DataCoord try reconnect failed", zap.Error(err))
S
sunby 已提交
124 125
		return err
	}
126
	c.grpcClient = datapb.NewDataCoordClient(c.conn)
S
sunby 已提交
127 128 129
	return nil
}

G
godchen 已提交
130 131 132 133 134
func (c *Client) recall(caller func() (interface{}, error)) (interface{}, error) {
	ret, err := caller()
	if err == nil {
		return ret, nil
	}
G
godchen 已提交
135
	err = c.connect()
G
godchen 已提交
136
	if err != nil {
G
godchen 已提交
137
		return ret, err
G
godchen 已提交
138 139 140 141 142
	}
	ret, err = caller()
	if err == nil {
		return ret, nil
	}
G
godchen 已提交
143 144 145
	return ret, err
}

S
sunby 已提交
146 147 148 149 150
func (c *Client) Start() error {
	return nil
}

func (c *Client) Stop() error {
G
godchen 已提交
151
	c.cancel()
S
sunby 已提交
152
	return c.conn.Close()
S
sunby 已提交
153 154
}

155 156 157 158 159
// Register dumy
func (c *Client) Register() error {
	return nil
}

G
godchen 已提交
160
func (c *Client) GetComponentStates(ctx context.Context) (*internalpb.ComponentStates, error) {
G
godchen 已提交
161 162 163 164
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpcClient.GetComponentStates(ctx, &internalpb.GetComponentStatesRequest{})
	})
	return ret.(*internalpb.ComponentStates), err
S
sunby 已提交
165 166
}

G
godchen 已提交
167
func (c *Client) GetTimeTickChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
G
godchen 已提交
168 169 170 171
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpcClient.GetTimeTickChannel(ctx, &internalpb.GetTimeTickChannelRequest{})
	})
	return ret.(*milvuspb.StringResponse), err
S
sunby 已提交
172 173
}

G
godchen 已提交
174
func (c *Client) GetStatisticsChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
G
godchen 已提交
175 176 177 178
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpcClient.GetStatisticsChannel(ctx, &internalpb.GetStatisticsChannelRequest{})
	})
	return ret.(*milvuspb.StringResponse), err
S
sunby 已提交
179 180
}

G
godchen 已提交
181
func (c *Client) Flush(ctx context.Context, req *datapb.FlushRequest) (*commonpb.Status, error) {
G
godchen 已提交
182 183 184 185
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpcClient.Flush(ctx, req)
	})
	return ret.(*commonpb.Status), err
S
sunby 已提交
186 187
}

G
godchen 已提交
188
func (c *Client) AssignSegmentID(ctx context.Context, req *datapb.AssignSegmentIDRequest) (*datapb.AssignSegmentIDResponse, error) {
G
godchen 已提交
189 190 191 192
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpcClient.AssignSegmentID(ctx, req)
	})
	return ret.(*datapb.AssignSegmentIDResponse), err
S
sunby 已提交
193 194
}

G
godchen 已提交
195
func (c *Client) GetSegmentStates(ctx context.Context, req *datapb.GetSegmentStatesRequest) (*datapb.GetSegmentStatesResponse, error) {
G
godchen 已提交
196 197 198 199
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpcClient.GetSegmentStates(ctx, req)
	})
	return ret.(*datapb.GetSegmentStatesResponse), err
S
sunby 已提交
200 201
}

G
godchen 已提交
202
func (c *Client) GetInsertBinlogPaths(ctx context.Context, req *datapb.GetInsertBinlogPathsRequest) (*datapb.GetInsertBinlogPathsResponse, error) {
G
godchen 已提交
203 204 205 206
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpcClient.GetInsertBinlogPaths(ctx, req)
	})
	return ret.(*datapb.GetInsertBinlogPathsResponse), err
S
sunby 已提交
207 208
}

G
godchen 已提交
209
func (c *Client) GetCollectionStatistics(ctx context.Context, req *datapb.GetCollectionStatisticsRequest) (*datapb.GetCollectionStatisticsResponse, error) {
G
godchen 已提交
210 211 212 213
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpcClient.GetCollectionStatistics(ctx, req)
	})
	return ret.(*datapb.GetCollectionStatisticsResponse), err
S
sunby 已提交
214 215
}

G
godchen 已提交
216
func (c *Client) GetPartitionStatistics(ctx context.Context, req *datapb.GetPartitionStatisticsRequest) (*datapb.GetPartitionStatisticsResponse, error) {
G
godchen 已提交
217 218 219 220
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpcClient.GetPartitionStatistics(ctx, req)
	})
	return ret.(*datapb.GetPartitionStatisticsResponse), err
S
sunby 已提交
221
}
N
neza2017 已提交
222

G
godchen 已提交
223
func (c *Client) GetSegmentInfoChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
G
godchen 已提交
224 225 226 227
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpcClient.GetSegmentInfoChannel(ctx, &datapb.GetSegmentInfoChannelRequest{})
	})
	return ret.(*milvuspb.StringResponse), err
N
neza2017 已提交
228
}
229

G
godchen 已提交
230
func (c *Client) GetSegmentInfo(ctx context.Context, req *datapb.GetSegmentInfoRequest) (*datapb.GetSegmentInfoResponse, error) {
G
godchen 已提交
231 232 233 234
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpcClient.GetSegmentInfo(ctx, req)
	})
	return ret.(*datapb.GetSegmentInfoResponse), err
X
XuanYang-cn 已提交
235
}
236

237 238 239 240
func (c *Client) SaveBinlogPaths(ctx context.Context, req *datapb.SaveBinlogPathsRequest) (*commonpb.Status, error) {
	return c.grpcClient.SaveBinlogPaths(ctx, req)
}

241 242 243 244 245 246
func (c *Client) GetRecoveryInfo(ctx context.Context, req *datapb.GetRecoveryInfoRequest) (*datapb.GetRecoveryInfoResponse, error) {
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpcClient.GetRecoveryInfo(ctx, req)
	})
	return ret.(*datapb.GetRecoveryInfoResponse), err
}