2012-09-26 10:27:10 +00:00
|
|
|
package operation
|
|
|
|
|
|
|
|
import (
|
2014-04-15 16:09:40 +00:00
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2015-12-03 08:27:02 +00:00
|
|
|
"net/http"
|
|
|
|
|
2018-10-14 07:12:28 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2012-09-26 10:27:10 +00:00
|
|
|
)
|
|
|
|
|
2014-04-15 16:09:40 +00:00
|
|
|
type DeleteResult struct {
|
2015-12-03 08:27:02 +00:00
|
|
|
Fid string `json:"fid"`
|
|
|
|
Size int `json:"size"`
|
|
|
|
Status int `json:"status"`
|
|
|
|
Error string `json:"error,omitempty"`
|
2014-04-15 16:09:40 +00: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
|
|
|
|
}
|
|
|
|
|
2018-10-14 07:12:28 +00:00
|
|
|
// DeleteFiles batch deletes a list of fileIds
|
|
|
|
func DeleteFiles(master string, fileIds []string) ([]*volume_server_pb.DeleteResult, error) {
|
|
|
|
|
|
|
|
var ret []*volume_server_pb.DeleteResult
|
2014-04-15 16:09:40 +00: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 07:12:28 +00:00
|
|
|
ret = append(ret, &volume_server_pb.DeleteResult{
|
|
|
|
FileId: vid,
|
2015-12-03 08:27:02 +00:00
|
|
|
Status: http.StatusBadRequest,
|
|
|
|
Error: err.Error()},
|
|
|
|
)
|
2014-04-15 16:09:40 +00: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)
|
|
|
|
}
|
|
|
|
|
|
|
|
lookupResults, err := LookupVolumeIds(master, vids)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
server_to_fileIds := make(map[string][]string)
|
|
|
|
for vid, result := range lookupResults {
|
|
|
|
if result.Error != "" {
|
2018-10-14 07:12:28 +00:00
|
|
|
ret = append(ret, &volume_server_pb.DeleteResult{
|
|
|
|
FileId: vid,
|
|
|
|
Status: http.StatusBadRequest,
|
|
|
|
Error: err.Error()},
|
|
|
|
)
|
2014-04-15 16:09:40 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, location := range result.Locations {
|
2015-02-02 18:16:50 +00:00
|
|
|
if _, ok := server_to_fileIds[location.Url]; !ok {
|
|
|
|
server_to_fileIds[location.Url] = make([]string, 0)
|
2014-04-15 16:09:40 +00:00
|
|
|
}
|
2015-02-02 18:16:50 +00:00
|
|
|
server_to_fileIds[location.Url] = append(
|
|
|
|
server_to_fileIds[location.Url], vid_to_fileIds[vid]...)
|
2014-04-15 16:09:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
2015-12-03 08:27:02 +00:00
|
|
|
|
2014-04-15 16:09:40 +00:00
|
|
|
for server, fidList := range server_to_fileIds {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(server string, fidList []string) {
|
|
|
|
defer wg.Done()
|
2018-10-14 07:12:28 +00:00
|
|
|
|
|
|
|
if deleteResults, deleteErr := DeleteFilesAtOneVolumeServer(server, fidList); deleteErr != nil {
|
|
|
|
err = deleteErr
|
|
|
|
} else {
|
|
|
|
ret = append(ret, deleteResults...)
|
2014-04-15 16:09:40 +00:00
|
|
|
}
|
2018-10-14 07:12:28 +00:00
|
|
|
|
2014-04-15 16:09:40 +00:00
|
|
|
}(server, fidList)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
2018-10-14 07:12:28 +00:00
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFilesAtOneVolumeServer deletes a list of files that is on one volume server via gRpc
|
|
|
|
func DeleteFilesAtOneVolumeServer(volumeServer string, fileIds []string) (ret []*volume_server_pb.DeleteResult, err error) {
|
|
|
|
|
|
|
|
err = withVolumeServerClient(volumeServer, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
|
|
|
|
|
|
|
|
req := &volume_server_pb.BatchDeleteRequest{
|
|
|
|
FileIds: fileIds,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := volumeServerClient.BatchDelete(context.Background(), req)
|
|
|
|
|
|
|
|
fmt.Printf("deleted %v %v: %v\n", fileIds, err, resp)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, resp.Results...)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, result := range ret {
|
|
|
|
if result.Error != "" {
|
|
|
|
return nil, fmt.Errorf("delete fileId %s: %v", result.FileId, result.Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2014-04-15 16:09:40 +00:00
|
|
|
}
|