client.go 8.1 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"
S
sunby 已提交
18 19
	"time"

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 {
G
godchen 已提交
80
	return c.connect(retry.Attempts(20))
G
godchen 已提交
81 82
}

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

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

G
godchen 已提交
129 130 131 132 133
func (c *Client) recall(caller func() (interface{}, error)) (interface{}, error) {
	ret, err := caller()
	if err == nil {
		return ret, nil
	}
G
godchen 已提交
134
	err = c.connect()
G
godchen 已提交
135
	if err != nil {
G
godchen 已提交
136
		return ret, errors.New("Connect to datacoord failed with error:\n" + err.Error())
G
godchen 已提交
137 138 139 140 141
	}
	ret, err = caller()
	if err == nil {
		return ret, nil
	}
G
godchen 已提交
142 143 144
	return ret, err
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

240 241 242 243 244 245
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
}