From 24e5b821e3a32dcef2670bfae6422f562d4dee47 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 08:29:31 -0800 Subject: [PATCH] sw-block: the delete's whole stderr is best effort; a stuck stderr never holds it - diagWriter is now the volume-delete command's stderr: Write never blocks (queued in order to one writer goroutine, dropped when the queue is full), so the sweeps, the error line written before stdout, the answer on stdout and the exit never wait on stderr. close runs after the answer, waits at most 200 ms for queued lines, then abandons a stuck writer: nothing more is written. All of the command's stderr goes through it, so its writes never overlap. - deleteVolumeWithProgress writes to the stderr it is given (it must not block) and no longer owns a writer. - harness: a server whose launch failed reports 'not started', not running. Co-Authored-By: Claude Opus 5.5 --- .../workspace_weed_integration_test.go | 5 ++ seaweed-block/cmd/sw-block/chunk_diag.go | 73 +++++++++++++------ .../cmd/sw-block/chunk_volume_delete.go | 20 +++-- 3 files changed, 68 insertions(+), 30 deletions(-) diff --git a/seaweed-block/cmd/blockvolume/workspace_weed_integration_test.go b/seaweed-block/cmd/blockvolume/workspace_weed_integration_test.go index 8ee85c2893..51bdcdaffe 100644 --- a/seaweed-block/cmd/blockvolume/workspace_weed_integration_test.go +++ b/seaweed-block/cmd/blockvolume/workspace_weed_integration_test.go @@ -313,6 +313,11 @@ func (c *w4WeedCluster) servers() []weedServer { out := make([]weedServer, 0, len(c.procs)) for name, p := range c.procs { s := weedServer{name: name, log: p.log.Name()} + if p.done == nil { // its launch failed: it never ran + s.exited, s.err = true, fmt.Errorf("not started") + out = append(out, s) + continue + } select { case <-p.done: s.exited, s.err = true, *p.exit diff --git a/seaweed-block/cmd/sw-block/chunk_diag.go b/seaweed-block/cmd/sw-block/chunk_diag.go index 9d1d3430db..04ff21fb09 100644 --- a/seaweed-block/cmd/sw-block/chunk_diag.go +++ b/seaweed-block/cmd/sw-block/chunk_diag.go @@ -1,46 +1,75 @@ package main import ( - "fmt" "io" + "sync" + "sync/atomic" + "time" ) -// diagWriter writes a command's diagnostic lines best effort: one goroutine -// owns the writes, a line that finds the queue full is dropped, and printf -// never blocks. A slow stderr therefore never delays the work the lines -// describe, nor the decision of its answer. close joins the goroutine, so the -// command owns its writer to the end: nothing it queued is written beside the -// caller's own later writes, and a writer need not be safe for concurrent use. +// diagWriter is a command's stderr, best effort: Write never blocks. One +// goroutine owns the writes to the real stderr, in order; a line that finds +// the queue full is dropped. A slow or stuck stderr therefore delays neither +// the work, nor its answer on stdout, nor the exit. +// +// close (after the answer is on stdout) waits at most diagDrain for the queued +// lines; a writer still stuck then is abandoned, and nothing more is written +// by the command: later lines and the rest of the queue are dropped. All of +// the command's stderr goes through one diagWriter, so its writes never +// overlap, and a stderr need not be safe for concurrent use. type diagWriter struct { - lines chan string - done chan struct{} + mu sync.Mutex // orders Write against close + closed bool + lines chan []byte + done chan struct{} + abandoned atomic.Bool } -// diagQueue bounds the lines waiting for a slow stderr, and so how long close -// can take after the answer is decided (at most this many writes). -const diagQueue = 4 +const diagQueue = 64 + +var diagDrain = 200 * time.Millisecond func newDiagWriter(w io.Writer) *diagWriter { - d := &diagWriter{lines: make(chan string, diagQueue), done: make(chan struct{})} + d := &diagWriter{lines: make(chan []byte, diagQueue), done: make(chan struct{})} go func() { defer close(d.done) for l := range d.lines { - _, _ = io.WriteString(w, l) + if !d.abandoned.Load() { + _, _ = w.Write(l) + } } }() return d } -// printf queues one line, or drops it when the queue is full. -func (d *diagWriter) printf(format string, args ...any) { - select { - case d.lines <- fmt.Sprintf(format, args...): - default: +// Write queues p, or drops it when the queue is full or the writer is closed; +// it reports every byte written either way (a diagnostic is never an error). +func (d *diagWriter) Write(p []byte) (int, error) { + d.mu.Lock() + defer d.mu.Unlock() + if !d.closed { + select { + case d.lines <- append([]byte(nil), p...): + default: + } } + return len(p), nil } -// close takes no more lines and waits until the queued ones are written. +// close takes no more lines, waits at most diagDrain for the queued ones, and +// then abandons a writer that is still stuck. func (d *diagWriter) close() { - close(d.lines) - <-d.done + d.mu.Lock() + if !d.closed { + d.closed = true + close(d.lines) + } + d.mu.Unlock() + t := time.NewTimer(diagDrain) + defer t.Stop() + select { + case <-d.done: + case <-t.C: + d.abandoned.Store(true) + } } diff --git a/seaweed-block/cmd/sw-block/chunk_volume_delete.go b/seaweed-block/cmd/sw-block/chunk_volume_delete.go index 61bacf05c8..fe1a7089ca 100644 --- a/seaweed-block/cmd/sw-block/chunk_volume_delete.go +++ b/seaweed-block/cmd/sw-block/chunk_volume_delete.go @@ -26,7 +26,12 @@ import ( // update that marks HEAD deleting. A refused delete leaves the volume // reserved; rerun with the same --request-id once its cause is cleared. A // rerun on a deleted volume sweeps any object that landed late. -func chunkVolumeDelete(args []string, stdout, stderr io.Writer) int { +func chunkVolumeDelete(args []string, stdout, realStderr io.Writer) int { + // The command's whole stderr is best effort (diagWriter): its answer and + // exit never wait on a slow or stuck stderr. close runs after the answer + // is on stdout. + stderr := newDiagWriter(realStderr) + defer stderr.close() c := newBCommand("volume-delete", 120*time.Second, stdout, stderr) c.growing = true c.fs.Lookup("timeout").Usage = "bound for one delete sweep and the no-progress window; progress permits further sweeps" @@ -99,7 +104,8 @@ func authorityErr(err error) error { // deleteVolumeWithProgress repeats the existing resumable kernel operation. // There is no new durable cursor: each sweep lists what remains. A confirmed // removal extends the no-progress window; an expired sweep alone does not end -// a growing delete. Errors and progress use stderr, leaving stdout one answer. +// a growing delete. Errors and progress use stderr, leaving stdout one answer; +// stderr must not block (the command passes its diagWriter). func deleteVolumeWithProgress(ctx context.Context, cat *chunkstore.BCatalog, ref chunkstore.VolumeRef, auth layout.DeleteAuthority, bound time.Duration, stderr io.Writer) ([]layout.RootName, error) { done := map[string]bool{} known := map[string]bool{} @@ -111,10 +117,8 @@ func deleteVolumeWithProgress(ctx context.Context, cat *chunkstore.BCatalog, ref stalled := func(err error) error { return &chunkstore.CatalogError{Code: "unavailable", Retryable: true, Message: fmt.Sprintf("deleted %d of %d objects; no progress at %s; retry with the same --request-id", len(done), total, object), Err: err} } - // Every progress line goes through diag, best effort: a slow stderr never - // delays a sweep, the retry window or the answer. - diag := newDiagWriter(stderr) - defer diag.close() + // stderr is the command's diagWriter: its writes never block, so a slow + // stderr delays neither a sweep, the retry window nor the answer. // A sweep stuck inside the store says so while it waits (every quarter of // the bound, at most every 5 s): a stalled store is told apart from a slow // delete before the bound ends the command. mu guards the sweep's counters, @@ -134,7 +138,7 @@ func deleteVolumeWithProgress(ctx context.Context, cat *chunkstore.BCatalog, ref idle, n, of, at := time.Since(lastProgress), len(done), total, object mu.Unlock() if idle >= stallEvery { - diag.printf("volume-delete: no progress for %s, waiting on %s (deleted %d of %d objects)\n", idle.Round(time.Millisecond), at, n, of) + fmt.Fprintf(stderr, "volume-delete: no progress for %s, waiting on %s (deleted %d of %d objects)\n", idle.Round(time.Millisecond), at, n, of) } } } @@ -175,7 +179,7 @@ func deleteVolumeWithProgress(ctx context.Context, cat *chunkstore.BCatalog, ref cancel() close(stop) <-reported // prompt: the reporter never blocks on stderr - diag.printf("volume-delete: deleted %d of %d objects\n", len(done), total) + fmt.Fprintf(stderr, "volume-delete: deleted %d of %d objects\n", len(done), total) for _, rn := range skipped { skippedSet[rn] = true }