@@ -15,6 +15,7 @@ import (
1515 artifactpb "github.com/instill-ai/protogen-go/artifact/artifact/v1alpha"
1616 "go.uber.org/zap"
1717 "google.golang.org/protobuf/types/known/timestamppb"
18+ "gorm.io/gorm"
1819)
1920
2021func (ph * PublicHandler ) UploadCatalogFile (ctx context.Context , req * artifactpb.UploadCatalogFileRequest ) (* artifactpb.UploadCatalogFileResponse , error ) {
@@ -135,8 +136,9 @@ func (ph *PublicHandler) UploadCatalogFile(ctx context.Context, req *artifactpb.
135136 return nil , err
136137 }
137138
138- // increase catalog usage
139- err = ph .service .Repository .IncreaseKnowledgeBaseUsage (ctx , kb .UID .String (), int (fileSize ))
139+ // increase catalog usage. need to increase after the file is created.
140+ // Note: in the future, we need to increase the usage in transaction with creating the file.
141+ err = ph .service .Repository .IncreaseKnowledgeBaseUsage (ctx , nil , kb .UID .String (), int (fileSize ))
140142 if err != nil {
141143 log .Error ("failed to increase catalog usage" , zap .Error (err ))
142144 return nil , err
@@ -318,14 +320,16 @@ func (ph *PublicHandler) DeleteCatalogFile(
318320
319321 // ACL - check user's permission to write catalog of kb file
320322 kbfs , err := ph .service .Repository .GetKnowledgeBaseFilesByFileUIDs (ctx , []uuid.UUID {uuid .FromStringOrNil (req .FileUid )})
321- if err != nil && len (kbfs ) == 0 {
322- log .Error ("failed to get catalog" , zap .Error (err ))
323- return nil , fmt .Errorf (ErrorListKnowledgeBasesMsg , err )
323+ if err != nil {
324+ log .Error ("failed to get catalog files" , zap .Error (err ))
325+ return nil , fmt .Errorf ("failed to get catalog files. err: %w" , err )
326+ } else if len (kbfs ) == 0 {
327+ return nil , fmt .Errorf ("file not found. err: %w" , customerror .ErrNotFound )
324328 }
325329 granted , err := ph .service .ACLClient .CheckPermission (ctx , "knowledgebase" , kbfs [0 ].KnowledgeBaseUID , "writer" )
326330 if err != nil {
327331 log .Error ("failed to check permission" , zap .Error (err ))
328- return nil , fmt .Errorf (ErrorUpdateKnowledgeBaseMsg , err )
332+ return nil , fmt .Errorf ("failed to check permission. err: %w" , err )
329333 }
330334 if ! granted {
331335 log .Error ("no permission to delete catalog file" )
@@ -345,66 +349,106 @@ func (ph *PublicHandler) DeleteCatalogFile(
345349 files , err := ph .service .Repository .GetKnowledgeBaseFilesByFileUIDs (ctx , []uuid.UUID {fUID })
346350 if err != nil {
347351 return nil , err
348- }
349- if len (files ) == 0 {
352+ } else if len (files ) == 0 {
350353 return nil , fmt .Errorf ("file not found. err: %w" , customerror .ErrNotFound )
351354 }
352355
356+ startSignal := make (chan bool )
353357 // TODO: need to use clean worker in the future
354358 go utils .GoRecover (
355359 func () {
356- // to prevent parent context from being cancelled, create a new context
360+ // Create a new context to prevent the parent context from being cancelled
357361 ctx := context .TODO ()
358- // delete the file from minio
362+ log , _ := logger .GetZapLogger (ctx )
363+ canStart := <- startSignal
364+ if ! canStart {
365+ log .Info ("DeleteCatalogFile: received stop signal" )
366+ return
367+ }
368+ log .Info ("DeleteCatalogFile: start deleting file from minio, database and milvus" )
369+ allPass := true
370+ // Delete the file from MinIO
359371 objectPaths := []string {}
360- // kb file in minio
372+ // Add the knowledge base file in MinIO to the list of objects to delete
361373 objectPaths = append (objectPaths , files [0 ].Destination )
362- // converted file in minio
374+ // Add the converted file in MinIO to the list of objects to delete
363375 cf , err := ph .service .Repository .GetConvertedFileByFileUID (ctx , fUID )
364- if err == nil {
376+ if err != nil {
377+ if err != gorm .ErrRecordNotFound {
378+ log .Error ("failed to get converted file by file uid" , zap .Error (err ))
379+ allPass = false
380+ }
381+ } else if cf != nil {
365382 objectPaths = append (objectPaths , cf .Destination )
366383 }
367- // chunks in minio
368- chunks , _ := ph .service .Repository .ListChunksByKbFileUID (ctx , fUID )
369- if len (chunks ) > 0 {
384+ // Add the chunks in MinIO to the list of objects to delete
385+ chunks , err := ph .service .Repository .ListChunksByKbFileUID (ctx , fUID )
386+ if err != nil {
387+ log .Error ("failed to get chunks by kb file uid" , zap .Error (err ))
388+ allPass = false
389+ } else if len (chunks ) > 0 {
370390 for _ , chunk := range chunks {
371391 objectPaths = append (objectPaths , chunk .ContentDest )
372392 }
373393 }
374- // delete the embeddings in milvus(need to delete first)
394+ // Delete the embeddings in Milvus (this better to be done first)
375395 embUIDs := []string {}
376396 embeddings , _ := ph .service .Repository .ListEmbeddingsByKbFileUID (ctx , fUID )
377397 for _ , emb := range embeddings {
378398 embUIDs = append (embUIDs , emb .UID .String ())
379399 }
380- _ = ph .service .MilvusClient .DeleteEmbeddingsInKb (ctx , files [0 ].KnowledgeBaseUID .String (), embUIDs )
381-
382- _ = ph .service .MinIO .DeleteFiles (ctx , objectPaths )
383- // delete the converted file in postgreSQL
384- _ = ph .service .Repository .HardDeleteConvertedFileByFileUID (ctx , fUID )
385- // delete the chunks in postgreSQL
386- _ = ph .service .Repository .HardDeleteChunksByKbFileUID (ctx , fUID )
387- // delete the embeddings in postgreSQL
388- _ = ph .service .Repository .HardDeleteEmbeddingsByKbFileUID (ctx , fUID )
389- // print success message and file uid
390- log .Info ("Successfully deleted file from minio, database and milvus" , zap .String ("file_uid" , fUID .String ()))
400+ err = ph .service .MilvusClient .DeleteEmbeddingsInKb (ctx , files [0 ].KnowledgeBaseUID .String (), embUIDs )
401+ if err != nil {
402+ log .Error ("failed to delete embeddings in milvus" , zap .Error (err ))
403+ allPass = false
404+ }
405+
406+ // Delete the files in MinIO
407+ errChan := ph .service .MinIO .DeleteFiles (ctx , objectPaths )
408+ for err := range errChan {
409+ if err != nil {
410+ log .Error ("failed to delete files in minio" , zap .Error (err ))
411+ allPass = false
412+ }
413+ }
414+ // Delete the converted file in PostgreSQL
415+ err = ph .service .Repository .HardDeleteConvertedFileByFileUID (ctx , fUID )
416+ if err != nil {
417+ log .Error ("failed to delete converted file in postgreSQL" , zap .Error (err ))
418+ allPass = false
419+ }
420+ // Delete the chunks in PostgreSQL
421+ err = ph .service .Repository .HardDeleteChunksByKbFileUID (ctx , fUID )
422+ if err != nil {
423+ log .Error ("failed to delete chunks in postgreSQL" , zap .Error (err ))
424+ allPass = false
425+ }
426+ // Delete the embeddings in PostgreSQL
427+ err = ph .service .Repository .HardDeleteEmbeddingsByKbFileUID (ctx , fUID )
428+ if err != nil {
429+ log .Error ("failed to delete embeddings in postgreSQL" , zap .Error (err ))
430+ allPass = false
431+ }
432+ if allPass {
433+ log .Info ("DeleteCatalogFile: successfully deleted file from minio, database and milvus" , zap .String ("file_uid" , fUID .String ()))
434+ } else {
435+ log .Error ("DeleteCatalogFile: failed to delete file from minio, database and milvus" , zap .String ("file_uid" , fUID .String ()))
436+ }
391437 },
392438 "DeleteCatalogFile" ,
393439 )
394440
395- // delete the file in postgreSQL
396- err = ph .service .Repository .DeleteKnowledgeBaseFile (ctx , req .FileUid )
397- if err != nil {
398- return nil , err
399- }
400- // decrease catalog usage
401- err = ph .service .Repository .IncreaseKnowledgeBaseUsage (ctx , files [0 ].KnowledgeBaseUID .String (), int (- files [0 ].Size ))
441+ err = ph .service .Repository .DeleteKnowledgeBaseFileAndDecreaseUsage (ctx , fUID )
402442 if err != nil {
443+ log .Error ("failed to delete knowledge base file and decrease usage" , zap .Error (err ))
444+ startSignal <- false
403445 return nil , err
404446 }
447+ // start the background deletion
448+ startSignal <- true
405449
406450 return & artifactpb.DeleteCatalogFileResponse {
407- FileUid : req . FileUid ,
451+ FileUid : fUID . String () ,
408452 }, nil
409453
410454}
@@ -425,8 +469,7 @@ func (ph *PublicHandler) ProcessCatalogFiles(ctx context.Context, req *artifactp
425469 kbfs , err := ph .service .Repository .GetKnowledgeBaseFilesByFileUIDs (ctx , fileUUIDs )
426470 if err != nil {
427471 return nil , err
428- }
429- if len (kbfs ) == 0 {
472+ } else if len (kbfs ) == 0 {
430473 return nil , fmt .Errorf ("file not found. err: %w" , customerror .ErrNotFound )
431474 }
432475 // check write permission for the catalog
0 commit comments