mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3e00277382 | ||
|
|
15253151b5 | ||
|
|
11ec2458f0 | ||
|
|
b9b1356bc9 | ||
|
|
3af39efaf6 | ||
|
|
84f05e2fe6 | ||
|
|
b15183e4d3 | ||
|
|
d0420bcae0 | ||
|
|
8d60f3bde9 |
No files matched your search
+32
-17
@@ -345,6 +345,7 @@ func (f *Filer) AggregateFromPeers(self pb.ServerAddress, existingNodes []*maste
|
||||
f.EmptyFolderCleaner = empty_folder_cleanup.NewEmptyFolderCleaner(f, f.Dlm.LockRing, self, f.DirBucketsPath, f.EmptyFolderCleanupDelay)
|
||||
|
||||
f.MetaAggregator = NewMetaAggregator(f, self, f.GrpcDialOption)
|
||||
go f.loopSweepingExpiredEntries(self)
|
||||
// The ring starts empty while peer history sits on disk: mark the pre-startFrom
|
||||
// range evicted so a cursor there reads disk, not the ring's earliest entry.
|
||||
f.MetaAggregator.MetaLogBuffer.MarkEvictedThrough(startFrom.UnixNano())
|
||||
@@ -1165,7 +1166,7 @@ func (f *Filer) doListDirectoryEntries(ctx context.Context, p util.FullPath, sta
|
||||
}
|
||||
}
|
||||
for _, entry := range expiredEntries {
|
||||
if delErr := f.Store.DeleteOneEntry(ctx, entry); delErr != nil {
|
||||
if delErr := f.deleteExpiredEntry(ctx, entry.FullPath); delErr != nil {
|
||||
glog.ErrorfCtx(ctx, "doListDirectoryEntries DeleteOneEntry %s failed: %v", entry.FullPath, delErr)
|
||||
}
|
||||
}
|
||||
@@ -1225,31 +1226,45 @@ func (f *Filer) DeleteEmptyParentDirectories(ctx context.Context, dirPath util.F
|
||||
return
|
||||
}
|
||||
|
||||
// Check if directory is empty
|
||||
parentDir, _ := dirPath.DirAndName()
|
||||
if f.deleteEmptyDirectory(ctx, dirPath) {
|
||||
f.DeleteEmptyParentDirectories(ctx, util.FullPath(parentDir), stopAtPath)
|
||||
}
|
||||
}
|
||||
|
||||
// deleteEmptyDirectory removes the directory row under the lock writers take,
|
||||
// unless the row holds an S3 object or a child, or a writer has the path.
|
||||
// Emptiness is checked last, so a child created meanwhile keeps the row.
|
||||
func (f *Filer) deleteEmptyDirectory(ctx context.Context, dirPath util.FullPath) bool {
|
||||
if lt := f.EntryLockTable; lt != nil {
|
||||
lock, acquired := lt.TryAcquireLock("DeleteEmptyParentDirectories", dirPath, util.ExclusiveLock)
|
||||
if !acquired {
|
||||
return false
|
||||
}
|
||||
defer lt.ReleaseLock(dirPath, lock)
|
||||
}
|
||||
if isKeyObject, keyErr := f.IsDirectoryKeyObject(ctx, dirPath); keyErr != nil || isKeyObject {
|
||||
return false
|
||||
}
|
||||
dirEntry, findErr := f.FindEntry(ctx, dirPath)
|
||||
if findErr != nil {
|
||||
return false
|
||||
}
|
||||
isEmpty, err := f.IsDirectoryEmpty(ctx, dirPath)
|
||||
if err != nil {
|
||||
glog.V(3).InfofCtx(ctx, "DeleteEmptyParentDirectories: error checking %s: %v", dirPath, err)
|
||||
return
|
||||
return false
|
||||
}
|
||||
|
||||
if !isEmpty {
|
||||
// Directory is not empty, stop checking upward
|
||||
glog.V(3).InfofCtx(ctx, "DeleteEmptyParentDirectories: directory %s is not empty, stopping cleanup", dirPath)
|
||||
return
|
||||
return false
|
||||
}
|
||||
|
||||
// Directory is empty, try to delete it
|
||||
glog.V(2).InfofCtx(ctx, "DeleteEmptyParentDirectories: deleting empty directory %s", dirPath)
|
||||
parentDir, _ := dirPath.DirAndName()
|
||||
if dirEntry, findErr := f.FindEntry(ctx, dirPath); findErr == nil {
|
||||
if delErr := f.doDeleteEntryMetaAndData(f.emptyFolderDeleteContext(ctx, dirPath), dirEntry, false, false, nil); delErr == nil {
|
||||
// Successfully deleted, continue checking upwards
|
||||
f.DeleteEmptyParentDirectories(ctx, util.FullPath(parentDir), stopAtPath)
|
||||
} else {
|
||||
// Failed to delete, stop cleanup
|
||||
glog.V(3).InfofCtx(ctx, "DeleteEmptyParentDirectories: failed to delete %s: %v", dirPath, delErr)
|
||||
}
|
||||
if delErr := f.doDeleteEntryMetaAndData(f.emptyFolderDeleteContext(ctx, dirPath), dirEntry, false, false, nil); delErr != nil {
|
||||
glog.V(3).InfofCtx(ctx, "DeleteEmptyParentDirectories: failed to delete %s: %v", dirPath, delErr)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// IsDirectoryEmpty checks if a directory contains any entries
|
||||
|
||||
@@ -0,0 +1,216 @@
|
||||
package filer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/glog"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
)
|
||||
|
||||
const (
|
||||
expiredEntrySweepMinInterval = time.Hour
|
||||
expiredEntrySweepMaxInterval = 24 * time.Hour
|
||||
expiredEntrySweepPageSize = 1024
|
||||
)
|
||||
|
||||
// loopSweepingExpiredEntries walks the paths that have a TTL rule. Expiry is
|
||||
// otherwise applied only when an entry is looked up or its directory listed,
|
||||
// so on a store without native TTL the rows of entries nobody visits again
|
||||
// outlive their volumes.
|
||||
func (f *Filer) loopSweepingExpiredEntries(self pb.ServerAddress) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go func() {
|
||||
<-f.deletionQuit
|
||||
cancel()
|
||||
}()
|
||||
|
||||
lastSwept := make(map[string]time.Time)
|
||||
ticker := time.NewTicker(expiredEntrySweepMinInterval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
}
|
||||
f.sweepExpiredEntries(ctx, self, lastSwept, time.Now())
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Filer) sweepExpiredEntries(ctx context.Context, self pb.ServerAddress, lastSwept map[string]time.Time, now time.Time) {
|
||||
fc := f.FilerConf
|
||||
if fc == nil {
|
||||
return
|
||||
}
|
||||
for locationPrefix, interval := range expiredEntrySweepPrefixes(fc) {
|
||||
if now.Sub(lastSwept[locationPrefix]) < interval || !f.sweepsExpiredEntries(locationPrefix, self) {
|
||||
continue
|
||||
}
|
||||
expired, err := f.sweepExpiredEntriesUnder(ctx, locationPrefix)
|
||||
if err != nil {
|
||||
glog.V(0).Infof("sweep expired entries under %s: %v", locationPrefix, err)
|
||||
continue
|
||||
}
|
||||
lastSwept[locationPrefix] = now
|
||||
if expired > 0 {
|
||||
glog.V(0).Infof("removed %d expired entries under %s", expired, locationPrefix)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// expiredEntrySweepPrefixes maps each TTL rule that no other TTL rule
|
||||
// encloses to how often it is swept: its TTL, kept between an hour and a day.
|
||||
func expiredEntrySweepPrefixes(fc *FilerConf) map[string]time.Duration {
|
||||
intervals := make(map[string]time.Duration)
|
||||
for _, rule := range fc.ToProto().Locations {
|
||||
ttl, err := needle.ReadTTL(rule.GetTtl())
|
||||
if err != nil || ttl.ToSeconds() == 0 {
|
||||
continue
|
||||
}
|
||||
interval := time.Duration(ttl.ToSeconds()) * time.Second
|
||||
if interval < expiredEntrySweepMinInterval {
|
||||
interval = expiredEntrySweepMinInterval
|
||||
} else if interval > expiredEntrySweepMaxInterval {
|
||||
interval = expiredEntrySweepMaxInterval
|
||||
}
|
||||
intervals[rule.LocationPrefix] = interval
|
||||
}
|
||||
prefixes := make([]string, 0, len(intervals))
|
||||
for locationPrefix := range intervals {
|
||||
prefixes = append(prefixes, locationPrefix)
|
||||
}
|
||||
sort.Strings(prefixes)
|
||||
swept := make(map[string]time.Duration)
|
||||
enclosing := ""
|
||||
for _, locationPrefix := range prefixes {
|
||||
if enclosing != "" && strings.HasPrefix(locationPrefix, enclosing) {
|
||||
if intervals[locationPrefix] < swept[enclosing] {
|
||||
swept[enclosing] = intervals[locationPrefix]
|
||||
}
|
||||
continue
|
||||
}
|
||||
enclosing = locationPrefix
|
||||
swept[locationPrefix] = intervals[locationPrefix]
|
||||
}
|
||||
return swept
|
||||
}
|
||||
|
||||
// sweepsExpiredEntries leaves a prefix to its lock-ring owner when that filer
|
||||
// runs on this filer's store; a filer with a store of its own sweeps it.
|
||||
func (f *Filer) sweepsExpiredEntries(locationPrefix string, self pb.ServerAddress) bool {
|
||||
if f.Dlm == nil {
|
||||
return true
|
||||
}
|
||||
primary := f.Dlm.LockRing.GetPrimary(locationPrefix)
|
||||
if primary == "" || primary == self {
|
||||
return true
|
||||
}
|
||||
return f.MetaAggregator == nil || !f.MetaAggregator.SharesStore(primary)
|
||||
}
|
||||
|
||||
func (f *Filer) sweepExpiredEntriesUnder(ctx context.Context, locationPrefix string) (int64, error) {
|
||||
ctx = context.WithValue(ctx, lazyListContextKey{}, true)
|
||||
if strings.HasSuffix(locationPrefix, "/") {
|
||||
dir := strings.TrimSuffix(locationPrefix, "/")
|
||||
if dir == "" {
|
||||
dir = "/"
|
||||
}
|
||||
return f.sweepExpiredEntriesIn(ctx, util.FullPath(dir), "")
|
||||
}
|
||||
dir, name := util.FullPath(locationPrefix).DirAndName()
|
||||
return f.sweepExpiredEntriesIn(ctx, util.FullPath(dir), name)
|
||||
}
|
||||
|
||||
// sweepExpiredEntriesIn lists dir and everything below it, so the listing
|
||||
// removes the expired entries it passes, then drops dir if that emptied it.
|
||||
func (f *Filer) sweepExpiredEntriesIn(ctx context.Context, dir util.FullPath, namePrefix string) (expired int64, err error) {
|
||||
startFileName := ""
|
||||
for {
|
||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
||||
return expired, ctxErr
|
||||
}
|
||||
var subDirs []util.FullPath
|
||||
var listed int64
|
||||
expiredCount, lastFileName, listErr := f.doListDirectoryEntries(ctx, dir, startFileName, false, expiredEntrySweepPageSize, namePrefix, func(entry *Entry) (bool, error) {
|
||||
listed++
|
||||
if entry.IsDirectory() {
|
||||
subDirs = append(subDirs, entry.FullPath)
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
if listErr != nil {
|
||||
return expired, listErr
|
||||
}
|
||||
expired += expiredCount
|
||||
for _, subDir := range subDirs {
|
||||
subExpired, subErr := f.sweepExpiredEntriesIn(ctx, subDir, "")
|
||||
expired += subExpired
|
||||
if subErr != nil {
|
||||
return expired, subErr
|
||||
}
|
||||
}
|
||||
if listed+expiredCount < expiredEntrySweepPageSize || lastFileName == "" {
|
||||
break
|
||||
}
|
||||
startFileName = lastFileName
|
||||
}
|
||||
if expired > 0 && dir != "/" {
|
||||
f.DeleteEmptyParentDirectories(ctx, dir, util.FullPath(f.DirBucketsPath))
|
||||
}
|
||||
return expired, nil
|
||||
}
|
||||
|
||||
// deleteExpiredEntry removes the row at p only while it still holds an
|
||||
// expired entry, under the lock writers take, so a write that replaced the
|
||||
// listed entry keeps its row. A contended path is left to a later listing.
|
||||
// Where the store refuses a commit over another writer's change, the re-read
|
||||
// and the delete share a transaction, which keeps a replacement written
|
||||
// through another filer too.
|
||||
func (f *Filer) deleteExpiredEntry(ctx context.Context, p util.FullPath) error {
|
||||
if lt := f.EntryLockTable; lt != nil {
|
||||
lock, acquired := lt.TryAcquireLock("deleteExpiredEntry", p, util.ExclusiveLock)
|
||||
if !acquired {
|
||||
return nil
|
||||
}
|
||||
defer lt.ReleaseLock(p, lock)
|
||||
}
|
||||
if !f.RefusesConflictingCommits(p) || ctx.Value(storeTransactionKey{}) != nil {
|
||||
return f.deleteIfExpired(ctx, p)
|
||||
}
|
||||
txCtx, err := f.BeginTransaction(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = f.deleteIfExpired(txCtx, p); err == nil {
|
||||
err = f.CommitTransaction(txCtx)
|
||||
}
|
||||
if err != nil {
|
||||
f.RollbackTransaction(txCtx)
|
||||
if errors.Is(err, ErrTransactionConflict) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (f *Filer) deleteIfExpired(ctx context.Context, p util.FullPath) error {
|
||||
current, err := f.Store.FindEntry(ctx, p)
|
||||
if err != nil {
|
||||
if errors.Is(err, filer_pb.ErrNotFound) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if current.IsDirectory() || current.TtlSec <= 0 || current.IsExpireS3Enabled() || !current.Crtime.Add(time.Duration(current.TtlSec)*time.Second).Before(time.Now()) {
|
||||
return nil
|
||||
}
|
||||
return f.Store.DeleteOneEntry(ctx, current)
|
||||
}
|
||||
@@ -0,0 +1,333 @@
|
||||
package filer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
)
|
||||
|
||||
func putSweepTestEntry(t *testing.T, store *stubFilerStore, path string, isDir bool, ttlSec int32, crtime time.Time) {
|
||||
t.Helper()
|
||||
mode := os.FileMode(0o644)
|
||||
if isDir {
|
||||
mode = os.ModeDir | 0o755
|
||||
}
|
||||
entry := &Entry{FullPath: util.FullPath(path), Attr: Attr{Mode: mode, Crtime: crtime, Mtime: crtime, TtlSec: ttlSec}}
|
||||
if err := store.InsertEntry(context.Background(), entry); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSweepExpiredEntriesRemovesRowsNobodyVisits(t *testing.T) {
|
||||
f, store := newTestFilerWithStubStore(t)
|
||||
f.DirBucketsPath = "/buckets"
|
||||
f.FilerConf = NewFilerConf()
|
||||
for _, rule := range []*filer_pb.FilerConf_PathConf{
|
||||
{LocationPrefix: "/buckets/ttl-demo/", Ttl: "2m"},
|
||||
{LocationPrefix: "/ttl-http/logs", Ttl: "3d"},
|
||||
} {
|
||||
if err := f.FilerConf.SetLocationConf(rule); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
old := now.Add(-time.Hour)
|
||||
for _, dir := range []string{"/buckets", "/buckets/ttl-demo", "/buckets/other", "/ttl-http"} {
|
||||
putSweepTestEntry(t, store, dir, true, 0, old)
|
||||
}
|
||||
for _, dir := range []string{"day01", "day02", "day03"} {
|
||||
putSweepTestEntry(t, store, "/buckets/ttl-demo/"+dir, true, 0, old)
|
||||
}
|
||||
for i := 0; i < expiredEntrySweepPageSize+300; i++ {
|
||||
putSweepTestEntry(t, store, fmt.Sprintf("/buckets/ttl-demo/day01/f%05d", i), false, 120, old)
|
||||
}
|
||||
putSweepTestEntry(t, store, "/buckets/ttl-demo/day02/f1", false, 120, old)
|
||||
putSweepTestEntry(t, store, "/buckets/ttl-demo/day03/old", false, 120, old)
|
||||
putSweepTestEntry(t, store, "/buckets/ttl-demo/day03/fresh", false, 120, now)
|
||||
putSweepTestEntry(t, store, "/buckets/other/old", false, 120, old)
|
||||
putSweepTestEntry(t, store, "/ttl-http/logs1", false, 120, old)
|
||||
putSweepTestEntry(t, store, "/ttl-http/other", false, 120, old)
|
||||
|
||||
lastSwept := make(map[string]time.Time)
|
||||
f.sweepExpiredEntries(context.Background(), "", lastSwept, now)
|
||||
|
||||
gone := []string{
|
||||
"/buckets/ttl-demo/day01", "/buckets/ttl-demo/day01/f00000", "/buckets/ttl-demo/day01/f01300",
|
||||
"/buckets/ttl-demo/day02", "/buckets/ttl-demo/day02/f1",
|
||||
"/buckets/ttl-demo/day03/old", "/ttl-http/logs1",
|
||||
}
|
||||
for _, p := range gone {
|
||||
if store.getEntry(p) != nil {
|
||||
t.Errorf("%s still stored after the sweep", p)
|
||||
}
|
||||
}
|
||||
kept := []string{
|
||||
"/buckets/ttl-demo", "/buckets/ttl-demo/day03", "/buckets/ttl-demo/day03/fresh",
|
||||
"/buckets/other/old", "/ttl-http/other",
|
||||
}
|
||||
for _, p := range kept {
|
||||
if store.getEntry(p) == nil {
|
||||
t.Errorf("%s removed by the sweep", p)
|
||||
}
|
||||
}
|
||||
if !lastSwept["/buckets/ttl-demo/"].Equal(now) || !lastSwept["/ttl-http/logs"].Equal(now) {
|
||||
t.Fatalf("sweep times not recorded: %v", lastSwept)
|
||||
}
|
||||
|
||||
putSweepTestEntry(t, store, "/buckets/ttl-demo/day03/later", false, 120, old)
|
||||
f.sweepExpiredEntries(context.Background(), "", lastSwept, now.Add(30*time.Minute))
|
||||
if store.getEntry("/buckets/ttl-demo/day03/later") == nil {
|
||||
t.Fatal("swept again before its interval passed")
|
||||
}
|
||||
f.sweepExpiredEntries(context.Background(), "", lastSwept, now.Add(expiredEntrySweepMinInterval))
|
||||
if store.getEntry("/buckets/ttl-demo/day03/later") != nil {
|
||||
t.Fatal("not swept once its interval passed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpiredEntrySweepPrefixes(t *testing.T) {
|
||||
fc := NewFilerConf()
|
||||
for _, rule := range []*filer_pb.FilerConf_PathConf{
|
||||
{LocationPrefix: "/a/", Ttl: "3d"},
|
||||
{LocationPrefix: "/a/b/", Ttl: "2m"},
|
||||
{LocationPrefix: "/a0/", Ttl: "1w"},
|
||||
{LocationPrefix: "/c/", Collection: "c"},
|
||||
{LocationPrefix: "/d/", Ttl: "5h"},
|
||||
} {
|
||||
if err := fc.SetLocationConf(rule); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
got := expiredEntrySweepPrefixes(fc)
|
||||
want := map[string]time.Duration{
|
||||
"/a/": expiredEntrySweepMinInterval,
|
||||
"/a0/": expiredEntrySweepMaxInterval,
|
||||
"/d/": 5 * time.Hour,
|
||||
}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
for prefix, interval := range want {
|
||||
if got[prefix] != interval {
|
||||
t.Errorf("%s: got %v, want %v", prefix, got[prefix], interval)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSweepsExpiredEntriesOnlyOnceForASharedStore(t *testing.T) {
|
||||
f, _ := newTestFilerWithStubStore(t)
|
||||
self, peer := pb.ServerAddress("filer1:8888"), pb.ServerAddress("filer2:8888")
|
||||
f.Dlm.LockRing.SetSnapshot([]pb.ServerAddress{self, peer}, 1)
|
||||
f.MetaAggregator = &MetaAggregator{peerSharesStore: map[pb.ServerAddress]bool{peer: true}}
|
||||
|
||||
var ownedBySelf, ownedByPeer string
|
||||
for i := 0; ownedBySelf == "" || ownedByPeer == ""; i++ {
|
||||
prefix := fmt.Sprintf("/buckets/b%d/", i)
|
||||
if f.Dlm.LockRing.GetPrimary(prefix) == self {
|
||||
ownedBySelf = prefix
|
||||
} else {
|
||||
ownedByPeer = prefix
|
||||
}
|
||||
}
|
||||
if !f.sweepsExpiredEntries(ownedBySelf, self) {
|
||||
t.Errorf("%s: owner does not sweep", ownedBySelf)
|
||||
}
|
||||
if f.sweepsExpiredEntries(ownedByPeer, self) {
|
||||
t.Errorf("%s: swept here although its owner shares this store", ownedByPeer)
|
||||
}
|
||||
f.MetaAggregator.peerSharesStore[peer] = false
|
||||
if !f.sweepsExpiredEntries(ownedByPeer, self) {
|
||||
t.Errorf("%s: not swept here although its owner has a store of its own", ownedByPeer)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSweepExpiredEntriesKeepsADirectoryKeyObject(t *testing.T) {
|
||||
f, store := newTestFilerWithStubStore(t)
|
||||
f.DirBucketsPath = "/buckets"
|
||||
f.FilerConf = NewFilerConf()
|
||||
if err := f.FilerConf.SetLocationConf(&filer_pb.FilerConf_PathConf{LocationPrefix: "/buckets/b/", Ttl: "2m"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := time.Now().Add(-time.Hour)
|
||||
putSweepTestEntry(t, store, "/buckets", true, 0, old)
|
||||
putSweepTestEntry(t, store, "/buckets/b", true, 0, old)
|
||||
reports := &Entry{FullPath: "/buckets/b/reports", Attr: Attr{Mode: os.ModeDir | 0o755, Crtime: old, Mtime: old, Mime: "text/plain"},
|
||||
Extended: map[string][]byte{s3_constants.SeaweedFSPrefixObject: []byte("true")}}
|
||||
if err := store.InsertEntry(context.Background(), reports); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
putSweepTestEntry(t, store, "/buckets/b/reports/day1", false, 120, old)
|
||||
|
||||
f.sweepExpiredEntries(context.Background(), "", make(map[string]time.Time), time.Now())
|
||||
|
||||
if store.getEntry("/buckets/b/reports/day1") != nil {
|
||||
t.Fatal("expired child not removed")
|
||||
}
|
||||
if store.getEntry("/buckets/b/reports") == nil {
|
||||
t.Fatal("directory holding an object removed with its last child")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteExpiredEntryKeepsAReplacement(t *testing.T) {
|
||||
f, store := newTestFilerWithStubStore(t)
|
||||
ctx := context.Background()
|
||||
p := util.FullPath("/logs/current")
|
||||
|
||||
putSweepTestEntry(t, store, string(p), false, 120, time.Now())
|
||||
if err := f.deleteExpiredEntry(ctx, p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if store.getEntry(string(p)) == nil {
|
||||
t.Fatal("live replacement removed")
|
||||
}
|
||||
|
||||
putSweepTestEntry(t, store, string(p), false, 120, time.Now().Add(-time.Hour))
|
||||
lock := f.EntryLockTable.AcquireLock("test", p, util.ExclusiveLock)
|
||||
if err := f.deleteExpiredEntry(ctx, p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.EntryLockTable.ReleaseLock(p, lock)
|
||||
if store.getEntry(string(p)) == nil {
|
||||
t.Fatal("removed while a writer held the path")
|
||||
}
|
||||
|
||||
s3Expiring := &Entry{FullPath: p, Attr: Attr{Mode: 0o644, Crtime: time.Now().Add(-time.Hour), Mtime: time.Now(), TtlSec: 120},
|
||||
Extended: map[string][]byte{s3_constants.SeaweedFSExpiresS3: []byte("true")}}
|
||||
if err := store.InsertEntry(ctx, s3Expiring); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.deleteExpiredEntry(ctx, p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if store.getEntry(string(p)) == nil {
|
||||
t.Fatal("replacement under S3 expiration removed by its creation time")
|
||||
}
|
||||
|
||||
putSweepTestEntry(t, store, string(p), false, 120, time.Now().Add(-time.Hour))
|
||||
if err := f.deleteExpiredEntry(ctx, p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if store.getEntry(string(p)) != nil {
|
||||
t.Fatal("expired entry kept")
|
||||
}
|
||||
}
|
||||
|
||||
// Another filer on the same store does not take this filer's lock: a write it
|
||||
// makes between the re-read and the delete gets the delete's commit refused.
|
||||
func TestDeleteExpiredEntryKeepsAReplacementFromAnotherFiler(t *testing.T) {
|
||||
f, _ := newTestFilerWithStubStore(t)
|
||||
store := newVersionedStore()
|
||||
f.Store = NewFilerStoreWrapper(store)
|
||||
other := NewFilerStoreWrapper(store)
|
||||
ctx := context.Background()
|
||||
p := util.FullPath("/logs/current")
|
||||
old := time.Now().Add(-time.Hour)
|
||||
expired := &Entry{FullPath: p, Attr: Attr{Mode: 0o644, Crtime: old, Mtime: old, TtlSec: 120}}
|
||||
|
||||
if err := other.InsertEntry(ctx, expired); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ran := store.once(t, "r"+string(p), func() error {
|
||||
return other.InsertEntry(ctx, &Entry{FullPath: p, Attr: Attr{Mode: 0o644, Crtime: time.Now(), Mtime: time.Now(), TtlSec: 120}})
|
||||
})
|
||||
if err := f.deleteExpiredEntry(ctx, p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !*ran {
|
||||
t.Fatal("the expired entry was not re-read in a transaction")
|
||||
}
|
||||
if store.getEntry(string(p)) == nil {
|
||||
t.Fatal("replacement written through another filer removed")
|
||||
}
|
||||
|
||||
store.afterRead = nil
|
||||
if err := other.InsertEntry(ctx, expired); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.deleteExpiredEntry(ctx, p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if store.getEntry(string(p)) != nil {
|
||||
t.Fatal("expired entry kept")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSweepExpiredEntriesKeepsADirectoryAWriterHolds(t *testing.T) {
|
||||
f, store := newTestFilerWithStubStore(t)
|
||||
f.DirBucketsPath = "/buckets"
|
||||
f.FilerConf = NewFilerConf()
|
||||
if err := f.FilerConf.SetLocationConf(&filer_pb.FilerConf_PathConf{LocationPrefix: "/buckets/b/", Ttl: "2m"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := time.Now().Add(-time.Hour)
|
||||
for _, dir := range []string{"/buckets", "/buckets/b", "/buckets/b/day1"} {
|
||||
putSweepTestEntry(t, store, dir, true, 0, old)
|
||||
}
|
||||
putSweepTestEntry(t, store, "/buckets/b/day1/f", false, 120, old)
|
||||
|
||||
lock := f.EntryLockTable.AcquireLock("test", "/buckets/b/day1", util.ExclusiveLock)
|
||||
f.sweepExpiredEntries(context.Background(), "", make(map[string]time.Time), time.Now())
|
||||
f.EntryLockTable.ReleaseLock("/buckets/b/day1", lock)
|
||||
|
||||
if store.getEntry("/buckets/b/day1/f") != nil {
|
||||
t.Fatal("expired child not removed")
|
||||
}
|
||||
if store.getEntry("/buckets/b/day1") == nil {
|
||||
t.Fatal("directory removed while a writer held it")
|
||||
}
|
||||
}
|
||||
|
||||
// childArrivingStore creates child the first time dir is read after gone is
|
||||
// removed, as a writer would between an emptiness check and the delete.
|
||||
type childArrivingStore struct {
|
||||
*stubFilerStore
|
||||
dir, gone, child util.FullPath
|
||||
arrived bool
|
||||
}
|
||||
|
||||
func (s *childArrivingStore) FindEntry(ctx context.Context, p util.FullPath) (*Entry, error) {
|
||||
if p == s.dir && !s.arrived && s.getEntry(string(s.gone)) == nil {
|
||||
s.arrived = true
|
||||
now := time.Now()
|
||||
if err := s.InsertEntry(ctx, &Entry{FullPath: s.child, Attr: Attr{Mode: 0o644, Crtime: now, Mtime: now}}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return s.stubFilerStore.FindEntry(ctx, p)
|
||||
}
|
||||
|
||||
func TestSweepExpiredEntriesKeepsADirectoryAChildArrivesIn(t *testing.T) {
|
||||
f, stub := newTestFilerWithStubStore(t)
|
||||
store := &childArrivingStore{stubFilerStore: stub, dir: "/buckets/b/day1", gone: "/buckets/b/day1/f", child: "/buckets/b/day1/g"}
|
||||
f.Store = NewFilerStoreWrapper(store)
|
||||
f.DirBucketsPath = "/buckets"
|
||||
f.FilerConf = NewFilerConf()
|
||||
if err := f.FilerConf.SetLocationConf(&filer_pb.FilerConf_PathConf{LocationPrefix: "/buckets/b/", Ttl: "2m"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := time.Now().Add(-time.Hour)
|
||||
for _, dir := range []string{"/buckets", "/buckets/b", "/buckets/b/day1"} {
|
||||
putSweepTestEntry(t, stub, dir, true, 0, old)
|
||||
}
|
||||
putSweepTestEntry(t, stub, "/buckets/b/day1/f", false, 120, old)
|
||||
|
||||
f.sweepExpiredEntries(context.Background(), "", make(map[string]time.Time), time.Now())
|
||||
|
||||
if !store.arrived {
|
||||
t.Fatal("the directory was not read after its last child expired")
|
||||
}
|
||||
if stub.getEntry("/buckets/b/day1/f") != nil {
|
||||
t.Fatal("expired child not removed")
|
||||
}
|
||||
if stub.getEntry("/buckets/b/day1") == nil {
|
||||
t.Fatal("directory removed after a child was created in it")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user