diff --git a/seaweed-block/core/frontend/durable/provider_chunk.go b/seaweed-block/core/frontend/durable/provider_chunk.go index fb836d1f92..8b40e2f64a 100644 --- a/seaweed-block/core/frontend/durable/provider_chunk.go +++ b/seaweed-block/core/frontend/durable/provider_chunk.go @@ -517,6 +517,7 @@ func (p *DurableProvider) newChunkLayer(ctx context.Context, volumeID string, h } var remote storage.ChunkRemote = &layoutBoundRemote{closeBoundRemote: bound, keyer: keyer} cfg := storage.ChunkLayerConfig{ + VolumeID: volumeID, OnPublishSettled: p.publishSettledHook(volumeID), ChunkBlocks: p.cfg.ChunkBlocks, RestoreChunkBytes: uint64(ws.BlockSize()) * uint64(p.cfg.ChunkBlocks), diff --git a/seaweed-block/core/storage/chunk_cut_logging_test.go b/seaweed-block/core/storage/chunk_cut_logging_test.go new file mode 100644 index 0000000000..ab9a3558ec --- /dev/null +++ b/seaweed-block/core/storage/chunk_cut_logging_test.go @@ -0,0 +1,142 @@ +package storage + +import ( + "bytes" + "context" + "errors" + "log" + "regexp" + "strconv" + "strings" + "testing" + "time" + + "github.com/seaweedfs/seaweed-block/internal/logging" +) + +func TestCutPhaseTimingAtDefaultInfoKeepsTheMeasuredHeadTime(t *testing.T) { + old, flags := log.Writer(), log.Flags() + t.Cleanup(func() { _ = logging.Configure("info"); log.SetOutput(old); log.SetFlags(flags) }) + var out bytes.Buffer + log.SetOutput(&out) + s, remote, layer, writes := precopyLayer(t, 1024, 0) + layer.cfg.VolumeID = "cut-log-volume" + layer.remote = &fixedSpanRemote{fakeChunkRemote: remote, delay: 20 * time.Millisecond} + writes.write(t, s, 1, 0x32) + out.Reset() // The fixture itself publishes an initial empty image. + if err := logging.Configure("info"); err != nil { + t.Fatal(err) + } + if _, err := layer.Cut(ctTimeout(t)); err != nil { + t.Fatal(err) + } + first := out.String() + if strings.Count(first, "cut phases") != 1 || !strings.Contains(first, "level=INFO") || cutLogMilliseconds(t, first, "head_write_ms") < 20 { + t.Fatalf("default INFO omitted the measured cut: %s", first) + } + out.Reset() + if r, err := layer.Cut(ctTimeout(t)); err != nil || !r.Noop { + t.Fatalf("no-op control: %+v %v", r, err) + } + if strings.Contains(out.String(), "cut phases") { + t.Fatalf("default INFO exposed a successful no-op: %s", out.String()) + } + _ = logging.Configure("debug") + if r, err := layer.Cut(ctTimeout(t)); err != nil || !r.Noop { + t.Fatalf("debug no-op control: %+v %v", r, err) + } + if strings.Count(out.String(), "cut phases") != 1 || !strings.Contains(out.String(), "level=DEBUG") || !strings.Contains(out.String(), "noop=true") || !strings.Contains(out.String(), `err=""`) { + t.Fatalf("debug omitted the successful no-op: %s", out.String()) + } + out.Reset() + writes.write(t, s, 2, 0x41) + if err := logging.Configure("debug"); err != nil { + t.Fatal(err) + } + r, err := layer.Cut(ctTimeout(t)) + if err != nil { + t.Fatal(err) + } + line := out.String() + for _, field := range []string{"level=INFO", "cut phases", `volume="cut-log-volume"`, `err=""`, "candidates_ms=", "compare_prepare_ms=", "upload_ms=", "commit_ms=", "head_write_ms=", "settle_ms=", "total_ms="} { + if !strings.Contains(line, field) { + t.Fatalf("missing %s: %q", field, line) + } + } + if strings.Count(line, "cut phases") != 1 || r.Stats.CASWriteNs < int64(20*time.Millisecond) { + t.Fatalf("one cut with measured HEAD time: stats=%+v log=%s", r.Stats, line) + } + if cutLogMilliseconds(t, line, "head_write_ms") < 20 { + t.Fatalf("log omitted the delayed HEAD exchange: %s", line) + } + assertEveryHeadIsPrefixImage(t, remote, writes) +} + +func cutLogMilliseconds(t *testing.T, line, field string) float64 { + t.Helper() + m := regexp.MustCompile(regexp.QuoteMeta(field) + `=([0-9.]+)`).FindStringSubmatch(line) + if len(m) != 2 { + t.Fatalf("missing numeric %s: %s", field, line) + } + v, err := strconv.ParseFloat(m[1], 64) + if err != nil { + t.Fatal(err) + } + return v +} + +type cutLogFailedBegin struct { + *fakeChunkRemote + fault error +} + +func (r cutLogFailedBegin) Begin(context.Context) (string, error) { + time.Sleep(20 * time.Millisecond) + return "", r.fault +} + +func TestCutTimingFailedPhaseCanHaveIncompleteSpans(t *testing.T) { + old, flags := log.Writer(), log.Flags() + t.Cleanup(func() { _ = logging.Configure("info"); log.SetOutput(old); log.SetFlags(flags) }) + s, remote, layer, writes := precopyLayer(t, 1024, 0) + fault := errors.New("begin unavailable") + layer.remote = cutLogFailedBegin{remote, fault} + writes.write(t, s, 1, 0x31) + var out bytes.Buffer + log.SetOutput(&out) + _ = logging.Configure("info") + if _, err := layer.Cut(ctTimeout(t)); !errors.Is(err, fault) { + t.Fatalf("error=%v", err) + } + line := out.String() + if !strings.Contains(line, "level=INFO") || !strings.Contains(line, "begin unavailable") || !strings.Contains(line, "phase_scope=publication") || cutLogMilliseconds(t, line, "total_ms") < 20 || cutLogMilliseconds(t, line, "compare_prepare_ms") != 0 { + t.Fatalf("failed Begin duration was misrepresented: %s", line) + } +} + +func TestCutTimingPendingRecoveryIsOnlyInTheAttemptTotal(t *testing.T) { + old, flags := log.Writer(), log.Flags() + t.Cleanup(func() { _ = logging.Configure("info"); log.SetOutput(old); log.SetFlags(flags) }) + s, remote, layer, writes := precopyLayer(t, 1024, 0) + layer.remote = &fixedSpanRemote{remote, 20 * time.Millisecond} + writes.write(t, s, 1, 0x31) + remote.mu.Lock() + remote.casModes = []fakeCASMode{casUnknownNotApplied} + remote.mu.Unlock() + if _, err := layer.Cut(ctTimeout(t)); !errors.Is(err, errChunkCASOutcomeUnknown) { + t.Fatalf("first cut=%v", err) + } + before := remote.stats().cas + var out bytes.Buffer + log.SetOutput(&out) + _ = logging.Configure("info") + r, err := layer.Cut(ctTimeout(t)) + if err != nil || !r.Noop || remote.stats().cas != before+1 { + t.Fatalf("pending replay/no-op: %+v %v", r, err) + } + line := out.String() + if !strings.Contains(line, "level=INFO") || !strings.Contains(line, "phase_scope=publication") || cutLogMilliseconds(t, line, "head_write_ms") != 0 || cutLogMilliseconds(t, line, "total_ms") < 20 { + t.Fatalf("recovery HEAD exchange was not distinguished: %s", line) + } + assertEveryHeadIsPrefixImage(t, remote, writes) +} diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index 277becf626..558e20d4fa 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -20,6 +20,7 @@ import ( "encoding/hex" "errors" "fmt" + "log" "math" "os" "path/filepath" @@ -30,6 +31,8 @@ import ( "sync" "sync/atomic" "time" + + "github.com/seaweedfs/seaweed-block/internal/logging" ) // chunkHooks is what the WALStore holds for an attached layer. @@ -725,6 +728,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch } defer done() start := time.Now() + recoveredPublication := l.pending != nil var st chunkCutStats persistCapturedMap := func(versions map[uint64]uint64) { start := time.Now() @@ -767,6 +771,22 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch observe(time.Duration(st.BarrierPauseNs)) } l.recordCut(res, st, err) + errorText := "" + if err != nil { + errorText = err.Error() + } + // Existing spans overlap (upload includes copy, commit includes its + // HEAD exchange); report observations, never add them into a new total. + // Pending recovery can write HEAD even when normal publication is a no-op. + emit := func(format string, args ...any) { log.Printf("level=INFO "+format, args...) } + if err == nil && res.Noop && !recoveredPublication && st.CandidateChunks == 0 && st.UploadedChunks == 0 { + emit = logging.Debugf + } + emit("storage: cut phases phase_scope=publication volume=%q holder=%q epoch=%d h=%d noop=%t candidates=%d uploaded=%d candidates_ms=%.3f compare_prepare_ms=%.3f upload_ms=%.3f commit_ms=%.3f head_write_ms=%.3f settle_ms=%.3f total_ms=%.3f err=%q", + l.cfg.VolumeID, l.cfg.Holder, l.cfg.Epoch, res.H, res.Noop, st.CandidateChunks, st.UploadedChunks, + float64(st.CandidateNs)/1e6, float64(st.PublishPrepareNs)/1e6, float64(st.UploadNs)/1e6, + float64(st.CASNs)/1e6, float64(st.CASWriteNs)/1e6, + float64(st.HeadReadbackNs+st.CommitStateNs+st.MapPersistNs)/1e6, float64(st.TotalNs)/1e6, errorText) }() if err := l.admit(); err != nil { return chunkCutResult{}, err diff --git a/seaweed-block/core/storage/chunk_seam.go b/seaweed-block/core/storage/chunk_seam.go index 54f15338c5..3395a0881e 100644 --- a/seaweed-block/core/storage/chunk_seam.go +++ b/seaweed-block/core/storage/chunk_seam.go @@ -265,6 +265,8 @@ func validChunkKey(key string) error { // chunkLayerConfig configures one attachment. type chunkLayerConfig struct { + // VolumeID labels diagnostics only; it never decides identity or admission. + VolumeID string // OnPublishSettled, when set, is called once per confirmed publication // (a cut, a detach, or an earlier unresolved CAS found applied), with the // HEAD before and after it (INV-PUBLISH-SETTLED-001). Calls are diff --git a/seaweed-block/docs/operator-guide.md b/seaweed-block/docs/operator-guide.md index 3731241556..d608a3183a 100644 --- a/seaweed-block/docs/operator-guide.md +++ b/seaweed-block/docs/operator-guide.md @@ -21,9 +21,9 @@ that runs it. `blockmaster` and `blockvolume` accept `--log-level info|debug` (default `info`). For the CLI, put the global flag before the subcommand: -`sw-block --log-level debug ops workspace show ...`. This switch configures the -DEBUG filter; additional cut diagnostics follow separately. Ordinary logs and -every acknowledged HEAD write remain visible at INFO. +`sw-block --log-level debug ops workspace show ...`. This configures the DEBUG +filter. HEAD writes and failed or working cut attempts remain INFO at both settings; +successful cut no-ops without pending recovery or candidate/upload work appear only at DEBUG. An unsupported level is refused before the command runs. Standard log timestamps are UTC with six fractional digits, for example @@ -31,9 +31,33 @@ Standard log timestamps are UTC with six fractional digits, for example their existing formats. A HEAD-write line identifies `head_write`, volume, generation, session, resulting HEAD facts and elapsed milliseconds. Begin and commit are separate lines with the same session; errors do not claim a -successful write. Log timestamps are observations, not authority or ordering +successful write. An ambiguous store answer emits WARN `outcome=unknown` with +the expected generation: it describes an attempt, never proof of persistence. +Log timestamps are observations, not authority or ordering proofs: use the generation and session fields to correlate writes. +At the default INFO level, failed cut attempts and attempts that do work +report `storage: cut phases`: `candidates_ms` +is the existing candidate-scan span; `compare_prepare_ms` includes comparison, +proposal preparation and session admission; `upload_ms` includes copy/upload; +`commit_ms` includes the HEAD exchange, also shown as `head_write_ms`; +`settle_ms` groups HEAD readback, local commit state and map persistence. +These are existing diagnostic spans and can overlap: do not sum them. +`phase_scope=publication` means the phase spans describe the normal publication +path. Recovery of an earlier unresolved publication can exchange HEAD before +this path, then finish as a no-op: that recovery cost appears in `total_ms`, +not necessarily in `commit_ms` or `head_write_ms`. `noop` does not mean the +invocation made no remote call. + +Spans are completion-dependent and rounded to three decimal places in +milliseconds: zero can mean +unfinished or unmeasured work, or rounding, rather than no work. A failed +session begin or upload can consume time without its completion span being +recorded. `noop` and `err` distinguish outcomes. `total_ms` is the independent +attempt measurement up to logging; synchronous log output and subsequent +cleanup can extend caller latency beyond it. Logging releases the statistics +mutex first but remains within the cut's capture lock and in-flight lifetime. + ## Clock safety requirement While volumes are served, blockmaster's host must keep its wall clock **slewed, not stepped**. Configure chrony to