Files
seaweedfs/weed/storage/erasure_coding/shard_distribution_test.go
T
Chris LuandClaude Opus 5.5 d6dee41f00 ec: do not place shards on a volume server in maintenance mode (#13227)
* master: report a volume server's maintenance mode in DataNodeInfo

Planners that work from VolumeList had no way to tell a volume server in
maintenance mode from any other, so they kept choosing it as a target.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* admin: give a volume server in maintenance mode no free capacity

It refuses new data, so EC encode and balance planning must not pick it as
a destination; it stays listed so it can still be a source.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ec_balance: never move shards to a volume server in maintenance mode

It refuses every shard copy, and right after an evacuate it is the emptiest
server, so the planner kept targeting it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: count no free EC shard slots on a volume server in maintenance mode

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ec: report the volume server's error when it refuses a shard upload

A refused ReceiveFile ends the stream, so Send returns only io.EOF and the
job logged "failed to send file content: EOF" instead of the reason.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ec: test that a refused shard upload reports the server's error

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: never plan EC shard moves onto a volume server in maintenance mode

Zero free slots alone did not keep it out: a shard the planner moves off
the node credits a slot back, and a later move could take it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ec: test that ReceiveFileSendError reports a refusal in the response's Error field

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ec_balance: move the maintenance-target test to the end of the file

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 11:06:18 -08:00

466 lines
12 KiB
Go

package erasure_coding
import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
"github.com/seaweedfs/seaweedfs/weed/pb/worker_pb"
"google.golang.org/grpc"
)
func intPtr(n int) *int { return &n }
func writeShardFiles(t *testing.T, names ...string) map[string]string {
t.Helper()
dir := t.TempDir()
files := make(map[string]string, len(names))
for _, name := range names {
p := filepath.Join(dir, name)
if err := os.WriteFile(p, []byte(name), 0644); err != nil {
t.Fatal(err)
}
files[name] = p
}
return files
}
func shardTarget(node string, disk uint32, shards ...uint32) *worker_pb.TaskTarget {
return &worker_pb.TaskTarget{Node: node, DiskId: disk, ShardIds: shards}
}
type flightRecorder struct {
mu sync.Mutex
inFlight int
maxFlight int
keyFlight map[string]int
keyMax int
attempts []string
sent []string
failOn string
failFirst bool
enter chan struct{}
goAhead chan struct{}
noGate atomic.Bool
}
func newFlightRecorder() *flightRecorder {
return &flightRecorder{
keyFlight: make(map[string]int),
enter: make(chan struct{}, 32),
goAhead: make(chan struct{}),
}
}
func (r *flightRecorder) release() {
r.noGate.Store(true)
close(r.goAhead)
}
func (r *flightRecorder) send(ctx context.Context, _ uint32, _ string, _ grpc.DialOption, destServer string, diskID uint32, _ string, shardType string) error {
id := destServer + "/" + shardType
key := shardTransferKey(shardJob{destNode: destServer, diskID: diskID, hasDisk: true})
r.mu.Lock()
r.inFlight++
if r.inFlight > r.maxFlight {
r.maxFlight = r.inFlight
}
r.keyFlight[key]++
if r.keyFlight[key] > r.keyMax {
r.keyMax = r.keyFlight[key]
}
r.attempts = append(r.attempts, id)
r.mu.Unlock()
defer func() {
r.mu.Lock()
r.keyFlight[key]--
r.inFlight--
r.mu.Unlock()
}()
if r.failOn == id || r.failFirst {
r.failFirst = false
return errors.New("mock send failed")
}
if !r.noGate.Load() {
select {
case r.enter <- struct{}{}:
case <-ctx.Done():
return ctx.Err()
}
select {
case <-r.goAhead:
case <-ctx.Done():
return ctx.Err()
}
}
r.mu.Lock()
r.sent = append(r.sent, id)
r.mu.Unlock()
return nil
}
func waitEnters(t *testing.T, rec *flightRecorder, n int) {
t.Helper()
deadline := time.After(2 * time.Second)
for i := 0; i < n; i++ {
select {
case <-rec.enter:
case <-deadline:
t.Fatalf("timed out waiting for %d in-flight sends (got %d)", n, i)
}
}
}
func assertNoExtraEnter(t *testing.T, rec *flightRecorder, wait time.Duration) {
t.Helper()
select {
case <-rec.enter:
t.Fatal("unexpected extra in-flight send")
case <-time.After(wait):
}
}
func runDistribute(t *testing.T, targets []*worker_pb.TaskTarget, files map[string]string, rec *flightRecorder, max *int) (map[string][]string, error) {
t.Helper()
opts := &ShardDistributionOptions{
MaxShardTransfers: max,
send: rec.send,
}
return DistributeEcShards(1, "col", targets, files, nil, nil, opts)
}
func TestDistributeEcShardsZeroIsSequential(t *testing.T) {
files := writeShardFiles(t, "ec00", "ec01", "ec02", "ec03")
targets := []*worker_pb.TaskTarget{
shardTarget("full-01:8080", 0, 0),
shardTarget("full-01:8080", 1, 1),
shardTarget("full-02:8080", 0, 2),
shardTarget("full-02:8080", 1, 3),
}
rec := newFlightRecorder()
errCh := make(chan error, 1)
go func() {
_, err := runDistribute(t, targets, files, rec, intPtr(0))
errCh <- err
}()
waitEnters(t, rec, 1)
assertNoExtraEnter(t, rec, 40*time.Millisecond)
rec.mu.Lock()
if rec.maxFlight != 1 {
rec.mu.Unlock()
t.Fatalf("max in-flight=%d, want 1 for max_shard_transfers=0", rec.maxFlight)
}
rec.mu.Unlock()
rec.release()
if err := <-errCh; err != nil {
t.Fatal(err)
}
rec.mu.Lock()
defer rec.mu.Unlock()
if rec.maxFlight != 1 {
t.Fatalf("max in-flight=%d after release, want 1", rec.maxFlight)
}
if rec.keyMax != 1 {
t.Fatalf("per-key in-flight=%d, want 1", rec.keyMax)
}
if len(rec.sent) != 4 {
t.Fatalf("sent %d shards, want 4: %v", len(rec.sent), rec.sent)
}
}
func TestDistributeEcShardsNoCapParallelByKey(t *testing.T) {
files := writeShardFiles(t, "ec00", "ec01", "ec02", "ec03")
targets := []*worker_pb.TaskTarget{
shardTarget("full-01:8080", 1, 0),
shardTarget("full-01:8080", 2, 1),
shardTarget("full-02:8080", 0, 2),
shardTarget("full-02:8081", 0, 3), // same rack host, different destNode
}
rec := newFlightRecorder()
errCh := make(chan error, 1)
go func() {
_, err := runDistribute(t, targets, files, rec, nil)
errCh <- err
}()
waitEnters(t, rec, 4)
rec.mu.Lock()
if rec.maxFlight != 4 {
rec.mu.Unlock()
t.Fatalf("no cap: max in-flight=%d, want 4 (one per dest disk/node)", rec.maxFlight)
}
if rec.keyMax != 1 {
rec.mu.Unlock()
t.Fatalf("per-key in-flight=%d, want 1", rec.keyMax)
}
rec.mu.Unlock()
rec.release()
if err := <-errCh; err != nil {
t.Fatal(err)
}
rec.mu.Lock()
defer rec.mu.Unlock()
if len(rec.sent) != 4 {
t.Fatalf("sent %d, want 4: %v", len(rec.sent), rec.sent)
}
}
func TestDistributeEcShardsSameDiskQueued(t *testing.T) {
files := writeShardFiles(t, "ec00", "ec01")
targets := []*worker_pb.TaskTarget{
shardTarget("full-01:8080", 0, 0, 1),
}
rec := newFlightRecorder()
errCh := make(chan error, 1)
go func() {
_, err := runDistribute(t, targets, files, rec, nil)
errCh <- err
}()
waitEnters(t, rec, 1)
assertNoExtraEnter(t, rec, 40*time.Millisecond)
rec.release()
if err := <-errCh; err != nil {
t.Fatal(err)
}
rec.mu.Lock()
defer rec.mu.Unlock()
if rec.maxFlight != 1 {
t.Fatalf("same disk: max in-flight=%d, want 1", rec.maxFlight)
}
if rec.sent[0] != "full-01:8080/ec00" || rec.sent[1] != "full-01:8080/ec01" {
t.Fatalf("same-disk order %v, want ec00 then ec01", rec.sent)
}
}
func TestDistributeEcShardsAutoDiskSerializesServer(t *testing.T) {
files := writeShardFiles(t, "ec00", "ec01", "ec02")
targets := []*worker_pb.TaskTarget{
shardTarget("full-01:8080", 0, 0), // disk picked by the receiver
shardTarget("full-01:8080", 1, 1),
shardTarget("full-02:8080", 1, 2),
}
rec := newFlightRecorder()
errCh := make(chan error, 1)
go func() {
_, err := runDistribute(t, targets, files, rec, nil)
errCh <- err
}()
waitEnters(t, rec, 2)
rec.mu.Lock()
if rec.maxFlight != 2 {
rec.mu.Unlock()
t.Fatalf("auto-placement server must serialize: in-flight=%d, want 2 (one per server)", rec.maxFlight)
}
rec.mu.Unlock()
rec.release()
if err := <-errCh; err != nil {
t.Fatal(err)
}
rec.mu.Lock()
defer rec.mu.Unlock()
if len(rec.sent) != 3 {
t.Fatalf("sent %d, want 3", len(rec.sent))
}
}
func TestDistributeEcShardsTwoDisksTwoStreams(t *testing.T) {
files := writeShardFiles(t, "ec00", "ec01")
targets := []*worker_pb.TaskTarget{
shardTarget("full-01:8080", 1, 0),
shardTarget("full-01:8080", 2, 1),
}
rec := newFlightRecorder()
errCh := make(chan error, 1)
go func() {
_, err := runDistribute(t, targets, files, rec, nil)
errCh <- err
}()
waitEnters(t, rec, 2)
rec.mu.Lock()
if rec.maxFlight != 2 {
rec.mu.Unlock()
t.Fatalf("two disks on one VS: in-flight=%d, want 2", rec.maxFlight)
}
rec.mu.Unlock()
rec.release()
if err := <-errCh; err != nil {
t.Fatal(err)
}
}
func TestDistributeEcShardsCapNeverExceedsN(t *testing.T) {
files := writeShardFiles(t, "ec00", "ec01", "ec02", "ec03")
targets := []*worker_pb.TaskTarget{
shardTarget("full-01:8080", 1, 0),
shardTarget("full-01:8080", 2, 1),
shardTarget("full-02:8080", 1, 2),
shardTarget("full-02:8080", 2, 3),
}
rec := newFlightRecorder()
errCh := make(chan error, 1)
go func() {
_, err := runDistribute(t, targets, files, rec, intPtr(2))
errCh <- err
}()
waitEnters(t, rec, 2)
assertNoExtraEnter(t, rec, 40*time.Millisecond)
rec.mu.Lock()
if rec.maxFlight != 2 {
rec.mu.Unlock()
t.Fatalf("cap 2: in-flight=%d, want 2", rec.maxFlight)
}
rec.mu.Unlock()
rec.release()
if err := <-errCh; err != nil {
t.Fatal(err)
}
rec.mu.Lock()
defer rec.mu.Unlock()
if rec.maxFlight > 2 {
t.Fatalf("cap 2: max in-flight=%d", rec.maxFlight)
}
if rec.keyMax != 1 {
t.Fatalf("per-key in-flight=%d, want 1 under cap", rec.keyMax)
}
if len(rec.sent) != 4 {
t.Fatalf("sent %d, want 4", len(rec.sent))
}
}
func TestDistributeEcShardsErrorAbortSameKey(t *testing.T) {
files := writeShardFiles(t, "ec00", "ec01", "ec02")
targets := []*worker_pb.TaskTarget{
shardTarget("full-01:8080", 0, 0, 1, 2),
}
rec := newFlightRecorder()
rec.noGate.Store(true)
rec.failOn = "full-01:8080/ec01"
_, err := runDistribute(t, targets, files, rec, nil)
if err == nil {
t.Fatal("expected send error")
}
if !strings.Contains(err.Error(), "failed to send ec01 to full-01:8080") {
t.Fatalf("error wrap: %v", err)
}
rec.mu.Lock()
defer rec.mu.Unlock()
if got := strings.Join(rec.attempts, ","); got != "full-01:8080/ec00,full-01:8080/ec01" {
t.Fatalf("attempts %v, want ec00 then ec01 (ec02 aborted)", rec.attempts)
}
if len(rec.sent) != 1 || rec.sent[0] != "full-01:8080/ec00" {
t.Fatalf("successful sends %v, want only ec00", rec.sent)
}
}
func TestDistributeEcShardsErrorAbortCancelsOtherKeys(t *testing.T) {
files := writeShardFiles(t, "ec00", "ec01", "ec02")
targets := []*worker_pb.TaskTarget{
shardTarget("full-01:8080", 0, 0),
shardTarget("full-02:8080", 0, 1),
shardTarget("full-03:8080", 0, 2),
}
rec := newFlightRecorder()
rec.noGate.Store(true)
rec.failFirst = true
_, err := runDistribute(t, targets, files, rec, intPtr(1))
if err == nil {
t.Fatal("expected send error")
}
rec.mu.Lock()
defer rec.mu.Unlock()
if len(rec.attempts) != 1 {
t.Fatalf("cap=1 abort: attempts %v, want only the failing send", rec.attempts)
}
if len(rec.sent) != 0 {
t.Fatalf("successful sends %v, want none", rec.sent)
}
}
func TestDistributeEcShardsMissingFile(t *testing.T) {
files := writeShardFiles(t, "ec00")
targets := []*worker_pb.TaskTarget{
shardTarget("full-01:8080", 0, 1),
}
for _, max := range []*int{intPtr(0), nil} {
_, err := DistributeEcShards(1, "col", targets, files, nil, nil, &ShardDistributionOptions{
MaxShardTransfers: max,
send: func(context.Context, uint32, string, grpc.DialOption, string, uint32, string, string) error {
t.Fatal("send should not run for missing file")
return nil
},
})
if err == nil || !strings.Contains(err.Error(), "shard file ec01 not found") {
t.Fatalf("missing file error (max=%v): %v", max, err)
}
}
}
func TestShardTransferKeyUsesDestNodeNotRack(t *testing.T) {
a := shardTransferKey(shardJob{destNode: "full-01:8080", diskID: 0, hasDisk: true})
b := shardTransferKey(shardJob{destNode: "full-01:8081", diskID: 0, hasDisk: true})
if a == b {
t.Fatalf("host:port keys collided: %s", a)
}
noDisk := shardTransferKey(shardJob{destNode: "full-01:8080", hasDisk: false})
if noDisk != "full-01:8080" {
t.Fatalf("no disk id key=%q, want destNode", noDisk)
}
if fmt.Sprint(a) == noDisk {
t.Fatalf("disk 0 key should differ from destNode-only when hasDisk")
}
}
// receiveFileStream ends an upload the way a server that rejects it does: the
// next Send sees io.EOF and the stream closes with the given result.
type receiveFileStream struct {
volume_server_pb.VolumeServer_ReceiveFileClient
resp *volume_server_pb.ReceiveFileResponse
err error
}
func (s receiveFileStream) CloseAndRecv() (*volume_server_pb.ReceiveFileResponse, error) {
return s.resp, s.err
}
// A server that rejects an upload may say why in the response's Error field
// with an OK status, as the Rust volume server does; the sender must report
// that instead of the bare EOF its Send saw.
func TestReceiveFileSendErrorReportsTheServersReason(t *testing.T) {
rejected := receiveFileStream{resp: &volume_server_pb.ReceiveFileResponse{Error: "no space left on disk 1"}}
if err := ReceiveFileSendError(rejected, io.EOF); err == nil || !strings.Contains(err.Error(), "no space left on disk 1") {
t.Fatalf("got %v, want the server's reason", err)
}
failed := receiveFileStream{err: errors.New("rpc error: reservation conflict")}
if err := ReceiveFileSendError(failed, io.EOF); err == nil || !strings.Contains(err.Error(), "reservation conflict") {
t.Fatalf("got %v, want the stream's status", err)
}
clean := receiveFileStream{resp: &volume_server_pb.ReceiveFileResponse{}}
if err := ReceiveFileSendError(clean, io.EOF); !errors.Is(err, io.EOF) {
t.Fatalf("got %v, want the EOF when the server gives no reason", err)
}
if err := ReceiveFileSendError(clean, errors.New("connection reset")); err == nil || err.Error() != "connection reset" {
t.Fatalf("got %v, want a non-EOF send error unchanged", err)
}
}