Files
seaweedfs/weed/filer/dirusage/dirusage_test.go
T
8760e2778b worker: dir_usage takes off what a TTL expires (#13369)
* worker: dir_usage takes off what a TTL expires

A plain TTL removes an expired row without an event, so a record kept
counting it until its bucket was seeded again. An entry's attributes say
when listings start leaving it out (Entry.ExpiresAt, which listingExpired
now uses), so a record keeps what each directory has still to expire,
grouped by the step it expires in, and each run takes off what came off
by its position. A step is an hour, or for a TTL of 200 hours or more the
largest power of two hours up to 1% of it, so a record stays bounded.

An event counts an entry only if the record has not taken it off yet, so
an S3 expiry, which logs a delete, comes off once. Record version 2 seeds
the records made before again.

Part of #13257.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* dir_usage: decide a seeded directory's share after what expired

The seed took off what had expired by its position only after the final
share check, so a directory the walk kept could be dropped against a
total that still counted those entries. Take them off first, and hand
what a dropped directory has still to expire to the directory above.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* dir_usage: drop a seeded directory below its share with dropTracked

A directory below its share has none above its share below it, so
dropping it with what is below, its entries staying counted above, is
what untrack did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-12-03 23:40:58 -08:00

97 lines
3.2 KiB
Go

package dirusage
import (
"math"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
func TestRootsOfAPath(t *testing.T) {
for _, buckets := range []string{"/buckets", "/buckets/", ""} {
roots := NewRoots(buckets)
for path, want := range map[util.FullPath]util.FullPath{
"/buckets/b": "/buckets/b",
"/buckets/b/x/y": "/buckets/b",
"/home/u/f": "/home",
"/topics/.system": "/topics",
"/topics/.system/log/2026-12-03": "",
"/etc/seaweedfs/dir_usage/1/top/x": "",
"/buckets": "",
"/": "",
} {
root, ok := roots.RootOf(path)
if !ok {
root = ""
}
if root != want {
t.Errorf("buckets folder %q: %s counts toward %q, want %q", buckets, path, root, want)
}
}
}
nested := NewRoots("/tenant/buckets/")
if root, ok := nested.RootOf("/tenant/buckets/b/f"); !ok || root != "/tenant/buckets/b" {
t.Errorf("/tenant/buckets/b/f counts toward %q, %v", root, ok)
}
if _, ok := nested.RootOf("/tenant/other"); ok {
t.Errorf("what holds the buckets folder counts")
}
}
func TestExpiresByTheStepOfItsTTL(t *testing.T) {
expiresAt := time.Date(2026, 12, 4, 10, 20, 0, 0, time.UTC)
file := func(ttl time.Duration) *filer_pb.Entry {
return &filer_pb.Entry{Name: "f", Attributes: &filer_pb.FuseAttributes{
FileSize: 1,
Crtime: expiresAt.Add(-ttl).Unix(),
TtlSec: int32(ttl / time.Second),
}}
}
for _, c := range []struct{ ttl, step time.Duration }{
{30 * time.Minute, time.Hour},
{7 * 24 * time.Hour, time.Hour},
{200 * time.Hour, 2 * time.Hour},
{30 * 24 * time.Hour, 4 * time.Hour},
{365 * 24 * time.Hour, 64 * time.Hour},
} {
step := int64(c.step / time.Second)
want := (expiresAt.Unix() + step - 1) / step * step
if got := Expires(file(c.ttl)); got != want {
t.Errorf("TTL %v: comes off at %d, want %d, the end of its %v step", c.ttl, got, want, c.step)
}
}
if got := Expires(&filer_pb.Entry{Name: "f", Attributes: &filer_pb.FuseAttributes{TtlSec: 60}}); got != 3600 {
t.Errorf("an entry that expired long ago comes off at %d", got)
}
for _, entry := range []*filer_pb.Entry{nil, {Name: "f", Attributes: &filer_pb.FuseAttributes{FileSize: 1}},
{Name: "d", IsDirectory: true, Attributes: &filer_pb.FuseAttributes{TtlSec: 60}}} {
if got := Expires(entry); got != 0 {
t.Errorf("%v comes off at %d", entry, got)
}
}
}
func TestExpiryStepsAreFewAndShort(t *testing.T) {
hour := int64(time.Hour / time.Second)
for ttl := int64(1); ttl <= math.MaxInt32; ttl = ttl*9/8 + 1 {
step := expiryStep(ttl)
if step%hour != 0 || step/hour&(step/hour-1) != 0 {
t.Fatalf("TTL %ds: step %ds is not a power of two hours", ttl, step)
}
if step > max(hour, ttl/100) {
t.Errorf("TTL %ds: step %ds counts an expired entry for longer than an hour or 1%% of it", ttl, step)
}
if ttl/step >= 200 {
t.Errorf("TTL %ds: %d steps of %ds", ttl, ttl/step, step)
}
}
if !TakenOff(10, 10*int64(time.Second)) || TakenOff(10, 10*int64(time.Second)-1) || TakenOff(0, math.MaxInt64) {
t.Errorf("TakenOff")
}
if TakenOff(math.MaxInt64/2, math.MaxInt64) {
t.Errorf("a step beyond the last position is taken off")
}
}