Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ServiceDiscovery pkg #27683

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/datacoord/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/milvus-io/milvus/internal/storage"
"github.com/milvus-io/milvus/internal/types"
"github.com/milvus-io/milvus/internal/util/dependency"
"github.com/milvus-io/milvus/internal/util/grpcclient"
"github.com/milvus-io/milvus/internal/util/sessionutil"
"github.com/milvus-io/milvus/pkg/log"
"github.com/milvus-io/milvus/pkg/metrics"
Expand Down Expand Up @@ -236,7 +237,7 @@ func defaultIndexNodeCreatorFunc(ctx context.Context, addr string, nodeID int64)
}

func defaultRootCoordCreatorFunc(ctx context.Context, metaRootPath string, client *clientv3.Client) (types.RootCoordClient, error) {
return rootcoordclient.NewClient(ctx, metaRootPath, client)
return rootcoordclient.NewClient(ctx, grpcclient.NewRawEntryProvider(client, metaRootPath, typeutil.RootCoordRole))
}

// QuitSignal returns signal when server quits
Expand Down
36 changes: 9 additions & 27 deletions internal/distributed/datacoord/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ package grpcdatacoordclient

import (
"context"
"fmt"

clientv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
"google.golang.org/grpc"

Expand All @@ -31,7 +29,6 @@ import (
"github.com/milvus-io/milvus/internal/proto/internalpb"
"github.com/milvus-io/milvus/internal/types"
"github.com/milvus-io/milvus/internal/util/grpcclient"
"github.com/milvus-io/milvus/internal/util/sessionutil"
"github.com/milvus-io/milvus/pkg/log"
"github.com/milvus-io/milvus/pkg/util/commonpbutil"
"github.com/milvus-io/milvus/pkg/util/funcutil"
Expand All @@ -46,28 +43,20 @@ var _ types.DataCoordClient = (*Client)(nil)
// Client is the datacoord grpc client
type Client struct {
grpcClient grpcclient.GrpcClient[datapb.DataCoordClient]
sess *sessionutil.Session
sp grpcclient.ServiceProvider
sourceID int64
}

// NewClient creates a new client instance
func NewClient(ctx context.Context, metaRoot string, etcdCli *clientv3.Client) (*Client, error) {
sess := sessionutil.NewSession(ctx, metaRoot, etcdCli)
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
}

func NewClient(ctx context.Context, sp grpcclient.ServiceProvider) (*Client, error) {
config := &Params.DataCoordGrpcClientCfg
client := &Client{
grpcClient: grpcclient.NewClientBase[datapb.DataCoordClient](config, "milvus.proto.data.DataCoord"),
sess: sess,
sp: sp,
}
client.grpcClient.SetRole(typeutil.DataCoordRole)
client.grpcClient.SetGetAddrFunc(client.getDataCoordAddr)
client.grpcClient.SetNewGrpcClientFunc(client.newGrpcClient)
client.grpcClient.SetSession(sess)

return client, nil
}
Expand All @@ -76,21 +65,14 @@ func (c *Client) newGrpcClient(cc *grpc.ClientConn) datapb.DataCoordClient {
return datapb.NewDataCoordClient(cc)
}

func (c *Client) getDataCoordAddr() (string, error) {
key := c.grpcClient.GetRole()
msess, _, err := c.sess.GetSessions(key)
func (c *Client) getDataCoordAddr(ctx context.Context) (string, error) {
addr, serverID, err := c.sp.GetServiceEntry(ctx)
if err != nil {
log.Debug("DataCoordClient, getSessions failed", zap.Any("key", key), zap.Error(err))
log.Warn("DataCoordClient get service entry failed", zap.Error(err))
return "", err
}
ms, ok := msess[key]
if !ok {
log.Debug("DataCoordClient, not existed in msess ", zap.Any("key", key), zap.Any("len of msess", len(msess)))
return "", fmt.Errorf("find no available datacoord, check datacoord state")
}

c.grpcClient.SetNodeID(ms.ServerID)
return ms.Address, nil
c.grpcClient.SetNodeID(serverID)
return addr, nil
}

// Stop stops the client
Expand Down Expand Up @@ -329,7 +311,7 @@ func (c *Client) GetRecoveryInfoV2(ctx context.Context, req *datapb.GetRecoveryI
req = typeutil.Clone(req)
commonpbutil.UpdateMsgBase(
req.GetBase(),
commonpbutil.FillMsgBaseFromClient(paramtable.GetNodeID(), commonpbutil.WithTargetID(c.sess.ServerID)),
commonpbutil.FillMsgBaseFromClient(paramtable.GetNodeID(), commonpbutil.WithTargetID(c.grpcClient.GetNodeID())),
)
return wrapGrpcCall(ctx, c, func(client datapb.DataCoordClient) (*datapb.GetRecoveryInfoResponseV2, error) {
return client.GetRecoveryInfoV2(ctx, req)
Expand Down
10 changes: 6 additions & 4 deletions internal/distributed/datacoord/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ import (
"time"

"github.com/cockroachdb/errors"
"github.com/stretchr/testify/assert"
"github.com/milvus-io/milvus/internal/util/grpcclient"
"github.com/milvus-io/milvus/internal/util/mock"
"go.uber.org/zap"
"google.golang.org/grpc"

"github.com/milvus-io/milvus/internal/proto/datapb"
"github.com/milvus-io/milvus/internal/proxy"
"github.com/milvus-io/milvus/internal/util/mock"
"github.com/milvus-io/milvus/pkg/log"
"github.com/milvus-io/milvus/pkg/util/etcd"
"github.com/milvus-io/milvus/pkg/util/paramtable"
"github.com/milvus-io/milvus/pkg/util/typeutil"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -66,7 +68,7 @@ func Test_NewClient(t *testing.T) {
Params.EtcdCfg.EtcdTLSCACert.GetValue(),
Params.EtcdCfg.EtcdTLSMinVersion.GetValue())
assert.NoError(t, err)
client, err := NewClient(ctx, proxy.Params.EtcdCfg.MetaRootPath.GetValue(), etcdCli)
client, err := NewClient(ctx, grpcclient.NewRawEntryProvider(etcdCli, proxy.Params.EtcdCfg.MetaRootPath.GetValue(), typeutil.DataCoordRole))
assert.NoError(t, err)
assert.NotNil(t, client)

Expand Down
2 changes: 1 addition & 1 deletion internal/distributed/datanode/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (c *Client) newGrpcClient(cc *grpc.ClientConn) datapb.DataNodeClient {
return datapb.NewDataNodeClient(cc)
}

func (c *Client) getAddr() (string, error) {
func (c *Client) getAddr(_ context.Context) (string, error) {
return c.addr, nil
}

Expand Down
21 changes: 12 additions & 9 deletions internal/distributed/datanode/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import (
"sync"
"time"

"github.com/milvus-io/milvus/internal/util/componentutil"
"github.com/milvus-io/milvus/internal/util/dependency"
"github.com/milvus-io/milvus/internal/util/grpcclient"
"github.com/milvus-io/milvus/pkg/tracer"
"github.com/milvus-io/milvus/pkg/util/interceptor"

grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
clientv3 "go.etcd.io/etcd/client/v3"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
Expand All @@ -41,17 +47,14 @@ import (
"github.com/milvus-io/milvus/internal/proto/datapb"
"github.com/milvus-io/milvus/internal/proto/internalpb"
"github.com/milvus-io/milvus/internal/types"
"github.com/milvus-io/milvus/internal/util/componentutil"
"github.com/milvus-io/milvus/internal/util/dependency"
"github.com/milvus-io/milvus/pkg/log"
"github.com/milvus-io/milvus/pkg/tracer"
"github.com/milvus-io/milvus/pkg/util/etcd"
"github.com/milvus-io/milvus/pkg/util/funcutil"
"github.com/milvus-io/milvus/pkg/util/interceptor"
"github.com/milvus-io/milvus/pkg/util/logutil"
"github.com/milvus-io/milvus/pkg/util/merr"
"github.com/milvus-io/milvus/pkg/util/paramtable"
"github.com/milvus-io/milvus/pkg/util/retry"
"github.com/milvus-io/milvus/pkg/util/typeutil"
)

type Server struct {
Expand All @@ -75,17 +78,17 @@ type Server struct {

// NewServer new DataNode grpc server
func NewServer(ctx context.Context, factory dependency.Factory) (*Server, error) {
ctx1, cancel := context.WithCancel(ctx)
s := &Server{
ctx: ctx1,
ctx, cancel := context.WithCancel(ctx)
var s = &Server{
ctx: ctx,
cancel: cancel,
factory: factory,
grpcErrChan: make(chan error),
newRootCoordClient: func(etcdMetaRoot string, client *clientv3.Client) (types.RootCoordClient, error) {
return rcc.NewClient(ctx1, etcdMetaRoot, client)
return rcc.NewClient(ctx, grpcclient.NewRawEntryProvider(client, etcdMetaRoot, typeutil.RootCoordRole))
},
newDataCoordClient: func(etcdMetaRoot string, client *clientv3.Client) (types.DataCoordClient, error) {
return dcc.NewClient(ctx1, etcdMetaRoot, client)
return dcc.NewClient(ctx, grpcclient.NewRawEntryProvider(client, etcdMetaRoot, typeutil.DataCoordRole))
},
}

Expand Down
2 changes: 1 addition & 1 deletion internal/distributed/indexnode/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *Client) newGrpcClient(cc *grpc.ClientConn) indexpb.IndexNodeClient {
return indexpb.NewIndexNodeClient(cc)
}

func (c *Client) getAddr() (string, error) {
func (c *Client) getAddr(_ context.Context) (string, error) {
return c.addr, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/distributed/proxy/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *Client) newGrpcClient(cc *grpc.ClientConn) proxypb.ProxyClient {
return proxypb.NewProxyClient(cc)
}

func (c *Client) getAddr() (string, error) {
func (c *Client) getAddr(_ context.Context) (string, error) {
return c.addr, nil
}

Expand Down
33 changes: 19 additions & 14 deletions internal/distributed/proxy/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,30 @@ import (
"sync"
"time"

"github.com/milvus-io/milvus/pkg/util/merr"

"google.golang.org/grpc/credentials"

management "github.com/milvus-io/milvus/internal/http"
"github.com/milvus-io/milvus/internal/proxy/accesslog"
"github.com/milvus-io/milvus/internal/util/componentutil"
"github.com/milvus-io/milvus/internal/util/dependency"
"github.com/milvus-io/milvus/internal/util/grpcclient"
"github.com/milvus-io/milvus/pkg/tracer"
"github.com/milvus-io/milvus/pkg/util/interceptor"
"github.com/milvus-io/milvus/pkg/util/metricsinfo"
"github.com/milvus-io/milvus/pkg/util/typeutil"
"github.com/soheilhy/cmux"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"

"github.com/gin-gonic/gin"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
"github.com/soheilhy/cmux"
clientv3 "go.etcd.io/etcd/client/v3"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.uber.org/atomic"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/status"
Expand All @@ -53,22 +66,14 @@ import (
qcc "github.com/milvus-io/milvus/internal/distributed/querycoord/client"
rcc "github.com/milvus-io/milvus/internal/distributed/rootcoord/client"
"github.com/milvus-io/milvus/internal/distributed/utils"
management "github.com/milvus-io/milvus/internal/http"
"github.com/milvus-io/milvus/internal/proto/internalpb"
"github.com/milvus-io/milvus/internal/proto/proxypb"
"github.com/milvus-io/milvus/internal/proxy"
"github.com/milvus-io/milvus/internal/proxy/accesslog"
"github.com/milvus-io/milvus/internal/types"
"github.com/milvus-io/milvus/internal/util/componentutil"
"github.com/milvus-io/milvus/internal/util/dependency"
"github.com/milvus-io/milvus/pkg/log"
"github.com/milvus-io/milvus/pkg/tracer"
"github.com/milvus-io/milvus/pkg/util/etcd"
"github.com/milvus-io/milvus/pkg/util/funcutil"
"github.com/milvus-io/milvus/pkg/util/interceptor"
"github.com/milvus-io/milvus/pkg/util/logutil"
"github.com/milvus-io/milvus/pkg/util/merr"
"github.com/milvus-io/milvus/pkg/util/metricsinfo"
"github.com/milvus-io/milvus/pkg/util/paramtable"
)

Expand Down Expand Up @@ -509,7 +514,7 @@ func (s *Server) init() error {
if s.rootCoordClient == nil {
var err error
log.Debug("create RootCoord client for Proxy")
s.rootCoordClient, err = rcc.NewClient(s.ctx, proxy.Params.EtcdCfg.MetaRootPath.GetValue(), etcdCli)
s.rootCoordClient, err = rcc.NewClient(s.ctx, grpcclient.NewRawEntryProvider(etcdCli, proxy.Params.EtcdCfg.MetaRootPath.GetValue(), typeutil.RootCoordRole))
if err != nil {
log.Warn("failed to create RootCoord client for Proxy", zap.Error(err))
return err
Expand All @@ -531,7 +536,7 @@ func (s *Server) init() error {
if s.dataCoordClient == nil {
var err error
log.Debug("create DataCoord client for Proxy")
s.dataCoordClient, err = dcc.NewClient(s.ctx, proxy.Params.EtcdCfg.MetaRootPath.GetValue(), etcdCli)
s.dataCoordClient, err = dcc.NewClient(s.ctx, grpcclient.NewRawEntryProvider(etcdCli, proxy.Params.EtcdCfg.MetaRootPath.GetValue(), typeutil.DataCoordRole))
if err != nil {
log.Warn("failed to create DataCoord client for Proxy", zap.Error(err))
return err
Expand All @@ -553,7 +558,7 @@ func (s *Server) init() error {
if s.queryCoordClient == nil {
var err error
log.Debug("create QueryCoord client for Proxy")
s.queryCoordClient, err = qcc.NewClient(s.ctx, proxy.Params.EtcdCfg.MetaRootPath.GetValue(), etcdCli)
s.queryCoordClient, err = qcc.NewClient(s.ctx, grpcclient.NewRawEntryProvider(etcdCli, proxy.Params.EtcdCfg.MetaRootPath.GetValue(), typeutil.QueryCoordRole))
if err != nil {
log.Warn("failed to create QueryCoord client for Proxy", zap.Error(err))
return err
Expand Down
39 changes: 14 additions & 25 deletions internal/distributed/querycoord/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ package grpcquerycoordclient

import (
"context"
"fmt"

clientv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
"google.golang.org/grpc"

Expand All @@ -29,7 +27,6 @@ import (
"github.com/milvus-io/milvus/internal/proto/internalpb"
"github.com/milvus-io/milvus/internal/proto/querypb"
"github.com/milvus-io/milvus/internal/util/grpcclient"
"github.com/milvus-io/milvus/internal/util/sessionutil"
"github.com/milvus-io/milvus/pkg/log"
"github.com/milvus-io/milvus/pkg/util/commonpbutil"
"github.com/milvus-io/milvus/pkg/util/funcutil"
Expand All @@ -42,44 +39,36 @@ var Params *paramtable.ComponentParam = paramtable.Get()
// Client is the grpc client of QueryCoord.
type Client struct {
grpcClient grpcclient.GrpcClient[querypb.QueryCoordClient]
sess *sessionutil.Session
sp grpcclient.ServiceProvider
}

// NewClient creates a client for QueryCoord grpc call.
func NewClient(ctx context.Context, metaRoot string, etcdCli *clientv3.Client) (*Client, error) {
sess := sessionutil.NewSession(ctx, metaRoot, etcdCli)
if sess == nil {
err := fmt.Errorf("new session error, maybe can not connect to etcd")
log.Debug("QueryCoordClient NewClient failed", zap.Error(err))
return nil, err
}
func NewClient(ctx context.Context, sp grpcclient.ServiceProvider) (*Client, error) {
config := &Params.QueryCoordGrpcClientCfg
client := &Client{
grpcClient: grpcclient.NewClientBase[querypb.QueryCoordClient](config, "milvus.proto.query.QueryCoord"),
sess: sess,
sp: sp,
}
client.grpcClient.SetRole(typeutil.QueryCoordRole)
client.grpcClient.SetGetAddrFunc(client.getQueryCoordAddr)
client.grpcClient.SetNewGrpcClientFunc(client.newGrpcClient)
client.grpcClient.SetSession(sess)

return client, nil
}

func (c *Client) getQueryCoordAddr() (string, error) {
key := c.grpcClient.GetRole()
msess, _, err := c.sess.GetSessions(key)
// Init initializes QueryCoord's grpc client.
func (c *Client) Init() error {
return nil
}

func (c *Client) getQueryCoordAddr(ctx context.Context) (string, error) {
addr, serverID, err := c.sp.GetServiceEntry(ctx)
if err != nil {
log.Debug("QueryCoordClient GetSessions failed", zap.Error(err))
log.Warn("QueryCoordClient get service entry failed", zap.Error(err))
return "", err
}
ms, ok := msess[key]
if !ok {
log.Debug("QueryCoordClient msess key not existed", zap.Any("key", key))
return "", fmt.Errorf("find no available querycoord, check querycoord state")
}
c.grpcClient.SetNodeID(ms.ServerID)
return ms.Address, nil
c.grpcClient.SetNodeID(serverID)
return addr, nil
}

func (c *Client) newGrpcClient(cc *grpc.ClientConn) querypb.QueryCoordClient {
Expand Down Expand Up @@ -202,7 +191,7 @@ func (c *Client) SyncNewCreatedPartition(ctx context.Context, req *querypb.SyncN
req = typeutil.Clone(req)
commonpbutil.UpdateMsgBase(
req.GetBase(),
commonpbutil.FillMsgBaseFromClient(paramtable.GetNodeID(), commonpbutil.WithTargetID(c.sess.ServerID)),
commonpbutil.FillMsgBaseFromClient(paramtable.GetNodeID(), commonpbutil.WithTargetID(c.grpcClient.GetNodeID())),
)
return wrapGrpcCall(ctx, c, func(client querypb.QueryCoordClient) (*commonpb.Status, error) {
return client.SyncNewCreatedPartition(ctx, req)
Expand Down
Loading
Loading