client.go 6.7 KB
Newer Older
X
XuanYang-cn 已提交
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 grpcdatanodeclient
X
XuanYang-cn 已提交
13 14 15

import (
	"context"
G
godchen 已提交
16
	"fmt"
D
dragondriver 已提交
17
	"sync"
G
godchen 已提交
18
	"time"
X
XuanYang-cn 已提交
19

X
Xiangyu Wang 已提交
20 21
	"github.com/milvus-io/milvus/internal/log"
	"github.com/milvus-io/milvus/internal/util/retry"
N
neza2017 已提交
22

G
godchen 已提交
23 24
	grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
	grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
G
godchen 已提交
25
	grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
X
Xiangyu Wang 已提交
26 27 28 29
	"github.com/milvus-io/milvus/internal/proto/commonpb"
	"github.com/milvus-io/milvus/internal/proto/datapb"
	"github.com/milvus-io/milvus/internal/proto/internalpb"
	"github.com/milvus-io/milvus/internal/proto/milvuspb"
G
godchen 已提交
30
	"github.com/milvus-io/milvus/internal/util/trace"
31
	"google.golang.org/grpc/codes"
X
XuanYang-cn 已提交
32

X
XuanYang-cn 已提交
33
	"go.uber.org/zap"
X
XuanYang-cn 已提交
34
	"google.golang.org/grpc"
X
XuanYang-cn 已提交
35 36
)

S
sunby 已提交
37
// Client is the grpc client for DataCoord
X
XuanYang-cn 已提交
38
type Client struct {
G
godchen 已提交
39 40
	ctx    context.Context
	cancel context.CancelFunc
G
godchen 已提交
41

D
dragondriver 已提交
42 43 44
	grpc    datapb.DataNodeClient
	conn    *grpc.ClientConn
	grpcMtx sync.RWMutex
G
godchen 已提交
45

G
godchen 已提交
46
	addr string
G
godchen 已提交
47

G
godchen 已提交
48
	retryOptions []retry.Option
49 50 51 52 53 54

	getGrpcClient func() (datapb.DataNodeClient, error)
}

func (c *Client) setGetGrpcClientFunc() {
	c.getGrpcClient = c.getGrpcClientFunc
X
XuanYang-cn 已提交
55
}
X
XuanYang-cn 已提交
56

57
func (c *Client) getGrpcClientFunc() (datapb.DataNodeClient, error) {
D
dragondriver 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	c.grpcMtx.RLock()
	if c.grpc != nil {
		defer c.grpcMtx.RUnlock()
		return c.grpc, nil
	}
	c.grpcMtx.RUnlock()

	c.grpcMtx.Lock()
	defer c.grpcMtx.Unlock()

	if c.grpc != nil {
		return c.grpc, nil
	}

	// FIXME(dragondriver): how to handle error here?
	// if we return nil here, then we should check if client is nil outside,
	err := c.connect(retry.Attempts(20))
	if err != nil {
		return nil, err
	}

	return c.grpc, nil
}

func (c *Client) resetConnection() {
	c.grpcMtx.Lock()
	defer c.grpcMtx.Unlock()

	if c.conn != nil {
		_ = c.conn.Close()
	}
	c.conn = nil
	c.grpc = nil
}

G
godchen 已提交
93
func NewClient(ctx context.Context, addr string, retryOptions ...retry.Option) (*Client, error) {
G
godchen 已提交
94
	if addr == "" {
G
godchen 已提交
95
		return nil, fmt.Errorf("address is empty")
G
godchen 已提交
96 97
	}

G
godchen 已提交
98
	ctx, cancel := context.WithCancel(ctx)
99
	client := &Client{
G
godchen 已提交
100 101 102 103
		ctx:          ctx,
		cancel:       cancel,
		addr:         addr,
		retryOptions: retryOptions,
104 105 106 107
	}

	client.setGetGrpcClientFunc()
	return client, nil
X
XuanYang-cn 已提交
108 109
}

N
neza2017 已提交
110
func (c *Client) Init() error {
111
	Params.Init()
112
	return nil
G
godchen 已提交
113 114
}

G
godchen 已提交
115
func (c *Client) connect(retryOptions ...retry.Option) error {
116
	connectGrpcFunc := func() error {
G
godchen 已提交
117 118
		opts := trace.GetInterceptorOpts()
		log.Debug("DataNode connect ", zap.String("address", c.addr))
G
godchen 已提交
119 120 121
		ctx, cancel := context.WithTimeout(c.ctx, 15*time.Second)
		defer cancel()
		conn, err := grpc.DialContext(ctx, c.addr,
G
godchen 已提交
122
			grpc.WithInsecure(), grpc.WithBlock(),
123 124 125
			grpc.WithDefaultCallOptions(
				grpc.MaxCallRecvMsgSize(Params.ClientMaxRecvSize),
				grpc.MaxCallSendMsgSize(Params.ClientMaxSendSize)),
C
congqixia 已提交
126
			grpc.WithDisableRetry(),
G
godchen 已提交
127
			grpc.WithUnaryInterceptor(
G
godchen 已提交
128
				grpc_middleware.ChainUnaryClient(
129 130 131 132
					grpc_retry.UnaryClientInterceptor(
						grpc_retry.WithMax(3),
						grpc_retry.WithCodes(codes.Aborted, codes.Unavailable),
					),
G
godchen 已提交
133 134
					grpc_opentracing.UnaryClientInterceptor(opts...),
				)),
G
godchen 已提交
135
			grpc.WithStreamInterceptor(
G
godchen 已提交
136
				grpc_middleware.ChainStreamClient(
137 138 139 140
					grpc_retry.StreamClientInterceptor(
						grpc_retry.WithMax(3),
						grpc_retry.WithCodes(codes.Aborted, codes.Unavailable),
					),
G
godchen 已提交
141 142 143
					grpc_opentracing.StreamClientInterceptor(opts...),
				)),
		)
144 145
		if err != nil {
			return err
X
XuanYang-cn 已提交
146
		}
D
dragondriver 已提交
147 148 149
		if c.conn != nil {
			_ = c.conn.Close()
		}
150 151
		c.conn = conn
		return nil
X
XuanYang-cn 已提交
152
	}
153

G
godchen 已提交
154
	err := retry.Do(c.ctx, connectGrpcFunc, retryOptions...)
X
XuanYang-cn 已提交
155
	if err != nil {
156
		log.Debug("DataNodeClient try connect failed", zap.Error(err))
X
XuanYang-cn 已提交
157 158
		return err
	}
159
	log.Debug("DataNodeClient connect success")
X
XuanYang-cn 已提交
160 161
	c.grpc = datapb.NewDataNodeClient(c.conn)
	return nil
X
XuanYang-cn 已提交
162 163
}

