client.go 6.5 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 37
)

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

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

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

G
godchen 已提交
47
	retryOptions []retry.Option
X
XuanYang-cn 已提交
48
}
X
XuanYang-cn 已提交
49

D
dragondriver 已提交
50 51 52 53 54 55 56 57 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
func (c *Client) getGrpcClient() (datapb.DataNodeClient, error) {
	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 已提交
86
func NewClient(ctx context.Context, addr string, retryOptions ...retry.Option) (*Client, error) {
G
godchen 已提交
87
	if addr == "" {
G
godchen 已提交
88
		return nil, fmt.Errorf("address is empty")
G
godchen 已提交
89 90
	}

G
godchen 已提交
91
	ctx, cancel := context.WithCancel(ctx)
G
godchen 已提交
92
	return &Client{
G
godchen 已提交
93 94 95 96
		ctx:          ctx,
		cancel:       cancel,
		addr:         addr,
		retryOptions: retryOptions,
G
godchen 已提交
97
	}, nil
X
XuanYang-cn 已提交
98 99
}

N
neza2017 已提交
100
func (c *Client) Init() error {
101
	Params.Init()
G
godchen 已提交
102
	return c.connect(retry.Attempts(20))
G
godchen 已提交
103 104
}

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

G
godchen 已提交
144
	err := retry.Do(c.ctx, connectGrpcFunc, retryOptions...)
X
XuanYang-cn 已提交
145
	if err != nil {
146
		log.Debug("DataNodeClient try connect failed", zap.Error(err))
X
XuanYang-cn 已提交
147 148
		return err
	}
149
	log.Debug("DataNodeClient connect success")
X
XuanYang-cn 已提交
150 151
	c.grpc = datapb.NewDataNodeClient(c.conn)
	return nil
X
XuanYang-cn 已提交
152 153
}

G
godchen 已提交
154 155 156 157 158
func (c *Client) recall(caller func() (interface{}, error)) (interface{}, error) {
	ret, err := caller()
	if err == nil {
		return ret, nil
	}
N
neza2017 已提交
159
	log.Debug("DataNode Client grpc error", zap.Error(err))
D
dragondriver 已提交
160 161 162

	c.resetConnection()

G
godchen 已提交
163 164 165 166
	ret, err = caller()
	if err == nil {
		return ret, nil
	}
G
godchen 已提交
167 168 169
	return ret, err
}

N
neza2017 已提交
170
func (c *Client) Start() error {
X
XuanYang-cn 已提交
171
	return nil
X
XuanYang-cn 已提交
172 173
}

N
neza2017 已提交
174
func (c *Client) Stop() error {
G
godchen 已提交
175
	c.cancel()
D
dragondriver 已提交
176 177 178 179 180 181
	c.grpcMtx.Lock()
	defer c.grpcMtx.Unlock()
	if c.conn != nil {
		return c.conn.Close()
	}
	return nil
X
XuanYang-cn 已提交
182 183
}

184 185 186 187 188
// Register dummy
func (c *Client) Register() error {
	return nil
}

G
godchen 已提交
189
func (c *Client) GetComponentStates(ctx context.Context) (*internalpb.ComponentStates, error) {
G
godchen 已提交
190
	ret, err := c.recall(func() (interface{}, error) {
D
dragondriver 已提交
191 192 193 194 195 196
		client, err := c.getGrpcClient()
		if err != nil {
			return nil, err
		}

		return client.GetComponentStates(ctx, &internalpb.GetComponentStatesRequest{})
G
godchen 已提交
197
	})
D
dragondriver 已提交
198 199 200
	if err != nil || ret == nil {
		return nil, err
	}
G
godchen 已提交
201
	return ret.(*internalpb.ComponentStates), err
N
neza2017 已提交
202 203 204
}

func (c *Client) GetStatisticsChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
G
godchen 已提交
205
	ret, err := c.recall(func() (interface{}, error) {
D
dragondriver 已提交
206 207 208 209 210 211
		client, err := c.getGrpcClient()
		if err != nil {
			return nil, err
		}

		return client.GetStatisticsChannel(ctx, &internalpb.GetStatisticsChannelRequest{})
G
godchen 已提交
212
	})
D
dragondriver 已提交
213 214 215
	if err != nil || ret == nil {
		return nil, err
	}
G
godchen 已提交
216
	return ret.(*milvuspb.StringResponse), err
X
XuanYang-cn 已提交
217 218
}

G
godchen 已提交
219
func (c *Client) WatchDmChannels(ctx context.Context, req *datapb.WatchDmChannelsRequest) (*commonpb.Status, error) {
G
godchen 已提交
220
	ret, err := c.recall(func() (interface{}, error) {
D
dragondriver 已提交
221 222 223 224 225 226
		client, err := c.getGrpcClient()
		if err != nil {
			return nil, err
		}

		return client.WatchDmChannels(ctx, req)
G
godchen 已提交
227
	})
D
dragondriver 已提交
228 229 230
	if err != nil || ret == nil {
		return nil, err
	}
G
godchen 已提交
231
	return ret.(*commonpb.Status), err
X
XuanYang-cn 已提交
232 233
}

G
godchen 已提交
234
func (c *Client) FlushSegments(ctx context.Context, req *datapb.FlushSegmentsRequest) (*commonpb.Status, error) {
G
godchen 已提交
235
	ret, err := c.recall(func() (interface{}, error) {
D
dragondriver 已提交
236 237 238 239 240 241
		client, err := c.getGrpcClient()
		if err != nil {
			return nil, err
		}

		return client.FlushSegments(ctx, req)
G
godchen 已提交
242
	})
D
dragondriver 已提交
243 244 245
	if err != nil || ret == nil {
		return nil, err
	}
G
godchen 已提交
246
	return ret.(*commonpb.Status), err
X
XuanYang-cn 已提交
247
}
248 249 250

func (c *Client) GetMetrics(ctx context.Context, req *milvuspb.GetMetricsRequest) (*milvuspb.GetMetricsResponse, error) {
	ret, err := c.recall(func() (interface{}, error) {
D
dragondriver 已提交
251 252 253 254 255 256
		client, err := c.getGrpcClient()
		if err != nil {
			return nil, err
		}

		return client.GetMetrics(ctx, req)
257
	})
D
dragondriver 已提交
258 259 260
	if err != nil || ret == nil {
		return nil, err
	}
261 262
	return ret.(*milvuspb.GetMetricsResponse), err
}