2012-09-26 18:27:10 +08:00
|
|
|
package operation
|
|
|
|
|
|
|
|
import (
|
2018-10-14 15:30:20 +08:00
|
|
|
"context"
|
2014-04-16 00:09:40 +08:00
|
|
|
"errors"
|
2018-10-14 15:30:20 +08:00
|
|
|
"fmt"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
2021-01-25 11:01:58 +08:00
|
|
|
"google.golang.org/grpc"
|
2018-10-14 15:30:20 +08:00
|
|
|
"net/http"
|
2014-04-16 00:09:40 +08:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2020-01-09 13:50:59 +08:00
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
2012-09-26 18:27:10 +08:00
|
|
|
)
|
|
|
|
|
2014-04-16 00:09:40 +08:00
|
|
|
type DeleteResult struct {
|
2015-12-03 16:27:02 +08:00
|
|
|
Fid string `json:"fid"`
|
|
|
|
Size int `json:"size"`
|
|
|
|
Status int `json:"status"`
|
|
|
|
Error string `json:"error,omitempty"`
|
2014-04-16 00:09:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func ParseFileId(fid string) (vid string, key_cookie string, err error) {
|
|
|
|
commaIndex := strings.Index(fid, ",")
|
|
|
|
if commaIndex <= 0 {
|
|
|
|
return "", "", errors.New("Wrong fid format.")
|
|
|
|
}
|
|
|
|
return fid[:commaIndex], fid[commaIndex+1:], nil
|
|
|
|
}
|
|
|
|
|
2024-08-02 14:27:23 +08:00
|
|
|
// DeleteFileIds batch deletes a list of fileIds
|
|
|
|
func DeleteFileIds(masterFn GetMasterFn, usePublicUrl bool, grpcDialOption grpc.DialOption, fileIds []string) ([]*volume_server_pb.DeleteResult, error) {
|
2018-10-14 15:12:28 +08:00
|
|
|
|
2021-08-13 12:40:33 +08:00
|
|
|
lookupFunc := func(vids []string) (results map[string]*LookupResult, err error) {
|
2021-02-18 12:55:55 +08:00
|
|
|
results, err = LookupVolumeIds(masterFn, grpcDialOption, vids)
|
2020-03-19 02:16:45 +08:00
|
|
|
if err == nil && usePublicUrl {
|
|
|
|
for _, result := range results {
|
|
|
|
for _, loc := range result.Locations {
|
|
|
|
loc.Url = loc.PublicUrl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2018-11-21 12:56:28 +08:00
|
|
|
}
|
|
|
|
|
2024-08-02 14:27:23 +08:00
|
|
|
return DeleteFileIdsWithLookupVolumeId(grpcDialOption, fileIds, lookupFunc)
|
2018-11-21 12:56:28 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-08-02 14:27:23 +08:00
|
|
|
func DeleteFileIdsWithLookupVolumeId(grpcDialOption grpc.DialOption, fileIds []string, lookupFunc func(vid []string) (map[string]*LookupResult, error)) ([]*volume_server_pb.DeleteResult, error) {
|
2018-11-21 12:56:28 +08:00
|
|
|
|
2018-10-14 15:12:28 +08:00
|
|
|
var ret []*volume_server_pb.DeleteResult
|
2014-04-16 00:09:40 +08:00
|
|
|
|
|
|
|
vid_to_fileIds := make(map[string][]string)
|
|
|
|
var vids []string
|
|
|
|
for _, fileId := range fileIds {
|
|
|
|
vid, _, err := ParseFileId(fileId)
|
|
|
|
if err != nil {
|
2018-10-14 15:12:28 +08:00
|
|
|
ret = append(ret, &volume_server_pb.DeleteResult{
|
2019-02-15 16:09:19 +08:00
|
|
|
FileId: fileId,
|
2015-12-03 16:27:02 +08:00
|
|
|
Status: http.StatusBadRequest,
|
|
|
|
Error: err.Error()},
|
|
|
|
)
|
2014-04-16 00:09:40 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := vid_to_fileIds[vid]; !ok {
|
|
|
|
vid_to_fileIds[vid] = make([]string, 0)
|
|
|
|
vids = append(vids, vid)
|
|
|
|
}
|
|
|
|
vid_to_fileIds[vid] = append(vid_to_fileIds[vid], fileId)
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:56:28 +08:00
|
|
|
lookupResults, err := lookupFunc(vids)
|
2014-04-16 00:09:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:47:52 +08:00
|
|
|
server_to_fileIds := make(map[pb.ServerAddress][]string)
|
2014-04-16 00:09:40 +08:00
|
|
|
for vid, result := range lookupResults {
|
|
|
|
if result.Error != "" {
|
2018-10-14 15:12:28 +08:00
|
|
|
ret = append(ret, &volume_server_pb.DeleteResult{
|
|
|
|
FileId: vid,
|
|
|
|
Status: http.StatusBadRequest,
|
2022-02-15 23:07:03 +08:00
|
|
|
Error: result.Error},
|
2018-10-14 15:12:28 +08:00
|
|
|
)
|
2014-04-16 00:09:40 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, location := range result.Locations {
|
2021-09-13 13:47:52 +08:00
|
|
|
serverAddress := location.ServerAddress()
|
|
|
|
if _, ok := server_to_fileIds[serverAddress]; !ok {
|
|
|
|
server_to_fileIds[serverAddress] = make([]string, 0)
|
2014-04-16 00:09:40 +08:00
|
|
|
}
|
2021-09-13 13:47:52 +08:00
|
|
|
server_to_fileIds[serverAddress] = append(
|
|
|
|
server_to_fileIds[serverAddress], vid_to_fileIds[vid]...)
|
2014-04-16 00:09:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 16:12:41 +08:00
|
|
|
resultChan := make(chan []*volume_server_pb.DeleteResult, len(server_to_fileIds))
|
2014-04-16 00:09:40 +08:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for server, fidList := range server_to_fileIds {
|
|
|
|
wg.Add(1)
|
2021-09-13 13:47:52 +08:00
|
|
|
go func(server pb.ServerAddress, fidList []string) {
|
2014-04-16 00:09:40 +08:00
|
|
|
defer wg.Done()
|
2018-10-14 15:12:28 +08:00
|
|
|
|
2024-08-02 14:27:23 +08:00
|
|
|
if deleteResults, deleteErr := DeleteFileIdsAtOneVolumeServer(server, grpcDialOption, fidList, false); deleteErr != nil {
|
2018-10-14 15:12:28 +08:00
|
|
|
err = deleteErr
|
2020-01-09 13:50:59 +08:00
|
|
|
} else if deleteResults != nil {
|
2019-06-28 16:12:41 +08:00
|
|
|
resultChan <- deleteResults
|
2014-04-16 00:09:40 +08:00
|
|
|
}
|
2018-10-14 15:12:28 +08:00
|
|
|
|
2014-04-16 00:09:40 +08:00
|
|
|
}(server, fidList)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2019-06-28 16:12:41 +08:00
|
|
|
close(resultChan)
|
|
|
|
|
|
|
|
for result := range resultChan {
|
|
|
|
ret = append(ret, result...)
|
|
|
|
}
|
|
|
|
|
2018-10-14 15:12:28 +08:00
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2024-08-02 14:27:23 +08:00
|
|
|
// DeleteFileIdsAtOneVolumeServer deletes a list of files that is on one volume server via gRpc
|
|
|
|
func DeleteFileIdsAtOneVolumeServer(volumeServer pb.ServerAddress, grpcDialOption grpc.DialOption, fileIds []string, includeCookie bool) (ret []*volume_server_pb.DeleteResult, err error) {
|
2018-10-14 15:12:28 +08:00
|
|
|
|
2021-12-26 16:15:03 +08:00
|
|
|
err = WithVolumeServerClient(false, volumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
|
2018-10-14 15:12:28 +08:00
|
|
|
|
|
|
|
req := &volume_server_pb.BatchDeleteRequest{
|
2020-03-25 17:21:15 +08:00
|
|
|
FileIds: fileIds,
|
|
|
|
SkipCookieCheck: !includeCookie,
|
2018-10-14 15:12:28 +08:00
|
|
|
}
|
|
|
|
|
2019-02-20 17:01:01 +08:00
|
|
|
resp, err := volumeServerClient.BatchDelete(context.Background(), req)
|
2018-10-14 15:12:28 +08:00
|
|
|
|
2018-11-19 03:51:38 +08:00
|
|
|
// fmt.Printf("deleted %v %v: %v\n", fileIds, err, resp)
|
2018-10-14 15:12:28 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, resp.Results...)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, result := range ret {
|
2019-01-06 11:52:38 +08:00
|
|
|
if result.Error != "" && result.Error != "not found" {
|
2018-10-14 15:12:28 +08:00
|
|
|
return nil, fmt.Errorf("delete fileId %s: %v", result.FileId, result.Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2014-04-16 00:09:40 +08:00
|
|
|
}
|