2024-07-22 03:32:04 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
clientv3 "go.etcd.io/etcd/client/v3"
|
|
|
|
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
|
2024-11-18 02:46:31 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/json"
|
2024-07-22 03:32:04 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/streamingcoord/client/assignment"
|
2025-01-09 08:24:55 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/streamingcoord/client/broadcast"
|
2024-07-22 03:32:04 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/util/sessionutil"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/streamingutil/service/balancer/picker"
|
|
|
|
streamingserviceinterceptor "github.com/milvus-io/milvus/internal/util/streamingutil/service/interceptor"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/streamingutil/service/lazygrpc"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/streamingutil/service/resolver"
|
2025-01-09 08:24:55 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/util/streamingutil/util"
|
2025-01-10 02:49:01 +00:00
|
|
|
"github.com/milvus-io/milvus/pkg/proto/streamingpb"
|
2025-01-09 08:24:55 +00:00
|
|
|
"github.com/milvus-io/milvus/pkg/streaming/util/message"
|
2024-07-22 03:32:04 +00:00
|
|
|
"github.com/milvus-io/milvus/pkg/streaming/util/types"
|
|
|
|
"github.com/milvus-io/milvus/pkg/tracer"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/interceptor"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/typeutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ Client = (*clientImpl)(nil)
|
|
|
|
|
|
|
|
// AssignmentService is the interface of assignment service.
|
|
|
|
type AssignmentService interface {
|
|
|
|
// AssignmentDiscover is used to watches the assignment discovery.
|
|
|
|
types.AssignmentDiscoverWatcher
|
|
|
|
}
|
|
|
|
|
2025-01-09 08:24:55 +00:00
|
|
|
// BroadcastService is the interface of broadcast service.
|
|
|
|
type BroadcastService interface {
|
|
|
|
// Broadcast sends a broadcast message to the streaming service.
|
|
|
|
Broadcast(ctx context.Context, msg message.BroadcastMutableMessage) (*types.BroadcastAppendResult, error)
|
|
|
|
}
|
|
|
|
|
2024-07-22 03:32:04 +00:00
|
|
|
// Client is the interface of log service client.
|
|
|
|
type Client interface {
|
2025-01-09 08:24:55 +00:00
|
|
|
Broadcast() BroadcastService
|
|
|
|
|
2024-07-22 03:32:04 +00:00
|
|
|
// Assignment access assignment service.
|
|
|
|
Assignment() AssignmentService
|
|
|
|
|
|
|
|
// Close close the client.
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a new client.
|
|
|
|
func NewClient(etcdCli *clientv3.Client) Client {
|
|
|
|
// StreamingCoord is deployed on DataCoord node.
|
2025-01-07 09:42:57 +00:00
|
|
|
role := sessionutil.GetSessionPrefixByRole(typeutil.RootCoordRole)
|
|
|
|
rb := resolver.NewSessionExclusiveBuilder(etcdCli, role)
|
2024-07-22 03:32:04 +00:00
|
|
|
dialTimeout := paramtable.Get().StreamingCoordGrpcClientCfg.DialTimeout.GetAsDuration(time.Millisecond)
|
|
|
|
dialOptions := getDialOptions(rb)
|
|
|
|
conn := lazygrpc.NewConn(func(ctx context.Context) (*grpc.ClientConn, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, dialTimeout)
|
|
|
|
defer cancel()
|
|
|
|
return grpc.DialContext(
|
|
|
|
ctx,
|
2025-01-07 09:42:57 +00:00
|
|
|
resolver.SessionResolverScheme+":///"+typeutil.RootCoordRole,
|
2024-07-22 03:32:04 +00:00
|
|
|
dialOptions...,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
assignmentService := lazygrpc.WithServiceCreator(conn, streamingpb.NewStreamingCoordAssignmentServiceClient)
|
2025-01-09 08:24:55 +00:00
|
|
|
broadcastService := lazygrpc.WithServiceCreator(conn, streamingpb.NewStreamingCoordBroadcastServiceClient)
|
2024-07-22 03:32:04 +00:00
|
|
|
return &clientImpl{
|
|
|
|
conn: conn,
|
|
|
|
rb: rb,
|
|
|
|
assignmentService: assignment.NewAssignmentService(assignmentService),
|
2025-01-09 08:24:55 +00:00
|
|
|
broadcastService: broadcast.NewBroadcastService(util.MustSelectWALName(), broadcastService),
|
2024-07-22 03:32:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDialOptions returns grpc dial options.
|
|
|
|
func getDialOptions(rb resolver.Builder) []grpc.DialOption {
|
|
|
|
cfg := ¶mtable.Get().StreamingCoordGrpcClientCfg
|
|
|
|
retryPolicy := cfg.GetDefaultRetryPolicy()
|
|
|
|
retryPolicy["retryableStatusCodes"] = []string{"UNAVAILABLE"}
|
|
|
|
defaultServiceConfig := map[string]interface{}{
|
|
|
|
"loadBalancingConfig": []map[string]interface{}{
|
|
|
|
{picker.ServerIDPickerBalancerName: map[string]interface{}{}},
|
|
|
|
},
|
|
|
|
"methodConfig": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"name": []map[string]string{
|
|
|
|
{"service": "milvus.proto.streaming.StreamingCoordAssignmentService"},
|
|
|
|
},
|
|
|
|
"waitForReady": true,
|
|
|
|
"retryPolicy": retryPolicy,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
defaultServiceConfigJSON, err := json.Marshal(defaultServiceConfig)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
dialOptions := cfg.GetDialOptionsFromConfig()
|
|
|
|
dialOptions = append(dialOptions,
|
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.WithResolvers(rb),
|
|
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
|
|
grpc.WithChainUnaryInterceptor(
|
|
|
|
otelgrpc.UnaryClientInterceptor(tracer.GetInterceptorOpts()...),
|
|
|
|
interceptor.ClusterInjectionUnaryClientInterceptor(),
|
|
|
|
streamingserviceinterceptor.NewStreamingServiceUnaryClientInterceptor(),
|
|
|
|
),
|
|
|
|
grpc.WithChainStreamInterceptor(
|
|
|
|
otelgrpc.StreamClientInterceptor(tracer.GetInterceptorOpts()...),
|
|
|
|
interceptor.ClusterInjectionStreamClientInterceptor(),
|
|
|
|
streamingserviceinterceptor.NewStreamingServiceStreamClientInterceptor(),
|
|
|
|
),
|
|
|
|
grpc.WithReturnConnectionError(),
|
|
|
|
grpc.WithDefaultServiceConfig(string(defaultServiceConfigJSON)),
|
|
|
|
)
|
|
|
|
return dialOptions
|
|
|
|
}
|