Files
seaweedfs/weed/mount/page_writer_test.go
T
Yip LeoandClaude Opus 5.5 e8fef6bdfd mount: fail a read of dirty pages that cannot be read back from the swap file (#13215)
Dirty pages that weed mount buffers in the swap file (non-sequential
writes, or when the memory chunks run out) also serve reads until they
upload. SwapFileChunk.ReadDataAt logged a read error and stopped,
returning only how far it had read; UploadPipeline.MaybeReadDataAt and
DirtyPages.ReadDirtyDataAt passed that offset up. The mount read path
then served the rest of the range from the stored chunks or from zeros,
and the read returned OK. A read error on the cache disk, or a swap file
truncated by an outside cleanup, is enough: the application reads back
bytes it was told were written and gets other bytes, with no error
anywhere. This is the read-side twin of #13198, which made the upload
of those bytes fail.

PageChunk.ReadDataAt, UploadPipeline.MaybeReadDataAt and
DirtyPages.ReadDirtyDataAt now return an error besides the offset. A
swap chunk that cannot read back an interval overlapping the requested
range returns it, wrapping the new ErrDirtyDataUnreadable and the read
error (a short read is io.ErrUnexpectedEOF). Intervals outside the range
are not read, so a read that does not touch the lost bytes still
succeeds. MemChunk cannot fail. The read handler and copy_file_range
turn the error into EIO. Read does not refresh the filer entry and retry
for this error, since the entry has no say over bytes that only exist in
the swap file.

A failed read does not mark the handle failed. Reads of other ranges and
writes keep working, and the upload path already records the failure
when it cannot read the same bytes, so flush, fsync and close return EIO.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 11:58:05 -08:00

56 lines
1.6 KiB
Go

package mount
import (
"testing"
"github.com/seaweedfs/seaweedfs/weed/mount/page_writer"
)
// stubDirtyPages reports dirty data only for the chunks listed in dirty,
// filling the whole requested range of those chunks.
type stubDirtyPages struct {
page_writer.DirtyPages
chunkSize int64
dirty map[int64]bool
}
func (s *stubDirtyPages) ReadDirtyDataAt(data []byte, startOffset int64, tsNs int64) (maxStop int64, err error) {
if !s.dirty[startOffset/s.chunkSize] {
return 0, nil
}
for i := range data {
data[i] = 'x'
}
return startOffset + int64(len(data)), nil
}
func TestPageWriterReadDirtyDataAtAcrossChunks(t *testing.T) {
const chunkSize = 4
tests := []struct {
name string
dirty map[int64]bool
offset int64
size int
want int64
}{
{name: "dirty then clean", dirty: map[int64]bool{0: true}, offset: 2, size: 4, want: 4},
{name: "clean then dirty", dirty: map[int64]bool{1: true}, offset: 2, size: 4, want: 6},
{name: "dirty clean clean", dirty: map[int64]bool{0: true}, offset: 0, size: 12, want: 4},
{name: "dirty clean dirty", dirty: map[int64]bool{0: true, 2: true}, offset: 0, size: 12, want: 12},
{name: "all clean", dirty: map[int64]bool{}, offset: 0, size: 12, want: 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pw := &PageWriter{
fh: &FileHandle{},
chunkSize: chunkSize,
randomWriter: &stubDirtyPages{chunkSize: chunkSize, dirty: tt.dirty},
}
if got, err := pw.ReadDirtyDataAt(make([]byte, tt.size), tt.offset, 0); err != nil || got != tt.want {
t.Errorf("ReadDirtyDataAt(offset=%d, size=%d) = %d, want %d", tt.offset, tt.size, got, tt.want)
}
})
}
}