Files
seaweedfs/seaweed-block/cmd/blockvolume/newer_version_exit_test.go
T
pingqiuandClaude Opus 5.5 55562d718e blockvolume: a master's final exit wins over a newer HEAD version
The newer-HEAD-version exit (78 head_newer_version) is chosen before the
master's final reasons, so a startup that also met a final refusal keeps
its exit: moved and deleted stay 78 with their reason, revoked stays 79.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 14:55:02 -08:00

94 lines
3.7 KiB
Go

package main
import (
"context"
"errors"
"fmt"
"testing"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/seaweedfs/seaweed-block/core/rpc/control"
"github.com/seaweedfs/seaweed-block/core/storage"
)
// exitNewerVersion is a startup outcome that ended on a HEAD written by a
// newer version.
type exitNewerVersion struct{ exitStartupOutcome }
func (exitNewerVersion) ChunkStartupNewerVersion(string, uint64) bool { return true }
// A startup ended on a HEAD written by a newer version exits 78
// head_newer_version (no restart: a restart reads the same HEAD), where the
// same ended startup otherwise exits the restartable 75.
func TestANewerHeadVersionExits78NotRestartable(t *testing.T) {
h, _ := exitAuthorityHost(t)
e := newProcessExit(context.Background())
if !chooseChunkStartupExit(e, flags{workspaceLease: true, durableImpl: "walstore-chunk", volumeID: "v"}, h, &exitNewerVersion{exitStartupOutcome{expired: true}}) {
t.Fatal("the newer-version startup did not end the process")
}
if out := <-e.decision; out.code != 78 || out.reason != "head_newer_version" {
t.Fatalf("newer-version startup exit = %+v, want 78 head_newer_version", out)
}
h2, _ := exitAuthorityHost(t)
e2 := newProcessExit(context.Background())
chooseChunkStartupExit(e2, flags{workspaceLease: true, durableImpl: "walstore-chunk", volumeID: "v"}, h2, &exitStartupOutcome{expired: true})
if out := <-e2.decision; out.code != 75 {
t.Fatalf("control: an ended startup without the refusal exits %+v, want 75", out)
}
}
// A fatal durable open of a store written by a newer format exits 78
// store_format_newer; any other fatal open exits 1 as before.
func TestAStoreOfANewerFormatExits78(t *testing.T) {
if code, reason := durableOpenFatalExit(fmt.Errorf("durable: OpenWALStore x: %w", storage.ErrStoreFormatNewer)); code != 78 || reason != "store_format_newer" {
t.Fatalf("newer-format store exit = %d %q, want 78 store_format_newer", code, reason)
}
if code, reason := durableOpenFatalExit(errors.New("durable: OpenWALStore x: invalid superblock")); code != 1 || reason != "" {
t.Fatalf("other fatal open exit = %d %q, want 1", code, reason)
}
}
// A master's final refusal keeps its exit when the same startup also ended
// on a newer HEAD version: MOVED stays 78 moved, FINAL stays 79 revoked.
func TestAMasterFinalExitWinsOverANewerHeadVersion(t *testing.T) {
for _, tc := range []struct {
kind control.WorkspaceLeaseRefusal_Kind
final string
code int
reason string
}{
{control.WorkspaceLeaseRefusal_MOVED, "moved", 78, "moved"},
{control.WorkspaceLeaseRefusal_FINAL, "revoked", 79, "revoked"},
} {
t.Run(tc.final, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
refusal, err := status.New(codes.FailedPrecondition, "final refusal").WithDetails(&control.WorkspaceLeaseRefusal{Kind: tc.kind})
if err != nil {
t.Fatal(err)
}
h, _ := exitAuthorityHost(t, func(s *exitAssignmentStream) { s.streamRefuse = refusal.Err() })
for {
if _, final := h.MasterWaitState(); final == tc.final {
break
}
select {
case <-ctx.Done():
t.Fatalf("the master's %s refusal never reached the host", tc.final)
case <-time.After(time.Millisecond):
}
}
e := newProcessExit(ctx)
if !chooseChunkStartupExit(e, flags{workspaceLease: true, durableImpl: "walstore-chunk", volumeID: "v"}, h, &exitNewerVersion{exitStartupOutcome{expired: true}}) {
t.Fatal("no startup decision")
}
if out := <-e.decision; out.code != tc.code || out.reason != tc.reason {
t.Fatalf("final %s with a newer HEAD version chose %+v; want %d %s", tc.final, out, tc.code, tc.reason)
}
})
}
}