2022-09-16 09:56:47 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you 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.
|
|
|
|
|
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
|
2022-09-16 09:56:47 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RateLimitInterceptor returns a new unary server interceptors that performs request rate limiting.
|
|
|
|
func RateLimitInterceptor(limiter types.Limiter) grpc.UnaryServerInterceptor {
|
|
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
2023-04-25 15:54:35 +08:00
|
|
|
collectionID, rt, n, err := getRequestInfo(req)
|
2022-12-30 18:35:32 +08:00
|
|
|
if err != nil {
|
|
|
|
return handler(ctx, req)
|
|
|
|
}
|
2023-04-25 15:54:35 +08:00
|
|
|
|
2023-05-09 16:56:40 +08:00
|
|
|
code := limiter.Check(collectionID, rt, n)
|
|
|
|
if code != commonpb.ErrorCode_Success {
|
|
|
|
rsp := getFailedResponse(req, rt, code, info.FullMethod)
|
|
|
|
if rsp != nil {
|
|
|
|
return rsp, nil
|
2023-04-25 15:54:35 +08:00
|
|
|
}
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|
2023-05-09 16:56:40 +08:00
|
|
|
return handler(ctx, req)
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 15:54:35 +08:00
|
|
|
// getRequestInfo returns collection name and rateType of request and return tokens needed.
|
|
|
|
func getRequestInfo(req interface{}) (int64, internalpb.RateType, int, error) {
|
2022-09-16 09:56:47 +08:00
|
|
|
switch r := req.(type) {
|
|
|
|
case *milvuspb.InsertRequest:
|
2023-06-25 17:20:43 +08:00
|
|
|
collectionID, _ := globalMetaCache.GetCollectionID(context.TODO(), r.GetDbName(), r.GetCollectionName())
|
2023-04-25 15:54:35 +08:00
|
|
|
return collectionID, internalpb.RateType_DMLInsert, proto.Size(r), nil
|
2023-07-11 11:20:34 +08:00
|
|
|
case *milvuspb.UpsertRequest:
|
|
|
|
collectionID, _ := globalMetaCache.GetCollectionID(context.TODO(), r.GetDbName(), r.GetCollectionName())
|
|
|
|
return collectionID, internalpb.RateType_DMLUpsert, proto.Size(r), nil
|
2022-09-16 09:56:47 +08:00
|
|
|
case *milvuspb.DeleteRequest:
|
2023-06-25 17:20:43 +08:00
|
|
|
collectionID, _ := globalMetaCache.GetCollectionID(context.TODO(), r.GetDbName(), r.GetCollectionName())
|
2023-04-25 15:54:35 +08:00
|
|
|
return collectionID, internalpb.RateType_DMLDelete, proto.Size(r), nil
|
2022-10-18 16:53:26 +08:00
|
|
|
case *milvuspb.ImportRequest:
|
2023-06-25 17:20:43 +08:00
|
|
|
collectionID, _ := globalMetaCache.GetCollectionID(context.TODO(), r.GetDbName(), r.GetCollectionName())
|
2023-04-25 15:54:35 +08:00
|
|
|
return collectionID, internalpb.RateType_DMLBulkLoad, proto.Size(r), nil
|
2022-09-16 09:56:47 +08:00
|
|
|
case *milvuspb.SearchRequest:
|
2023-06-25 17:20:43 +08:00
|
|
|
collectionID, _ := globalMetaCache.GetCollectionID(context.TODO(), r.GetDbName(), r.GetCollectionName())
|
2023-04-25 15:54:35 +08:00
|
|
|
return collectionID, internalpb.RateType_DQLSearch, int(r.GetNq()), nil
|
2022-09-16 09:56:47 +08:00
|
|
|
case *milvuspb.QueryRequest:
|
2023-06-25 17:20:43 +08:00
|
|
|
collectionID, _ := globalMetaCache.GetCollectionID(context.TODO(), r.GetDbName(), r.GetCollectionName())
|
|
|
|
return collectionID, internalpb.RateType_DQLQuery, 1, nil // think of the query request's nq as 1
|
2023-04-25 15:54:35 +08:00
|
|
|
case *milvuspb.CreateCollectionRequest:
|
2023-06-25 17:20:43 +08:00
|
|
|
collectionID, _ := globalMetaCache.GetCollectionID(context.TODO(), r.GetDbName(), r.GetCollectionName())
|
|
|
|
return collectionID, internalpb.RateType_DDLCollection, 1, nil
|
2023-04-25 15:54:35 +08:00
|
|
|
case *milvuspb.DropCollectionRequest:
|
2023-06-25 17:20:43 +08:00
|
|
|
collectionID, _ := globalMetaCache.GetCollectionID(context.TODO(), r.GetDbName(), r.GetCollectionName())
|
|
|
|
return collectionID, internalpb.RateType_DDLCollection, 1, nil
|
2023-04-25 15:54:35 +08:00
|
|
|
case *milvuspb.LoadCollectionRequest:
|
2023-06-25 17:20:43 +08:00
|
|
|
collectionID, _ := globalMetaCache.GetCollectionID(context.TODO(), r.GetDbName(), r.GetCollectionName())
|
|
|
|
return collectionID, internalpb.RateType_DDLCollection, 1, nil
|
2023-04-25 15:54:35 +08:00
|
|
|
case *milvuspb.ReleaseCollectionRequest:
|
2023-06-25 17:20:43 +08:00
|
|
|
collectionID, _ := globalMetaCache.GetCollectionID(context.TODO(), r.GetDbName(), r.GetCollectionName())
|
|
|
|
return collectionID, internalpb.RateType_DDLCollection, 1, nil
|
2023-04-25 15:54:35 +08:00
|
|
|
case *milvuspb.CreatePartitionRequest:
|
2023-06-25 17:20:43 +08:00
|
|
|
collectionID, _ := globalMetaCache.GetCollectionID(context.TODO(), r.GetDbName(), r.GetCollectionName())
|
|
|
|
return collectionID, internalpb.RateType_DDLPartition, 1, nil
|
2023-04-25 15:54:35 +08:00
|
|
|
case *milvuspb.DropPartitionRequest:
|
2023-06-25 17:20:43 +08:00
|
|
|
collectionID, _ := globalMetaCache.GetCollectionID(context.TODO(), r.GetDbName(), r.GetCollectionName())
|
|
|
|
return collectionID, internalpb.RateType_DDLPartition, 1, nil
|
2023-04-25 15:54:35 +08:00
|
|
|
case *milvuspb.LoadPartitionsRequest:
|
2023-06-25 17:20:43 +08:00
|
|
|
collectionID, _ := globalMetaCache.GetCollectionID(context.TODO(), r.GetDbName(), r.GetCollectionName())
|
|
|
|
return collectionID, internalpb.RateType_DDLPartition, 1, nil
|
2023-04-25 15:54:35 +08:00
|
|
|
case *milvuspb.ReleasePartitionsRequest:
|
2023-06-25 17:20:43 +08:00
|
|
|
collectionID, _ := globalMetaCache.GetCollectionID(context.TODO(), r.GetDbName(), r.GetCollectionName())
|
|
|
|
return collectionID, internalpb.RateType_DDLPartition, 1, nil
|
2023-04-25 15:54:35 +08:00
|
|
|
case *milvuspb.CreateIndexRequest:
|
2023-06-25 17:20:43 +08:00
|
|
|
collectionID, _ := globalMetaCache.GetCollectionID(context.TODO(), r.GetDbName(), r.GetCollectionName())
|
|
|
|
return collectionID, internalpb.RateType_DDLIndex, 1, nil
|
2023-04-25 15:54:35 +08:00
|
|
|
case *milvuspb.DropIndexRequest:
|
2023-06-25 17:20:43 +08:00
|
|
|
collectionID, _ := globalMetaCache.GetCollectionID(context.TODO(), r.GetDbName(), r.GetCollectionName())
|
|
|
|
return collectionID, internalpb.RateType_DDLIndex, 1, nil
|
2022-09-16 09:56:47 +08:00
|
|
|
case *milvuspb.FlushRequest:
|
2023-04-25 15:54:35 +08:00
|
|
|
return 0, internalpb.RateType_DDLFlush, 1, nil
|
2022-09-16 09:56:47 +08:00
|
|
|
case *milvuspb.ManualCompactionRequest:
|
2023-05-25 14:55:32 +08:00
|
|
|
return 0, internalpb.RateType_DDLCompaction, 1, nil
|
2022-09-16 09:56:47 +08:00
|
|
|
// TODO: support more request
|
|
|
|
default:
|
|
|
|
if req == nil {
|
2023-04-25 15:54:35 +08:00
|
|
|
return 0, 0, 0, fmt.Errorf("null request")
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|
2023-04-25 15:54:35 +08:00
|
|
|
return 0, 0, 0, fmt.Errorf("unsupported request type %s", reflect.TypeOf(req).Name())
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// failedStatus returns failed status.
|
|
|
|
func failedStatus(code commonpb.ErrorCode, reason string) *commonpb.Status {
|
|
|
|
return &commonpb.Status{
|
|
|
|
ErrorCode: code,
|
|
|
|
Reason: reason,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// failedMutationResult returns failed mutation result.
|
|
|
|
func failedMutationResult(code commonpb.ErrorCode, reason string) *milvuspb.MutationResult {
|
|
|
|
return &milvuspb.MutationResult{
|
|
|
|
Status: failedStatus(code, reason),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// failedBoolResponse returns failed boolean response.
|
|
|
|
func failedBoolResponse(code commonpb.ErrorCode, reason string) *milvuspb.BoolResponse {
|
|
|
|
return &milvuspb.BoolResponse{
|
|
|
|
Status: failedStatus(code, reason),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 14:31:37 +08:00
|
|
|
func wrapQuotaError(rt internalpb.RateType, errCode commonpb.ErrorCode, fullMethod string) error {
|
|
|
|
if errCode == commonpb.ErrorCode_RateLimit {
|
|
|
|
return fmt.Errorf("request is rejected by grpc RateLimiter middleware, please retry later, req: %s", fullMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
// deny to write/read
|
|
|
|
var op string
|
|
|
|
switch rt {
|
2023-07-11 11:20:34 +08:00
|
|
|
case internalpb.RateType_DMLInsert, internalpb.RateType_DMLUpsert, internalpb.RateType_DMLDelete, internalpb.RateType_DMLBulkLoad:
|
2023-01-06 14:31:37 +08:00
|
|
|
op = "write"
|
|
|
|
case internalpb.RateType_DQLSearch, internalpb.RateType_DQLQuery:
|
|
|
|
op = "read"
|
|
|
|
}
|
|
|
|
return fmt.Errorf("deny to %s, reason: %s, req: %s", op, GetQuotaErrorString(errCode), fullMethod)
|
|
|
|
}
|
|
|
|
|
2022-09-16 09:56:47 +08:00
|
|
|
// getFailedResponse returns failed response.
|
2023-01-06 14:31:37 +08:00
|
|
|
func getFailedResponse(req interface{}, rt internalpb.RateType, errCode commonpb.ErrorCode, fullMethod string) interface{} {
|
|
|
|
err := wrapQuotaError(rt, errCode, fullMethod)
|
2022-09-16 09:56:47 +08:00
|
|
|
switch req.(type) {
|
2023-08-09 14:29:16 +08:00
|
|
|
case *milvuspb.InsertRequest, *milvuspb.DeleteRequest, *milvuspb.UpsertRequest:
|
2023-01-06 14:31:37 +08:00
|
|
|
return failedMutationResult(errCode, err.Error())
|
2022-10-19 21:43:30 +08:00
|
|
|
case *milvuspb.ImportRequest:
|
|
|
|
return &milvuspb.ImportResponse{
|
2023-01-06 14:31:37 +08:00
|
|
|
Status: failedStatus(errCode, err.Error()),
|
2022-12-30 18:35:32 +08:00
|
|
|
}
|
2022-09-16 09:56:47 +08:00
|
|
|
case *milvuspb.SearchRequest:
|
|
|
|
return &milvuspb.SearchResults{
|
2023-01-06 14:31:37 +08:00
|
|
|
Status: failedStatus(errCode, err.Error()),
|
2022-12-30 18:35:32 +08:00
|
|
|
}
|
2022-09-16 09:56:47 +08:00
|
|
|
case *milvuspb.QueryRequest:
|
|
|
|
return &milvuspb.QueryResults{
|
2023-01-06 14:31:37 +08:00
|
|
|
Status: failedStatus(errCode, err.Error()),
|
2022-12-30 18:35:32 +08:00
|
|
|
}
|
2022-09-16 09:56:47 +08:00
|
|
|
case *milvuspb.CreateCollectionRequest, *milvuspb.DropCollectionRequest,
|
|
|
|
*milvuspb.LoadCollectionRequest, *milvuspb.ReleaseCollectionRequest,
|
|
|
|
*milvuspb.CreatePartitionRequest, *milvuspb.DropPartitionRequest,
|
|
|
|
*milvuspb.LoadPartitionsRequest, *milvuspb.ReleasePartitionsRequest,
|
|
|
|
*milvuspb.CreateIndexRequest, *milvuspb.DropIndexRequest:
|
2023-01-06 14:31:37 +08:00
|
|
|
return failedStatus(errCode, err.Error())
|
2022-09-16 09:56:47 +08:00
|
|
|
case *milvuspb.FlushRequest:
|
|
|
|
return &milvuspb.FlushResponse{
|
2023-01-06 14:31:37 +08:00
|
|
|
Status: failedStatus(errCode, err.Error()),
|
2022-12-30 18:35:32 +08:00
|
|
|
}
|
2022-09-16 09:56:47 +08:00
|
|
|
case *milvuspb.ManualCompactionRequest:
|
|
|
|
return &milvuspb.ManualCompactionResponse{
|
2023-01-06 14:31:37 +08:00
|
|
|
Status: failedStatus(errCode, err.Error()),
|
2022-12-30 18:35:32 +08:00
|
|
|
}
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|
2022-12-30 18:35:32 +08:00
|
|
|
return nil
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|