2020-09-01 07:21:19 +00:00
|
|
|
package filer
|
2018-11-21 04:56:28 +00:00
|
|
|
|
|
|
|
import (
|
2022-10-31 18:34:45 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage"
|
2022-03-08 08:22:43 +00:00
|
|
|
"math"
|
2020-08-16 00:06:16 +00:00
|
|
|
"strings"
|
2018-11-21 04:56:28 +00:00
|
|
|
"time"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"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 04:56:28 +00:00
|
|
|
)
|
|
|
|
|
2021-08-13 04:40:33 +00: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 04:56:28 +00:00
|
|
|
for _, vid := range vids {
|
2020-03-25 09:20:19 +00:00
|
|
|
locs, _ := masterClient.GetVidLocations(vid)
|
2018-11-21 04:56:28 +00:00
|
|
|
var locations []operation.Location
|
|
|
|
for _, loc := range locs {
|
|
|
|
locations = append(locations, operation.Location{
|
|
|
|
Url: loc.Url,
|
|
|
|
PublicUrl: loc.PublicUrl,
|
|
|
|
})
|
|
|
|
}
|
2021-08-13 04:40:33 +00:00
|
|
|
m[vid] = &operation.LookupResult{
|
|
|
|
VolumeOrFileId: vid,
|
|
|
|
Locations: locations,
|
2018-11-21 04:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
2020-03-25 09:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filer) loopProcessingDeletion() {
|
|
|
|
|
|
|
|
lookupFunc := LookupByMasterClientFn(f.MasterClient)
|
2018-11-21 04:56:28 +00:00
|
|
|
|
2020-05-11 06:25:39 +00:00
|
|
|
DeletionBatchSize := 100000 // roughly 20 bytes cost per file id.
|
|
|
|
|
2020-02-20 23:44:17 +00:00
|
|
|
var deletionCount int
|
2018-11-21 04:56:28 +00:00
|
|
|
for {
|
2020-02-20 23:44:17 +00:00
|
|
|
deletionCount = 0
|
|
|
|
f.fileIdDeletionQueue.Consume(func(fileIds []string) {
|
2020-05-11 06:25:39 +00: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 00:06:16 +00:00
|
|
|
_, err := operation.DeleteFilesWithLookupVolumeId(f.GrpcDialOption, toDeleteFileIds, lookupFunc)
|
2020-05-11 06:25:39 +00:00
|
|
|
if err != nil {
|
2022-10-31 18:34:45 +00:00
|
|
|
if !strings.Contains(err.Error(), storage.ErrorDeleted.Error()) {
|
2020-08-16 00:06:16 +00:00
|
|
|
glog.V(0).Infof("deleting fileIds len=%d error: %v", deletionCount, err)
|
|
|
|
}
|
2020-05-11 06:25:39 +00:00
|
|
|
} else {
|
2022-09-06 15:19:13 +00:00
|
|
|
glog.V(2).Infof("deleting fileIds %+v", toDeleteFileIds)
|
2020-05-11 06:25:39 +00:00
|
|
|
}
|
2020-03-21 06:38:24 +00:00
|
|
|
}
|
2020-02-20 23:44:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if deletionCount == 0 {
|
|
|
|
time.Sleep(1123 * time.Millisecond)
|
2018-11-21 04:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 01:20:21 +00: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 {
|
2022-10-31 18:34:45 +00:00
|
|
|
if !strings.Contains(err.Error(), storage.ErrorDeleted.Error()) {
|
2020-11-18 01:20:21 +00:00
|
|
|
glog.V(0).Infof("deleting fileIds len=%d error: %v", deletionCount, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filer) DirectDeleteChunks(chunks []*filer_pb.FileChunk) {
|
2022-09-15 09:09:06 +00:00
|
|
|
var fileIdsToDelete []string
|
2020-11-18 01:20:21 +00:00
|
|
|
for _, chunk := range chunks {
|
|
|
|
if !chunk.IsChunkManifest {
|
2022-09-15 09:09:06 +00:00
|
|
|
fileIdsToDelete = append(fileIdsToDelete, chunk.GetFileIdString())
|
2020-11-18 01:20:21 +00:00
|
|
|
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 {
|
2022-09-15 09:09:06 +00:00
|
|
|
fileIdsToDelete = append(fileIdsToDelete, dChunk.GetFileIdString())
|
2020-11-18 01:20:21 +00:00
|
|
|
}
|
2022-09-15 09:09:06 +00:00
|
|
|
fileIdsToDelete = append(fileIdsToDelete, chunk.GetFileIdString())
|
2020-11-18 01:20:21 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 09:09:06 +00:00
|
|
|
f.doDeleteFileIds(fileIdsToDelete)
|
2020-11-18 01:20:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 08:23:05 +00:00
|
|
|
func (f *Filer) DeleteChunks(chunks []*filer_pb.FileChunk) {
|
2018-11-21 04:56:28 +00:00
|
|
|
for _, chunk := range chunks {
|
2020-08-30 09:07:14 +00:00
|
|
|
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 07:16:03 +00:00
|
|
|
f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
|
2018-11-21 04:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 07:14:24 +00:00
|
|
|
func (f *Filer) DeleteChunksNotRecursive(chunks []*filer_pb.FileChunk) {
|
|
|
|
for _, chunk := range chunks {
|
|
|
|
f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 04:56:28 +00:00
|
|
|
func (f *Filer) deleteChunksIfNotNew(oldEntry, newEntry *Entry) {
|
|
|
|
|
|
|
|
if oldEntry == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if newEntry == nil {
|
2019-12-13 08:23:05 +00:00
|
|
|
f.DeleteChunks(oldEntry.Chunks)
|
2021-11-29 08:32:21 +00:00
|
|
|
return
|
2018-11-21 04:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var toDelete []*filer_pb.FileChunk
|
2019-06-22 20:22:22 +00:00
|
|
|
newChunkIds := make(map[string]bool)
|
2022-03-08 08:22:43 +00:00
|
|
|
newDataChunks, newManifestChunks, err := ResolveChunkManifest(f.MasterClient.GetLookupFileIdFunction(),
|
|
|
|
newEntry.Chunks, 0, math.MaxInt64)
|
2022-03-07 07:14:24 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Failed to resolve new entry chunks when delete old entry chunks. new: %s, old: %s",
|
|
|
|
newEntry.Chunks, oldEntry.Chunks)
|
|
|
|
return
|
|
|
|
}
|
2022-03-08 08:22:43 +00:00
|
|
|
for _, newChunk := range newDataChunks {
|
2022-03-07 07:14:24 +00:00
|
|
|
newChunkIds[newChunk.GetFileIdString()] = true
|
|
|
|
}
|
2022-03-08 08:22:43 +00:00
|
|
|
for _, newChunk := range newManifestChunks {
|
2019-06-22 20:22:22 +00:00
|
|
|
newChunkIds[newChunk.GetFileIdString()] = true
|
|
|
|
}
|
2018-11-21 04:56:28 +00:00
|
|
|
|
2022-03-08 08:22:43 +00:00
|
|
|
oldDataChunks, oldManifestChunks, err := ResolveChunkManifest(f.MasterClient.GetLookupFileIdFunction(),
|
|
|
|
oldEntry.Chunks, 0, math.MaxInt64)
|
2022-03-07 07:14:24 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Failed to resolve old entry chunks when delete old entry chunks. new: %s, old: %s",
|
|
|
|
newEntry.Chunks, oldEntry.Chunks)
|
|
|
|
return
|
|
|
|
}
|
2022-03-08 08:22:43 +00:00
|
|
|
for _, oldChunk := range oldDataChunks {
|
2022-03-07 07:14:24 +00:00
|
|
|
if _, found := newChunkIds[oldChunk.GetFileIdString()]; !found {
|
|
|
|
toDelete = append(toDelete, oldChunk)
|
|
|
|
}
|
|
|
|
}
|
2022-03-08 08:22:43 +00:00
|
|
|
for _, oldChunk := range oldManifestChunks {
|
2019-06-22 20:22:22 +00:00
|
|
|
if _, found := newChunkIds[oldChunk.GetFileIdString()]; !found {
|
2018-11-21 04:56:28 +00:00
|
|
|
toDelete = append(toDelete, oldChunk)
|
|
|
|
}
|
|
|
|
}
|
2022-03-07 07:14:24 +00:00
|
|
|
f.DeleteChunksNotRecursive(toDelete)
|
2018-11-21 04:56:28 +00:00
|
|
|
}
|