From 39e4b9934a730c90e1025a0670e33cf16fc6e2cc Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 16 Nov 2026 16:08:41 +0800 Subject: [PATCH] archive: an upload during a bucket pass runs another only when that pass didn't list it TestRequestArchiveKeptUnderItsSize went red on macOS CI at 50f7a8e01 (job 133136246258: "50 LISTs for 200 calls"). Any upload made while a prune pass ran set `again`, and the pass after it mostly listed a bucket the first had just listed, under the size: on one CPU 44 to 68 LISTs for 200 calls, where a LIST per tenth of the size is about 39. Now a call uploaded during a pass is held by name, and after the pass only the ones its listing didn't have are added to the count; another pass runs only when that brings it past the size. Run on mote at this tree: - go test ./internal/davsync -run TestRequestArchiveKeptUnderItsSize -count=10 -cpu 1,4: 20 of 20 ok (32-34 LISTs on one CPU, 39 on four). On the old code the same run was red 5 of 8 at -cpu 1 (44-52 LISTs). - go test ./internal/davsync -count=1: ok. - go test ./internal/gateway -run 'Archive|Bucket' -count=3: ok; -race over gateway and davsync's Bucket|Archive tests -count=3: ok. - go vet ./internal/gateway: ok. Not run: the full suite. --- internal/gateway/archive_bucket.go | 48 +++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/internal/gateway/archive_bucket.go b/internal/gateway/archive_bucket.go index 2965266de..ccc3a089d 100644 --- a/internal/gateway/archive_bucket.go +++ b/internal/gateway/archive_bucket.go @@ -52,7 +52,7 @@ var bucketKept struct { total int64 // its bytes, as last listed and uploaded since known bool pruning bool - again bool // another pass is wanted once this one ends + during map[string]int64 // calls uploaded while a pass runs, by name } // bucketCap is the size; a test's own when set. @@ -92,6 +92,13 @@ func bucketAdded(k archiveKeeper, kept string, n int64) { if bucketKept.where != k.Where() { bucketKept.where, bucketKept.known = k.Where(), false } + if bucketKept.pruning { + // the running pass counts it once it ends, unless its listing had + // it already + bucketKept.during[kept] = n + bucketKept.mu.Unlock() + return + } if bucketKept.known { bucketKept.total += n if bucketKept.total <= bucketCap() { @@ -111,18 +118,21 @@ func bucketAdded(k archiveKeeper, kept string, n int64) { // pruneBucketArchive lists the archive in k and removes its oldest calls // past the size, down to a tenth under it; never kept, the call just -// uploaded. One pass at a time: a pass asked for during one runs after it. +// uploaded. One pass at a time: a call uploaded during one is counted +// after it when its listing didn't have it, and another pass runs only +// when that brings the count past the size. Before, any upload during a +// pass ran another, which mostly listed a bucket the first had just +// listed: on one CPU a LIST per three calls, not per tenth of the size. func pruneBucketArchive(ctx context.Context, k archiveKeeper, kept string) { bucketKept.mu.Lock() if bucketKept.pruning { - bucketKept.again = true bucketKept.mu.Unlock() return } - bucketKept.pruning = true + bucketKept.pruning, bucketKept.during = true, map[string]int64{} bucketKept.mu.Unlock() for { - total, err := pruneBucketOnce(ctx, k, kept) + total, listed, err := pruneBucketOnce(ctx, k, kept) bucketKept.mu.Lock() if err != nil { log.Printf("request archive: keeping %s under %d MiB: %v", k.Where(), bucketCap()>>20, err) @@ -130,13 +140,20 @@ func pruneBucketArchive(ctx context.Context, k archiveKeeper, kept string) { // listed is tried again only a tenth of the size later, not // on every upload } + unlisted := false + for name, n := range bucketKept.during { + if !listed[name] { + total += n + unlisted, kept = true, name + } + } + bucketKept.during = map[string]int64{} bucketKept.where, bucketKept.total, bucketKept.known = k.Where(), total, true - if !bucketKept.again || ctx.Err() != nil { - bucketKept.pruning, bucketKept.again = false, false + if !unlisted || total <= bucketCap() || err != nil || ctx.Err() != nil { + bucketKept.pruning, bucketKept.during = false, nil bucketKept.mu.Unlock() return } - bucketKept.again = false bucketKept.mu.Unlock() } } @@ -146,23 +163,26 @@ type bucketCall struct { size int64 } -// pruneBucketOnce is one pass: the archive's bytes after it. -func pruneBucketOnce(ctx context.Context, k archiveKeeper, kept string) (int64, error) { +// pruneBucketOnce is one pass: the archive's bytes after it, and the +// calls its listing had. +func pruneBucketOnce(ctx context.Context, k archiveKeeper, kept string) (int64, map[string]bool, error) { var calls []bucketCall var total int64 + listed := map[string]bool{} err := k.Each(ctx, "archive/", func(name string, size int64) { if !archiveObject(name) { return // not one magpie made: never counted, never removed } calls = append(calls, bucketCall{name, size}) + listed[name] = true total += size }) if err != nil { - return total, err + return total, listed, err } most := bucketCap() if total <= most { - return total, nil + return total, listed, nil } // "archive//-…" sorts by when the call was made sort.Slice(calls, func(i, j int) bool { return calls[i].name < calls[j].name }) @@ -217,12 +237,12 @@ func pruneBucketOnce(ctx context.Context, k archiveKeeper, kept string) (int64, wg.Wait() back += unsent if ctx.Err() != nil { - return total + back, ctx.Err() + return total + back, listed, ctx.Err() } if len(gone) > 0 && back == 0 { log.Printf("request archive: removed the %d oldest calls in %s, kept under %d MiB", len(gone), k.Where(), most>>20) } - return total + back, lastErr + return total + back, listed, lastErr } // archiveObject says whether name, as the bucket lists it, is a call