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

enhance: enable limiter for restful v1(#38160) #38190

Merged
merged 1 commit into from
Dec 4, 2024
Merged
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
42 changes: 42 additions & 0 deletions internal/distributed/proxy/httpserver/handler_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,13 @@ func (h *HandlersV1) query(c *gin.Context) {
username, _ := c.Get(ContextUsername)
ctx := proxy.NewContextWithMetadata(c, username.(string), req.DbName)
response, err := h.executeRestRequestInterceptor(ctx, c, req, func(reqCtx context.Context, req any) (any, error) {
if _, err := CheckLimiter(ctx, &req, h.proxy); err != nil {
c.AbortWithStatusJSON(http.StatusOK, gin.H{
HTTPReturnCode: merr.Code(err),
HTTPReturnMessage: err.Error() + ", error: " + err.Error(),
})
return nil, err
}
return h.proxy.Query(reqCtx, req.(*milvuspb.QueryRequest))
})
if err == RestRequestInterceptorErr {
Expand Down Expand Up @@ -588,6 +595,13 @@ func (h *HandlersV1) get(c *gin.Context) {
return nil, RestRequestInterceptorErr
}
queryReq := req.(*milvuspb.QueryRequest)
if _, err := CheckLimiter(ctx, &req, h.proxy); err != nil {
c.AbortWithStatusJSON(http.StatusOK, gin.H{
HTTPReturnCode: merr.Code(err),
HTTPReturnMessage: err.Error() + ", error: " + err.Error(),
})
return nil, err
}
queryReq.Expr = filter
return h.proxy.Query(reqCtx, queryReq)
})
Expand Down Expand Up @@ -661,6 +675,13 @@ func (h *HandlersV1) delete(c *gin.Context) {
}
deleteReq.Expr = filter
}
if _, err := CheckLimiter(ctx, &req, h.proxy); err != nil {
c.AbortWithStatusJSON(http.StatusOK, gin.H{
HTTPReturnCode: merr.Code(err),
HTTPReturnMessage: err.Error() + ", error: " + err.Error(),
})
return nil, err
}
return h.proxy.Delete(ctx, deleteReq)
})
if err == RestRequestInterceptorErr {
Expand Down Expand Up @@ -737,6 +758,13 @@ func (h *HandlersV1) insert(c *gin.Context) {
})
return nil, RestRequestInterceptorErr
}
if _, err := CheckLimiter(ctx, &req, h.proxy); err != nil {
c.AbortWithStatusJSON(http.StatusOK, gin.H{
HTTPReturnCode: merr.Code(err),
HTTPReturnMessage: err.Error() + ", error: " + err.Error(),
})
return nil, err
}
return h.proxy.Insert(ctx, insertReq)
})
if err == RestRequestInterceptorErr {
Expand Down Expand Up @@ -836,6 +864,13 @@ func (h *HandlersV1) upsert(c *gin.Context) {
})
return nil, RestRequestInterceptorErr
}
if _, err := CheckLimiter(ctx, &req, h.proxy); err != nil {
c.AbortWithStatusJSON(http.StatusOK, gin.H{
HTTPReturnCode: merr.Code(err),
HTTPReturnMessage: err.Error() + ", error: " + err.Error(),
})
return nil, err
}
return h.proxy.Upsert(ctx, upsertReq)
})
if err == RestRequestInterceptorErr {
Expand Down Expand Up @@ -932,6 +967,13 @@ func (h *HandlersV1) search(c *gin.Context) {
username, _ := c.Get(ContextUsername)
ctx := proxy.NewContextWithMetadata(c, username.(string), req.DbName)
response, err := h.executeRestRequestInterceptor(ctx, c, req, func(reqCtx context.Context, req any) (any, error) {
if _, err := CheckLimiter(ctx, &req, h.proxy); err != nil {
c.AbortWithStatusJSON(http.StatusOK, gin.H{
HTTPReturnCode: merr.Code(err),
HTTPReturnMessage: err.Error() + ", error: " + err.Error(),
})
return nil, err
}
return h.proxy.Search(ctx, req.(*milvuspb.SearchRequest))
})
if err == RestRequestInterceptorErr {
Expand Down
28 changes: 28 additions & 0 deletions internal/distributed/proxy/httpserver/handler_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,10 @@ func TestQuery(t *testing.T) {
expectedBody: "{\"code\":200,\"data\":[{\"book_id\":1,\"book_intro\":[0.1,0.11],\"word_count\":1000},{\"book_id\":2,\"book_intro\":[0.2,0.22],\"word_count\":2000},{\"book_id\":3,\"book_intro\":[0.3,0.33],\"word_count\":3000}]}",
})

// disable rate limit
paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "false")
defer paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "true")

