delete_content.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package operation
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "sync"
  9. "google.golang.org/grpc"
  10. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  11. )
  12. type DeleteResult struct {
  13. Fid string `json:"fid"`
  14. Size int `json:"size"`
  15. Status int `json:"status"`
  16. Error string `json:"error,omitempty"`
  17. }
  18. func ParseFileId(fid string) (vid string, key_cookie string, err error) {
  19. commaIndex := strings.Index(fid, ",")
  20. if commaIndex <= 0 {
  21. return "", "", errors.New("Wrong fid format.")
  22. }
  23. return fid[:commaIndex], fid[commaIndex+1:], nil
  24. }
  25. // DeleteFiles batch deletes a list of fileIds
  26. func DeleteFiles(master string, grpcDialOption grpc.DialOption, fileIds []string) ([]*volume_server_pb.DeleteResult, error) {
  27. lookupFunc := func(vids []string) (map[string]LookupResult, error) {
  28. return LookupVolumeIds(master, grpcDialOption, vids)
  29. }
  30. return DeleteFilesWithLookupVolumeId(grpcDialOption, fileIds, lookupFunc)
  31. }
  32. func DeleteFilesWithLookupVolumeId(grpcDialOption grpc.DialOption, fileIds []string, lookupFunc func(vid []string) (map[string]LookupResult, error)) ([]*volume_server_pb.DeleteResult, error) {
  33. var ret []*volume_server_pb.DeleteResult
  34. vid_to_fileIds := make(map[string][]string)
  35. var vids []string
  36. for _, fileId := range fileIds {
  37. vid, _, err := ParseFileId(fileId)
  38. if err != nil {
  39. ret = append(ret, &volume_server_pb.DeleteResult{
  40. FileId: fileId,
  41. Status: http.StatusBadRequest,
  42. Error: err.Error()},
  43. )
  44. continue
  45. }
  46. if _, ok := vid_to_fileIds[vid]; !ok {
  47. vid_to_fileIds[vid] = make([]string, 0)
  48. vids = append(vids, vid)
  49. }
  50. vid_to_fileIds[vid] = append(vid_to_fileIds[vid], fileId)
  51. }
  52. lookupResults, err := lookupFunc(vids)
  53. if err != nil {
  54. return ret, err
  55. }
  56. server_to_fileIds := make(map[string][]string)
  57. for vid, result := range lookupResults {
  58. if result.Error != "" {
  59. ret = append(ret, &volume_server_pb.DeleteResult{
  60. FileId: vid,
  61. Status: http.StatusBadRequest,
  62. Error: err.Error()},
  63. )
  64. continue
  65. }
  66. for _, location := range result.Locations {
  67. if _, ok := server_to_fileIds[location.Url]; !ok {
  68. server_to_fileIds[location.Url] = make([]string, 0)
  69. }
  70. server_to_fileIds[location.Url] = append(
  71. server_to_fileIds[location.Url], vid_to_fileIds[vid]...)
  72. }
  73. }
  74. resultChan := make(chan []*volume_server_pb.DeleteResult, len(server_to_fileIds))
  75. var wg sync.WaitGroup
  76. for server, fidList := range server_to_fileIds {
  77. wg.Add(1)
  78. go func(server string, fidList []string) {
  79. defer wg.Done()
  80. if deleteResults, deleteErr := DeleteFilesAtOneVolumeServer(server, grpcDialOption, fidList); deleteErr != nil {
  81. err = deleteErr
  82. } else if deleteResults != nil {
  83. resultChan <- deleteResults
  84. }
  85. }(server, fidList)
  86. }
  87. wg.Wait()
  88. close(resultChan)
  89. for result := range resultChan {
  90. ret = append(ret, result...)
  91. }
  92. return ret, err
  93. }
  94. // DeleteFilesAtOneVolumeServer deletes a list of files that is on one volume server via gRpc
  95. func DeleteFilesAtOneVolumeServer(volumeServer string, grpcDialOption grpc.DialOption, fileIds []string) (ret []*volume_server_pb.DeleteResult, err error) {
  96. err = WithVolumeServerClient(volumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  97. req := &volume_server_pb.BatchDeleteRequest{
  98. FileIds: fileIds,
  99. }
  100. resp, err := volumeServerClient.BatchDelete(context.Background(), req)
  101. // fmt.Printf("deleted %v %v: %v\n", fileIds, err, resp)
  102. if err != nil {
  103. return err
  104. }
  105. ret = append(ret, resp.Results...)
  106. return nil
  107. })
  108. if err != nil {
  109. return
  110. }
  111. for _, result := range ret {
  112. if result.Error != "" && result.Error != "not found" {
  113. return nil, fmt.Errorf("delete fileId %s: %v", result.FileId, result.Error)
  114. }
  115. }
  116. return
  117. }