mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
retire-host follows finalize-format's contract: exit 0 retired, 2 refused, 75 the master could not be reached, 1 any other failure or usage error, and --json prints result, reason, server, volumes and message for every outcome. The master's HOST_HOLDS_WORK refusal names each held volume in its typed metadata. The forced-move preview answers the typed reason the move itself would carry (reason_code, e.g. BINARY_TOO_OLD), and force-move --dry-run --json shows it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
129 lines
5.8 KiB
Go
129 lines
5.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
control "github.com/seaweedfs/seaweed-block/core/rpc/control"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type fakeClusterMaster struct {
|
|
control.LifecycleServiceClient
|
|
finalize func(*control.FinalizeFormatRequest) (*control.FinalizeFormatResponse, error)
|
|
retire func(*control.RetireHostRequest) (*control.RetireHostResponse, error)
|
|
level func() (*control.FormatLevelResponse, error)
|
|
}
|
|
|
|
func (f *fakeClusterMaster) FormatLevel(context.Context, *control.FormatLevelRequest, ...grpc.CallOption) (*control.FormatLevelResponse, error) {
|
|
return f.level()
|
|
}
|
|
|
|
func (f *fakeClusterMaster) FinalizeFormat(_ context.Context, r *control.FinalizeFormatRequest, _ ...grpc.CallOption) (*control.FinalizeFormatResponse, error) {
|
|
return f.finalize(r)
|
|
}
|
|
|
|
func (f *fakeClusterMaster) RetireHost(_ context.Context, r *control.RetireHostRequest, _ ...grpc.CallOption) (*control.RetireHostResponse, error) {
|
|
return f.retire(r)
|
|
}
|
|
|
|
func withClusterMaster(t *testing.T, f *fakeClusterMaster) {
|
|
t.Helper()
|
|
saved := clusterLifecycleDial
|
|
t.Cleanup(func() { clusterLifecycleDial = saved })
|
|
clusterLifecycleDial = func(string) (control.LifecycleServiceClient, func() error, error) {
|
|
return f, func() error { return nil }, nil
|
|
}
|
|
}
|
|
|
|
// finalize-format asks level 1; a refusal names the old hosts and exits 2,
|
|
// a finalize and an already-finalized cluster exit 0, a malformed command 1.
|
|
func TestClusterFinalizeFormat(t *testing.T) {
|
|
refuse := true
|
|
f := &fakeClusterMaster{finalize: func(r *control.FinalizeFormatRequest) (*control.FinalizeFormatResponse, error) {
|
|
if r.GetLevel() != 1 {
|
|
t.Errorf("finalize asked level %d", r.GetLevel())
|
|
}
|
|
if refuse {
|
|
return nil, status.Error(codes.FailedPrecondition, "format finalize refused: registered hosts do not read format level 1: node-b (no report)")
|
|
}
|
|
return &control.FinalizeFormatResponse{FormatLevel: 1}, nil
|
|
}}
|
|
withClusterMaster(t, f)
|
|
var out, errOut bytes.Buffer
|
|
if code := run([]string{"cluster", "finalize-format", "--master", "m:1"}, &out, &errOut); code != 2 || !strings.Contains(errOut.String(), "node-b (no report)") {
|
|
t.Fatalf("refused finalize: exit %d, stderr %q", code, errOut.String())
|
|
}
|
|
refuse = false
|
|
out.Reset()
|
|
if code := run([]string{"cluster", "finalize-format", "--master", "m:1"}, &out, &errOut); code != 0 || !strings.Contains(out.String(), "format level 1 finalized") {
|
|
t.Fatalf("finalize: exit %d, stdout %q", code, out.String())
|
|
}
|
|
f.finalize = func(*control.FinalizeFormatRequest) (*control.FinalizeFormatResponse, error) {
|
|
return &control.FinalizeFormatResponse{FormatLevel: 1, Already: true}, nil
|
|
}
|
|
out.Reset()
|
|
if code := run([]string{"cluster", "finalize-format", "--master", "m:1"}, &out, &errOut); code != 0 || !strings.Contains(out.String(), "already finalized") {
|
|
t.Fatalf("already finalized: exit %d, stdout %q", code, out.String())
|
|
}
|
|
if code := run([]string{"cluster", "finalize-format"}, &out, &errOut); code != 1 {
|
|
t.Fatalf("finalize without --master: exit %d, want 1", code)
|
|
}
|
|
}
|
|
|
|
// retire-host sends the server id and its confirm; a refusal exits 2.
|
|
func TestClusterRetireHost(t *testing.T) {
|
|
var got *control.RetireHostRequest
|
|
f := &fakeClusterMaster{retire: func(r *control.RetireHostRequest) (*control.RetireHostResponse, error) {
|
|
got = r
|
|
if r.GetServerId() == "busy" {
|
|
return nil, status.Error(codes.FailedPrecondition, `retire refused: host "busy" holds a lease or an assignment for volumes v1; move them first`)
|
|
}
|
|
return &control.RetireHostResponse{ServerId: r.GetServerId()}, nil
|
|
}}
|
|
withClusterMaster(t, f)
|
|
var out, errOut bytes.Buffer
|
|
if code := run([]string{"cluster", "retire-host", "--master", "m:1", "--server", "node-c", "--confirm", "node-c"}, &out, &errOut); code != 0 || got.GetConfirm() != "node-c" || !strings.Contains(out.String(), "host node-c retired") {
|
|
t.Fatalf("retire: exit %d, request %+v, stdout %q", code, got, out.String())
|
|
}
|
|
if code := run([]string{"cluster", "retire-host", "--master", "m:1", "--server", "busy", "--confirm", "busy"}, &out, &errOut); code != 2 || !strings.Contains(errOut.String(), "holds a lease or an assignment for volumes v1") {
|
|
t.Fatalf("refused retire: exit %d, stderr %q", code, errOut.String())
|
|
}
|
|
if code := run([]string{"cluster", "retire-host", "--master", "m:1", "--server", "node-c"}, &out, &errOut); code != 1 {
|
|
t.Fatalf("retire without --confirm: exit %d, want 1", code)
|
|
}
|
|
}
|
|
|
|
// format-level prints the master's level (0 or 1) and exits 0; a master that
|
|
// cannot answer exits nonzero (a client decides from the level only when it
|
|
// has one; the snapshot CLI then runs at 0).
|
|
func TestClusterFormatLevel(t *testing.T) {
|
|
for _, want := range []uint32{0, 1} {
|
|
withClusterMaster(t, &fakeClusterMaster{level: func() (*control.FormatLevelResponse, error) {
|
|
return &control.FormatLevelResponse{FormatLevel: want}, nil
|
|
}})
|
|
var out, errOut bytes.Buffer
|
|
if code := run([]string{"cluster", "format-level", "--master", "m:1"}, &out, &errOut); code != 0 || strings.TrimSpace(out.String()) != fmt.Sprintf("format level %d", want) {
|
|
t.Fatalf("level %d: exit %d, stdout %q, stderr %q", want, code, out.String(), errOut.String())
|
|
}
|
|
if got, err := clusterFormatLevel(context.Background(), "m:1"); err != nil || got != want {
|
|
t.Fatalf("clusterFormatLevel: %d %v, want %d", got, err, want)
|
|
}
|
|
}
|
|
withClusterMaster(t, &fakeClusterMaster{level: func() (*control.FormatLevelResponse, error) {
|
|
return nil, status.Error(codes.Unavailable, "master down")
|
|
}})
|
|
var out, errOut bytes.Buffer
|
|
if code := run([]string{"cluster", "format-level", "--master", "m:1"}, &out, &errOut); code == 0 {
|
|
t.Fatalf("an unreachable master: exit 0, stdout %q", out.String())
|
|
}
|
|
if code := run([]string{"cluster", "format-level"}, &out, &errOut); code == 0 {
|
|
t.Fatal("format-level without --master: exit 0")
|
|
}
|
|
}
|