mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
sw-block, master: a failed --json answer fails the command; a retire's storage failure is Internal
Review fixes. When the --json answer cannot be written, retire-host and finalize-format exit 1 instead of 0. A retire whose record removal failed answers Internal (the CLI's failed, exit 1), not FailedPrecondition (refused, exit 2); the node store's test hook can fail a removal for the control. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
59a0ebca91
commit
a13f2d6fdf
5 files changed
+70
-4
No files matched your search
@@ -113,7 +113,7 @@ type finalizeHost struct {
|
||||
// back to v1.0.x works only before it: afterwards a v1.0.x master or holder
|
||||
// refuses the store (an expected refusal, not damage). The master refuses
|
||||
// it while any registered host does not read level 1, naming each one.
|
||||
func runClusterFinalizeFormat(args []string, stdout, stderr io.Writer) int {
|
||||
func runClusterFinalizeFormat(args []string, stdout, stderr io.Writer) (code int) {
|
||||
fs := flag.NewFlagSet("sw-block cluster finalize-format", flag.ContinueOnError)
|
||||
fs.SetOutput(stderr)
|
||||
master := fs.String("master", "", "blockmaster gRPC address (required)")
|
||||
@@ -129,6 +129,9 @@ func runClusterFinalizeFormat(args []string, stdout, stderr io.Writer) int {
|
||||
if *jsonOut {
|
||||
if err := json.NewEncoder(stdout).Encode(answer); err != nil {
|
||||
fmt.Fprintln(stderr, "sw-block: cluster finalize-format: JSON output:", err)
|
||||
if code == finalizeExitOK {
|
||||
code = finalizeExitFailed // the answer a script reads was not written
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
@@ -239,7 +242,7 @@ type retireAnswer struct {
|
||||
// The master refuses it while the host holds a lease or an assignment. Exit
|
||||
// codes as finalize-format's: 0 retired, 2 refused, 75 the master could not
|
||||
// be reached (EX_TEMPFAIL), 1 any other failure, a usage error included.
|
||||
func runClusterRetireHost(args []string, stdout, stderr io.Writer) int {
|
||||
func runClusterRetireHost(args []string, stdout, stderr io.Writer) (code int) {
|
||||
fs := flag.NewFlagSet("sw-block cluster retire-host", flag.ContinueOnError)
|
||||
fs.SetOutput(stderr)
|
||||
master := fs.String("master", "", "blockmaster gRPC address (required)")
|
||||
@@ -255,6 +258,9 @@ func runClusterRetireHost(args []string, stdout, stderr io.Writer) int {
|
||||
if jsonMode {
|
||||
if err := json.NewEncoder(stdout).Encode(answer); err != nil {
|
||||
fmt.Fprintln(stderr, "sw-block: cluster retire-host: JSON output:", err)
|
||||
if code == finalizeExitOK {
|
||||
code = finalizeExitFailed // the answer a script reads was not written
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -222,3 +222,31 @@ func TestClusterRetireHostTypedAnswer(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// failingWriter refuses every write: a stdout the JSON answer cannot reach.
|
||||
type failingWriter struct{}
|
||||
|
||||
func (failingWriter) Write([]byte) (int, error) { return 0, errors.New("stdout closed") }
|
||||
|
||||
// A --json answer that cannot be written fails the command: a script that
|
||||
// reads it must not see a successful exit without an answer.
|
||||
func TestClusterJSONOutputFailureFailsTheCommand(t *testing.T) {
|
||||
f := &fakeClusterMaster{
|
||||
finalize: func(*control.FinalizeFormatRequest) (*control.FinalizeFormatResponse, error) {
|
||||
return &control.FinalizeFormatResponse{FormatLevel: 1}, nil
|
||||
},
|
||||
retire: func(r *control.RetireHostRequest) (*control.RetireHostResponse, error) {
|
||||
return &control.RetireHostResponse{ServerId: r.GetServerId()}, nil
|
||||
},
|
||||
}
|
||||
withClusterMaster(t, f)
|
||||
for _, args := range [][]string{
|
||||
{"cluster", "finalize-format", "--master", "m:1", "--json"},
|
||||
{"cluster", "retire-host", "--master", "m:1", "--server", "node-c", "--confirm", "node-c", "--json"},
|
||||
} {
|
||||
var errOut bytes.Buffer
|
||||
if code := run(args, failingWriter{}, &errOut); code != 1 {
|
||||
t.Fatalf("%v with an unwritable stdout: exit %d, want 1 (stderr %q)", args, code, errOut.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -377,6 +377,13 @@ func (e errHostHoldsWork) Error() string {
|
||||
return fmt.Sprintf("retire refused: host %q holds a lease or an assignment for volumes %s; move them first", e.server, strings.Join(e.volumes, ", "))
|
||||
}
|
||||
|
||||
// errRetireStorage is a retire that failed to remove the host's record: a
|
||||
// storage failure, not a refusal (the host may still be registered).
|
||||
type errRetireStorage struct{ err error }
|
||||
|
||||
func (e errRetireStorage) Error() string { return "retire: " + e.err.Error() }
|
||||
func (e errRetireStorage) Unwrap() error { return e.err }
|
||||
|
||||
// metadata keys each held volume for the refusal's typed detail.
|
||||
func (e errHostHoldsWork) metadata() map[string]string {
|
||||
md := make(map[string]string, len(e.volumes))
|
||||
@@ -441,7 +448,7 @@ func (h *Host) RetireHost(server, confirm string) error {
|
||||
return errHostHoldsWork{server: server, volumes: vols}
|
||||
}
|
||||
if err := h.lifecycle.Nodes.RemoveNode(server); err != nil {
|
||||
return fmt.Errorf("retire: %w", err)
|
||||
return errRetireStorage{err}
|
||||
}
|
||||
h.reads.forget(server)
|
||||
h.log.Printf("blockmaster: host %s retired from the node inventory", server)
|
||||
@@ -487,9 +494,12 @@ func (s *services) RetireHost(ctx context.Context, req *control.RetireHostReques
|
||||
defer s.host.workspaceMu.Unlock()
|
||||
err := s.host.RetireHost(req.GetServerId(), req.GetConfirm())
|
||||
var holds errHostHoldsWork
|
||||
var storage errRetireStorage
|
||||
switch {
|
||||
case errors.As(err, &holds):
|
||||
return nil, lifecycleclient.WithReasonMetadata(status.New(codes.FailedPrecondition, err.Error()), lifecycleclient.ReasonHostHoldsWork, holds.metadata()).Err()
|
||||
case errors.As(err, &storage):
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
case err != nil:
|
||||
return nil, status.Error(codes.FailedPrecondition, err.Error())
|
||||
}
|
||||
|
||||
@@ -291,6 +291,13 @@ func TestRetireHostRefusesAHostThatHoldsWork(t *testing.T) {
|
||||
if _, err := r.lc.RetireHost(r.ctx, &control.RetireHostRequest{ServerId: "node-a", Confirm: "node-b"}); status.Code(err) != codes.FailedPrecondition {
|
||||
t.Fatalf("retire with a confirm that does not match: %v", err)
|
||||
}
|
||||
// A failure to remove the host's record is a storage failure (Internal),
|
||||
// not a refusal: the CLI then answers "failed", exit 1.
|
||||
r.m.Lifecycle().Nodes.SetPersistHookForTest(func() error { return errors.New("injected record removal failure") })
|
||||
if _, err := r.lc.RetireHost(r.ctx, &control.RetireHostRequest{ServerId: "node-a", Confirm: "node-a"}); status.Code(err) != codes.Internal {
|
||||
t.Fatalf("retire whose record removal failed: %v, want Internal", err)
|
||||
}
|
||||
r.m.Lifecycle().Nodes.SetPersistHookForTest(nil)
|
||||
if _, err := r.lc.RetireHost(r.ctx, &control.RetireHostRequest{ServerId: "node-a", Confirm: "node-a"}); err != nil {
|
||||
t.Fatalf("retire of a host holding nothing: %v", err)
|
||||
}
|
||||
|
||||
@@ -68,7 +68,17 @@ type NodeInventoryStore struct {
|
||||
mu sync.Mutex
|
||||
dir string
|
||||
records map[string]NodeRegistration
|
||||
published sync.Map // committed NodeRegistration values; holder host checks never take mu (INV-WS-RENEW-NONBLOCKING-001)
|
||||
published sync.Map // committed NodeRegistration values; holder host checks never take mu (INV-WS-RENEW-NONBLOCKING-001)
|
||||
failWrite func() error // tests: a non-nil error fails the next record write or removal
|
||||
}
|
||||
|
||||
// SetPersistHookForTest installs (nil clears) a callback that each later
|
||||
// record write or removal of this store runs under the store mutex first. A
|
||||
// non-nil error fails that change with nothing changed. Tests only.
|
||||
func (s *NodeInventoryStore) SetPersistHookForTest(hook func() error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.failWrite = hook
|
||||
}
|
||||
|
||||
// OpenNodeInventoryStore opens or creates a node inventory store.
|
||||
@@ -155,6 +165,11 @@ func (s *NodeInventoryStore) RemoveNode(serverID string) error {
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.failWrite != nil {
|
||||
if err := s.failWrite(); err != nil {
|
||||
return fmt.Errorf("lifecycle: remove node %s: %w", serverID, err)
|
||||
}
|
||||
}
|
||||
delete(s.records, serverID)
|
||||
if err := os.Remove(s.path(serverID)); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return fmt.Errorf("lifecycle: remove node %s: %w", serverID, err)
|
||||
|
||||
Reference in new issue
Block a user