client.go 8.6 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
	"errors"
G
godchen 已提交
17
	"fmt"
G
godchen 已提交
18
	"time"
S
sunby 已提交
19

G
godchen 已提交
20 21
	grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
	grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
G
godchen 已提交
22
	grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
X
Xiangyu Wang 已提交
23 24 25
	"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 已提交
26
	"github.com/milvus-io/milvus/internal/util/sessionutil"
G
godchen 已提交
27
	"github.com/milvus-io/milvus/internal/util/trace"
G
godchen 已提交
28
	"github.com/milvus-io/milvus/internal/util/typeutil"
G
godchen 已提交
29
	"go.uber.org/zap"
S
sunby 已提交
30
	"google.golang.org/grpc"
31
	"google.golang.org/grpc/codes"
S
sunby 已提交
32

X
Xiangyu Wang 已提交
33 34 35
	"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 已提交
36 37 38
)

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

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

G
godchen 已提交
45
	sess *sessionutil.Session
G
godchen 已提交
46
	addr string
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
func NewClient(ctx context.Context, metaRoot string, etcdEndpoints []string) (*Client, error) {
G
godchen 已提交
65 66 67 68 69
	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
	ctx, cancel := context.WithCancel(ctx)
	return &Client{
G
godchen 已提交
73 74 75
		ctx:    ctx,
		cancel: cancel,
		sess:   sess,
G
godchen 已提交
76
	}, nil
S
sunby 已提交
77 78 79
}

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

G
godchen 已提交
84
func (c *Client) connect(retryOptions ...retry.Option) error {
G
godchen 已提交
85
	var err error
G
godchen 已提交
86
	connectDataCoordFn := func() error {
87
		c.addr, err = getDataCoordAddress(c.sess)
G
godchen 已提交
88
		if err != nil {
G
godchen 已提交
89
			log.Debug("DataCoordClient getDataCoordAddr failed", zap.Error(err))
G
godchen 已提交
90 91
			return err
		}
G
godchen 已提交
92
		opts := trace.GetInterceptorOpts()
93
		log.Debug("DataCoordClient try reconnect ", zap.String("address", c.addr))
G
godchen 已提交
94 95 96
		ctx, cancel := context.WithTimeout(c.ctx, 15*time.Second)
		defer cancel()
		conn, err := grpc.DialContext(ctx, c.addr,
G
godchen 已提交
97
			grpc.WithInsecure(), grpc.WithBlock(),
98 99 100
			grpc.WithDefaultCallOptions(
				grpc.MaxCallRecvMsgSize(Params.ClientMaxRecvSize),
				grpc.MaxCallSendMsgSize(Params.ClientMaxSendSize)),
G
godchen 已提交
101
			grpc.WithUnaryInterceptor(
G
godchen 已提交
102
				grpc_middleware.ChainUnaryClient(
103 104 105 106
					grpc_retry.UnaryClientInterceptor(
						grpc_retry.WithMax(3),
						grpc_retry.WithCodes(codes.Aborted, codes.Unavailable),
					),
G
godchen 已提交
107 108
					grpc_opentracing.UnaryClientInterceptor(opts...),
				)),
G
godchen 已提交
109
			grpc.WithStreamInterceptor(
G
godchen 已提交
110
				grpc_middleware.ChainStreamClient(
111 112 113
					grpc_retry.StreamClientInterceptor(grpc_retry.WithMax(3),
						grpc_retry.WithCodes(codes.Aborted, codes.Unavailable),
					),
G
godchen 已提交
114 115 116
					grpc_opentracing.StreamClientInterceptor(opts...),
				)),
		)
117 118
		if err != nil {
			return err
S
sunby 已提交
119
		}
120 121
		c.conn = conn
		return nil
S
sunby 已提交
122
	}
123

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

G
godchen 已提交
133 134 135 136 137
func (c *Client) recall(caller func() (interface{}, error)) (interface{}, error) {
	ret, err := caller()
	if err == nil {
		return ret, nil
	}
N
neza2017 已提交
138
	log.Debug("DataCoord Client grpc error", zap.Error(err))
G
godchen 已提交
139
	err = c.connect()
G
godchen 已提交
140
	if err != nil {
G
godchen 已提交
141
		return ret, errors.New("Connect to datacoord failed with error:\n" + err.Error())
G
godchen 已提交
142 143 144 145 146
	}
	ret, err = caller()
	if err == nil {
		return ret, nil
	}
G
godchen 已提交
147 148 149
	return ret, err
}

S
sunby 已提交
150 151 152 153 154
func (c *Client) Start() error {
	return nil
}

func (c *Client) Stop() error {
G
godchen 已提交
155
	c.cancel()
S
sunby 已提交
156
	return c.conn.Close()
S
sunby 已提交
157 158
}

159 160 161 162 163
// Register dumy
func (c *Client) Register() error {
	return nil
}

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

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

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

185
func (c *Client) Flush(ctx context.Context, req *datapb.FlushRequest) (*datapb.FlushResponse, error) {
G
godchen 已提交
186 187 188
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpcClient.Flush(ctx, req)
	})
189
	return ret.(*datapb.FlushResponse), err
S
sunby 已提交
190 191
}

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

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

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

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

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

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

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

241 242 243 244
func (c *Client) SaveBinlogPaths(ctx context.Context, req *datapb.SaveBinlogPathsRequest) (*commonpb.Status, error) {
	return c.grpcClient.SaveBinlogPaths(ctx, req)
}

245 246 247 248 249 250
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
}
251 252 253 254 255 256 257

func (c *Client) GetFlushedSegments(ctx context.Context, req *datapb.GetFlushedSegmentsRequest) (*datapb.GetFlushedSegmentsResponse, error) {
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpcClient.GetFlushedSegments(ctx, req)
	})
	return ret.(*datapb.GetFlushedSegmentsResponse), err
}