client.go 5.3 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 17
	"fmt"
	"strconv"
X
XuanYang-cn 已提交
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"
G
godchen 已提交
22 23
	otgrpc "github.com/opentracing-contrib/go-grpc"
	"github.com/opentracing/opentracing-go"
N
neza2017 已提交
24

X
Xiangyu Wang 已提交
25 26 27 28
	"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"
X
XuanYang-cn 已提交
29

G
godchen 已提交
30 31
	"github.com/milvus-io/milvus/internal/util/sessionutil"
	"github.com/milvus-io/milvus/internal/util/typeutil"
X
XuanYang-cn 已提交
32
	"go.uber.org/zap"
X
XuanYang-cn 已提交
33
	"google.golang.org/grpc"
X
XuanYang-cn 已提交
34 35 36
)

type Client struct {
G
godchen 已提交
37 38 39 40 41 42 43 44 45 46 47 48
	ctx context.Context

	grpc datapb.DataNodeClient
	conn *grpc.ClientConn

	address  string
	serverID int64

	sess      *sessionutil.Session
	timeout   time.Duration
	reconnTry int
	recallTry int
X
XuanYang-cn 已提交
49
}
X
XuanYang-cn 已提交
50

G
godchen 已提交
51 52
func getDataNodeAddress(sess *sessionutil.Session, serverID int64) (string, error) {
	key := typeutil.DataNodeRole + "-" + strconv.FormatInt(serverID, 10)
G
godchen 已提交
53
	msess, _, err := sess.GetSessions(key)
G
godchen 已提交
54 55
	if err != nil {
		return "", err
X
XuanYang-cn 已提交
56
	}
G
godchen 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	ms, ok := msess[key]
	if !ok {
		return "", fmt.Errorf("number of master service is incorrect, %d", len(msess))
	}
	return ms.Address, nil
}

func NewClient(address string, serverID int64, etcdAddr []string, timeout time.Duration) (*Client, error) {
	sess := sessionutil.NewSession(context.Background(), etcdAddr)

	return &Client{
		grpc:      nil,
		conn:      nil,
		address:   address,
		ctx:       context.Background(),
		sess:      sess,
		timeout:   timeout,
		recallTry: 3,
		reconnTry: 10,
	}, nil
X
XuanYang-cn 已提交
77 78
}

N
neza2017 已提交
79
func (c *Client) Init() error {
G
godchen 已提交
80
	tracer := opentracing.GlobalTracer()
G
godchen 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	if c.address != "" {
		connectGrpcFunc := func() error {
			log.Debug("DataNode connect ", zap.String("address", c.address))
			conn, err := grpc.DialContext(c.ctx, c.address, grpc.WithInsecure(), grpc.WithBlock(),
				grpc.WithUnaryInterceptor(
					otgrpc.OpenTracingClientInterceptor(tracer)),
				grpc.WithStreamInterceptor(
					otgrpc.OpenTracingStreamClientInterceptor(tracer)))
			if err != nil {
				return err
			}
			c.conn = conn
			return nil
		}

		err := retry.Retry(c.reconnTry, time.Millisecond*500, connectGrpcFunc)
		if err != nil {
			return err
		}
	} else {
		return c.reconnect()
	}
	c.grpc = datapb.NewDataNodeClient(c.conn)
	return nil
}

func (c *Client) reconnect() error {
	tracer := opentracing.GlobalTracer()
	var err error
	getDataNodeAddressFn := func() error {
		c.address, err = getDataNodeAddress(c.sess, c.serverID)
		if err != nil {
			return err
		}
		return nil
	}
	err = retry.Retry(c.reconnTry, 3*time.Second, getDataNodeAddressFn)
	if err != nil {
		return err
	}
121
	connectGrpcFunc := func() error {
G
godchen 已提交
122
		log.Debug("DataNode connect ", zap.String("address", c.address))
G
godchen 已提交
123 124 125 126 127
		conn, err := grpc.DialContext(c.ctx, c.address, grpc.WithInsecure(), grpc.WithBlock(),
			grpc.WithUnaryInterceptor(
				otgrpc.OpenTracingClientInterceptor(tracer)),
			grpc.WithStreamInterceptor(
				otgrpc.OpenTracingStreamClientInterceptor(tracer)))
128 129
		if err != nil {
			return err
X
XuanYang-cn 已提交
130
		}
131 132
		c.conn = conn
		return nil
X
XuanYang-cn 已提交
133
	}
134

G
godchen 已提交
135
	err = retry.Retry(c.reconnTry, 500*time.Millisecond, connectGrpcFunc)
X
XuanYang-cn 已提交
136 137 138 139 140
	if err != nil {
		return err
	}
	c.grpc = datapb.NewDataNodeClient(c.conn)
	return nil
X
XuanYang-cn 已提交
141 142
}

G
godchen 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
func (c *Client) recall(caller func() (interface{}, error)) (interface{}, error) {
	ret, err := caller()
	if err == nil {
		return ret, nil
	}
	for i := 0; i < c.recallTry; i++ {
		err = c.reconnect()
		if err == nil {
			ret, err = caller()
			if err == nil {
				return ret, nil
			}
		}
	}
	return ret, err
}

N
neza2017 已提交
160
func (c *Client) Start() error {
X
XuanYang-cn 已提交
161
	return nil
X
XuanYang-cn 已提交
162 163
}

N
neza2017 已提交
164
func (c *Client) Stop() error {
X
XuanYang-cn 已提交
165
	return c.conn.Close()
X
XuanYang-cn 已提交
166 167
}

168 169 170 171 172
// Register dummy
func (c *Client) Register() error {
	return nil
}

G
godchen 已提交
173
func (c *Client) GetComponentStates(ctx context.Context) (*internalpb.ComponentStates, error) {
G
godchen 已提交
174 175 176 177
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpc.GetComponentStates(ctx, &internalpb.GetComponentStatesRequest{})
	})
	return ret.(*internalpb.ComponentStates), err
N
neza2017 已提交
178 179 180
}

func (c *Client) GetStatisticsChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
G
godchen 已提交
181 182 183 184
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpc.GetStatisticsChannel(ctx, &internalpb.GetStatisticsChannelRequest{})
	})
	return ret.(*milvuspb.StringResponse), err
X
XuanYang-cn 已提交
185 186
}

G
godchen 已提交
187
func (c *Client) WatchDmChannels(ctx context.Context, req *datapb.WatchDmChannelsRequest) (*commonpb.Status, error) {
G
godchen 已提交
188 189 190 191
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpc.WatchDmChannels(ctx, req)
	})
	return ret.(*commonpb.Status), err
X
XuanYang-cn 已提交
192 193
}

G
godchen 已提交
194
func (c *Client) FlushSegments(ctx context.Context, req *datapb.FlushSegmentsRequest) (*commonpb.Status, error) {
G
godchen 已提交
195 196 197 198
	ret, err := c.recall(func() (interface{}, error) {
		return c.grpc.FlushSegments(ctx, req)
	})
	return ret.(*commonpb.Status), err
X
XuanYang-cn 已提交
199
}