seaweedfs/weed/filer/filer_deletion.go

156 lines
4.6 KiB
Go
Raw Normal View History

2020-09-01 15:21:19 +08:00
package filer
2018-11-21 12:56:28 +08:00
import (
"github.com/seaweedfs/seaweedfs/weed/storage"
2020-08-16 08:06:16 +08:00
"strings"
2018-11-21 12:56:28 +08:00
"time"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/operation"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/wdclient"
2018-11-21 12:56:28 +08:00
)
2021-08-13 12:40:33 +08:00
func LookupByMasterClientFn(masterClient *wdclient.MasterClient) func(vids []string) (map[string]*operation.LookupResult, error) {
return func(vids []string) (map[string]*operation.LookupResult, error) {
m := make(map[string]*operation.LookupResult)
2018-11-21 12:56:28 +08:00
for _, vid := range vids {
2020-03-25 17:20:19 +08:00
locs, _ := masterClient.GetVidLocations(vid)
2018-11-21 12:56:28 +08:00
var locations []operation.Location
for _, loc := range locs {
locations = append(locations, operation.Location{
Url: loc.Url,
PublicUrl: loc.PublicUrl,
})
}
2021-08-13 12:40:33 +08:00
m[vid] = &operation.LookupResult{
VolumeOrFileId: vid,
Locations: locations,
2018-11-21 12:56:28 +08:00
}
}
return m, nil
}
2020-03-25 17:20:19 +08:00
}
func (f *Filer) loopProcessingDeletion() {
lookupFunc := LookupByMasterClientFn(f.MasterClient)
2018-11-21 12:56:28 +08:00
2020-05-11 14:25:39 +08:00
DeletionBatchSize := 100000 // roughly 20 bytes cost per file id.
var deletionCount int
2018-11-21 12:56:28 +08:00
for {
deletionCount = 0
f.fileIdDeletionQueue.Consume(func(fileIds []string) {
2020-05-11 14:25:39 +08:00
for len(fileIds) > 0 {
var toDeleteFileIds []string
if len(fileIds) > DeletionBatchSize {
toDeleteFileIds = fileIds[:DeletionBatchSize]
fileIds = fileIds[DeletionBatchSize:]
} else {
toDeleteFileIds = fileIds
fileIds = fileIds[:0]
}
deletionCount = len(toDeleteFileIds)
2020-08-16 08:06:16 +08:00
_, err := operation.DeleteFilesWithLookupVolumeId(f.GrpcDialOption, toDeleteFileIds, lookupFunc)
2020-05-11 14:25:39 +08:00
if err != nil {
if !strings.Contains(err.Error(), storage.ErrorDeleted.Error()) {
2020-08-16 08:06:16 +08:00
glog.V(0).Infof("deleting fileIds len=%d error: %v", deletionCount, err)
}
2020-05-11 14:25:39 +08:00
} else {
glog.V(2).Infof("deleting fileIds %+v", toDeleteFileIds)
2020-05-11 14:25:39 +08:00
}
2020-03-21 14:38:24 +08:00
}
})
if deletionCount == 0 {
time.Sleep(1123 * time.Millisecond)
2018-11-21 12:56:28 +08:00
}
}
}
func (f *Filer) doDeleteFileIds(fileIds []string) {
lookupFunc := LookupByMasterClientFn(f.MasterClient)
DeletionBatchSize := 100000 // roughly 20 bytes cost per file id.
for len(fileIds) > 0 {
var toDeleteFileIds []string
if len(fileIds) > DeletionBatchSize {
toDeleteFileIds = fileIds[:DeletionBatchSize]
fileIds = fileIds[DeletionBatchSize:]
} else {
toDeleteFileIds = fileIds
fileIds = fileIds[:0]
}
deletionCount := len(toDeleteFileIds)
_, err := operation.DeleteFilesWithLookupVolumeId(f.GrpcDialOption, toDeleteFileIds, lookupFunc)
if err != nil {
if !strings.Contains(err.Error(), storage.ErrorDeleted.Error()) {
glog.V(0).Infof("deleting fileIds len=%d error: %v", deletionCount, err)
}
}
}
}
func (f *Filer) DirectDeleteChunks(chunks []*filer_pb.FileChunk) {
var fileIdsToDelete []string
for _, chunk := range chunks {
if !chunk.IsChunkManifest {
fileIdsToDelete = append(fileIdsToDelete, chunk.GetFileIdString())
continue
}
dataChunks, manifestResolveErr := ResolveOneChunkManifest(f.MasterClient.LookupFileId, chunk)
if manifestResolveErr != nil {
glog.V(0).Infof("failed to resolve manifest %s: %v", chunk.FileId, manifestResolveErr)
}
for _, dChunk := range dataChunks {
fileIdsToDelete = append(fileIdsToDelete, dChunk.GetFileIdString())
}
fileIdsToDelete = append(fileIdsToDelete, chunk.GetFileIdString())
}
f.doDeleteFileIds(fileIdsToDelete)
}
func (f *Filer) DeleteChunks(chunks []*filer_pb.FileChunk) {
2018-11-21 12:56:28 +08:00
for _, chunk := range chunks {
if !chunk.IsChunkManifest {
f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
continue
}
dataChunks, manifestResolveErr := ResolveOneChunkManifest(f.MasterClient.LookupFileId, chunk)
if manifestResolveErr != nil {
glog.V(0).Infof("failed to resolve manifest %s: %v", chunk.FileId, manifestResolveErr)
}
for _, dChunk := range dataChunks {
f.fileIdDeletionQueue.EnQueue(dChunk.GetFileIdString())
}
2020-08-31 15:16:03 +08:00
f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
2018-11-21 12:56:28 +08:00
}
}
func (f *Filer) DeleteChunksNotRecursive(chunks []*filer_pb.FileChunk) {
for _, chunk := range chunks {
f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
}
}
2018-11-21 12:56:28 +08:00
func (f *Filer) deleteChunksIfNotNew(oldEntry, newEntry *Entry) {
2024-06-10 03:14:10 +08:00
var oldChunks, newChunks []*filer_pb.FileChunk
if oldEntry != nil {
oldChunks = oldEntry.GetChunks()
2018-11-21 12:56:28 +08:00
}
2024-06-10 03:14:10 +08:00
if newEntry != nil {
newChunks = newEntry.GetChunks()
2018-11-21 12:56:28 +08:00
}
2024-06-10 03:14:10 +08:00
toDelete, err := MinusChunks(f.MasterClient.GetLookupFileIdFunction(), oldChunks, newChunks)
if err != nil {
glog.Errorf("Failed to resolve old entry chunks when delete old entry chunks. new: %s, old: %s",
2024-06-10 03:14:10 +08:00
newChunks, oldChunks)
return
}
f.DeleteChunksNotRecursive(toDelete)
2018-11-21 12:56:28 +08:00
}