Merge remote-tracking branch 'pr/364' into train11

# Conflicts:
#	docs/design/invariants.md
This commit is contained in:
pingqiu committed 2026-11-21 14:25:08 -08:00
commit 0d3efefe18
2 files changed
+130 -1

No files matched your search

@@ -0,0 +1,129 @@
package durable_test
import (
"bytes"
"context"
"crypto/sha256"
"errors"
"sync"
"testing"
"time"
"github.com/seaweedfs/seaweed-block/core/frontend/durable"
"github.com/seaweedfs/seaweed-block/core/storage"
)
type sealProgressRemote struct {
*storage.MemChunkRemote
mu sync.Mutex
expire context.CancelCauseFunc
keys []string
images map[[32]byte]int
bytes int
calls int
}
func (r *sealProgressRemote) Put(ctx context.Context, key string, b []byte) error {
if err := ctx.Err(); err != nil {
return err
}
r.mu.Lock()
defer r.mu.Unlock()
r.calls++
if r.expire != nil && len(r.keys) == 2 {
r.expire(context.DeadlineExceeded)
r.expire = nil
return context.DeadlineExceeded
}
if err := r.MemChunkRemote.Put(ctx, key, b); err != nil {
return err
}
r.keys = append(r.keys, key)
r.images[sha256.Sum256(b)]++
r.bytes += len(b)
return nil
}
// The caller's deadline cause is injected at the third PUT, after exactly two
// completed uploads. No wall-clock race decides whether progress was made.
func TestPlainSealDeadlineRetryUploadsOnlyRemainingChunks(t *testing.T) {
const chunks = 5
const chunkBytes = 4096 * pcChunkBlocks
parent, stop := context.WithTimeout(context.Background(), time.Minute)
defer stop()
first, expire := context.WithCancelCause(parent)
defer expire(context.Canceled)
remote := &sealProgressRemote{MemChunkRemote: storage.NewMemChunkRemote(), expire: expire, images: map[[32]byte]int{}}
p, err := durable.NewDurableProvider(durable.ProviderConfig{
Impl: durable.ImplWALStoreChunk, StorageRoot: t.TempDir(), BlockSize: 4096,
NumBlocks: chunks * pcChunkBlocks, ChunkBlocks: pcChunkBlocks, ChunkUploadConcurrency: 1,
ChunkRemoteFor: func(string) (storage.ChunkRemote, error) { return remote, nil },
}, v1View())
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = p.Close() })
openRecovered(t, p)
b, err := p.Open(parent, "v1")
if err != nil {
t.Fatal(err)
}
for i := 0; i < chunks; i++ {
if _, err = b.Write(parent, int64(i*chunkBytes), block(byte(0x40+i))); err != nil {
t.Fatal(err)
}
}
if err = b.Close(); err != nil {
t.Fatal(err)
}
before, err := remote.GetHead(parent)
if err != nil {
t.Fatal(err)
}
if _, _, err = p.DetachVolumePlain(first, "v1"); !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("plain seal missed deadline event: %v", err)
}
if !errors.Is(context.Cause(first), context.DeadlineExceeded) || parent.Err() != nil {
t.Fatal("caller deadline event was not witnessed")
}
after, err := remote.GetHead(parent)
if err != nil || after.Gen != before.Gen || after.State != before.State {
t.Fatalf("aborted seal changed HEAD: %+v %v", after, err)
}
if remote.calls != 3 || len(remote.keys) != 2 || remote.bytes != 2*chunkBytes {
t.Fatalf("first attempt progress: calls=%d successful=%d bytes=%d", remote.calls, len(remote.keys), remote.bytes)
}
firstKeys := append([]string(nil), remote.keys...)
r, last, err := p.DetachVolumePlain(parent, "v1")
if err != nil || r.Head.State != storage.ChunkHeadDetached || r.H != last || r.Head.RemoteLSN != last {
t.Fatalf("plain seal retry failed: %+v last=%d err=%v", r, last, err)
}
if len(remote.keys) != chunks || remote.bytes != chunks*chunkBytes {
t.Fatalf("plain seal retransmitted completed chunks: successful=%d bytes=%d want=%d", len(remote.keys), remote.bytes, chunks*chunkBytes)
}
for sum, n := range remote.images {
if n != 1 {
t.Errorf("reuploaded unchanged payload %x count=%d", sum, n)
}
}
for _, key := range firstKeys {
found := false
for _, ref := range r.Head.Chunks {
if ref.Key == key {
found = true
}
}
if !found {
t.Errorf("completed first-attempt key not reused: %s", key)
}
}
for lba := 0; lba < chunks*pcChunkBlocks; lba++ {
want := make([]byte, 4096)
if lba%pcChunkBlocks == 0 {
want = block(byte(0x40 + lba/pcChunkBlocks))
}
if !bytes.Equal(headBlock(t, remote.MemChunkRemote, uint32(lba)), want) {
t.Fatalf("sealed image differs at lba=%d", lba)
}
}
}
+1 -1
View File
@@ -751,7 +751,7 @@ Format v3 row 4 remains dark. Its typed scan result preserves trusted records, a
| Wait | Machine / invariant | Bound | Release and cancellation | Locks / remote work |
|---|---|---|---|---|
| Offline seal | M5 PlainStop -> Quiesce; `INV-CUT-PLAIN-DETACH-001` | Caller context; HTTP requires positive timeoutMs before provider dispatch; no live-serving timer | Provider owns drain, detached cut and settlement. Cancellation preserves unknown/fenced outcome safety; a closed frontend stays closed. | No admission lock held during store or remote I/O. |
| Offline seal | M5 PlainStop -> Quiesce; `INV-CUT-PLAIN-DETACH-001` | Caller context; positive HTTP timeoutMs. Completed uploads resume only within one blockvolume process and matching owner/HEAD/chunk versions; after restart the seal must fit seal_timeout_ms. | Provider owns drain, detached cut and settlement. Cancellation preserves unknown/fenced outcome safety; a closed frontend stays closed. | No admission lock held during store or remote I/O. |
| Frontend pre-copy admission | M5 tentative admission -> resume; `INV-CUT-PRECOPY-001` (not reachable in this release) | Earlier of caller deadline and this command arrival + 5 s; one bound across retries, no inherited pause clock | Owner `finishPrecopyLocked`: resume or terminal Close clears the pause and grants all waiters in FIFO arrival order under one `b.mu` hold; each rechecks the ordinary gate. Expiry removes only its waiter and refuses with typed `ErrBackendBusy` + deadline cause (guest-visible: yes). Only a typed pause refusal requeues a granted waiter under the original command deadline; already-observed stale-primary and other gate errors are returned unchanged. Grant versus queue expiry is decided under the same lock; timely grants survive later queue-timer expiry but never caller expiry or a terminal gate. No dependency on another command's completion. | No `ioMu`, store lock, or remote call while queued. Admitted I/O retains normal per-LBA serialization and lifecycle accounting. |
Frozen estimate reporting: a tail above the existing 60 s estimator ceiling reports the same 60 s rejection sentinel used by admission. It is known but saturated, not a complete fixed-plus-tail duration; quiesce slack is zero. Consumers must not use it as a completion forecast. Drain refusals retain the observation before their reason changes; this reporting does not reclassify refusal causes.