diff --git a/seaweed-block/cmd/blockvolume/head_size_integration_test.go b/seaweed-block/cmd/blockvolume/head_size_integration_test.go index bfb8c84446..c7add5f4a3 100644 --- a/seaweed-block/cmd/blockvolume/head_size_integration_test.go +++ b/seaweed-block/cmd/blockvolume/head_size_integration_test.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "net/http" + "net/url" "os" "path/filepath" "strconv" @@ -27,29 +28,51 @@ import ( // room for is recorded as refused by the blockvolume's headroom check, never // shrunk or bypassed. The JSON report goes to HEADSZ_REPORT. // +// HEADSZ_SNAPSHOT_TIMEOUT is the snapshot command's --timeout (default 60s; +// the server caps it at its own operation deadline). HEADSZ_ATTEMPTS (default +// 1) retries a failed cut with the same request id; every attempt is +// recorded with the volume's chunk-key count in the filer after it, so a +// retry shows whether an earlier attempt's uploads were kept. A cut that never +// completes is recorded as cut_not_completed, not failed. +// // The filer runs on its default store (leveldb2) under the test's directory; // a filer.toml anywhere weed would read one fails the test before any start, // so the store measured is the one named in the report. const headSegmentBytes = 1 << 30 +// headSzAttempt is one snapshot command for one cut: a failed attempt is +// resent with the same request id, as the command's refusal says. +type headSzAttempt struct { + Attempt int `json:"attempt"` + ExitCode int `json:"exit_code"` + Code string `json:"code,omitempty"` + Message string `json:"message,omitempty"` + WallMs float64 `json:"wall_ms"` + KeysAfter int `json:"chunk_keys_after"` + LastCutTotalMs float64 `json:"last_cut_total_ms"` + LastCutChunks int `json:"last_cut_uploaded_chunks"` +} + type headSzCut struct { - Cut int `json:"cut"` - ChangedBlocks int `json:"changed_blocks"` - HeadBytes int `json:"head_bytes"` - HeadSegments int `json:"head_segments"` - CutTotalMs float64 `json:"cut_total_ms"` - BarrierPauseMs float64 `json:"cut_barrier_pause_ms"` - UploadedChunks int `json:"uploaded_chunks"` - UploadedBytes int64 `json:"uploaded_bytes"` - SnapshotMs float64 `json:"snapshot_command_ms"` + Cut int `json:"cut"` + Completed bool `json:"completed"` + Attempts []headSzAttempt `json:"attempts"` + ChangedBlocks int `json:"changed_blocks"` + HeadBytes int `json:"head_bytes"` + HeadSegments int `json:"head_segments"` + CutTotalMs float64 `json:"cut_total_ms"` + BarrierPauseMs float64 `json:"cut_barrier_pause_ms"` + UploadedChunks int `json:"uploaded_chunks"` + UploadedBytes int64 `json:"uploaded_bytes"` + SnapshotMs float64 `json:"snapshot_command_ms"` } type headSzSize struct { Size string `json:"size"` Bytes uint64 `json:"bytes"` Segments int `json:"segments"` - Result string `json:"result"` // measured | refused_headroom + Result string `json:"result"` // measured | refused_headroom | cut_not_completed Refusal string `json:"refusal,omitempty"` WriteMs float64 `json:"write_ms,omitempty"` Cuts []headSzCut `json:"cuts,omitempty"` @@ -103,6 +126,20 @@ func TestHeadSizeAgainstVolumeSize(t *testing.T) { } changeCuts = n } + cfg := headSzConfig{snapshotTimeout: "60s", attempts: 1} + if v := os.Getenv("HEADSZ_SNAPSHOT_TIMEOUT"); v != "" { + if _, err := time.ParseDuration(v); err != nil { + t.Fatalf("HEADSZ_SNAPSHOT_TIMEOUT=%q: %v", v, err) + } + cfg.snapshotTimeout = v + } + if v := os.Getenv("HEADSZ_ATTEMPTS"); v != "" { + n, err := strconv.Atoi(v) + if err != nil || n < 1 { + t.Fatalf("HEADSZ_ATTEMPTS=%q", v) + } + cfg.attempts = n + } h := newPlannedHandoffHarness(t) clusterDir := filepath.Join(h.dir, "weed") if err := os.MkdirAll(clusterDir, 0o700); err != nil { @@ -142,12 +179,20 @@ func TestHeadSizeAgainstVolumeSize(t *testing.T) { master.waitLog(t, 30*time.Second, "blockmaster: lock acquired, reloaded=") for _, s := range sizes { - rep.Sizes = append(rep.Sizes, headSzOne(t, env, s, changeCuts)) + rep.Sizes = append(rep.Sizes, headSzOne(t, env, cfg, s, changeCuts)) } } +// headSzConfig: HEADSZ_SNAPSHOT_TIMEOUT is the snapshot command's --timeout +// (the server caps it at its own operation deadline); HEADSZ_ATTEMPTS is how +// many times one cut is tried before it is recorded as not completed. +type headSzConfig struct { + snapshotTimeout string + attempts int +} + // headSzOne measures one size; its blockvolume is stopped at the end. -func headSzOne(t *testing.T, env *handoffEnv, size string, changeCuts int) headSzSize { +func headSzOne(t *testing.T, env *handoffEnv, cfg headSzConfig, size string, changeCuts int) headSzSize { t.Helper() bytes := headSzParse(t, size) id := "h" + strings.ToLower(strings.TrimSpace(size)) @@ -194,7 +239,12 @@ func headSzOne(t *testing.T, env *handoffEnv, size string, changeCuts int) headS c.flush(t) changed = 1 } - row.Cuts = append(row.Cuts, headSzCutOnce(t, env, b, cut, changed)) + r := headSzCutOnce(t, env, cfg, b, cut, changed) + row.Cuts = append(row.Cuts, r) + if !r.Completed { + row.Result = "cut_not_completed" + return row + } } row.Result = "measured" if row.Segments > 0 && len(row.Cuts) > 0 { @@ -203,16 +253,39 @@ func headSzOne(t *testing.T, env *handoffEnv, size string, changeCuts int) headS return row } -// headSzCutOnce takes one snapshot and reads what it wrote: HEAD from the -// filer, the cut from the volume's durable status. -func headSzCutOnce(t *testing.T, env *handoffEnv, b bvHost, cut, changed int) headSzCut { +// headSzCutOnce takes one snapshot, retried with the same request id up to +// cfg.attempts times, and reads what it wrote: HEAD from the filer, the cut +// from the volume's durable status. After every attempt it counts the +// volume's chunk keys in the filer, so a retry shows whether the chunks an +// earlier attempt uploaded are still there. +func headSzCutOnce(t *testing.T, env *handoffEnv, cfg headSzConfig, b bvHost, cut, changed int) headSzCut { t.Helper() r := headSzCut{Cut: cut, ChangedBlocks: changed} start := time.Now() - out, errOut := env.h.command(t, "ops", "chunk", "snapshot", "--volume", b.volume, "--status-addr", b.status, - "--name", fmt.Sprintf("headsz-%d", cut), "--request-id", fmt.Sprintf("headsz-%s-%d", b.volume, cut)) + for a := 1; a <= cfg.attempts; a++ { + as := time.Now() + out, _, code := env.h.run(t, "ops", "chunk", "snapshot", "--volume", b.volume, "--status-addr", b.status, "--timeout", cfg.snapshotTimeout, + "--name", fmt.Sprintf("headsz-%d", cut), "--request-id", fmt.Sprintf("headsz-%s-%d", b.volume, cut)) + at := headSzAttempt{Attempt: a, ExitCode: code, WallMs: ms(time.Since(as)), KeysAfter: headSzCountDir(t, env, env.chunkPath+"/"+b.volume+"/keys")} + if code != 0 { + var e struct{ Code, Message string } + if json.Unmarshal([]byte(strings.TrimSpace(out)), &e) == nil { + at.Code, at.Message = e.Code, e.Message + } else { + at.Message = strings.TrimSpace(out) + } + } + at.LastCutTotalMs, at.LastCutChunks = headSzLastCut(t, b) + r.Attempts = append(r.Attempts, at) + if code == 0 { + r.Completed = true + break + } + } r.SnapshotMs = ms(time.Since(start)) - _, _ = out, errOut + if !r.Completed { + return r + } var st struct { Volumes []struct { VolumeID string @@ -249,6 +322,69 @@ func headSzCutOnce(t *testing.T, env *handoffEnv, b bvHost, cut, changed int) he return r } +// headSzLastCut reads the volume's last cut total (ms) and uploaded chunks +// from its durable status; zeros when there is no last cut yet. +func headSzLastCut(t *testing.T, b bvHost) (float64, int) { + t.Helper() + var st struct { + Volumes []struct { + VolumeID string + Chunk *struct { + Stats struct { + LastCut *struct { + TotalNs int64 + UploadedChunks int + } + } + } + } + } + if err := json.Unmarshal(headSzGet(t, "http://"+b.status+"/status/durable?volume="+b.volume, 4<<20), &st); err != nil { + t.Fatalf("durable status of %s: %v", b.volume, err) + } + for _, s := range st.Volumes { + if s.VolumeID == b.volume && s.Chunk != nil && s.Chunk.Stats.LastCut != nil { + return float64(s.Chunk.Stats.LastCut.TotalNs) / 1e6, s.Chunk.Stats.LastCut.UploadedChunks + } + } + return 0, 0 +} + +// headSzCountDir counts the entries of one filer directory by its JSON +// listing; a directory that does not exist yet counts 0. +func headSzCountDir(t *testing.T, env *handoffEnv, dir string) int { + t.Helper() + c := &http.Client{Timeout: 30 * time.Second} + n, last := 0, "" + for { + req, _ := http.NewRequest(http.MethodGet, "http://"+env.cluster.filerHTTP+dir+"/?limit=1000&lastFileName="+url.QueryEscape(last), nil) + req.Header.Set("Accept", "application/json") + resp, err := c.Do(req) + if err != nil { + t.Fatalf("filer list %s: %v", dir, err) + } + if resp.StatusCode == http.StatusNotFound { + resp.Body.Close() + return n + } + var page struct { + Entries []struct{ FullPath string } + ShouldDisplayLoadMore bool + LastFileName string + } + err = json.NewDecoder(resp.Body).Decode(&page) + resp.Body.Close() + if err != nil { + t.Fatalf("filer list %s: %v", dir, err) + } + n += len(page.Entries) + if !page.ShouldDisplayLoadMore || page.LastFileName == "" { + return n + } + last = page.LastFileName + } +} + func headSzGet(t *testing.T, u string, limit int64) []byte { t.Helper() c := &http.Client{Timeout: 30 * time.Second}