Files
seaweedfs/weed/s3api/s3api_object_put_fallback_condition_test.go
Chris LuandClaude Opus 5.5 137e4aeede s3api: keep the write condition on the lock fallback create (#12846)
* s3api: keep the write condition on the lock fallback create

Without a ring the PUT checks its conditional headers at the gateway and
then sends the create unconditionally under the object write lock. That
lock is a short lease, so a paused or slow holder lets a second
conditional writer in, and both writes succeed. Send the reduced
condition with the create so the filer re-checks it under its path lock.
Suspended-versioning writes keep the gateway check, since their condition
names the latest version rather than the create's path.

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

* s3api: test two If-None-Match PUTs racing on the lock fallback

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

* s3api: keep the chunks of a fallback create whose outcome is unknown

A conditional create is forwarded by the filer to its ring owner, which
a gateway without a ring may not know, so its landed-check can miss a
commit the owner made before the response was lost. Reclaim the chunks
of such a create only when the filer refused it.

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

* s3api: test exact fallback race results and a lost conditional create

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

* s3api: reclaim the chunks of a fallback create the filer refused

A precondition refusal proves the create never committed, but the
landed-check finds the winning writer's entry and reports the key as
present, so the refused writer's chunks were left orphaned. The
ambiguous-route recovery keeps its own rules.

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

* s3api: test that the refused fallback writer's chunk is reclaimed

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

* s3api: keep the chunks of a recovery create whose outcome is unknown

The owner can forward the conditional recovery create of an ambiguous
routed PUT to a filer the gateway cannot look up, so the cleanup gate
now follows the condition each create actually sent.

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

* s3api: test that the fallback race deletes only the refused writer's chunk

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 13:06:24 -08:00

175 lines
5.6 KiB
Go

package s3api
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// fallbackRaceFiler holds the first create of key until the test releases it.
type fallbackRaceFiler struct {
*ambiguousPutFiler
key string
mu sync.Mutex
creates int
firstAtCreate chan struct{}
releaseFirst chan struct{}
}
func (f *fallbackRaceFiler) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (*filer_pb.CreateEntryResponse, error) {
if req.Directory+"/"+req.Entry.GetName() == f.key {
f.mu.Lock()
f.creates++
first := f.creates == 1
f.mu.Unlock()
if first {
close(f.firstAtCreate)
select {
case <-f.releaseFirst:
case <-time.After(10 * time.Second):
}
}
}
return f.ambiguousPutFiler.CreateEntry(ctx, req)
}
func (f *fallbackRaceFiler) createCount() int {
f.mu.Lock()
defer f.mu.Unlock()
return f.creates
}
// expiredLeaseLock grants every request, as the object write lock does once the
// previous holder's lease ran out.
type expiredLeaseLock struct{}
func (expiredLeaseLock) StopShortLivedLock() error { return nil }
// Two "If-None-Match: *" PUTs of one key on the lock fallback path (no ring):
// writer 1 passes its gateway check and is held before its create while
// writer 2 runs to completion under a lock whose lease has run out. Only the
// write condition on the create lets the filer refuse the second create.
func TestPutFallbackKeepsWriteCondition(t *testing.T) {
const objectPath = "/buckets/b/k"
volume := startFakeVolumeServer(t)
inner := &ambiguousPutFiler{
volume: volume,
entries: map[string]*filer_pb.Entry{
"/buckets/b": {Name: "b", IsDirectory: true, Attributes: &filer_pb.FuseAttributes{}},
},
apply: true,
}
filerImpl := &fallbackRaceFiler{
ambiguousPutFiler: inner,
key: objectPath,
firstAtCreate: make(chan struct{}),
releaseFirst: make(chan struct{}),
}
s3a := newPutTestServer(t, startFakeFiler(t, filerImpl))
s3a.bucketConfigCache = NewBucketConfigCache(time.Minute)
var lockMu sync.Mutex
lockGrants := 0
s3a.newObjectWriteLock = func(bucket, object string) (objectWriteLock, error) {
lockMu.Lock()
lockGrants++
lockMu.Unlock()
return expiredLeaseLock{}, nil
}
if s3a.routableWriteOwner("b", "k") != "" {
t.Fatal("the gateway has a ring: the PUTs would be routed, not take the fallback path")
}
put := func(body string) s3err.ErrorCode {
r := httptest.NewRequest(http.MethodPut, "/b/k", nil)
r.Header.Set(s3_constants.IfNoneMatch, "*")
_, code, _ := s3a.putToFiler(r, objectPath, strings.NewReader(body), "b", "k", 1, 0, nil, false, "")
return code
}
var code1 s3err.ErrorCode
done1 := make(chan struct{})
go func() {
defer close(done1)
code1 = put("writer one")
}()
select {
case <-filerImpl.firstAtCreate:
case <-done1:
t.Fatalf("writer 1 finished (%v) without its create reaching the filer: the race was not exercised", code1)
case <-time.After(10 * time.Second):
close(filerImpl.releaseFirst)
t.Fatal("writer 1 never reached its create: the race was not exercised")
}
code2 := put("writer two")
if n := filerImpl.createCount(); n != 2 {
close(filerImpl.releaseFirst)
<-done1
t.Fatalf("writer 2 made %d creates of the key while writer 1 was paused, want its own create (total 2): the race was not exercised", n)
}
close(filerImpl.releaseFirst)
<-done1
lockMu.Lock()
grants := lockGrants
lockMu.Unlock()
if grants < 2 {
t.Fatalf("the object write lock was taken %d times, want one per PUT at least: the PUTs did not run the lock fallback", grants)
}
inner.mu.Lock()
conds := append([]*filer_pb.WriteCondition(nil), inner.conds...)
inner.mu.Unlock()
t.Logf("writer 1: %v, writer 2: %v; conditions on the creates the filer received: %v", code1, code2, conds)
if code1 != s3err.ErrPreconditionFailed || code2 != s3err.ErrNone {
t.Fatalf("writer 1 got %v and writer 2 got %v, want ErrPreconditionFailed and ErrNone", code1, code2)
}
inner.mu.Lock()
winner := inner.entries[objectPath]
inner.mu.Unlock()
if len(winner.GetChunks()) != 1 {
t.Fatalf("stored entry %+v, want writer 2's one chunk", winner)
}
deleted := volume.deleted()
if len(deleted) != 1 || sameFileChunks([]*filer_pb.FileChunk{{FileId: deleted[0]}}, winner.GetChunks()) {
t.Fatalf("deleted chunks %v, want only the refused writer's one chunk, not the stored %s", deleted, winner.GetChunks()[0].GetFileIdString())
}
}
// Without a ring the filer forwards a conditional create to its owner, so a
// lost response can hide a commit the gateway's filers do not hold yet.
func TestPutFallbackLostConditionalCreateKeepsChunks(t *testing.T) {
volume := startFakeVolumeServer(t)
filerImpl := &ambiguousPutFiler{
volume: volume,
entries: map[string]*filer_pb.Entry{
"/buckets/b": {Name: "b", IsDirectory: true, Attributes: &filer_pb.FuseAttributes{}},
},
createErr: status.Error(codes.Unavailable, "forward to owner: connection reset"),
}
s3a := newPutTestServer(t, startFakeFiler(t, filerImpl))
s3a.bucketConfigCache = NewBucketConfigCache(time.Minute)
r := httptest.NewRequest(http.MethodPut, "/b/k", nil)
r.Header.Set(s3_constants.IfNoneMatch, "*")
if _, code, _ := s3a.putToFiler(r, "/buckets/b/k", strings.NewReader("hello"), "b", "k", 1, 0, nil, false, ""); code == s3err.ErrNone {
t.Fatal("putToFiler succeeded although the create's outcome is unknown")
}
if deleted := volume.deleted(); len(deleted) != 0 {
t.Fatalf("chunks of a create that may have committed were deleted: %v", deleted)
}
}