G
godchen 已提交
164 165 166 167 168
func (c *Client) recall(caller func() (interface{}, error)) (interface{}, error) {
	ret, err := caller()
	if err == nil {
		return ret, nil
	}
N
neza2017 已提交
169
	log.Debug("DataNode Client grpc error", zap.Error(err))
D
dragondriver 已提交
170 171 172

	c.resetConnection()

G
godchen 已提交
173 174 175 176
	ret, err = caller()
	if err == nil {
		return ret, nil
	}
G
godchen 已提交
177 178 179
	return ret, err
}

N
neza2017 已提交
180
func (c *Client) Start() error {
X
XuanYang-cn 已提交
181
	return nil
X
XuanYang-cn 已提交
182 183
}

N
neza2017 已提交
184
func (c *Client) Stop() error {
G
godchen 已提交
185
	c.cancel()
D
dragondriver 已提交
186 187 188 189 190 191
	c.grpcMtx.Lock()
	defer c.grpcMtx.Unlock()
	if c.conn != nil {
		return c.conn.Close()
	}
	return nil
X
XuanYang-cn 已提交
192 193
}

194 195 196 197 198
// Register dummy
func (c *Client) Register() error {
	return nil
}

G
godchen 已提交
199
func (c *Client) GetComponentStates(ctx context.Context) (*internalpb.ComponentStates, error) {
G
godchen 已提交
200
	ret, err := c.recall(func() (interface{}, error) {
D
dragondriver 已提交
201 202 203 204 205 206
		client, err := c.getGrpcClient()
		if err != nil {
			return nil, err
		}

		return client.GetComponentStates(ctx, &internalpb.GetComponentStatesRequest{})
G
godchen 已提交
207
	})
D
dragondriver 已提交
208 209 210
	if err != nil || ret == nil {
		return nil, err
	}
G
godchen 已提交
211
	return ret.(*internalpb.ComponentStates), err
N
neza2017 已提交
212 213 214
}

func (c *Client) GetStatisticsChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
G
godchen 已提交
215
	ret, err := c.recall(func() (interface{}, error) {
D
dragondriver 已提交
216 217 218 219 220 221
		client, err := c.getGrpcClient()
		if err != nil {
			return nil, err
		}

		return client.GetStatisticsChannel(ctx, &internalpb.GetStatisticsChannelRequest{})
G
godchen 已提交
222
	})
D
dragondriver 已提交
223 224 225
	if err != nil || ret == nil {
		return nil, err
	}
G
godchen 已提交
226
	return ret.(*milvuspb.StringResponse), err
X
XuanYang-cn 已提交
227 228
}

G
godchen 已提交
229
func (c *Client) WatchDmChannels(ctx context.Context, req *datapb.WatchDmChannelsRequest) (*commonpb.Status, error) {
G
godchen 已提交
230
	ret, err := c.recall(func() (interface{}, error) {
D
dragondriver 已提交
231 232 233 234 235 236
		client, err := c.getGrpcClient()
		if err != nil {
			return nil, err
		}

		return client.WatchDmChannels(ctx, req)
G
godchen 已提交
237
	})
D
dragondriver 已提交
238 239 240
	if err != nil || ret == nil {
		return nil, err
	}
G
godchen 已提交
241
	return ret.(*commonpb.Status), err
X
XuanYang-cn 已提交
242 243
}

G
godchen 已提交
244
func (c *Client) FlushSegments(ctx context.Context, req *datapb.FlushSegmentsRequest) (*commonpb.Status, error) {
G
godchen 已提交
245
	ret, err := c.recall(func() (interface{}, error) {
D
dragondriver 已提交
246 247 248 249 250 251
		client, err := c.getGrpcClient()
		if err != nil {
			return nil, err
		}

		return client.FlushSegments(ctx, req)
G
godchen 已提交
252
	})
D
dragondriver 已提交
253 254 255
	if err != nil || ret == nil {
		return nil, err
	}
G
godchen 已提交
256
	return ret.(*commonpb.Status), err
X
XuanYang-cn 已提交
257
}
258 259 260

func (c *Client) GetMetrics(ctx context.Context, req *milvuspb.GetMetricsRequest) (*milvuspb.GetMetricsResponse, error) {
	ret, err := c.recall(func() (interface{}, error) {
D
dragondriver 已提交
261 262 263 264 265 266
		client, err := c.getGrpcClient()
		if err != nil {
			return nil, err
		}

		return client.GetMetrics(ctx, req)
267
	})
D
dragondriver 已提交
268 269 270
	if err != nil || ret == nil {
		return nil, err
	}
271 272
	return ret.(*milvuspb.GetMetricsResponse), err
}