mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-09-29 19:25:35 +00:00
* filer.remote.sync: stamp entries with IF_CHUNKS_EQUAL so a stale write-back cannot delete live chunks updateLocalEntry records the RemoteEntry stamp after an upload by writing the event's entry back with UpdateEntry. The filer deletes every stored chunk absent from an updated entry, so when the file was rewritten while its upload was in flight (or the event is a replay), the stale snapshot deletes the rewrite's chunks: the entry then points at the new fid with no needle behind it, and the rewrite's own upload fails and is skipped as superseded. The stamp write now carries WriteCondition IF_CHUNKS_EQUAL over the event's chunk fids, evaluated by the filer under the path lock. A refused stamp means the filer moved past this event; the superseding event follows in the log and stamps the current entry, so the refusal is logged and skipped like a superseded upload. Reproduction: weed server -filer plus a weed server -s3 remote, remote.mount, filer.remote.sync; hold the remote (docker pause) so one upload stays in flight, rewrite the file through the filer, unpause. Before: the entry's chunk is 404 on every volume server. After: the stale stamp is refused, the rewrite's chunk stays live and reads back after a vacuum. * filer.remote.sync: stamp entries with IF_ENTRY_EQUAL so stale inline content or metadata cannot be restored The IF_CHUNKS_EQUAL guard compared only the chunk fid multiset, so a rewrite that touched inline content or metadata alone still compared equal and the stale snapshot overwrote the live entry. The new clause compares the whole stored entry against the event's entry under the same path lock. * filer: route conditional UpdateEntry to the entry's owner filer Two filers locking the same path locally could still pass a stale condition on the non-owner while the owner's entry had moved on. When a condition or expected_extended precondition is set, forward the request to the entry's owner the same way conditional CreateEntry does, with is_moved bounding the hop. * filer: compare IF_ENTRY_EQUAL against the normalized expected entry FindEntry grows FileSize to the chunk extent, so a raw event entry with FileSize still zero failed the condition on an unchanged file and the stamp was skipped, letting a replay upload the object again. * filer.remote.sync: classify refused stamps by gRPC status only A FailedPrecondition substring in an unrelated error would have been swallowed as a skipped stamp; status.FromError already unwraps. * remote sync: keep the event entry intact for IF_ENTRY_EQUAL * filer: honor is_moved only from ring member connections is_moved is caller-controlled, so a request could set it to skip owner routing and run a conditional check under a non-owner's lock. Verify the marker against the peer's connection address and the lock ring members; an unverified marker is ignored and the request routes like a fresh one. * filer: refuse unverifiable is_moved at a non-owner, cache ring IPs Follow-up fixes from review on the is_moved provenance check: - checkMovedMarker replaces "ignore and re-forward" for markers that did not arrive on a ring member's connection. Re-forwarding a claimed hop could cycle while rings disagree; instead the request is refused with FailedPrecondition unless this filer is the key's owner, in which case applying locally is correct anyway. - ringMemberIPs caches resolved member addresses per ring membership so hostname-advertising deployments do not pay a DNS lookup per forwarded request; failed lookups are not cached so a DNS blip self-heals. - DistributedUnlock no longer dereferences the nil response of a failed next-hop RPC. * filer: refuse unverifiable is_moved with PermissionDenied, not FailedPrecondition A routing refusal is different in kind from a write-condition mismatch: remote sync treats FailedPrecondition as a stale stamp and skips it, so reusing that code let a routing failure pass as synced. Owner checks now also run before the peer-IP lookup so the common accept path does no DNS. * filer: expire resolved ring member IPs after 5 minutes A member's hostname can re-resolve to a new IP while its ring address stays unchanged; caching forever would reject its genuine forwards until a membership change or restart. * filer: deduplicate concurrent ring member DNS lookups At cache expiry, parallel forwarded requests would each resolve every member hostname serially; singleflight collapses them into one lookup per ring membership. * filer: detach the shared ring lookup from the caller's context The singleflight winner's ctx is cancelled when its request ends; the shared result would then be an incomplete member list and genuine forwards denied. The lookup now runs on a detached context with its own deadline so a canceled caller cannot poison it. * filer: resolve ring member hostnames in parallel The shared lookup gave every member one serial budget, so a few slow resolutions could leave later members out of the cached list and reject their genuine forwards. Each member now resolves concurrently under its own detached deadline. * filer: gather literal member IPs before spawning lookups A ring mixing IP literals and hostnames raced: the literal appends ran unlocked alongside the resolver goroutines' locked appends. Split into two passes so only hostname results share the mutex. --------- Co-authored-by: jsas <1351492+jsas@users.noreply.github.com>
235 lines
8.0 KiB
Go
235 lines
8.0 KiB
Go
package weed_server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"testing"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
"google.golang.org/grpc/peer"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/cluster/lock_manager"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
// createRouteServer builds a filer whose ring holds two unreachable peers, so a
|
|
// forwarded create fails at the dial and is distinguishable from a local apply.
|
|
func createRouteServer(t *testing.T) (*FilerServer, *renameTestStore) {
|
|
t.Helper()
|
|
const self = pb.ServerAddress("127.0.0.1:18888")
|
|
store := newRenameTestStore()
|
|
f := newRenameTestFiler(t, store)
|
|
f.DirBucketsPath = "/buckets"
|
|
f.Dlm = lock_manager.NewDistributedLockManager(self)
|
|
f.Dlm.LockRing.SetSnapshot([]pb.ServerAddress{self, "127.0.0.1:18889"}, 1)
|
|
return &FilerServer{
|
|
filer: f,
|
|
option: &FilerOption{Host: self},
|
|
grpcDialOption: grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
entryLockTable: util.NewLockTable[util.FullPath](),
|
|
}, store
|
|
}
|
|
|
|
// peerOwnedName returns a child name under /test whose entry the ring assigns to
|
|
// the peer, so a routed create must leave this filer.
|
|
func peerOwnedName(t *testing.T, fs *FilerServer) string {
|
|
t.Helper()
|
|
for i := 0; i < 4000; i++ {
|
|
name := fmt.Sprintf("obj-%d", i)
|
|
key := entryRouteKey(util.NewFullPath("/test", name))
|
|
if fs.filer.Dlm.LockRing.GetPrimary(key) != fs.option.Host {
|
|
return name
|
|
}
|
|
}
|
|
t.Skip("no name owned by the peer")
|
|
return ""
|
|
}
|
|
|
|
func createReq(name string, oExcl bool) *filer_pb.CreateEntryRequest {
|
|
return &filer_pb.CreateEntryRequest{
|
|
Directory: "/test",
|
|
OExcl: oExcl,
|
|
SkipCheckParentDirectory: true,
|
|
Entry: &filer_pb.Entry{
|
|
Name: name,
|
|
Attributes: &filer_pb.FuseAttributes{Mtime: 1700000000, FileMode: 0644, Inode: 1},
|
|
},
|
|
}
|
|
}
|
|
|
|
// An exclusive create for a peer-owned path must leave this filer rather than
|
|
// take the local lock, whose arbitration would not bind the other filers.
|
|
func TestCreateEntryExclusiveLeavesNonOwner(t *testing.T) {
|
|
fs, store := createRouteServer(t)
|
|
name := peerOwnedName(t, fs)
|
|
|
|
_, err := fs.CreateEntry(context.Background(), createReq(name, true))
|
|
if err == nil {
|
|
t.Fatal("exclusive create on a non-owner must not be applied locally")
|
|
}
|
|
if _, found := store.entries[string(util.NewFullPath("/test", name))]; found {
|
|
t.Fatal("forwarded create must not also write locally")
|
|
}
|
|
}
|
|
|
|
// Plain creates are upserts whichever filer applies them, so routing them would
|
|
// buy nothing and cost a hop.
|
|
func TestCreateEntryPlainStaysLocal(t *testing.T) {
|
|
fs, store := createRouteServer(t)
|
|
name := peerOwnedName(t, fs)
|
|
|
|
resp, err := fs.CreateEntry(context.Background(), createReq(name, false))
|
|
if err != nil || resp.Error != "" {
|
|
t.Fatalf("plain create must be applied locally, got err=%v resp=%v", err, resp.Error)
|
|
}
|
|
if _, found := store.entries[string(util.NewFullPath("/test", name))]; !found {
|
|
t.Fatal("plain create must reach the local store")
|
|
}
|
|
}
|
|
|
|
// ringPeerCtx simulates a request arriving over a connection from a ring
|
|
// member, the provenance an is_moved marker needs before it is trusted.
|
|
func ringPeerCtx() context.Context {
|
|
return peer.NewContext(context.Background(), &peer.Peer{
|
|
Addr: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 49999},
|
|
})
|
|
}
|
|
|
|
// is_moved bounds forwarding to one hop: the owner applies it even if its own
|
|
// ring view says someone else owns the key.
|
|
func TestCreateEntryMovedAppliesLocally(t *testing.T) {
|
|
fs, store := createRouteServer(t)
|
|
name := peerOwnedName(t, fs)
|
|
|
|
req := createReq(name, true)
|
|
req.IsMoved = true
|
|
resp, err := fs.CreateEntry(ringPeerCtx(), req)
|
|
if err != nil || resp.Error != "" {
|
|
t.Fatalf("forwarded create must be applied locally, got err=%v resp=%v", err, resp.Error)
|
|
}
|
|
if _, found := store.entries[string(util.NewFullPath("/test", name))]; !found {
|
|
t.Fatal("forwarded create must reach the local store")
|
|
}
|
|
}
|
|
|
|
// A caller can set is_moved, but without a ring member's connection it is not
|
|
// trusted: on a non-owner the request is refused rather than evaluated under
|
|
// the wrong lock or forwarded again, where it could cycle while rings disagree.
|
|
func TestCreateEntryMovedFromClientIsNotTrusted(t *testing.T) {
|
|
fs, store := createRouteServer(t)
|
|
name := peerOwnedName(t, fs)
|
|
|
|
req := createReq(name, true)
|
|
req.IsMoved = true
|
|
_, err := fs.CreateEntry(context.Background(), req)
|
|
if status.Code(err) != codes.PermissionDenied {
|
|
t.Fatalf("a forged is_moved must be refused, got err=%v", err)
|
|
}
|
|
if _, found := store.entries[string(util.NewFullPath("/test", name))]; found {
|
|
t.Fatal("a forged is_moved must not cause a local write")
|
|
}
|
|
}
|
|
|
|
// Forwarding an unlock to an unreachable owner must surface the RPC error, not
|
|
// panic dereferencing the nil response of the failed call.
|
|
func TestDistributedUnlockFailedForwardReturnsError(t *testing.T) {
|
|
fs, _ := createRouteServer(t)
|
|
|
|
var name string
|
|
for i := 0; i < 4000; i++ {
|
|
candidate := fmt.Sprintf("lock-%d", i)
|
|
if fs.filer.Dlm.LockRing.GetPrimary(candidate) != fs.option.Host {
|
|
name = candidate
|
|
break
|
|
}
|
|
}
|
|
if name == "" {
|
|
t.Skip("no lock owned by the peer")
|
|
}
|
|
|
|
resp, err := fs.DistributedUnlock(context.Background(), &filer_pb.UnlockRequest{
|
|
Name: name,
|
|
RenewToken: "tok",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("unlock forward should report via resp.Error, not err: %v", err)
|
|
}
|
|
if resp.Error == "" {
|
|
t.Fatal("a failed forward must surface an error")
|
|
}
|
|
}
|
|
|
|
// An exclusive create this filer owns is applied locally, under its per-path lock.
|
|
func TestCreateEntryExclusiveAppliedByOwner(t *testing.T) {
|
|
fs, store := createRouteServer(t)
|
|
var name string
|
|
for i := 0; i < 4000 && name == ""; i++ {
|
|
candidate := fmt.Sprintf("obj-%d", i)
|
|
key := entryRouteKey(util.NewFullPath("/test", candidate))
|
|
if fs.filer.Dlm.LockRing.GetPrimary(key) == fs.option.Host {
|
|
name = candidate
|
|
}
|
|
}
|
|
if name == "" {
|
|
t.Skip("no name owned by this filer")
|
|
}
|
|
|
|
resp, err := fs.CreateEntry(context.Background(), createReq(name, true))
|
|
if err != nil || resp.Error != "" {
|
|
t.Fatalf("owner must apply the create, got err=%v resp=%v", err, resp.Error)
|
|
}
|
|
if _, found := store.entries[string(util.NewFullPath("/test", name))]; !found {
|
|
t.Fatal("owner's create must reach the local store")
|
|
}
|
|
}
|
|
|
|
// A key still inside the cooling-off window belongs to its prior owner. If that
|
|
// filer cannot be reached the create fails: it may be partitioned rather than
|
|
// down, and applying here would be the second serialization point on the key.
|
|
func TestCreateEntryFailsWhenPriorOwnerIsUnreachable(t *testing.T) {
|
|
const self = pb.ServerAddress("127.0.0.1:28888")
|
|
const peer = pb.ServerAddress("127.0.0.1:28889")
|
|
store := newRenameTestStore()
|
|
f := newRenameTestFiler(t, store)
|
|
f.DirBucketsPath = "/buckets"
|
|
f.Dlm = lock_manager.NewDistributedLockManager(self)
|
|
// Dropping the peer moves every key it owned to this filer, with the peer as
|
|
// their prior owner — and nothing is listening on it.
|
|
f.Dlm.LockRing.SetSnapshot([]pb.ServerAddress{self, peer}, 1)
|
|
f.Dlm.LockRing.SetSnapshot([]pb.ServerAddress{self}, 2)
|
|
fs := &FilerServer{
|
|
filer: f,
|
|
option: &FilerOption{Host: self},
|
|
grpcDialOption: grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
entryLockTable: util.NewLockTable[util.FullPath](),
|
|
}
|
|
|
|
var name string
|
|
for i := 0; i < 4000 && name == ""; i++ {
|
|
candidate := fmt.Sprintf("obj-%d", i)
|
|
if fs.filer.Dlm.LockRing.PriorOwner(entryRouteKey(util.NewFullPath("/test", candidate))) == peer {
|
|
name = candidate
|
|
}
|
|
}
|
|
if name == "" {
|
|
t.Skip("no key whose prior owner is the departed peer")
|
|
}
|
|
|
|
resp, err := fs.CreateEntry(context.Background(), createReq(name, true))
|
|
if err == nil {
|
|
t.Fatalf("unreachable prior owner must fail the create, got resp=%v", resp)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("a failed create must still answer with a response, not nil")
|
|
}
|
|
if _, found := store.entries[string(util.NewFullPath("/test", name))]; found {
|
|
t.Fatal("a create routed away must not also write locally")
|
|
}
|
|
}
|