Files
seaweedfs/weed/filer/reader_at_test.go
T
Chris LuandClaude Opus 5.5 a5e52b68f1 server, filer, s3: log downloads the client hung up on below error level (#12991)
* server: log a response the client hung up on below error level

A client that closes a download early (a video player seeking, a browser
cancel) fails the next response write with a broken pipe, a connection
reset, or an HTTP/2 stream close, and ProcessRangeRequest logged each one
as an ERROR. net/http cancels the request context once a write to the
client fails, so a canceled context now logs at V(3); a failure under a
live request still logs as an error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: log a stream the client hung up on below error level

The filer logged "failed to stream content" as an ERROR for every
download a client closed early, next to ProcessRangeRequest's own line.
It now goes through the same check, so only a failure under a live
request reaches the error log.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: log a GET the client hung up on below error level

A client closing a download early made GetObject log two ERROR lines,
from streamFromVolumeServers and from GetObjectHandler, and the SSE copy
paths logged a third. The write fails with a broken pipe or connection
reset rather than context.Canceled, so the canceled-stream check missed
it. net/http cancels the request context once a write to the client
fails, so streamCanceled also treats a canceled request as the client
leaving.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: log a canceled chunk read below error level

A ranged S3 GET reads through ChunkReadAt with the request context, so a
client hanging up cancels the chunk fetch and the reader logged
"fetching chunk ...: context canceled" (and, when the lookup was the
step canceled, "operation LookupFileId ... failed") as an ERROR. A read
whose caller canceled it now logs at V(3).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 13:12:03 -08:00

285 lines
7.9 KiB
Go

package filer
import (
"bytes"
"context"
"errors"
"io"
"math"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/seaweedfs/seaweedfs/weed/glog"
)
type mockChunkCache struct {
}
func (m *mockChunkCache) GetChunk(fileId string, minSize uint64) (data []byte) {
x, _ := strconv.Atoi(fileId)
data = make([]byte, minSize)
for i := 0; i < int(minSize); i++ {
data[i] = byte(x)
}
return data
}
func (m *mockChunkCache) ReadChunkAt(data []byte, fileId string, offset uint64) (n int, err error) {
x, _ := strconv.Atoi(fileId)
for i := 0; i < len(data); i++ {
data[i] = byte(x)
}
return len(data), nil
}
func (m *mockChunkCache) SetChunk(fileId string, data []byte) {
}
func (m *mockChunkCache) GetMaxFilePartSizeInCache() uint64 {
return 0
}
func (m *mockChunkCache) IsInCache(fileId string, lockNeeded bool) (answer bool) {
return false
}
func TestReaderAt(t *testing.T) {
visibles := NewIntervalList[*VisibleInterval]()
addVisibleInterval(visibles, &VisibleInterval{
start: 1,
stop: 2,
fileId: "1",
chunkSize: 9,
})
addVisibleInterval(visibles, &VisibleInterval{
start: 3,
stop: 4,
fileId: "3",
chunkSize: 1,
})
addVisibleInterval(visibles, &VisibleInterval{
start: 5,
stop: 6,
fileId: "5",
chunkSize: 2,
})
addVisibleInterval(visibles, &VisibleInterval{
start: 7,
stop: 9,
fileId: "7",
chunkSize: 2,
})
addVisibleInterval(visibles, &VisibleInterval{
start: 9,
stop: 10,
fileId: "9",
chunkSize: 2,
})
readerAt := &ChunkReadAt{
chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
fileSize: 10,
readerCache: NewReaderCache(3, &mockChunkCache{}, nil, nil),
readerPattern: NewReaderPattern(),
}
testReadAt(t, readerAt, 0, 10, 10, io.EOF, nil, nil)
testReadAt(t, readerAt, 0, 12, 10, io.EOF, nil, nil)
testReadAt(t, readerAt, 2, 8, 8, io.EOF, nil, nil)
testReadAt(t, readerAt, 3, 6, 6, nil, nil, nil)
}
func testReadAt(t *testing.T, readerAt *ChunkReadAt, offset int64, size int, expectedN int, expectedErr error, data, expectedData []byte) {
if data == nil {
data = make([]byte, size)
}
n, _, err := readerAt.doReadAt(context.Background(), data, offset)
if expectedN != n {
t.Errorf("unexpected read size: %d, expect: %d", n, expectedN)
}
if err != expectedErr {
t.Errorf("unexpected read error: %v, expect: %v", err, expectedErr)
}
if expectedData != nil && !bytes.Equal(data, expectedData) {
t.Errorf("unexpected read data: %v, expect: %v", data, expectedData)
}
}
func TestReaderAt0(t *testing.T) {
visibles := NewIntervalList[*VisibleInterval]()
addVisibleInterval(visibles, &VisibleInterval{
start: 2,
stop: 5,
fileId: "1",
chunkSize: 9,
})
addVisibleInterval(visibles, &VisibleInterval{
start: 7,
stop: 9,
fileId: "2",
chunkSize: 9,
})
readerAt := &ChunkReadAt{
chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
fileSize: 10,
readerCache: NewReaderCache(3, &mockChunkCache{}, nil, nil),
readerPattern: NewReaderPattern(),
}
testReadAt(t, readerAt, 0, 10, 10, io.EOF, nil, nil)
testReadAt(t, readerAt, 3, 16, 7, io.EOF, nil, nil)
testReadAt(t, readerAt, 3, 5, 5, nil, nil, nil)
testReadAt(t, readerAt, 11, 5, 0, io.EOF, nil, nil)
testReadAt(t, readerAt, 10, 5, 0, io.EOF, nil, nil)
}
func TestReaderAt1(t *testing.T) {
visibles := NewIntervalList[*VisibleInterval]()
addVisibleInterval(visibles, &VisibleInterval{
start: 2,
stop: 5,
fileId: "1",
chunkSize: 9,
})
readerAt := &ChunkReadAt{
chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
fileSize: 20,
readerCache: NewReaderCache(3, &mockChunkCache{}, nil, nil),
readerPattern: NewReaderPattern(),
}
testReadAt(t, readerAt, 0, 20, 20, io.EOF, nil, nil)
testReadAt(t, readerAt, 1, 7, 7, nil, nil, nil)
testReadAt(t, readerAt, 0, 1, 1, nil, nil, nil)
testReadAt(t, readerAt, 18, 4, 2, io.EOF, nil, nil)
testReadAt(t, readerAt, 12, 4, 4, nil, nil, nil)
testReadAt(t, readerAt, 4, 20, 16, io.EOF, nil, nil)
testReadAt(t, readerAt, 4, 10, 10, nil, nil, nil)
testReadAt(t, readerAt, 1, 10, 10, nil, nil, nil)
}
func TestReaderAtGappedChunksDoNotLeak(t *testing.T) {
visibles := NewIntervalList[*VisibleInterval]()
addVisibleInterval(visibles, &VisibleInterval{
start: 2,
stop: 3,
fileId: "1",
chunkSize: 5,
})
addVisibleInterval(visibles, &VisibleInterval{
start: 7,
stop: 9,
fileId: "1",
chunkSize: 4,
})
readerAt := &ChunkReadAt{
chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
fileSize: 9,
readerCache: NewReaderCache(3, &mockChunkCache{}, nil, nil),
readerPattern: NewReaderPattern(),
}
testReadAt(t, readerAt, 0, 9, 9, io.EOF, []byte{2, 2, 2, 2, 2, 2, 2, 2, 2}, []byte{0, 0, 1, 0, 0, 0, 0, 1, 1})
testReadAt(t, readerAt, 1, 8, 8, io.EOF, []byte{2, 2, 2, 2, 2, 2, 2, 2}, []byte{0, 1, 0, 0, 0, 0, 1, 1})
}
func TestReaderAtSparseFileDoesNotLeak(t *testing.T) {
readerAt := &ChunkReadAt{
chunkViews: ViewFromVisibleIntervals(NewIntervalList[*VisibleInterval](), 0, math.MaxInt64),
fileSize: 3,
readerCache: NewReaderCache(3, &mockChunkCache{}, nil, nil),
readerPattern: NewReaderPattern(),
}
testReadAt(t, readerAt, 0, 3, 3, io.EOF, []byte{2, 2, 2}, []byte{0, 0, 0})
testReadAt(t, readerAt, 1, 2, 2, io.EOF, []byte{2, 2}, []byte{0, 0})
}
// holeChunkCache serves every chunk but one, so that chunk falls through to a
// lookup that fails.
type holeChunkCache struct {
mockChunkCache
missingFileId string
}
func (c *holeChunkCache) ReadChunkAt(data []byte, fileId string, offset uint64) (int, error) {
if fileId == c.missingFileId {
return 0, nil
}
return c.mockChunkCache.ReadChunkAt(data, fileId, offset)
}
func (c *holeChunkCache) GetMaxFilePartSizeInCache() uint64 {
return math.MaxUint64
}
// The parallel reads place their bytes directly in the output buffer, so a
// failing middle chunk leaves a hole with valid data after it. Reporting that
// length would hand the caller bytes it never read.
func TestReaderAtParallelFailureReturnsContiguousPrefix(t *testing.T) {
visibles := NewIntervalList[*VisibleInterval]()
for i, fileId := range []string{"1", "2", "3"} {
addVisibleInterval(visibles, &VisibleInterval{
start: int64(i) * 4,
stop: int64(i)*4 + 4,
fileId: fileId,
chunkSize: 4,
})
}
readerAt := &ChunkReadAt{
ctx: context.Background(),
chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
fileSize: 12,
readerCache: NewReaderCache(3, &holeChunkCache{missingFileId: "2"}, func(ctx context.Context, fileId string) ([]string, error) {
return nil, errors.New("volume down")
}, nil),
readerPattern: NewReaderPattern(),
prefetchCount: 3,
}
buf := make([]byte, 12)
n, err := readerAt.ReadAt(buf, 0)
require.Error(t, err)
assert.Equal(t, 4, n, "only the bytes before the failed chunk are readable")
assert.Equal(t, []byte{1, 1, 1, 1}, buf[:n])
}
// A read whose caller went away (an S3 client hanging up mid-GET) is canceled,
// not failed, and stays out of the error log.
func TestReaderAtCanceledReadIsNotAnError(t *testing.T) {
visibles := NewIntervalList[*VisibleInterval]()
addVisibleInterval(visibles, &VisibleInterval{start: 0, stop: 8, fileId: "1", chunkSize: 8})
readerAt := &ChunkReadAt{
ctx: context.Background(),
chunkViews: ViewFromVisibleIntervals(visibles, 2, 4),
fileSize: 8,
readerCache: NewReaderCache(3, &holeChunkCache{missingFileId: "1"}, func(ctx context.Context, fileId string) ([]string, error) {
return nil, ctx.Err()
}, nil),
readerPattern: NewReaderPattern(),
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
errorsBefore := glog.Stats.Error.Lines()
_, _, err := readerAt.ReadAtWithTime(ctx, make([]byte, 4), 2)
require.ErrorIs(t, err, context.Canceled)
assert.Equal(t, int64(0), glog.Stats.Error.Lines()-errorsBefore, "ERROR lines logged for a canceled read")
}