mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
filer: send a rename whose source misses locally to the write owner
Exclusive and conditional creates are stored on the path's write owner. With separate filer stores, a rename sent to another filer before it has replayed the create found no source and failed, so a mount that flushed a deferred create and renamed the file right after got EIO. Such a rename now goes to the source's write owner, which moves the entry and whose events are relayed to the caller, unless the owner runs on this filer's store. A forwarded rename is applied only by the filer its own ring names as the owner. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
db924fcd38
commit
e2f4dd5a75
3 files changed
+139
No files matched your search
@@ -100,7 +100,22 @@ func (fs *FilerServer) StreamRenameEntry(req *filer_pb.StreamRenameEntryRequest,
|
||||
oldParent := util.FullPath(filepath.ToSlash(req.OldDirectory))
|
||||
newParent := util.FullPath(filepath.ToSlash(req.NewDirectory))
|
||||
|
||||
oldPath := oldParent.Child(req.OldName)
|
||||
if req.IsMoved {
|
||||
if err := fs.checkForwardedWrite(stream.Context(), entryRouteKey(oldPath)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := fs.filer.CanRename(stream.Context(), oldParent, newParent, req.OldName); err != nil {
|
||||
// Exclusive and conditional creates land on the write owner, so a
|
||||
// source missing here may only not have replicated yet: the owner
|
||||
// renames it. A scoped rename stays local, since the owner would skip
|
||||
// the scope's checks.
|
||||
if err == filer_pb.ErrNotFound && !req.IsMoved && !scopeKeepOnly(stream.Context()) && fs.ownerKeepsOwnStore(entryRouteKey(oldPath)) {
|
||||
if handled, forwardErr := fs.forwardStreamRenameEntry(req, stream, oldPath); handled {
|
||||
return forwardErr
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
package weed_server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
)
|
||||
|
||||
// A file created with IF_NOT_EXISTS lands on the write owner. A rename sent
|
||||
// to another filer right after must move it there, even though that filer's
|
||||
// own store has not seen the create yet.
|
||||
func TestStreamRenameEntryMissReachesOwnerStore(t *testing.T) {
|
||||
c := newTwoFilers(t)
|
||||
storeB := separateStores(c)
|
||||
sharedWriteRing(c)
|
||||
p := c.keyOwnedBy(t, c.addrB, c.addrB)
|
||||
dir, name := p.DirAndName()
|
||||
ctx := context.Background()
|
||||
|
||||
create := &filer_pb.CreateEntryRequest{Directory: dir, Entry: &filer_pb.Entry{
|
||||
Name: name, Attributes: &filer_pb.FuseAttributes{Mtime: 1700000000, FileMode: 0644},
|
||||
}, Condition: &filer_pb.WriteCondition{Clauses: []*filer_pb.WriteCondition_Clause{{Kind: filer_pb.WriteCondition_IF_NOT_EXISTS}}}}
|
||||
if resp, err := c.fsA.CreateEntry(ctx, create); err != nil || resp.GetError() != "" {
|
||||
t.Fatalf("create via A: resp=%v err=%v", resp, err)
|
||||
}
|
||||
if _, err := c.store.FindEntry(ctx, p); err != filer_pb.ErrNotFound {
|
||||
t.Fatalf("A's store must not have the entry yet: %v", err)
|
||||
}
|
||||
|
||||
stream := &renameTestStream{}
|
||||
err := c.fsA.StreamRenameEntry(&filer_pb.StreamRenameEntryRequest{
|
||||
OldDirectory: dir, OldName: name, NewDirectory: dir, NewName: name + ".renamed",
|
||||
}, stream)
|
||||
if err != nil {
|
||||
t.Fatalf("rename via A: %v", err)
|
||||
}
|
||||
if _, err := storeB.FindEntry(ctx, p); err != filer_pb.ErrNotFound {
|
||||
t.Fatalf("the owner kept the source after the rename: %v", err)
|
||||
}
|
||||
if _, err := storeB.FindEntry(ctx, p+".renamed"); err != nil {
|
||||
t.Fatalf("the owner has no renamed entry: %v", err)
|
||||
}
|
||||
if len(stream.sent) == 0 {
|
||||
t.Fatal("the owner's rename events were not relayed to the caller")
|
||||
}
|
||||
}
|
||||
|
||||
// A source missing on the owner too is still reported as not found.
|
||||
func TestStreamRenameEntryMissOnOwnerNotFound(t *testing.T) {
|
||||
c := newTwoFilers(t)
|
||||
separateStores(c)
|
||||
sharedWriteRing(c)
|
||||
p := c.keyOwnedBy(t, c.addrB, c.addrB)
|
||||
dir, name := p.DirAndName()
|
||||
|
||||
err := c.fsA.StreamRenameEntry(&filer_pb.StreamRenameEntryRequest{
|
||||
OldDirectory: dir, OldName: name, NewDirectory: dir, NewName: name + ".renamed",
|
||||
}, &renameTestStream{})
|
||||
if err == nil || !strings.Contains(err.Error(), filer_pb.ErrNotFound.Error()) {
|
||||
t.Fatalf("rename of an absent entry: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// While B's ring still gives the path to A, B refuses the rename A forwards
|
||||
// rather than answer for a store the creates may not have reached.
|
||||
func TestForwardedStreamRenameEntryRefusedByNonOwner(t *testing.T) {
|
||||
slack := notWriteOwnerRetrySlack
|
||||
notWriteOwnerRetrySlack = 200 * time.Millisecond
|
||||
t.Cleanup(func() { notWriteOwnerRetrySlack = slack })
|
||||
|
||||
c := newTwoFilers(t)
|
||||
storeB := separateStores(c)
|
||||
setRing(c.fsA, 0, []pb.ServerAddress{c.addrA, c.addrB})
|
||||
setRing(c.fsB, time.Minute, []pb.ServerAddress{c.addrA}, []pb.ServerAddress{c.addrA, c.addrB})
|
||||
p := c.keyOwnedBy(t, c.addrB, c.addrA)
|
||||
if err := storeB.InsertEntry(context.Background(), newFileEntry(string(p), 7)); err != nil {
|
||||
t.Fatalf("seed B: %v", err)
|
||||
}
|
||||
|
||||
dir, name := p.DirAndName()
|
||||
err := c.fsA.StreamRenameEntry(&filer_pb.StreamRenameEntryRequest{
|
||||
OldDirectory: dir, OldName: name, NewDirectory: dir, NewName: name + ".renamed",
|
||||
}, &renameTestStream{})
|
||||
if !isNotWriteOwner(err) {
|
||||
t.Fatalf("B applied or failed otherwise a rename its ring gives to A: %v", err)
|
||||
}
|
||||
if _, err := storeB.FindEntry(context.Background(), p); err != nil {
|
||||
t.Fatalf("a refused forward moved the entry: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package weed_server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -71,6 +72,35 @@ func (fs *FilerServer) forwardDeleteEntry(ctx context.Context, req *filer_pb.Del
|
||||
return ownerResp, handled, nil
|
||||
}
|
||||
|
||||
// forwardStreamRenameEntry sends the rename to its source's write owner and
|
||||
// relays the owner's responses. handled=false means this filer is the owner
|
||||
// and applies it.
|
||||
func (fs *FilerServer) forwardStreamRenameEntry(req *filer_pb.StreamRenameEntryRequest, stream filer_pb.SeaweedFiler_StreamRenameEntryServer, oldPath util.FullPath) (bool, error) {
|
||||
ctx := stream.Context()
|
||||
return fs.forwardToWriteOwner(ctx, entryRouteKey(oldPath), func(owner pb.ServerAddress) error {
|
||||
glog.V(2).InfofCtx(ctx, "StreamRenameEntry %s: forwarding to owner %s", oldPath, owner)
|
||||
req.IsMoved = true
|
||||
return pb.WithFilerClient(true, 0, owner, fs.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
||||
forwarded, err := client.StreamRenameEntry(withCallerAuthContext(ctx), req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for {
|
||||
resp, recvErr := forwarded.Recv()
|
||||
if recvErr == io.EOF {
|
||||
return nil
|
||||
}
|
||||
if recvErr != nil {
|
||||
return recvErr
|
||||
}
|
||||
if sendErr := stream.Send(resp); sendErr != nil {
|
||||
return sendErr
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// forwardToWriteOwner sends the request to key's write owner so a single filer's
|
||||
// per-path lock arbitrates every writer of that key. handled=false means this
|
||||
// filer is the owner and the caller should apply the request locally.
|
||||
|
||||
Reference in new issue
Block a user