diff --git a/internal/distributed/indexnode/client/client.go b/internal/distributed/indexnode/client/client.go index 142023f30f..4599788c24 100644 --- a/internal/distributed/indexnode/client/client.go +++ b/internal/distributed/indexnode/client/client.go @@ -101,87 +101,58 @@ func (c *Client) getAddr() (string, error) { return c.addr, nil } -// GetComponentStates gets the component states of IndexNode. -func (c *Client) GetComponentStates(ctx context.Context) (*milvuspb.ComponentStates, error) { +func wrapGrpcCall[T any](ctx context.Context, c *Client, call func(indexClient indexpb.IndexNodeClient) (*T, error)) (*T, error) { ret, err := c.grpcClient.ReCall(ctx, func(client indexpb.IndexNodeClient) (any, error) { if !funcutil.CheckCtxValid(ctx) { return nil, ctx.Err() } - return client.GetComponentStates(ctx, &milvuspb.GetComponentStatesRequest{}) + return call(client) }) if err != nil || ret == nil { return nil, err } - return ret.(*milvuspb.ComponentStates), err + return ret.(*T), err +} + +// GetComponentStates gets the component states of IndexNode. +func (c *Client) GetComponentStates(ctx context.Context) (*milvuspb.ComponentStates, error) { + return wrapGrpcCall(ctx, c, func(client indexpb.IndexNodeClient) (*milvuspb.ComponentStates, error) { + return client.GetComponentStates(ctx, &milvuspb.GetComponentStatesRequest{}) + }) } func (c *Client) GetStatisticsChannel(ctx context.Context) (*milvuspb.StringResponse, error) { - ret, err := c.grpcClient.ReCall(ctx, func(client indexpb.IndexNodeClient) (any, error) { - if !funcutil.CheckCtxValid(ctx) { - return nil, ctx.Err() - } + return wrapGrpcCall(ctx, c, func(client indexpb.IndexNodeClient) (*milvuspb.StringResponse, error) { return client.GetStatisticsChannel(ctx, &internalpb.GetStatisticsChannelRequest{}) }) - if err != nil || ret == nil { - return nil, err - } - return ret.(*milvuspb.StringResponse), err } // CreateJob sends the build index request to IndexNode. func (c *Client) CreateJob(ctx context.Context, req *indexpb.CreateJobRequest) (*commonpb.Status, error) { - ret, err := c.grpcClient.ReCall(ctx, func(client indexpb.IndexNodeClient) (any, error) { - if !funcutil.CheckCtxValid(ctx) { - return nil, ctx.Err() - } + return wrapGrpcCall(ctx, c, func(client indexpb.IndexNodeClient) (*commonpb.Status, error) { return client.CreateJob(ctx, req) }) - if err != nil || ret == nil { - return nil, err - } - return ret.(*commonpb.Status), err } // QueryJobs query the task info of the index task. func (c *Client) QueryJobs(ctx context.Context, req *indexpb.QueryJobsRequest) (*indexpb.QueryJobsResponse, error) { - ret, err := c.grpcClient.ReCall(ctx, func(client indexpb.IndexNodeClient) (any, error) { - if !funcutil.CheckCtxValid(ctx) { - return nil, ctx.Err() - } + return wrapGrpcCall(ctx, c, func(client indexpb.IndexNodeClient) (*indexpb.QueryJobsResponse, error) { return client.QueryJobs(ctx, req) }) - if err != nil || ret == nil { - return nil, err - } - return ret.(*indexpb.QueryJobsResponse), err } // DropJobs query the task info of the index task. func (c *Client) DropJobs(ctx context.Context, req *indexpb.DropJobsRequest) (*commonpb.Status, error) { - ret, err := c.grpcClient.ReCall(ctx, func(client indexpb.IndexNodeClient) (any, error) { - if !funcutil.CheckCtxValid(ctx) { - return nil, ctx.Err() - } + return wrapGrpcCall(ctx, c, func(client indexpb.IndexNodeClient) (*commonpb.Status, error) { return client.DropJobs(ctx, req) }) - if err != nil || ret == nil { - return nil, err - } - return ret.(*commonpb.Status), err } // GetJobStats query the task info of the index task. func (c *Client) GetJobStats(ctx context.Context, req *indexpb.GetJobStatsRequest) (*indexpb.GetJobStatsResponse, error) { - ret, err := c.grpcClient.ReCall(ctx, func(client indexpb.IndexNodeClient) (any, error) { - if !funcutil.CheckCtxValid(ctx) { - return nil, ctx.Err() - } + return wrapGrpcCall(ctx, c, func(client indexpb.IndexNodeClient) (*indexpb.GetJobStatsResponse, error) { return client.GetJobStats(ctx, req) }) - if err != nil || ret == nil { - return nil, err - } - return ret.(*indexpb.GetJobStatsResponse), err } // ShowConfigurations gets specified configurations para of IndexNode @@ -190,17 +161,9 @@ func (c *Client) ShowConfigurations(ctx context.Context, req *internalpb.ShowCon commonpbutil.UpdateMsgBase( req.GetBase(), commonpbutil.FillMsgBaseFromClient(paramtable.GetNodeID())) - ret, err := c.grpcClient.ReCall(ctx, func(client indexpb.IndexNodeClient) (any, error) { - if !funcutil.CheckCtxValid(ctx) { - return nil, ctx.Err() - } + return wrapGrpcCall(ctx, c, func(client indexpb.IndexNodeClient) (*internalpb.ShowConfigurationsResponse, error) { return client.ShowConfigurations(ctx, req) }) - if err != nil || ret == nil { - return nil, err - } - - return ret.(*internalpb.ShowConfigurationsResponse), err } // GetMetrics gets the metrics info of IndexNode. @@ -209,14 +172,7 @@ func (c *Client) GetMetrics(ctx context.Context, req *milvuspb.GetMetricsRequest commonpbutil.UpdateMsgBase( req.GetBase(), commonpbutil.FillMsgBaseFromClient(paramtable.GetNodeID())) - ret, err := c.grpcClient.ReCall(ctx, func(client indexpb.IndexNodeClient) (any, error) { - if !funcutil.CheckCtxValid(ctx) { - return nil, ctx.Err() - } + return wrapGrpcCall(ctx, c, func(client indexpb.IndexNodeClient) (*milvuspb.GetMetricsResponse, error) { return client.GetMetrics(ctx, req) }) - if err != nil || ret == nil { - return nil, err - } - return ret.(*milvuspb.GetMetricsResponse), err } diff --git a/internal/distributed/proxy/client/client.go b/internal/distributed/proxy/client/client.go index 7877a5d7a1..3b2b4a24dd 100644 --- a/internal/distributed/proxy/client/client.go +++ b/internal/distributed/proxy/client/client.go @@ -97,32 +97,31 @@ func (c *Client) Register() error { return nil } -// GetComponentStates get the component state. -func (c *Client) GetComponentStates(ctx context.Context) (*milvuspb.ComponentStates, error) { +func wrapGrpcCall[T any](ctx context.Context, c *Client, call func(proxyClient proxypb.ProxyClient) (*T, error)) (*T, error) { ret, err := c.grpcClient.ReCall(ctx, func(client proxypb.ProxyClient) (any, error) { if !funcutil.CheckCtxValid(ctx) { return nil, ctx.Err() } - return client.GetComponentStates(ctx, &milvuspb.GetComponentStatesRequest{}) + return call(client) }) if err != nil || ret == nil { return nil, err } - return ret.(*milvuspb.ComponentStates), err + return ret.(*T), err +} + +// GetComponentStates get the component state. +func (c *Client) GetComponentStates(ctx context.Context) (*milvuspb.ComponentStates, error) { + return wrapGrpcCall(ctx, c, func(client proxypb.ProxyClient) (*milvuspb.ComponentStates, error) { + return client.GetComponentStates(ctx, &milvuspb.GetComponentStatesRequest{}) + }) } // GetStatisticsChannel return the statistics channel in string func (c *Client) GetStatisticsChannel(ctx context.Context) (*milvuspb.StringResponse, error) { - ret, err := c.grpcClient.ReCall(ctx, func(client proxypb.ProxyClient) (any, error) { - if !funcutil.CheckCtxValid(ctx) { - return nil, ctx.Err() - } + return wrapGrpcCall(ctx, c, func(client proxypb.ProxyClient) (*milvuspb.StringResponse, error) { return client.GetStatisticsChannel(ctx, &internalpb.GetStatisticsChannelRequest{}) }) - if err != nil || ret == nil { - return nil, err - } - return ret.(*milvuspb.StringResponse), err } // InvalidateCollectionMetaCache invalidate collection meta cache @@ -132,16 +131,9 @@ func (c *Client) InvalidateCollectionMetaCache(ctx context.Context, req *proxypb req.GetBase(), commonpbutil.FillMsgBaseFromClient(paramtable.GetNodeID(), commonpbutil.WithTargetID(c.grpcClient.GetNodeID())), ) - ret, err := c.grpcClient.ReCall(ctx, func(client proxypb.ProxyClient) (any, error) { - if !funcutil.CheckCtxValid(ctx) { - return nil, ctx.Err() - } + return wrapGrpcCall(ctx, c, func(client proxypb.ProxyClient) (*commonpb.Status, error) { return client.InvalidateCollectionMetaCache(ctx, req) }) - if err != nil || ret == nil { - return nil, err - } - return ret.(*commonpb.Status), err } func (c *Client) InvalidateCredentialCache(ctx context.Context, req *proxypb.InvalidateCredCacheRequest) (*commonpb.Status, error) { @@ -150,16 +142,9 @@ func (c *Client) InvalidateCredentialCache(ctx context.Context, req *proxypb.Inv req.GetBase(), commonpbutil.FillMsgBaseFromClient(paramtable.GetNodeID(), commonpbutil.WithTargetID(c.grpcClient.GetNodeID())), ) - ret, err := c.grpcClient.ReCall(ctx, func(client proxypb.ProxyClient) (any, error) { - if !funcutil.CheckCtxValid(ctx) { - return nil, ctx.Err() - } + return wrapGrpcCall(ctx, c, func(client proxypb.ProxyClient) (*commonpb.Status, error) { return client.InvalidateCredentialCache(ctx, req) }) - if err != nil || ret == nil { - return nil, err - } - return ret.(*commonpb.Status), err } func (c *Client) UpdateCredentialCache(ctx context.Context, req *proxypb.UpdateCredCacheRequest) (*commonpb.Status, error) { @@ -168,16 +153,9 @@ func (c *Client) UpdateCredentialCache(ctx context.Context, req *proxypb.UpdateC req.GetBase(), commonpbutil.FillMsgBaseFromClient(paramtable.GetNodeID(), commonpbutil.WithTargetID(c.grpcClient.GetNodeID())), ) - ret, err := c.grpcClient.ReCall(ctx, func(client proxypb.ProxyClient) (any, error) { - if !funcutil.CheckCtxValid(ctx) { - return nil, ctx.Err() - } + return wrapGrpcCall(ctx, c, func(client proxypb.ProxyClient) (*commonpb.Status, error) { return client.UpdateCredentialCache(ctx, req) }) - if err != nil || ret == nil { - return nil, err - } - return ret.(*commonpb.Status), err } func (c *Client) RefreshPolicyInfoCache(ctx context.Context, req *proxypb.RefreshPolicyInfoCacheRequest) (*commonpb.Status, error) { @@ -186,16 +164,9 @@ func (c *Client) RefreshPolicyInfoCache(ctx context.Context, req *proxypb.Refres req.GetBase(), commonpbutil.FillMsgBaseFromClient(paramtable.GetNodeID(), commonpbutil.WithTargetID(c.grpcClient.GetNodeID())), ) - ret, err := c.grpcClient.ReCall(ctx, func(client proxypb.ProxyClient) (any, error) { - if !funcutil.CheckCtxValid(ctx) { - return nil, ctx.Err() - } + return wrapGrpcCall(ctx, c, func(client proxypb.ProxyClient) (*commonpb.Status, error) { return client.RefreshPolicyInfoCache(ctx, req) }) - if err != nil { - return nil, err - } - return ret.(*commonpb.Status), err } // GetProxyMetrics gets the metrics of proxy, it's an internal interface which is different from GetMetrics interface, @@ -206,16 +177,9 @@ func (c *Client) GetProxyMetrics(ctx context.Context, req *milvuspb.GetMetricsRe req.GetBase(), commonpbutil.FillMsgBaseFromClient(paramtable.GetNodeID(), commonpbutil.WithTargetID(c.grpcClient.GetNodeID())), ) - ret, err := c.grpcClient.ReCall(ctx, func(client proxypb.ProxyClient) (any, error) { - if !funcutil.CheckCtxValid(ctx) { - return nil, ctx.Err() - } + return wrapGrpcCall(ctx, c, func(client proxypb.ProxyClient) (*milvuspb.GetMetricsResponse, error) { return client.GetProxyMetrics(ctx, req) }) - if err != nil || ret == nil { - return nil, err - } - return ret.(*milvuspb.GetMetricsResponse), err } // SetRates notifies Proxy to limit rates of requests. @@ -225,16 +189,9 @@ func (c *Client) SetRates(ctx context.Context, req *proxypb.SetRatesRequest) (*c req.GetBase(), commonpbutil.FillMsgBaseFromClient(paramtable.GetNodeID(), commonpbutil.WithTargetID(c.grpcClient.GetNodeID())), ) - ret, err := c.grpcClient.ReCall(ctx, func(client proxypb.ProxyClient) (any, error) { - if !funcutil.CheckCtxValid(ctx) { - return nil, ctx.Err() - } + return wrapGrpcCall(ctx, c, func(client proxypb.ProxyClient) (*commonpb.Status, error) { return client.SetRates(ctx, req) }) - if err != nil { - return nil, err - } - return ret.(*commonpb.Status), err } func (c *Client) ListClientInfos(ctx context.Context, req *proxypb.ListClientInfosRequest) (*proxypb.ListClientInfosResponse, error) { @@ -243,14 +200,7 @@ func (c *Client) ListClientInfos(ctx context.Context, req *proxypb.ListClientInf req.GetBase(), commonpbutil.FillMsgBaseFromClient(paramtable.GetNodeID(), commonpbutil.WithTargetID(c.grpcClient.GetNodeID())), ) - ret, err := c.grpcClient.ReCall(ctx, func(client proxypb.ProxyClient) (any, error) { - if !funcutil.CheckCtxValid(ctx) { - return nil, ctx.Err() - } + return wrapGrpcCall(ctx, c, func(client proxypb.ProxyClient) (*proxypb.ListClientInfosResponse, error) { return client.ListClientInfos(ctx, req) }) - if err != nil { - return nil, err - } - return ret.(*proxypb.ListClientInfosResponse), nil }