mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-08 21:08:27 +01:00
buildPolicyFromTaskConfigs passed a literal nil to vacuum, erasure_coding
and balance LoadConfigFromPersistence. Those functions look for their
LoadXTaskPolicy() accessor via a type assertion, which a nil interface can
never satisfy, so every call fell through to NewDefaultConfig() and the
policy came back with the compiled-in defaults - Enabled: true among them.
A task disabled on disk was therefore still scheduled, and the only trace
was a glog.V(1) "Using default ... configuration" line.
Thread the real ConfigPersistence through instead. There are two copies of
this function: the one in weed/admin/dash builds config.Policy on the
normal admin startup path and can simply take cp as its receiver, and the
one in weed/admin/maintenance is the fallback used when the config carries
no policy yet, which now receives the store from NewMaintenanceManager.
weed/admin/dash already imports weed/admin/maintenance, so the maintenance
side has to keep the duck-typed interface{} parameter that the task
loaders already use rather than importing the concrete type back.
The store is only handed over when a data directory is configured: an
unconfigured one has nothing to read, and a typed nil pointer would pass
the loaders' type assertion and then panic on first use.
Fixes #10874
141 lines
3.9 KiB
Go
141 lines
3.9 KiB
Go
package maintenance
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestMaintenanceManager_ErrorHandling(t *testing.T) {
|
|
config := DefaultMaintenanceConfig()
|
|
config.ScanIntervalSeconds = 1 // Short interval for testing (1 second)
|
|
|
|
manager := NewMaintenanceManager(nil, config, nil)
|
|
|
|
// Test initial state
|
|
if manager.errorCount != 0 {
|
|
t.Errorf("Expected initial error count to be 0, got %d", manager.errorCount)
|
|
}
|
|
|
|
if manager.backoffDelay != time.Second {
|
|
t.Errorf("Expected initial backoff delay to be 1s, got %v", manager.backoffDelay)
|
|
}
|
|
|
|
// Test error handling
|
|
err := errors.New("dial tcp [::1]:19333: connect: connection refused")
|
|
manager.handleScanError(err)
|
|
|
|
if manager.errorCount != 1 {
|
|
t.Errorf("Expected error count to be 1, got %d", manager.errorCount)
|
|
}
|
|
|
|
if manager.lastError != err {
|
|
t.Errorf("Expected last error to be set")
|
|
}
|
|
|
|
// Test exponential backoff
|
|
initialDelay := manager.backoffDelay
|
|
manager.handleScanError(err)
|
|
|
|
if manager.backoffDelay != initialDelay*2 {
|
|
t.Errorf("Expected backoff delay to double, got %v", manager.backoffDelay)
|
|
}
|
|
|
|
if manager.errorCount != 2 {
|
|
t.Errorf("Expected error count to be 2, got %d", manager.errorCount)
|
|
}
|
|
|
|
// Test backoff cap
|
|
for i := 0; i < 10; i++ {
|
|
manager.handleScanError(err)
|
|
}
|
|
|
|
if manager.backoffDelay > 5*time.Minute {
|
|
t.Errorf("Expected backoff delay to be capped at 5 minutes, got %v", manager.backoffDelay)
|
|
}
|
|
|
|
// Test error reset
|
|
manager.resetErrorTracking()
|
|
|
|
if manager.errorCount != 0 {
|
|
t.Errorf("Expected error count to be reset to 0, got %d", manager.errorCount)
|
|
}
|
|
|
|
if manager.backoffDelay != time.Second {
|
|
t.Errorf("Expected backoff delay to be reset to 1s, got %v", manager.backoffDelay)
|
|
}
|
|
|
|
if manager.lastError != nil {
|
|
t.Errorf("Expected last error to be reset to nil")
|
|
}
|
|
}
|
|
|
|
func TestIsConnectionError(t *testing.T) {
|
|
tests := []struct {
|
|
err error
|
|
expected bool
|
|
}{
|
|
{nil, false},
|
|
{errors.New("connection refused"), true},
|
|
{errors.New("dial tcp [::1]:19333: connect: connection refused"), true},
|
|
{errors.New("connection error: desc = \"transport: Error while dialing\""), true},
|
|
{errors.New("connection timeout"), true},
|
|
{errors.New("no route to host"), true},
|
|
{errors.New("network unreachable"), true},
|
|
{errors.New("some other error"), false},
|
|
{errors.New("invalid argument"), false},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
result := isConnectionError(test.err)
|
|
if result != test.expected {
|
|
t.Errorf("For error %v, expected %v, got %v", test.err, test.expected, result)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMaintenanceManager_GetErrorState(t *testing.T) {
|
|
config := DefaultMaintenanceConfig()
|
|
manager := NewMaintenanceManager(nil, config, nil)
|
|
|
|
// Test initial state
|
|
errorCount, lastError, backoffDelay := manager.GetErrorState()
|
|
if errorCount != 0 || lastError != nil || backoffDelay != time.Second {
|
|
t.Errorf("Expected initial state to be clean")
|
|
}
|
|
|
|
// Add some errors
|
|
err := errors.New("test error")
|
|
manager.handleScanError(err)
|
|
manager.handleScanError(err)
|
|
|
|
errorCount, lastError, backoffDelay = manager.GetErrorState()
|
|
if errorCount != 2 || lastError != err || backoffDelay != 2*time.Second {
|
|
t.Errorf("Expected error state to be tracked correctly: count=%d, err=%v, delay=%v",
|
|
errorCount, lastError, backoffDelay)
|
|
}
|
|
}
|
|
|
|
func TestMaintenanceManager_LogThrottling(t *testing.T) {
|
|
config := DefaultMaintenanceConfig()
|
|
manager := NewMaintenanceManager(nil, config, nil)
|
|
|
|
// This is a basic test to ensure the error handling doesn't panic
|
|
// In practice, you'd want to capture log output to verify throttling
|
|
err := errors.New("test error")
|
|
|
|
// Generate many errors to test throttling
|
|
for i := 0; i < 25; i++ {
|
|
manager.handleScanError(err)
|
|
}
|
|
|
|
// Should not panic and should have capped backoff
|
|
if manager.backoffDelay > 5*time.Minute {
|
|
t.Errorf("Expected backoff to be capped at 5 minutes")
|
|
}
|
|
|
|
if manager.errorCount != 25 {
|
|
t.Errorf("Expected error count to be 25, got %d", manager.errorCount)
|
|
}
|
|
}
|