for _, tt := range testCases {
reqs := []*http.Request{genQueryRequest(), genGetRequest()}
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -601,6 +605,10 @@ func TestDelete(t *testing.T) {
expectedBody: "{\"code\":200,\"data\":{}}",
})

// disable rate limit
paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "false")
defer paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "true")

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
testEngine := initHTTPServer(tt.mp, true)
Expand All @@ -624,11 +632,15 @@ func TestDelete(t *testing.T) {
}

func TestDeleteForFilter(t *testing.T) {
paramtable.Init()
jsonBodyList := [][]byte{
[]byte(`{"collectionName": "` + DefaultCollectionName + `" , "id": [1,2,3]}`),
[]byte(`{"collectionName": "` + DefaultCollectionName + `" , "filter": "id in [1,2,3]"}`),
[]byte(`{"collectionName": "` + DefaultCollectionName + `" , "id": [1,2,3], "filter": "id in [1,2,3]"}`),
}
// disable rate limit
paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "false")
defer paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "true")
for _, jsonBody := range jsonBodyList {
t.Run("delete success", func(t *testing.T) {
mp := mocks.NewMockProxy(t)
Expand Down Expand Up @@ -726,6 +738,10 @@ func TestInsert(t *testing.T) {
HTTPCollectionName: DefaultCollectionName,
HTTPReturnData: rows[0],
})
// disable rate limit
paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "false")
defer paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "true")

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
testEngine := initHTTPServer(tt.mp, true)
Expand Down Expand Up @@ -771,6 +787,9 @@ func TestInsertForDataType(t *testing.T) {
"[success]with dynamic field": withDynamicField(newCollectionSchema(generateCollectionSchema(schemapb.DataType_Int64, false, true))),
"[success]with array fields": withArrayField(newCollectionSchema(generateCollectionSchema(schemapb.DataType_Int64, false, true))),
}
// disable rate limit
paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "false")
defer paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "true")
for name, schema := range schemas {
t.Run(name, func(t *testing.T) {
mp := mocks.NewMockProxy(t)
Expand Down Expand Up @@ -838,6 +857,9 @@ func TestReturnInt64(t *testing.T) {
schemapb.DataType_Int64: "1,2,3",
schemapb.DataType_VarChar: "\"1\",\"2\",\"3\"",
}
// disable rate limit
paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "false")
defer paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "true")
for _, dataType := range schemas {
t.Run("[insert]httpCfg.allow: false", func(t *testing.T) {
schema := newCollectionSchema(generateCollectionSchema(dataType, false, true))
Expand Down Expand Up @@ -1167,6 +1189,9 @@ func TestUpsert(t *testing.T) {
HTTPCollectionName: DefaultCollectionName,
HTTPReturnData: rows[0],
})
// disable rate limit
paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "false")
defer paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "true")
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
testEngine := initHTTPServer(tt.mp, true)
Expand Down Expand Up @@ -1265,6 +1290,9 @@ func TestSearch(t *testing.T) {
exceptCode: 200,
expectedBody: "{\"code\":200,\"data\":[{\"book_id\":1,\"book_intro\":[0.1,0.11],\"distance\":0.01,\"word_count\":1000},{\"book_id\":2,\"book_intro\":[0.2,0.22],\"distance\":0.04,\"word_count\":2000},{\"book_id\":3,\"book_intro\":[0.3,0.33],\"distance\":0.09,\"word_count\":3000}]}",
})
// disable rate limit
paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "false")
defer paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "true")

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading