Files
s3/weed/storage/store_disk_space_test.go
zooqueen dd74d3e6d6 Replace AGPL MinIO fork with Apache-2.0 SeaweedFS fork; rebrand CLI weed->s3
hanzoai/s3 was an AGPL-3.0 MinIO fork whose network-copyleft blocks embedding
and reselling as a managed service. Replace it wholesale with a fork of
SeaweedFS (github.com/seaweedfs/seaweedfs, Apache-2.0, Go) which ships a
built-in S3 gateway + Filer.

- License: AGPL-3.0 -> Apache-2.0 (upstream LICENSE kept verbatim, incl.
  "Copyright 2025 Chris Lu"); NOTICE added crediting SeaweedFS. No AGPL/BUSL/
  SSPL/GPL or enterprise ("ee") files remain.
- Module: go.mod path set to github.com/hanzoai/s3 with a self-replace
  (replace github.com/seaweedfs/seaweedfs => ./) so the ~1800 internal Go files
  keep their upstream import paths with zero churn. Internal identifiers and
  import paths are intentionally NOT renamed.
- CLI rebrand: the binary is built as `s3` (was `weed`); root usage, per-command
  help/usage/flag strings, shell autocomplete program name, and the product name
  in CLI output now read "Hanzo S3" / `s3`. Users never see `weed`.
  (e.g. `s3 server`, `s3 filer`, `s3 master`, `s3 volume`, `s3 s3`, `s3 mount`.)
- Build: `make` (or `go build -o s3 ./weed`) builds the `s3` binary green;
  `cmd/weed-db` and `cmd/weed-sql` also build; `go vet ./weed/command/` clean.
- README/LLM.md rewritten for Hanzo S3; AGENTS.md/CLAUDE.md symlinks preserved.
- Dropped a 33MB prebuilt Mach-O `stress` binary upstream had committed; `.gitignore`
  updated to ignore the `s3` build output.
2026-06-18 23:41:25 -07:00

195 lines
5.6 KiB
Go

