feat: support truncate collection restful api v2 (#47032)

issue: https://github.com/milvus-io/milvus/issues/47031

---------

Signed-off-by: sijie-ni-0214 <sijie.ni@zilliz.com>
This commit is contained in:
sijie-ni-0214
2026-01-15 15:59:34 +08:00
committed by GitHub
parent b7c65f3f71
commit a4836f9c08
3 changed files with 61 additions and 0 deletions
@@ -77,6 +77,7 @@ const (
CompactAction = "compact"
CompactionStateAction = "get_compaction_state"
FlushAction = "flush"
TruncateAction = "truncate"
GetProgressAction = "get_progress" // deprecated, keep it for compatibility, use `/v2/vectordb/jobs/import/describe` instead
AddPrivilegesToGroupAction = "add_privileges_to_group"
RemovePrivilegesFromGroupAction = "remove_privileges_from_group"
@@ -77,6 +77,7 @@ func (h *HandlersV2) RegisterRoutesToV2(router gin.IRouter) {
router.POST(CollectionCategory+LoadStateAction, timeoutMiddleware(wrapperPost(func() any { return &CollectionNameReq{} }, wrapperTraceLog(h.getCollectionLoadState))))
router.POST(CollectionCategory+CreateAction, timeoutMiddleware(wrapperPost(func() any { return &CollectionReq{AutoID: DisableAutoID} }, wrapperTraceLog(h.createCollection))))
router.POST(CollectionCategory+DropAction, timeoutMiddleware(wrapperPost(func() any { return &CollectionNameReq{} }, wrapperTraceLog(h.dropCollection))))
router.POST(CollectionCategory+TruncateAction, timeoutMiddleware(wrapperPost(func() any { return &CollectionNameReq{} }, wrapperTraceLog(h.truncateCollection))))
router.POST(CollectionCategory+RenameAction, timeoutMiddleware(wrapperPost(func() any { return &RenameCollectionReq{} }, wrapperTraceLog(h.renameCollection))))
router.POST(CollectionCategory+LoadAction, timeoutMiddleware(wrapperPost(func() any { return &CollectionNameReq{} }, wrapperTraceLog(h.loadCollection))))
router.POST(CollectionCategory+RefreshLoadAction, timeoutMiddleware(wrapperPost(func() any { return &CollectionNameReq{} }, wrapperTraceLog(h.refreshLoadCollection))))
@@ -623,6 +624,22 @@ func (h *HandlersV2) dropCollection(ctx context.Context, c *gin.Context, anyReq
return resp, err
}
func (h *HandlersV2) truncateCollection(ctx context.Context, c *gin.Context, anyReq any, dbName string) (interface{}, error) {
getter, _ := anyReq.(requestutil.CollectionNameGetter)
req := &milvuspb.TruncateCollectionRequest{
DbName: dbName,
CollectionName: getter.GetCollectionName(),
}
c.Set(ContextRequest, req)
resp, err := wrapperProxyWithLimit(ctx, c, req, h.checkAuth, false, "/milvus.proto.milvus.MilvusService/TruncateCollection", true, h.proxy, func(reqCtx context.Context, req any) (interface{}, error) {
return h.proxy.TruncateCollection(reqCtx, req.(*milvuspb.TruncateCollectionRequest))
})
if err == nil {
HTTPReturn(c, http.StatusOK, wrapperReturnDefault())
}
return resp, err
}
func (h *HandlersV2) renameCollection(ctx context.Context, c *gin.Context, anyReq any, dbName string) (interface{}, error) {
httpReq := anyReq.(*RenameCollectionReq)
req := &milvuspb.RenameCollectionRequest{
@@ -2983,3 +2983,46 @@ func (s *CollectionFunctionSuite) TestDropCollectionFunctionNormal() {
validateRequestBodyTestCases(s.T(), s.testEngine, addFunctionTestCases, false)
})
}
func TestTruncateCollection(t *testing.T) {
paramtable.Init()
// disable rate limit
paramtable.Get().Save(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key, "false")
defer paramtable.Get().Reset(paramtable.Get().QuotaConfig.QuotaAndLimitsEnabled.Key)
mp := mocks.NewMockProxy(t)
mp.EXPECT().TruncateCollection(mock.Anything, mock.Anything).Return(&milvuspb.TruncateCollectionResponse{
Status: commonSuccessStatus,
}, nil).Times(2)
testEngine := initHTTPServerV2(mp, false)
testCases := []requestBodyTestCase{
{
path: versionalV2(CollectionCategory, TruncateAction),
requestBody: []byte(`{"dbName": "default", "collectionName": "` + DefaultCollectionName + `"}`),
errCode: 0,
errMsg: "",
},
{
path: versionalV2(CollectionCategory, TruncateAction),
requestBody: []byte(`{"collectionName": "` + DefaultCollectionName + `"}`),
errCode: 0,
errMsg: "",
},
{
path: versionalV2(CollectionCategory, TruncateAction),
requestBody: []byte(`{"dbName": "db", "collectionName": ""}`),
errCode: 1802,
errMsg: "missing required parameters",
},
{
path: versionalV2(CollectionCategory, TruncateAction),
requestBody: []byte(`invalid json`),
errCode: 1801,
errMsg: "can only accept json format request",
},
}
for _, testcase := range testCases {
sendReqAndVerify(t, testEngine, testcase.path, http.MethodPost, testcase)
}
}