2018-11-21 04:56:28 +00:00
|
|
|
package filer2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2018-11-23 08:26:15 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-11-21 04:56:28 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2020-03-25 09:20:19 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/wdclient"
|
2018-11-21 04:56:28 +00:00
|
|
|
)
|
|
|
|
|
2020-03-25 09:20:19 +00:00
|
|
|
func LookupByMasterClientFn(masterClient *wdclient.MasterClient) func(vids []string) (map[string]operation.LookupResult, error) {
|
|
|
|
return func(vids []string) (map[string]operation.LookupResult, error) {
|
2018-11-21 04:56:28 +00:00
|
|
|
m := make(map[string]operation.LookupResult)
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
m[vid] = operation.LookupResult{
|
|
|
|
VolumeId: vid,
|
|
|
|
Locations: locations,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
deleteResults, err := operation.DeleteFilesWithLookupVolumeId(f.GrpcDialOption, toDeleteFileIds, lookupFunc)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("deleting fileIds len=%d error: %v", deletionCount, err)
|
|
|
|
} else {
|
|
|
|
glog.V(1).Infof("deleting fileIds len=%d", deletionCount)
|
|
|
|
}
|
|
|
|
if len(deleteResults) != deletionCount {
|
|
|
|
glog.V(0).Infof("delete %d fileIds actual %d", deletionCount, len(deleteResults))
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-20 23:44:17 +00:00
|
|
|
f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
|
2018-11-21 04:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 20:57:45 +00:00
|
|
|
// DeleteFileByFileId direct delete by file id.
|
|
|
|
// Only used when the fileId is not being managed by snapshots.
|
2018-11-21 04:56:28 +00:00
|
|
|
func (f *Filer) DeleteFileByFileId(fileId string) {
|
2020-02-20 23:44:17 +00:00
|
|
|
f.fileIdDeletionQueue.EnQueue(fileId)
|
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)
|
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)
|
|
|
|
for _, newChunk := range newEntry.Chunks {
|
|
|
|
newChunkIds[newChunk.GetFileIdString()] = true
|
|
|
|
}
|
2018-11-21 04:56:28 +00:00
|
|
|
|
|
|
|
for _, oldChunk := range oldEntry.Chunks {
|
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)
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 08:23:05 +00:00
|
|
|
f.DeleteChunks(toDelete)
|
2018-11-21 04:56:28 +00:00
|
|
|
}
|