Files
seaweedfs/weed/server/volume_server_handlers_download_limit_test.go
T
Chris LuandClaude Opus 5.5 9e5357782e volume: keep reads over the download limit local when readMode is local (#12679)
* volume: keep reads over the download limit local when readMode is local

With -readMode=local the server never sends a read for a volume it does
not hold elsewhere, but a read over -concurrentDownloadLimitMB on a
replicated volume was still redirected to another replica. Skip the
replica fallback in local mode, so the read waits for a download slot
and answers 429 when the wait times out.

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

* volume(rs): keep reads over the download limit local when readMode is local

Mirror the Go volume server: with readMode local, a read over the
download limit waits for a slot instead of being redirected to a
replica. proxy_or_redirect_to_target no longer needs the local-mode
redirect switch.

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

* volume: check download capacity under the wait lock

waitForDownloadSlot read the in-flight size outside the cond lock and
the read handler broadcast without it, so a download finishing between
the check and the wait was missed and the read waited out the timeout
for a 429. Check capacity with util.WaitUntil under the lock and
broadcast under it.

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

* volume(rs): register for the download notification before checking capacity

The wait loop created its Notified future only after the capacity
check, so a body dropped in between had its notify_waiters() lost and
the read waited out the deadline for a 429. Create the future first,
then check, as Go now checks under the cond lock.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 16:20:15 -08:00

88 lines
2.6 KiB
Go

package weed_server
import (
"context"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/stats"
"github.com/seaweedfs/seaweedfs/weed/storage"
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
"github.com/seaweedfs/seaweedfs/weed/storage/types"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/stretchr/testify/require"
)
func TestCheckDownloadLimitLocalReadModeWaitsInsteadOfProxying(t *testing.T) {
dir := t.TempDir()
store := storage.NewStore(nil, "localhost", 8080, 18080, "http://localhost:8080", "store-id",
[]string{dir}, []int32{10}, []util.MinFreeSpace{{}}, "",
storage.NeedleMapInMemory, []types.DiskType{types.HardDriveType}, nil, 3, stats.DefaultDiskIOProbeConfig())
done := make(chan struct{})
go func() {
for {
select {
case <-store.NewVolumesChan:
case <-store.DeletedVolumesChan:
case <-store.NewEcShardsChan:
case <-store.DeletedEcShardsChan:
case <-store.StateUpdateChan:
case <-done:
return
}
}
}()
t.Cleanup(func() {
store.Close()
close(done)
})
require.NoError(t, store.AddVolume(1, "", storage.NeedleMapInMemory, "001", "", 0, needle.GetCurrentVersion(), 0, types.HardDriveType, 0))
vs := &VolumeServer{
store: store,
ReadMode: "local",
concurrentDownloadLimit: 1,
inFlightDownloadDataSize: 2,
inflightDownloadDataTimeout: 50 * time.Millisecond,
inFlightDownloadDataLimitCond: sync.NewCond(new(sync.Mutex)),
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
r := httptest.NewRequest(http.MethodGet, "/1,0123456789abcdef", nil).WithContext(ctx)
w := httptest.NewRecorder()
require.False(t, vs.checkDownloadLimit(w, r))
require.Equal(t, http.StatusTooManyRequests, w.Code)
}
func TestWaitForDownloadSlotSeesReleaseBeforeWaiting(t *testing.T) {
vs := &VolumeServer{
concurrentDownloadLimit: 1,
inFlightDownloadDataSize: 2,
inflightDownloadDataTimeout: 5 * time.Second,
inFlightDownloadDataLimitCond: sync.NewCond(new(sync.Mutex)),
}
r := httptest.NewRequest(http.MethodGet, "/1,0123456789abcdef", nil)
w := httptest.NewRecorder()
vs.inFlightDownloadDataLimitCond.L.Lock()
got := make(chan bool, 1)
go func() { got <- vs.waitForDownloadSlot(w, r) }()
time.Sleep(100 * time.Millisecond)
atomic.AddInt64(&vs.inFlightDownloadDataSize, -2)
vs.inFlightDownloadDataLimitCond.Broadcast()
vs.inFlightDownloadDataLimitCond.L.Unlock()
select {
case ok := <-got:
require.True(t, ok)
case <-time.After(2 * time.Second):
t.Fatal("waiter missed the released download slot")
}
}