mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
master: validate a move target's endpoints; log a refusal outside the lock
Fold of the #260 review: - the target endpoints get the binding endpoint rules before admission (host:port, numeric port 1..65535, data and ctrl distinct), without registering anything: a host match alone admitted ':0', ':65536', a non-numeric or empty port, or identical data and ctrl, and the move would have issued and cleared the source lease; - host parts compare in canonical form (equivalent IP spellings); - the refusal log runs after workspaceMu is released; - the comment no longer calls the host match a proof; the invariants ledger gets the move-admission row. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d8c42d0928
commit
ec40814443
5 files changed
+131
-14
No files matched your search
@@ -127,3 +127,76 @@ func TestWorkspaceMoveRefusalNamesTheMissingPrecondition(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// blockingWriter blocks the first write that contains marker until
|
||||
// released; other writes pass.
|
||||
type blockingWriter struct {
|
||||
marker string
|
||||
entered, release chan struct{}
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func (b *blockingWriter) Write(p []byte) (int, error) {
|
||||
if !strings.Contains(string(p), b.marker) {
|
||||
return len(p), nil
|
||||
}
|
||||
first := false
|
||||
b.once.Do(func() { first = true })
|
||||
if first {
|
||||
close(b.entered)
|
||||
<-b.release
|
||||
}
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// A refused move is logged after workspaceMu is released: while the log
|
||||
// writer is blocked, another workspace RPC still gets the lock.
|
||||
func TestWorkspaceMoveRefusalLogsOutsideTheWorkspaceLock(t *testing.T) {
|
||||
w := &blockingWriter{marker: "MoveWorkspace refused", entered: make(chan struct{}), release: make(chan struct{})}
|
||||
m, err := New(Config{Listen: "127.0.0.1:0", AuthorityStoreDir: t.TempDir(), LifecycleStoreDir: t.TempDir(), WorkspaceRPC: true, Logger: log.New(w, "", 0),
|
||||
WorkspaceHeadReader: func(context.Context, string) (authority.WorkspaceHead, error) { return authority.WorkspaceHead{}, nil }})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.Start()
|
||||
defer closeTestMaster(t, m)
|
||||
var releaseOnce sync.Once
|
||||
release := func() { releaseOnce.Do(func() { close(w.release) }) }
|
||||
defer release()
|
||||
conn, err := grpc.NewClient(m.Addr(), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
ws := control.NewWorkspaceServiceClient(conn)
|
||||
if _, err := control.NewLifecycleServiceClient(conn).CreateVolume(ctx, &control.CreateVolumeRequest{VolumeId: "v", SizeBytes: 1 << 20, ReplicationFactor: 1}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := ws.MoveWorkspace(ctx, &control.WorkspaceMove{RequestId: "r", VolumeId: "v", ExpectedReplicaId: "r1", ExpectedEpoch: 1, TargetReplicaId: "r1",
|
||||
TargetServerId: "node-b", TargetDataAddr: "127.0.0.1:19301", TargetCtrlAddr: "127.0.0.1:19302", Kind: "clean"})
|
||||
done <- err
|
||||
}()
|
||||
select {
|
||||
case <-w.entered:
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatal("the refusal was never logged")
|
||||
}
|
||||
qctx, qcancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer qcancel()
|
||||
if _, err := ws.QueryWorkspace(qctx, &control.WorkspaceVolume{VolumeId: "v"}); err != nil {
|
||||
t.Fatalf("workspace RPC while the refusal log is blocked: %v; want workspaceMu free", err)
|
||||
}
|
||||
release()
|
||||
select {
|
||||
case err := <-done:
|
||||
if status.Code(err) != codes.FailedPrecondition {
|
||||
t.Fatalf("move: %v", err)
|
||||
}
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatal("the move did not return after the log was released")
|
||||
}
|
||||
}
|
||||
@@ -100,6 +100,25 @@ func TestWorkspaceMoveToAnotherSlotOnTheTargetHost(t *testing.T) {
|
||||
if _, err := ws.MoveWorkspace(ctx, move("ctrl-other-host", d1, "10.9.9.9:19112")); err == nil || !strings.Contains(err.Error(), `on "node-a" are not registered`) {
|
||||
t.Fatalf("move with its ctrl endpoint on another host: %v; want the ownership refusal", err)
|
||||
}
|
||||
// Invalid endpoints on the right host are refused before anything is
|
||||
// written: the epoch, the journal, the lease and the ACK stay as they were.
|
||||
before := m.Publisher().WorkspaceState("a2")
|
||||
for _, bad := range []struct{ name, data, ctrl string }{
|
||||
{"non-numeric port", "127.0.0.1:bad", c1},
|
||||
{"port 0", "127.0.0.1:0", c1},
|
||||
{"port 65536", d1, "127.0.0.1:65536"},
|
||||
{"empty port", "127.0.0.1:", c1},
|
||||
{"data equals ctrl", d1, d1},
|
||||
} {
|
||||
if _, err := ws.MoveWorkspace(ctx, move("bad-"+bad.name, bad.data, bad.ctrl)); err == nil || !strings.Contains(err.Error(), "move target endpoints invalid") {
|
||||
t.Fatalf("%s: %v; want the invalid-endpoint refusal", bad.name, err)
|
||||
}
|
||||
after := m.Publisher().WorkspaceState("a2")
|
||||
line, _ := m.Publisher().VolumeAuthorityLine("a2")
|
||||
if line.Epoch != a2.Epoch || after == nil || len(after.Moves) != 0 || after.Ack == nil || before.Ack == nil || *after.Ack != *before.Ack || after.Lease == nil || !after.Lease.Revoking {
|
||||
t.Fatalf("%s: state changed by a refused move: line %+v state %+v", bad.name, line, after)
|
||||
}
|
||||
}
|
||||
g, err := ws.MoveWorkspace(ctx, move("slot-1", d1, c1))
|
||||
if err != nil {
|
||||
t.Fatalf("move to node-a slot 1 while slot 0 is held: %v", err)
|
||||
|
||||
@@ -3,7 +3,6 @@ package master
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -146,6 +145,14 @@ func (s *services) MoveWorkspace(ctx context.Context, q *control.WorkspaceMove)
|
||||
}
|
||||
// Remote reads never hold the workspace mutation lock. Re-read every
|
||||
// local precondition below; Publisher checks the expected epoch atomically.
|
||||
// A refusal is logged after the lock is released (deferred first, so it
|
||||
// runs last): a slow log writer never holds workspaceMu.
|
||||
var refusal string
|
||||
defer func() {
|
||||
if refusal != "" {
|
||||
s.host.log.Printf("blockmaster: MoveWorkspace refused volume=%q request=%q: %s", q.GetVolumeId(), q.GetRequestId(), refusal)
|
||||
}
|
||||
}()
|
||||
s.host.workspaceMu.Lock()
|
||||
defer s.host.workspaceMu.Unlock()
|
||||
if err := ctx.Err(); err != nil {
|
||||
@@ -161,17 +168,20 @@ func (s *services) MoveWorkspace(ctx context.Context, q *control.WorkspaceMove)
|
||||
return nil, workspaceError(err)
|
||||
}
|
||||
// A volume without per-volume bindings may move to any port on the
|
||||
// target node's registered hosts (a host runs several slots); the
|
||||
// collision check below still refuses endpoints another assignment or
|
||||
// binding owns. The host match is the proof the endpoints are that node's.
|
||||
// target node's registered hosts (a host runs several slots): the host
|
||||
// match ties the endpoints to that node's registration (it trusts the
|
||||
// registration; it is not an identity proof). The collision check below
|
||||
// still refuses endpoints another assignment or binding owns.
|
||||
matched := !explicit && sameHost(q.GetTargetDataAddr(), target.DataAddr) && sameHost(q.GetTargetCtrlAddr(), target.CtrlAddr)
|
||||
endpointErr := lifecycle.ValidateWorkspaceEndpoints(lifecycle.WorkspaceBinding{VolumeID: q.GetVolumeId(), ServerID: q.GetTargetServerId(), DataAddr: q.GetTargetDataAddr(), CtrlAddr: q.GetTargetCtrlAddr()})
|
||||
for _, b := range bindings {
|
||||
if !b.Retired && b.ServerID == q.GetTargetServerId() && b.DataAddr == q.GetTargetDataAddr() && b.CtrlAddr == q.GetTargetCtrlAddr() {
|
||||
matched = true
|
||||
}
|
||||
}
|
||||
var refusal string
|
||||
switch {
|
||||
case endpointErr != nil:
|
||||
refusal = fmt.Sprintf("move target endpoints invalid: %v", endpointErr)
|
||||
case oldHost == "":
|
||||
refusal = fmt.Sprintf("move source unknown: no issued move or placement names replica %q at epoch %d", q.GetExpectedReplicaId(), q.GetExpectedEpoch())
|
||||
case !ok:
|
||||
@@ -182,7 +192,6 @@ func (s *services) MoveWorkspace(ctx context.Context, q *control.WorkspaceMove)
|
||||
refusal = fmt.Sprintf("move target endpoints %s/%s on %q are not registered (%s)", q.GetTargetDataAddr(), q.GetTargetCtrlAddr(), target.ServerID, registeredTargetEndpoints(target, bindings, explicit))
|
||||
}
|
||||
if refusal != "" {
|
||||
s.host.log.Printf("blockmaster: MoveWorkspace refused volume=%q request=%q: %s", q.GetVolumeId(), q.GetRequestId(), refusal)
|
||||
return nil, status.Error(codes.FailedPrecondition, refusal)
|
||||
}
|
||||
if err = s.host.workspaceEndpointsAvailable(lifecycle.WorkspaceBinding{VolumeID: q.GetVolumeId(), ServerID: target.ServerID, DataAddr: q.GetTargetDataAddr(), CtrlAddr: q.GetTargetCtrlAddr()}); err != nil {
|
||||
@@ -199,15 +208,11 @@ func (s *services) MoveWorkspace(ctx context.Context, q *control.WorkspaceMove)
|
||||
return grantFact(r), nil
|
||||
}
|
||||
|
||||
// sameHost reports whether two host:port addresses name the same host: the
|
||||
// literal host parts after SplitHostPort, no name resolution.
|
||||
// sameHost reports whether two host:port addresses name the same host:
|
||||
// canonical host parts (equivalent IP spellings equal), no name resolution.
|
||||
func sameHost(a, b string) bool {
|
||||
ha, _, err := net.SplitHostPort(a)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
hb, _, err := net.SplitHostPort(b)
|
||||
return err == nil && ha != "" && ha == hb
|
||||
ha := lifecycle.WorkspaceEndpointHost(a)
|
||||
return ha != "" && ha == lifecycle.WorkspaceEndpointHost(b)
|
||||
}
|
||||
|
||||
// registeredTargetEndpoints describes what a move to target may name: the
|
||||
|
||||
@@ -119,6 +119,25 @@ func validateWorkspaceBinding(b WorkspaceBinding) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateWorkspaceEndpoints applies a binding's endpoint rules (host:port
|
||||
// with a numeric port 1..65535, data and ctrl distinct) without registering
|
||||
// anything.
|
||||
func ValidateWorkspaceEndpoints(b WorkspaceBinding) error { return validateWorkspaceBinding(b) }
|
||||
|
||||
// WorkspaceEndpointHost returns addr's host in canonical form (equivalent IP
|
||||
// spellings compare equal; names lower-cased; no DNS), or "" if addr is not
|
||||
// host:port.
|
||||
func WorkspaceEndpointHost(addr string) string {
|
||||
h, _, err := net.SplitHostPort(addr)
|
||||
if err != nil || h == "" {
|
||||
return ""
|
||||
}
|
||||
if ip := net.ParseIP(h); ip != nil {
|
||||
return ip.String()
|
||||
}
|
||||
return strings.ToLower(h)
|
||||
}
|
||||
|
||||
// Compare numeric ports and equivalent IP spellings without DNS/network I/O.
|
||||
func workspaceEndpointKey(addr string) string {
|
||||
h, p, err := net.SplitHostPort(addr)
|
||||
|
||||
@@ -93,6 +93,7 @@ blockmaster's publisher is the only component that mints epochs. Diagrams: the a
|
||||
| Granted | Revoking | `BeginWorkspaceRevocation` for the matching lease; a renew now returns the revoking lease with the granted expiry, min(horizon, deadline), without extending it | `core/authority/workspace.go` `BeginWorkspaceRevocation`; `core/authority/workspace_lease_view.go` `RenewWorkspaceLease`, `WorkspaceLeaseReply` | `INV-WS-CLEAN-MOVE-001` |
|
||||
| Revoking | Acked | `AckWorkspaceRevocation` from the holder at the current epoch, a nonzero HEAD generation, the lease still valid; a different second acknowledgement is refused | `core/authority/workspace.go` `AckWorkspaceRevocation` | `INV-WS-CLEAN-MOVE-001`, `INV-WS-REVOKE-ACK-001` |
|
||||
| Acked | Moved (clean) | HEAD detached by the expected holder at the expected epoch, and the acknowledgement names that HEAD generation | `core/authority/workspace.go` `prepareWorkspaceMoveLocked` | `INV-WS-CLEAN-MOVE-001` |
|
||||
| Acked or Expired | (move refused, nothing written) | blockmaster admits a move only with valid target endpoints (host:port, port 1..65535, data and ctrl distinct), a known source host, a registered target node other than the source, and target endpoints that either match a live per-volume binding or, for a volume without bindings, lie on the target node's registered hosts (any port) and are not owned by another assignment or binding; each refusal names its condition and is logged | `core/host/master/workspace_service.go` `MoveWorkspace` | `INV-WS-CLEAN-MOVE-001`, `INV-WS-FORCED-MOVE-001` |
|
||||
| Granted or Revoking | Expired | the durable horizon or the granted deadline passes | `core/authority/workspace_lease_view.go` `RenewWorkspaceLease`; `core/authority/workspace.go` `AckWorkspaceRevocation` | `INV-WS-FORCED-MOVE-001` |
|
||||
| Expired | Moved (forced) | explicit force; the persisted lease horizon (at least every grant) plus the clock-skew bound and the granted or restart deadline plus the bound have both passed | `core/authority/workspace.go` `prepareWorkspaceMoveLocked` | `INV-WS-FORCED-MOVE-001`, `INV-WS-LEASE-HORIZON-001` |
|
||||
| Moved | epoch issued | the move journal record is durable first; a retried request id gets the same epoch | `core/authority/workspace.go` `prepareWorkspaceMoveLocked`, `MoveWorkspace` | `INV-AUTH-MOVE-JOURNAL-001` |
|
||||
|
||||
Reference in new issue
Block a user