diff --git a/seaweed-block/.github/scripts/lab-tag-check.sh b/seaweed-block/.github/scripts/lab-tag-check.sh index e07cd4e1cd..c2b2215195 100644 --- a/seaweed-block/.github/scripts/lab-tag-check.sh +++ b/seaweed-block/.github/scripts/lab-tag-check.sh @@ -16,6 +16,10 @@ tag=swblock_lab_bigvol allowed='.github/scripts/lab-tag-check-test.sh .github/scripts/lab-tag-check.sh .github/workflows/test.yml +core/host/volume/status_heap_lab.go +core/host/volume/status_heap_lab_test.go +core/host/volume/status_heap_release.go +core/host/volume/status_heap_release_test.go core/frontend/durable/headroom.go core/frontend/durable/headroom_lab_test.go core/geometry/volume_size.go @@ -24,6 +28,7 @@ core/geometry/volume_size_lab_test.go core/geometry/volume_size_release.go core/geometry/volume_size_release_test.go docs/design/invariants.md +docs/lab-heap-profiles.md internal/buildinfo/buildinfo.go scripts/refuse-lab-binary.sh' fail() { echo "lab-tag: $*" >&2; exit 1; } diff --git a/seaweed-block/core/host/volume/status_heap_lab.go b/seaweed-block/core/host/volume/status_heap_lab.go new file mode 100644 index 0000000000..6fc6e22f7b --- /dev/null +++ b/seaweed-block/core/host/volume/status_heap_lab.go @@ -0,0 +1,21 @@ +//go:build swblock_lab_bigvol + +package volume + +import ( + "net/http" + "net/http/pprof" +) + +// registerLabHeapProfile uses the existing listener, but never exposes heap +// contents to a non-loopback client, even when status has an external bind. +func registerLabHeapProfile(mux *http.ServeMux) { + handler := pprof.Handler("heap") + mux.HandleFunc("GET /debug/pprof/heap", func(w http.ResponseWriter, r *http.Request) { + if !isLoopbackRemote(r.RemoteAddr) { + http.Error(w, "heap profile restricted to loopback", http.StatusForbidden) + return + } + handler.ServeHTTP(w, r) + }) +} diff --git a/seaweed-block/core/host/volume/status_heap_lab_test.go b/seaweed-block/core/host/volume/status_heap_lab_test.go new file mode 100644 index 0000000000..d48c3f7ac5 --- /dev/null +++ b/seaweed-block/core/host/volume/status_heap_lab_test.go @@ -0,0 +1,63 @@ +//go:build swblock_lab_bigvol + +package volume + +import ( + "compress/gzip" + "context" + "io" + "net/http" + "net/http/httptest" + "testing" + "time" +) + +func TestLabHeapProfileGET(t *testing.T) { + s := NewStatusServer(NewAdapterProjectionView(stubProjector{}, "v1", "r1", nil)) + addr, err := s.Start("127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + _ = s.Close(ctx) + }) + client := &http.Client{Timeout: 10 * time.Second} + resp, err := client.Get("http://" + addr + "/debug/pprof/heap?gc=1") + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + if resp.StatusCode != 200 { + t.Fatalf("heap GET status %d", resp.StatusCode) + } + zr, err := gzip.NewReader(resp.Body) + if err != nil { + t.Fatal(err) + } + defer zr.Close() + data, err := io.ReadAll(zr) + if err != nil || len(data) == 0 { + t.Fatalf("profile: bytes=%d err=%v", len(data), err) + } +} + +func TestLabHeapProfileRejectsRemoteAndPOST(t *testing.T) { + mux := http.NewServeMux() + registerLabHeapProfile(mux) + remote := httptest.NewRequest(http.MethodGet, "/debug/pprof/heap?gc=1", nil) + remote.RemoteAddr = "192.0.2.1:1234" + denied := httptest.NewRecorder() + mux.ServeHTTP(denied, remote) + if denied.Code != http.StatusForbidden { + t.Fatalf("remote heap status %d", denied.Code) + } + post := httptest.NewRequest(http.MethodPost, "/debug/pprof/heap", nil) + post.RemoteAddr = "127.0.0.1:1234" + rr := httptest.NewRecorder() + mux.ServeHTTP(rr, post) + if rr.Code != http.StatusMethodNotAllowed { + t.Fatalf("POST heap status %d", rr.Code) + } +} diff --git a/seaweed-block/core/host/volume/status_heap_release.go b/seaweed-block/core/host/volume/status_heap_release.go new file mode 100644 index 0000000000..b15d2036ec --- /dev/null +++ b/seaweed-block/core/host/volume/status_heap_release.go @@ -0,0 +1,7 @@ +//go:build !swblock_lab_bigvol + +package volume + +import "net/http" + +func registerLabHeapProfile(_ *http.ServeMux) {} diff --git a/seaweed-block/core/host/volume/status_heap_release_test.go b/seaweed-block/core/host/volume/status_heap_release_test.go new file mode 100644 index 0000000000..587b78819f --- /dev/null +++ b/seaweed-block/core/host/volume/status_heap_release_test.go @@ -0,0 +1,34 @@ +//go:build !swblock_lab_bigvol + +package volume + +import ( + "context" + "net/http" + "testing" + "time" +) + +func TestHeapProfileRouteAbsentFromRelease(t *testing.T) { + s := NewStatusServer(NewAdapterProjectionView(stubProjector{}, "v1", "r1", nil)) + addr, err := s.Start("127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + _ = s.Close(ctx) + }) + client := &http.Client{Timeout: 5 * time.Second} + for _, path := range []string{"/debug/pprof/heap", "/debug/pprof/", "/debug/pprof/profile"} { + resp, err := client.Get("http://" + addr + path) + if err != nil { + t.Fatal(err) + } + resp.Body.Close() + if resp.StatusCode != http.StatusNotFound { + t.Fatalf("release route %s returned %d, want 404", path, resp.StatusCode) + } + } +} diff --git a/seaweed-block/core/host/volume/status_server.go b/seaweed-block/core/host/volume/status_server.go index 7e82cf51a3..4dd71e8ff0 100644 --- a/seaweed-block/core/host/volume/status_server.go +++ b/seaweed-block/core/host/volume/status_server.go @@ -390,6 +390,7 @@ func (s *StatusServer) Start(addr string) (string, error) { s.addr = ln.Addr().String() mux := http.NewServeMux() + registerLabHeapProfile(mux) mux.HandleFunc("/status", s.handleStatus) mux.HandleFunc("/status/peers", s.handleStatusPeers) mux.HandleFunc("/status/durable", s.handleStatusDurable) diff --git a/seaweed-block/docs/lab-heap-profiles.md b/seaweed-block/docs/lab-heap-profiles.md new file mode 100644 index 0000000000..d418f14bb8 --- /dev/null +++ b/seaweed-block/docs/lab-heap-profiles.md @@ -0,0 +1,18 @@ +# Lab heap profiles + +Only a holder built with `-tags swblock_lab_bigvol` serves +`GET /debug/pprof/heap` on its existing `--status-addr` listener. +Release builds return 404. There is no additional flag, signal handler or +listener. The route always refuses non-loopback clients, including when the +status listener was explicitly allowed to bind externally. + +For current live allocations, fetch `/debug/pprof/heap?gc=1`: the standard +Go heap handler runs a full GC first and returns a binary pprof profile. +For example, save the response using `curl --fail --max-time 30 http://127.0.0.1:9181/debug/pprof/heap?gc=1 -o heap.pprof`, substituting the +holder's actual status address. Inspect it with +`go tool pprof -sample_index=inuse_space -top -cum heap.pprof`. + +GC and collection pause and perturb the workload; record the sample time in +performance evidence. This small diagnostic smoke does not measure large-volume +memory use. The standard handler's `debug` and `gc` query options retain their +normal meaning. CPU profiling via SIGUSR1 remains independent.