* proto: add BalanceMoveSpec and batch fields to BalanceTaskParams Add BalanceMoveSpec message for encoding individual volume moves, and max_concurrent_moves + repeated moves fields to BalanceTaskParams to support batching multiple volume moves in a single job. * balance handler: add batch execution with concurrent volume moves Refactor Execute() into executeSingleMove() (backward compatible) and executeBatchMoves() which runs multiple volume moves concurrently using a semaphore-bounded goroutine pool. When BalanceTaskParams.Moves is populated, the batch path is taken; otherwise the single-move path. Includes aggregate progress reporting across concurrent moves, per-move error collection, and partial failure support. * balance handler: add batch config fields to Descriptor and worker config Add max_concurrent_moves and batch_size fields to the worker config form and deriveBalanceWorkerConfig(). These control how many volume moves run concurrently within a batch job and the maximum batch size. * balance handler: group detection proposals into batch jobs When batch_size > 1, the Detect method groups detection results into batch proposals where each proposal encodes multiple BalanceMoveSpec entries in BalanceTaskParams.Moves. Single-result batches fall back to the existing single-move proposal format for backward compatibility. * admin UI: add volume balance execution plan and batch badge Add renderBalanceExecutionPlan() for rich rendering of volume balance jobs in the job detail modal. Single-move jobs show source/target/volume info; batch jobs show a moves table with all volume moves. Add batch badge (e.g., "5 moves") next to job type in the execution jobs table when the job has batch=true label. * Update plugin_templ.go * fix: detection algorithm uses greedy target instead of divergent topology scores The detection loop tracked effective volume counts via an adjustments map, but createBalanceTask independently called planBalanceDestination which used the topology's LoadCount — a separate, unadjusted source of truth. This divergence caused multiple moves to pile onto the same server. Changes: - Add resolveBalanceDestination to resolve the detection loop's greedy target (minServer) rather than independently picking a destination - Add oscillation guard: stop when max-min <= 1 since no single move can improve the balance beyond that point - Track unseeded destinations: if a target server wasn't in the initial serverVolumeCounts, add it so subsequent iterations include it - Add TestDetection_UnseededDestinationDoesNotOverload * fix: handler force_move propagation, partial failure, deterministic dedupe - Propagate ForceMove from outer BalanceTaskParams to individual move TaskParams so batch moves respect the force_move flag - Fix partial failure: mark job successful if at least one move succeeded (succeeded > 0 || failed == 0) to avoid re-running already-completed moves on retry - Use SHA-256 hash for deterministic dedupe key fallback instead of time.Now().UnixNano() which is non-deterministic - Remove unused successDetails variable - Extract maxProposalStringLength constant to replace magic number 200 * admin UI: use template literals in balance execution plan rendering * fix: integration test handles batch proposals from batched detection With batch_size=20, all moves are grouped into a single proposal containing BalanceParams.Moves instead of top-level Sources/Targets. Update assertions to handle both batch and single-move proposal formats. * fix: verify volume size on target before deleting source during balance Add a pre-delete safety check that reads the volume file status on both source and target, then compares .dat file size and file count. If they don't match, the move is aborted — leaving the source intact rather than risking irreversible data loss. Also removes the redundant mountVolume call since VolumeCopy already mounts the volume on the target server. * fix: clamp maxConcurrent, serialize progress sends, validate config as int64 - Clamp maxConcurrentMoves to defaultMaxConcurrentMoves before creating the semaphore so a stale or malicious job cannot request unbounded concurrent volume moves - Extend progressMu to cover sender.SendProgress calls since the underlying gRPC stream is not safe for concurrent writes - Perform bounds checks on max_concurrent_moves and batch_size in int64 space before casting to int, avoiding potential overflow on 32-bit * fix: check disk capacity in resolveBalanceDestination Skip disks where VolumeCount >= MaxVolumeCount so the detection loop does not propose moves to a full disk that would fail at execution time. * test: rename unseeded destination test to match actual behavior The test exercises a server with 0 volumes that IS seeded from topology (matching disk type), not an unseeded destination. Rename to TestDetection_ZeroVolumeServerIncludedInBalance and fix comments. * test: tighten integration test to assert exactly one batch proposal With default batch_size=20, all moves should be grouped into a single batch proposal. Assert len(proposals)==1 and require BalanceParams with Moves, removing the legacy single-move else branch. * fix: propagate ctx to RPCs and restore source writability on abort - All helper methods (markVolumeReadonly, copyVolume, tailVolume, readVolumeFileStatus, deleteVolume) now accept a context parameter instead of using context.Background(), so Execute's ctx propagates cancellation and timeouts into every volume server RPC - Add deferred cleanup that restores the source volume to writable if any step after markVolumeReadonly fails, preventing the source from being left permanently readonly on abort - Add markVolumeWritable helper using VolumeMarkWritableRequest * fix: deep-copy protobuf messages in test recording sender Use proto.Clone in recordingExecutionSender to store immutable snapshots of JobProgressUpdate and JobCompleted, preventing assertions from observing mutations if the handler reuses message pointers. * fix: add VolumeMarkWritable and ReadVolumeFileStatus to fake volume server The balance task now calls ReadVolumeFileStatus for pre-delete verification and VolumeMarkWritable to restore writability on abort. Add both RPCs to the test fake, and drop the mountCalls assertion since BalanceTask no longer calls VolumeMount directly (VolumeCopy handles it). * fix: use maxConcurrentMovesLimit (50) for clamp, not defaultMaxConcurrentMoves defaultMaxConcurrentMoves (5) is the fallback when the field is unset, not an upper bound. Clamping to it silently overrides valid config values like 10/20/50. Introduce maxConcurrentMovesLimit (50) matching the descriptor's MaxValue and clamp to that instead. * fix: cancel batch moves on progress stream failure Derive a cancellable batchCtx from the caller's ctx. If sender.SendProgress returns an error (client disconnect, context cancelled), capture it, skip further sends, and cancel batchCtx so in-flight moves abort via their propagated context rather than running blind to completion. * fix: bound cleanup timeout and validate batch move fields - Use a 30-second timeout for the deferred markVolumeWritable cleanup instead of context.Background() which can block indefinitely if the volume server is unreachable - Validate required fields (VolumeID, SourceNode, TargetNode) before appending moves to a batch proposal, skipping invalid entries - Fall back to a single-move proposal when filtering leaves only one valid move in a batch * fix: cancel task execution on SendProgress stream failure All handler progress callbacks previously ignored SendProgress errors, allowing tasks to continue executing after the client disconnected. Now each handler creates a derived cancellable context and cancels it on the first SendProgress error, stopping the in-flight task promptly. Handlers fixed: erasure_coding, vacuum, volume_balance (single-move), and admin_script (breaks command loop on send failure). * fix: validate batch moves before scheduling in executeBatchMoves Reject empty batches, enforce a hard upper bound (100 moves), and filter out nil or incomplete move specs (missing source/target/volume) before allocating progress tracking and launching goroutines. * test: add batch balance execution integration test Tests the batch move path with 3 volumes, max concurrency 2, using fake volume servers. Verifies all moves complete with correct readonly, copy, tail, and delete RPC counts. * test: add MarkWritableCount and ReadFileStatusCount accessors Expose the markWritableCalls and readFileStatusCalls counters on the fake volume server, following the existing MarkReadonlyCount pattern. * fix: oscillation guard uses global effective counts for heterogeneous capacity The oscillation guard (max-min <= 1) previously used maxServer/minServer which are determined by utilization ratio. With heterogeneous capacity, maxServer by utilization can have fewer raw volumes than minServer, producing a negative diff and incorrectly triggering the guard. Now scans all servers' effective counts to find the true global max/min volume counts, so the guard works correctly regardless of whether utilization-based or raw-count balancing is used. * fix: admin script handler breaks outer loop on SendProgress failure The break on SendProgress error inside the shell.Commands scan only exited the inner loop, letting the outer command loop continue executing commands on a broken stream. Use a sendBroken flag to propagate the break to the outer execCommands loop.
648 lines
20 KiB
Go
648 lines
20 KiB
Go
package pluginworker
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/plugin_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/worker_pb"
|
|
balancetask "github.com/seaweedfs/seaweedfs/weed/worker/tasks/balance"
|
|
workertypes "github.com/seaweedfs/seaweedfs/weed/worker/types"
|
|
"google.golang.org/protobuf/proto"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
)
|
|
|
|
func TestDecodeVolumeBalanceTaskParamsFromPayload(t *testing.T) {
|
|
expected := &worker_pb.TaskParams{
|
|
TaskId: "task-1",
|
|
VolumeId: 42,
|
|
Collection: "photos",
|
|
Sources: []*worker_pb.TaskSource{
|
|
{
|
|
Node: "10.0.0.1:8080",
|
|
VolumeId: 42,
|
|
},
|
|
},
|
|
Targets: []*worker_pb.TaskTarget{
|
|
{
|
|
Node: "10.0.0.2:8080",
|
|
VolumeId: 42,
|
|
},
|
|
},
|
|
TaskParams: &worker_pb.TaskParams_BalanceParams{
|
|
BalanceParams: &worker_pb.BalanceTaskParams{
|
|
ForceMove: true,
|
|
TimeoutSeconds: 1200,
|
|
},
|
|
},
|
|
}
|
|
payload, err := proto.Marshal(expected)
|
|
if err != nil {
|
|
t.Fatalf("marshal payload: %v", err)
|
|
}
|
|
|
|
job := &plugin_pb.JobSpec{
|
|
JobId: "job-from-admin",
|
|
Parameters: map[string]*plugin_pb.ConfigValue{
|
|
"task_params_pb": {Kind: &plugin_pb.ConfigValue_BytesValue{BytesValue: payload}},
|
|
},
|
|
}
|
|
|
|
actual, err := decodeVolumeBalanceTaskParams(job)
|
|
if err != nil {
|
|
t.Fatalf("decodeVolumeBalanceTaskParams() err = %v", err)
|
|
}
|
|
if !proto.Equal(expected, actual) {
|
|
t.Fatalf("decoded params mismatch\nexpected: %+v\nactual: %+v", expected, actual)
|
|
}
|
|
}
|
|
|
|
func TestDecodeVolumeBalanceTaskParamsFallback(t *testing.T) {
|
|
job := &plugin_pb.JobSpec{
|
|
JobId: "job-2",
|
|
Parameters: map[string]*plugin_pb.ConfigValue{
|
|
"volume_id": {Kind: &plugin_pb.ConfigValue_Int64Value{Int64Value: 7}},
|
|
"source_server": {Kind: &plugin_pb.ConfigValue_StringValue{StringValue: "127.0.0.1:8080"}},
|
|
"target_server": {Kind: &plugin_pb.ConfigValue_StringValue{StringValue: "127.0.0.2:8080"}},
|
|
"collection": {Kind: &plugin_pb.ConfigValue_StringValue{StringValue: "videos"}},
|
|
},
|
|
}
|
|
|
|
params, err := decodeVolumeBalanceTaskParams(job)
|
|
if err != nil {
|
|
t.Fatalf("decodeVolumeBalanceTaskParams() err = %v", err)
|
|
}
|
|
if params.TaskId != "job-2" || params.VolumeId != 7 || params.Collection != "videos" {
|
|
t.Fatalf("unexpected basic params: %+v", params)
|
|
}
|
|
if len(params.Sources) != 1 || params.Sources[0].Node != "127.0.0.1:8080" {
|
|
t.Fatalf("unexpected sources: %+v", params.Sources)
|
|
}
|
|
if len(params.Targets) != 1 || params.Targets[0].Node != "127.0.0.2:8080" {
|
|
t.Fatalf("unexpected targets: %+v", params.Targets)
|
|
}
|
|
if params.GetBalanceParams() == nil {
|
|
t.Fatalf("expected fallback balance params")
|
|
}
|
|
}
|
|
|
|
func TestDeriveBalanceWorkerConfig(t *testing.T) {
|
|
values := map[string]*plugin_pb.ConfigValue{
|
|
"imbalance_threshold": {
|
|
Kind: &plugin_pb.ConfigValue_DoubleValue{DoubleValue: 0.45},
|
|
},
|
|
"min_server_count": {
|
|
Kind: &plugin_pb.ConfigValue_Int64Value{Int64Value: 5},
|
|
},
|
|
"min_interval_seconds": {
|
|
Kind: &plugin_pb.ConfigValue_Int64Value{Int64Value: 33},
|
|
},
|
|
}
|
|
|
|
cfg := deriveBalanceWorkerConfig(values)
|
|
if cfg.TaskConfig.ImbalanceThreshold != 0.45 {
|
|
t.Fatalf("expected imbalance_threshold 0.45, got %v", cfg.TaskConfig.ImbalanceThreshold)
|
|
}
|
|
if cfg.TaskConfig.MinServerCount != 5 {
|
|
t.Fatalf("expected min_server_count 5, got %d", cfg.TaskConfig.MinServerCount)
|
|
}
|
|
if cfg.MinIntervalSeconds != 33 {
|
|
t.Fatalf("expected min_interval_seconds 33, got %d", cfg.MinIntervalSeconds)
|
|
}
|
|
// Defaults for batch config when not specified
|
|
if cfg.MaxConcurrentMoves != defaultMaxConcurrentMoves {
|
|
t.Fatalf("expected default max_concurrent_moves %d, got %d", defaultMaxConcurrentMoves, cfg.MaxConcurrentMoves)
|
|
}
|
|
if cfg.BatchSize != 20 {
|
|
t.Fatalf("expected default batch_size 20, got %d", cfg.BatchSize)
|
|
}
|
|
}
|
|
|
|
func TestDeriveBalanceWorkerConfigBatchFields(t *testing.T) {
|
|
values := map[string]*plugin_pb.ConfigValue{
|
|
"max_concurrent_moves": {
|
|
Kind: &plugin_pb.ConfigValue_Int64Value{Int64Value: 10},
|
|
},
|
|
"batch_size": {
|
|
Kind: &plugin_pb.ConfigValue_Int64Value{Int64Value: 50},
|
|
},
|
|
}
|
|
|
|
cfg := deriveBalanceWorkerConfig(values)
|
|
if cfg.MaxConcurrentMoves != 10 {
|
|
t.Fatalf("expected max_concurrent_moves 10, got %d", cfg.MaxConcurrentMoves)
|
|
}
|
|
if cfg.BatchSize != 50 {
|
|
t.Fatalf("expected batch_size 50, got %d", cfg.BatchSize)
|
|
}
|
|
}
|
|
|
|
func TestDeriveBalanceWorkerConfigBatchClamping(t *testing.T) {
|
|
values := map[string]*plugin_pb.ConfigValue{
|
|
"max_concurrent_moves": {
|
|
Kind: &plugin_pb.ConfigValue_Int64Value{Int64Value: 999},
|
|
},
|
|
"batch_size": {
|
|
Kind: &plugin_pb.ConfigValue_Int64Value{Int64Value: 0},
|
|
},
|
|
}
|
|
|
|
cfg := deriveBalanceWorkerConfig(values)
|
|
if cfg.MaxConcurrentMoves != 50 {
|
|
t.Fatalf("expected max_concurrent_moves clamped to 50, got %d", cfg.MaxConcurrentMoves)
|
|
}
|
|
if cfg.BatchSize != 1 {
|
|
t.Fatalf("expected batch_size clamped to 1, got %d", cfg.BatchSize)
|
|
}
|
|
}
|
|
|
|
func makeDetectionResult(volumeID uint32, source, target, collection string) *workertypes.TaskDetectionResult {
|
|
return &workertypes.TaskDetectionResult{
|
|
TaskID: fmt.Sprintf("balance-%d", volumeID),
|
|
TaskType: workertypes.TaskTypeBalance,
|
|
VolumeID: volumeID,
|
|
Server: source,
|
|
Collection: collection,
|
|
Priority: workertypes.TaskPriorityNormal,
|
|
Reason: "imbalanced",
|
|
TypedParams: &worker_pb.TaskParams{
|
|
VolumeId: volumeID,
|
|
Collection: collection,
|
|
VolumeSize: 1024,
|
|
Sources: []*worker_pb.TaskSource{
|
|
{Node: source, VolumeId: volumeID},
|
|
},
|
|
Targets: []*worker_pb.TaskTarget{
|
|
{Node: target, VolumeId: volumeID},
|
|
},
|
|
TaskParams: &worker_pb.TaskParams_BalanceParams{
|
|
BalanceParams: &worker_pb.BalanceTaskParams{TimeoutSeconds: 600},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestBuildBatchVolumeBalanceProposals_SingleBatch(t *testing.T) {
|
|
results := []*workertypes.TaskDetectionResult{
|
|
makeDetectionResult(1, "s1:8080", "t1:8080", "c1"),
|
|
makeDetectionResult(2, "s2:8080", "t2:8080", "c1"),
|
|
makeDetectionResult(3, "s1:8080", "t2:8080", "c1"),
|
|
}
|
|
|
|
proposals := buildBatchVolumeBalanceProposals(results, 10, 5)
|
|
if len(proposals) != 1 {
|
|
t.Fatalf("expected 1 batch proposal, got %d", len(proposals))
|
|
}
|
|
|
|
p := proposals[0]
|
|
if p.Labels["batch"] != "true" {
|
|
t.Fatalf("expected batch label")
|
|
}
|
|
if p.Labels["batch_size"] != "3" {
|
|
t.Fatalf("expected batch_size label '3', got %q", p.Labels["batch_size"])
|
|
}
|
|
|
|
// Decode and verify moves
|
|
payload := p.Parameters["task_params_pb"].GetBytesValue()
|
|
if len(payload) == 0 {
|
|
t.Fatalf("expected task_params_pb payload")
|
|
}
|
|
decoded := &worker_pb.TaskParams{}
|
|
if err := proto.Unmarshal(payload, decoded); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
moves := decoded.GetBalanceParams().GetMoves()
|
|
if len(moves) != 3 {
|
|
t.Fatalf("expected 3 moves, got %d", len(moves))
|
|
}
|
|
if moves[0].VolumeId != 1 || moves[1].VolumeId != 2 || moves[2].VolumeId != 3 {
|
|
t.Fatalf("unexpected volume IDs: %v", moves)
|
|
}
|
|
if decoded.GetBalanceParams().MaxConcurrentMoves != 5 {
|
|
t.Fatalf("expected MaxConcurrentMoves 5, got %d", decoded.GetBalanceParams().MaxConcurrentMoves)
|
|
}
|
|
}
|
|
|
|
func TestBuildBatchVolumeBalanceProposals_MultipleBatches(t *testing.T) {
|
|
results := make([]*workertypes.TaskDetectionResult, 5)
|
|
for i := range results {
|
|
results[i] = makeDetectionResult(uint32(i+1), "s1:8080", "t1:8080", "c1")
|
|
}
|
|
|
|
proposals := buildBatchVolumeBalanceProposals(results, 2, 3)
|
|
// 5 results / batch_size 2 = 3 proposals (2, 2, 1)
|
|
if len(proposals) != 3 {
|
|
t.Fatalf("expected 3 proposals, got %d", len(proposals))
|
|
}
|
|
|
|
// First two should be batch proposals
|
|
if proposals[0].Labels["batch"] != "true" {
|
|
t.Fatalf("first proposal should be batch")
|
|
}
|
|
if proposals[1].Labels["batch"] != "true" {
|
|
t.Fatalf("second proposal should be batch")
|
|
}
|
|
// Last one has only 1 result, should fall back to single-move proposal
|
|
if proposals[2].Labels["batch"] == "true" {
|
|
t.Fatalf("last proposal with 1 result should be single-move, not batch")
|
|
}
|
|
}
|
|
|
|
func TestBuildBatchVolumeBalanceProposals_BatchSizeOne(t *testing.T) {
|
|
results := []*workertypes.TaskDetectionResult{
|
|
makeDetectionResult(1, "s1:8080", "t1:8080", "c1"),
|
|
makeDetectionResult(2, "s2:8080", "t2:8080", "c1"),
|
|
}
|
|
|
|
// batch_size=1 should not be called (Detect guards this), but test the function directly
|
|
proposals := buildBatchVolumeBalanceProposals(results, 1, 5)
|
|
// Each result becomes its own single-move proposal
|
|
if len(proposals) != 2 {
|
|
t.Fatalf("expected 2 proposals, got %d", len(proposals))
|
|
}
|
|
}
|
|
|
|
func TestVolumeBalanceDescriptorHasBatchFields(t *testing.T) {
|
|
descriptor := NewVolumeBalanceHandler(nil).Descriptor()
|
|
if !workerConfigFormHasField(descriptor.WorkerConfigForm, "max_concurrent_moves") {
|
|
t.Fatalf("expected max_concurrent_moves in worker config form")
|
|
}
|
|
if !workerConfigFormHasField(descriptor.WorkerConfigForm, "batch_size") {
|
|
t.Fatalf("expected batch_size in worker config form")
|
|
}
|
|
}
|
|
|
|
func TestBuildVolumeBalanceProposal(t *testing.T) {
|
|
params := &worker_pb.TaskParams{
|
|
TaskId: "balance-task-1",
|
|
VolumeId: 55,
|
|
Collection: "images",
|
|
Sources: []*worker_pb.TaskSource{
|
|
{
|
|
Node: "source-a:8080",
|
|
VolumeId: 55,
|
|
},
|
|
},
|
|
Targets: []*worker_pb.TaskTarget{
|
|
{
|
|
Node: "target-b:8080",
|
|
VolumeId: 55,
|
|
},
|
|
},
|
|
TaskParams: &worker_pb.TaskParams_BalanceParams{
|
|
BalanceParams: &worker_pb.BalanceTaskParams{
|
|
TimeoutSeconds: 600,
|
|
},
|
|
},
|
|
}
|
|
result := &workertypes.TaskDetectionResult{
|
|
TaskID: "balance-task-1",
|
|
TaskType: workertypes.TaskTypeBalance,
|
|
VolumeID: 55,
|
|
Server: "source-a",
|
|
Collection: "images",
|
|
Priority: workertypes.TaskPriorityHigh,
|
|
Reason: "imbalanced load",
|
|
TypedParams: params,
|
|
}
|
|
|
|
proposal, err := buildVolumeBalanceProposal(result)
|
|
if err != nil {
|
|
t.Fatalf("buildVolumeBalanceProposal() err = %v", err)
|
|
}
|
|
if proposal.JobType != "volume_balance" {
|
|
t.Fatalf("unexpected job type %q", proposal.JobType)
|
|
}
|
|
if proposal.DedupeKey == "" {
|
|
t.Fatalf("expected dedupe key")
|
|
}
|
|
if proposal.Parameters["task_params_pb"] == nil {
|
|
t.Fatalf("expected serialized task params")
|
|
}
|
|
if proposal.Labels["source_node"] != "source-a:8080" {
|
|
t.Fatalf("unexpected source label %q", proposal.Labels["source_node"])
|
|
}
|
|
if proposal.Labels["target_node"] != "target-b:8080" {
|
|
t.Fatalf("unexpected target label %q", proposal.Labels["target_node"])
|
|
}
|
|
}
|
|
|
|
func TestVolumeBalanceHandlerRejectsUnsupportedJobType(t *testing.T) {
|
|
handler := NewVolumeBalanceHandler(nil)
|
|
err := handler.Detect(context.Background(), &plugin_pb.RunDetectionRequest{
|
|
JobType: "vacuum",
|
|
}, noopDetectionSender{})
|
|
if err == nil {
|
|
t.Fatalf("expected detect job type mismatch error")
|
|
}
|
|
|
|
err = handler.Execute(context.Background(), &plugin_pb.ExecuteJobRequest{
|
|
Job: &plugin_pb.JobSpec{JobId: "job-1", JobType: "vacuum"},
|
|
}, noopExecutionSender{})
|
|
if err == nil {
|
|
t.Fatalf("expected execute job type mismatch error")
|
|
}
|
|
}
|
|
|
|
func TestVolumeBalanceHandlerDetectSkipsByMinInterval(t *testing.T) {
|
|
handler := NewVolumeBalanceHandler(nil)
|
|
sender := &recordingDetectionSender{}
|
|
err := handler.Detect(context.Background(), &plugin_pb.RunDetectionRequest{
|
|
JobType: "volume_balance",
|
|
LastSuccessfulRun: timestamppb.New(time.Now().Add(-3 * time.Second)),
|
|
WorkerConfigValues: map[string]*plugin_pb.ConfigValue{
|
|
"min_interval_seconds": {Kind: &plugin_pb.ConfigValue_Int64Value{Int64Value: 10}},
|
|
},
|
|
}, sender)
|
|
if err != nil {
|
|
t.Fatalf("detect returned err = %v", err)
|
|
}
|
|
if sender.proposals == nil {
|
|
t.Fatalf("expected proposals message")
|
|
}
|
|
if len(sender.proposals.Proposals) != 0 {
|
|
t.Fatalf("expected zero proposals, got %d", len(sender.proposals.Proposals))
|
|
}
|
|
if sender.complete == nil || !sender.complete.Success {
|
|
t.Fatalf("expected successful completion message")
|
|
}
|
|
if len(sender.events) == 0 {
|
|
t.Fatalf("expected detector activity events")
|
|
}
|
|
if !strings.Contains(sender.events[0].Message, "min interval") {
|
|
t.Fatalf("unexpected skip-by-interval message: %q", sender.events[0].Message)
|
|
}
|
|
}
|
|
|
|
func TestEmitVolumeBalanceDetectionDecisionTraceNoTasks(t *testing.T) {
|
|
sender := &recordingDetectionSender{}
|
|
config := balancetask.NewDefaultConfig()
|
|
config.ImbalanceThreshold = 0.2
|
|
config.MinServerCount = 2
|
|
|
|
metrics := []*workertypes.VolumeHealthMetrics{
|
|
{VolumeID: 1, Server: "server-a", DiskType: "hdd"},
|
|
{VolumeID: 2, Server: "server-a", DiskType: "hdd"},
|
|
{VolumeID: 3, Server: "server-b", DiskType: "hdd"},
|
|
{VolumeID: 4, Server: "server-b", DiskType: "hdd"},
|
|
}
|
|
|
|
if err := emitVolumeBalanceDetectionDecisionTrace(sender, metrics, nil, config, nil); err != nil {
|
|
t.Fatalf("emitVolumeBalanceDetectionDecisionTrace error: %v", err)
|
|
}
|
|
if len(sender.events) < 2 {
|
|
t.Fatalf("expected at least 2 detection events, got %d", len(sender.events))
|
|
}
|
|
if sender.events[0].Source != plugin_pb.ActivitySource_ACTIVITY_SOURCE_DETECTOR {
|
|
t.Fatalf("expected detector source, got %v", sender.events[0].Source)
|
|
}
|
|
if !strings.Contains(sender.events[0].Message, "BALANCE: No tasks created for 4 volumes") {
|
|
t.Fatalf("unexpected summary message: %q", sender.events[0].Message)
|
|
}
|
|
foundDiskTypeDecision := false
|
|
for _, event := range sender.events {
|
|
if strings.Contains(event.Message, "BALANCE [hdd]: No tasks created - cluster well balanced") {
|
|
foundDiskTypeDecision = true
|
|
break
|
|
}
|
|
}
|
|
if !foundDiskTypeDecision {
|
|
t.Fatalf("expected per-disk-type decision message")
|
|
}
|
|
}
|
|
|
|
func TestVolumeBalanceDescriptorOmitsExecutionTuningFields(t *testing.T) {
|
|
descriptor := NewVolumeBalanceHandler(nil).Descriptor()
|
|
if descriptor == nil || descriptor.WorkerConfigForm == nil {
|
|
t.Fatalf("expected worker config form in descriptor")
|
|
}
|
|
if workerConfigFormHasField(descriptor.WorkerConfigForm, "timeout_seconds") {
|
|
t.Fatalf("unexpected timeout_seconds in volume balance worker config form")
|
|
}
|
|
if workerConfigFormHasField(descriptor.WorkerConfigForm, "force_move") {
|
|
t.Fatalf("unexpected force_move in volume balance worker config form")
|
|
}
|
|
}
|
|
|
|
type recordingExecutionSender struct {
|
|
mu sync.Mutex
|
|
progress []*plugin_pb.JobProgressUpdate
|
|
completed *plugin_pb.JobCompleted
|
|
}
|
|
|
|
func (r *recordingExecutionSender) SendProgress(p *plugin_pb.JobProgressUpdate) error {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.progress = append(r.progress, proto.Clone(p).(*plugin_pb.JobProgressUpdate))
|
|
return nil
|
|
}
|
|
|
|
func (r *recordingExecutionSender) SendCompleted(c *plugin_pb.JobCompleted) error {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.completed = proto.Clone(c).(*plugin_pb.JobCompleted)
|
|
return nil
|
|
}
|
|
|
|
func TestBuildMoveTaskParams(t *testing.T) {
|
|
move := &worker_pb.BalanceMoveSpec{
|
|
VolumeId: 42,
|
|
SourceNode: "10.0.0.1:8080",
|
|
TargetNode: "10.0.0.2:8080",
|
|
Collection: "photos",
|
|
VolumeSize: 1024 * 1024,
|
|
}
|
|
|
|
outerParams := &worker_pb.BalanceTaskParams{
|
|
ForceMove: true,
|
|
TimeoutSeconds: 300,
|
|
}
|
|
params := buildMoveTaskParams(move, outerParams)
|
|
if params.VolumeId != 42 {
|
|
t.Fatalf("expected volume_id 42, got %d", params.VolumeId)
|
|
}
|
|
if params.Collection != "photos" {
|
|
t.Fatalf("expected collection photos, got %s", params.Collection)
|
|
}
|
|
if params.VolumeSize != 1024*1024 {
|
|
t.Fatalf("expected volume_size %d, got %d", 1024*1024, params.VolumeSize)
|
|
}
|
|
if len(params.Sources) != 1 || params.Sources[0].Node != "10.0.0.1:8080" {
|
|
t.Fatalf("unexpected sources: %+v", params.Sources)
|
|
}
|
|
if len(params.Targets) != 1 || params.Targets[0].Node != "10.0.0.2:8080" {
|
|
t.Fatalf("unexpected targets: %+v", params.Targets)
|
|
}
|
|
bp := params.GetBalanceParams()
|
|
if bp == nil {
|
|
t.Fatalf("expected balance params")
|
|
}
|
|
if bp.TimeoutSeconds != 300 {
|
|
t.Fatalf("expected timeout 300, got %d", bp.TimeoutSeconds)
|
|
}
|
|
if !bp.ForceMove {
|
|
t.Fatalf("expected force_move to be propagated from outer params")
|
|
}
|
|
}
|
|
|
|
func TestBuildMoveTaskParamsDefaultTimeout(t *testing.T) {
|
|
move := &worker_pb.BalanceMoveSpec{
|
|
VolumeId: 1,
|
|
SourceNode: "a:8080",
|
|
TargetNode: "b:8080",
|
|
}
|
|
params := buildMoveTaskParams(move, nil)
|
|
if params.GetBalanceParams().TimeoutSeconds != defaultBalanceTimeoutSeconds {
|
|
t.Fatalf("expected default timeout %d, got %d", defaultBalanceTimeoutSeconds, params.GetBalanceParams().TimeoutSeconds)
|
|
}
|
|
if params.GetBalanceParams().ForceMove {
|
|
t.Fatalf("expected force_move to default to false with nil outer params")
|
|
}
|
|
}
|
|
|
|
func TestExecuteDispatchesBatchPath(t *testing.T) {
|
|
// Build a job with batch moves in BalanceTaskParams
|
|
bp := &worker_pb.BalanceTaskParams{
|
|
TimeoutSeconds: 60,
|
|
MaxConcurrentMoves: 2,
|
|
Moves: []*worker_pb.BalanceMoveSpec{
|
|
{VolumeId: 1, SourceNode: "s1:8080", TargetNode: "t1:8080", Collection: "c1"},
|
|
{VolumeId: 2, SourceNode: "s2:8080", TargetNode: "t2:8080", Collection: "c1"},
|
|
},
|
|
}
|
|
taskParams := &worker_pb.TaskParams{
|
|
TaskId: "batch-1",
|
|
TaskParams: &worker_pb.TaskParams_BalanceParams{
|
|
BalanceParams: bp,
|
|
},
|
|
}
|
|
payload, err := proto.Marshal(taskParams)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
|
|
job := &plugin_pb.JobSpec{
|
|
JobId: "batch-job-1",
|
|
JobType: "volume_balance",
|
|
Parameters: map[string]*plugin_pb.ConfigValue{
|
|
"task_params_pb": {Kind: &plugin_pb.ConfigValue_BytesValue{BytesValue: payload}},
|
|
},
|
|
}
|
|
|
|
handler := NewVolumeBalanceHandler(nil)
|
|
sender := &recordingExecutionSender{}
|
|
|
|
// Execute will enter the batch path. It will fail because there's no real gRPC server,
|
|
// but we verify it sends the assigned progress and eventually a completion.
|
|
err = handler.Execute(context.Background(), &plugin_pb.ExecuteJobRequest{
|
|
Job: job,
|
|
}, sender)
|
|
|
|
// Expect an error since no real volume servers exist
|
|
// But verify the batch path was taken by checking the assigned message
|
|
sender.mu.Lock()
|
|
defer sender.mu.Unlock()
|
|
|
|
if len(sender.progress) == 0 {
|
|
t.Fatalf("expected progress messages from batch path")
|
|
}
|
|
|
|
// First progress should be "assigned" with batch info
|
|
first := sender.progress[0]
|
|
if first.Stage != "assigned" {
|
|
t.Fatalf("expected first stage 'assigned', got %q", first.Stage)
|
|
}
|
|
if !strings.Contains(first.Message, "batch") || !strings.Contains(first.Message, "2 moves") {
|
|
t.Fatalf("expected batch assigned message, got %q", first.Message)
|
|
}
|
|
|
|
// Should have a completion with failure details (since no servers)
|
|
if sender.completed == nil {
|
|
t.Fatalf("expected completion message")
|
|
}
|
|
if sender.completed.Success {
|
|
t.Fatalf("expected failure since no real gRPC servers")
|
|
}
|
|
// Should report 0 succeeded, 2 failed
|
|
if v, ok := sender.completed.Result.OutputValues["failed"]; !ok || v.GetInt64Value() != 2 {
|
|
t.Fatalf("expected 2 failed moves, got %+v", sender.completed.Result.OutputValues)
|
|
}
|
|
}
|
|
|
|
func TestExecuteSingleMovePathUnchanged(t *testing.T) {
|
|
// Build a single-move job (no batch moves)
|
|
taskParams := &worker_pb.TaskParams{
|
|
TaskId: "single-1",
|
|
VolumeId: 99,
|
|
Collection: "videos",
|
|
Sources: []*worker_pb.TaskSource{
|
|
{Node: "src:8080", VolumeId: 99},
|
|
},
|
|
Targets: []*worker_pb.TaskTarget{
|
|
{Node: "dst:8080", VolumeId: 99},
|
|
},
|
|
TaskParams: &worker_pb.TaskParams_BalanceParams{
|
|
BalanceParams: &worker_pb.BalanceTaskParams{
|
|
TimeoutSeconds: 60,
|
|
},
|
|
},
|
|
}
|
|
payload, err := proto.Marshal(taskParams)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
|
|
job := &plugin_pb.JobSpec{
|
|
JobId: "single-job-1",
|
|
JobType: "volume_balance",
|
|
Parameters: map[string]*plugin_pb.ConfigValue{
|
|
"task_params_pb": {Kind: &plugin_pb.ConfigValue_BytesValue{BytesValue: payload}},
|
|
},
|
|
}
|
|
|
|
handler := NewVolumeBalanceHandler(nil)
|
|
sender := &recordingExecutionSender{}
|
|
|
|
// Execute single-move path. Will fail on gRPC but verify it takes the single-move path.
|
|
_ = handler.Execute(context.Background(), &plugin_pb.ExecuteJobRequest{
|
|
Job: job,
|
|
}, sender)
|
|
|
|
sender.mu.Lock()
|
|
defer sender.mu.Unlock()
|
|
|
|
if len(sender.progress) == 0 {
|
|
t.Fatalf("expected progress messages")
|
|
}
|
|
|
|
// Single-move path sends "volume balance job accepted" not "batch volume balance"
|
|
first := sender.progress[0]
|
|
if first.Stage != "assigned" {
|
|
t.Fatalf("expected first stage 'assigned', got %q", first.Stage)
|
|
}
|
|
if strings.Contains(first.Message, "batch") {
|
|
t.Fatalf("single-move path should not mention batch, got %q", first.Message)
|
|
}
|
|
}
|
|
|
|
func workerConfigFormHasField(form *plugin_pb.ConfigForm, fieldName string) bool {
|
|
if form == nil {
|
|
return false
|
|
}
|
|
for _, section := range form.Sections {
|
|
if section == nil {
|
|
continue
|
|
}
|
|
for _, field := range section.Fields {
|
|
if field != nil && field.Name == fieldName {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|