Files
s3/weed/storage/volume_mark_writable_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

88 lines
3.1 KiB
Go

package storage
import (
"errors"
"os"
"testing"
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
)
// A volume booted with .vif ReadOnly=true used to come back stuck: load()
// opens .idx as O_RDONLY and builds a SortedFileNeedleMap whose Put returns
// os.ErrInvalid, and MarkVolumeWritable only flipped the in-memory flag and
// rewrote .vif. Subsequent writes failed at v.nm.Put.
func TestMarkVolumeWritable_ReopensPersistedReadOnly(t *testing.T) {
dir := t.TempDir()
v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0)
if err != nil {
t.Fatalf("create volume: %v", err)
}
if _, _, _, err := v.writeNeedle2(newRandomNeedle(1), true, false); err != nil {
t.Fatalf("initial write: %v", err)
}
// Persist read-only state into .vif, then simulate a server restart by
// closing and re-opening the volume from the same directory.
v.PersistReadOnly(true)
v.Close()
v2, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0)
if err != nil {
t.Fatalf("reload volume: %v", err)
}
defer v2.Close()
if !v2.noWriteOrDelete {
t.Fatalf("reloaded volume should be in noWriteOrDelete=true after .vif ReadOnly=true")
}
if _, ok := v2.nm.(*SortedFileNeedleMap); !ok {
t.Fatalf("reloaded readonly volume should use SortedFileNeedleMap, got %T", v2.nm)
}
// Pre-fix behaviour: SortedFileNeedleMap.Put returns os.ErrInvalid even
// once noWriteOrDelete is cleared. Confirm the failure mode the issue
// describes — flipping only the flag is not enough.
v2.noWriteOrDelete = false
_, _, _, writeErr := v2.writeNeedle2(newRandomNeedle(2), true, false)
if !errors.Is(writeErr, os.ErrInvalid) {
t.Fatalf("expected write through SortedFileNeedleMap to fail with os.ErrInvalid, got %v", writeErr)
}
v2.noWriteOrDelete = true // restore so reopenIdxForWrite reflects the real entry condition
if err := v2.reopenIdxForWrite(); err != nil {
t.Fatalf("reopenIdxForWrite: %v", err)
}
if _, stillSorted := v2.nm.(*SortedFileNeedleMap); stillSorted {
t.Fatalf("reopenIdxForWrite left SortedFileNeedleMap in place")
}
v2.noWriteOrDelete = false
if _, _, _, err := v2.writeNeedle2(newRandomNeedle(3), true, false); err != nil {
t.Fatalf("write after reopen: %v", err)
}
}
// reopenIdxForWrite must be a no-op when the volume already has a writable
// needle map — otherwise repeated MarkVolumeWritable calls would churn the
// index file handle for no reason.
func TestReopenIdxForWrite_NoopWhenAlreadyWritable(t *testing.T) {
dir := t.TempDir()
v, err := NewVolume(dir, dir, "", 2, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0)
if err != nil {
t.Fatalf("create volume: %v", err)
}
defer v.Close()
before := v.nm
if err := v.reopenIdxForWrite(); err != nil {
t.Fatalf("reopenIdxForWrite on writable volume: %v", err)
}
if v.nm != before {
t.Fatalf("reopenIdxForWrite replaced nm on a writable volume (before=%p after=%p)", before, v.nm)
}
}