package storage
import (
"testing"
"github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding"
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
"github.com/seaweedfs/seaweedfs/weed/storage/types"
)
func TestHasFreeDiskLocation(t *testing.T) {
testCases := []struct {
name string
isDiskSpaceLow bool
maxVolumeCount int32
currentVolumes int
expected bool
}{
{
name: "low disk space prevents allocation",
isDiskSpaceLow: true,
maxVolumeCount: 10,
currentVolumes: 5,
expected: false,
},
{
name: "normal disk space and available volume count allows allocation",
isDiskSpaceLow: false,
maxVolumeCount: 10,
currentVolumes: 5,
expected: true,
},
{
name: "volume count at max prevents allocation",
isDiskSpaceLow: false,
maxVolumeCount: 2,
currentVolumes: 2,
expected: false,
},
{
name: "volume count over max prevents allocation",
isDiskSpaceLow: false,
maxVolumeCount: 2,
currentVolumes: 3,
expected: false,
},
{
name: "volume count just under max allows allocation",
isDiskSpaceLow: false,
maxVolumeCount: 2,
currentVolumes: 1,
expected: true,
},
{
name: "max volume count is 0 allows allocation",
isDiskSpaceLow: false,
maxVolumeCount: 0,
currentVolumes: 100,
expected: true,
},
{
name: "max volume count is 0 but low disk space prevents allocation",
isDiskSpaceLow: true,
maxVolumeCount: 0,
currentVolumes: 100,
expected: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// setup
diskLocation := &DiskLocation{
volumes: make(map[needle.VolumeId]*Volume),
MaxVolumeCount: tc.maxVolumeCount,
}
diskLocation.isDiskSpaceLow.Store(tc.isDiskSpaceLow)
for i := 0; i < tc.currentVolumes; i++ {
diskLocation.volumes[needle.VolumeId(i+1)] = &Volume{}
}
store := &Store{
Locations: []*DiskLocation{diskLocation},
}
// act
result := store.hasFreeDiskLocation(diskLocation)
// assert
if result != tc.expected {
t.Errorf("Expected hasFreeDiskLocation() = %v; want %v for volumes:%d/%d, lowSpace:%v",
result, tc.expected, len(diskLocation.volumes), diskLocation.MaxVolumeCount, diskLocation.isDiskSpaceLow.Load())
}
})
}
}
func newTestLocation(maxCount int32, isDiskLow bool, volCount int) *DiskLocation {
location := &DiskLocation{
volumes: make(map[needle.VolumeId]*Volume),
ecVolumes: make(map[needle.VolumeId]*erasure_coding.EcVolume),
MaxVolumeCount: maxCount,
DiskType: types.ToDiskType("hdd"),
}
location.isDiskSpaceLow.Store(isDiskLow)
for i := 1; i <= volCount; i++ {
location.volumes[needle.VolumeId(i)] = &Volume{}
}
return location
}
func TestCollectHeartbeatRespectsLowDiskSpace(t *testing.T) {
diskType := types.ToDiskType("hdd")
t.Run("low disk space", func(t *testing.T) {
location := newTestLocation(10, true, 3)
store := &Store{Locations: []*DiskLocation{location}}
hb := store.CollectHeartbeat()
if got := hb.MaxVolumeCounts[string(diskType)]; got != 3 {
t.Errorf("expected low disk space to cap max volume count to used slots, got %d", got)
}
})
t.Run("normal disk space", func(t *testing.T) {
location := newTestLocation(10, false, 3)
store := &Store{Locations: []*DiskLocation{location}}
hb := store.CollectHeartbeat()
if got := hb.MaxVolumeCounts[string(diskType)]; got != 10 {
t.Errorf("expected normal disk space to report configured max volume count, got %d", got)
}
})
t.Run("low disk space zero volumes", func(t *testing.T) {
location := newTestLocation(10, true, 0)
store := &Store{Locations: []*DiskLocation{location}}
hb := store.CollectHeartbeat()
if got := hb.MaxVolumeCounts[string(diskType)]; got != 0 {
t.Errorf("expected zero volumes to report zero capacity, got %d", got)
}
})
t.Run("low disk space with ec shards", func(t *testing.T) {
location := newTestLocation(10, true, 3)
ecVolume := &erasure_coding.EcVolume{VolumeId: 1}
const shardCount = 15
for i := 0; i < shardCount; i++ {
ecVolume.Shards = append(ecVolume.Shards, &erasure_coding.EcVolumeShard{
ShardId: erasure_coding.ShardId(i),
})
}
location.ecVolumes[ecVolume.VolumeId] = ecVolume
store := &Store{Locations: []*DiskLocation{location}}
hb := store.CollectHeartbeat()
expectedSlots := len(location.volumes) + (shardCount+erasure_coding.DataShardsCount-1)/erasure_coding.DataShardsCount
if got := hb.MaxVolumeCounts[string(diskType)]; got != uint32(expectedSlots) {
t.Errorf("expected low disk space to include ec shard contribution, got %d want %d", got, expectedSlots)
}
})
t.Run("low disk space with multiple ec volumes", func(t *testing.T) {
location := newTestLocation(10, true, 2)
totalShardCount := 0
addEcVolume := func(vid needle.VolumeId, shardCount int) {
ecVolume := &erasure_coding.EcVolume{VolumeId: vid}
for i := 0; i < shardCount; i++ {
ecVolume.Shards = append(ecVolume.Shards, &erasure_coding.EcVolumeShard{
ShardId: erasure_coding.ShardId(i),
})
}
location.ecVolumes[vid] = ecVolume
totalShardCount += shardCount
}
addEcVolume(1, 12)
addEcVolume(2, 6)
store := &Store{Locations: []*DiskLocation{location}}
hb := store.CollectHeartbeat()
expectedSlots := len(location.volumes)
expectedSlots += (totalShardCount + erasure_coding.DataShardsCount - 1) / erasure_coding.DataShardsCount
if got := hb.MaxVolumeCounts[string(diskType)]; got != uint32(expectedSlots) {
t.Errorf("expected multiple ec volumes to be counted, got %d want %d", got, expectedSlots)
}
})
}