mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
Merge pull request #849 from seaweedfs/feat/lab-heap-profile
Serve lab heap profiles on the existing status listener
This commit is contained in:
commit
8beecad1dc
7 files changed
+149
No files matched your search
@@ -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; }
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
//go:build !swblock_lab_bigvol
|
||||
|
||||
package volume
|
||||
|
||||
import "net/http"
|
||||
|
||||
func registerLabHeapProfile(_ *http.ServeMux) {}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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.
|
||||
Reference in new issue
Block a user