Files
seaweedfs/weed/util/http/read_token_retry_test.go
T
6adc9f0a21 mount: read chunks with filer-issued exact-file read tokens (#12800)
* mount: read chunks with filer-issued exact-file read tokens

* mount: preserve read authorization across delayed lookups and hard links

* mount: retain response tokens across read cache eviction

* mount: keep a shared read-token lookup alive when its first caller is cancelled

Token refreshes for one entry share a singleflight lookup that ran under
the first caller's context. An interrupted read cancelled that lookup, so
every other read of the entry waiting on it got no token, went out
unauthenticated, and failed with EIO on a volume server enforcing read
JWTs. Run the lookup detached from the caller (each attempt stays bounded
by the filer RPC wait) and let a cancelled caller stop waiting at once.

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

* filer: serve a cached chunk past the cache cutoff instead of fetching it

The full-chunk read path consulted the chunk cache only for chunks below
the cache size cutoff. Read-auth and peer-sharing mounts cache every
chunk they upload, so a read-after-write of a high-offset chunk went to
the volume, where a read-auth mount has no token before the entry is
published.

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

* mount: re-track read tokens when this mount renames an open file

Writeback mounts skip their own rename event, so only the subscriber
path re-tracked an open handle's chunks. After a local rename a token
refresh looked up the vacated name and the read failed. The rename
response, the subscriber rename and the surviving hard link now share
ReadAuth.TrackHandle.

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

---------

Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 23:07:14 -08:00

75 lines
2.3 KiB
Go

package http
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"github.com/seaweedfs/seaweedfs/weed/security"
)
// TestRetriedFetchChunkDataSelectsTokenPerAttempt proves that a filer-issued
// volume read token is chosen fresh for each attempt. The first attempt is
// answered with a retryable 500, forcing a backoff and retry; the second
// attempt must carry a different token than the first. A token selected once
// before the loop would be reused and could be expired by the time the retry
// runs, which the volume server would reject as non-retryable.
func TestRetriedFetchChunkDataSelectsTokenPerAttempt(t *testing.T) {
for _, isFullChunk := range []bool{true, false} {
name := "streamed"
if isFullChunk {
name = "direct"
}
t.Run(name, func(t *testing.T) {
InitGlobalHttpClient()
var mu sync.Mutex
var seenAuth []string
var calls int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
seenAuth = append(seenAuth, r.Header.Get("Authorization"))
mu.Unlock()
if atomic.AddInt32(&calls, 1) == 1 {
// Retryable (>= 499) so the fetch backs off and tries again.
w.WriteHeader(http.StatusInternalServerError)
return
}
_, _ = w.Write([]byte("abc"))
}))
defer server.Close()
var seq int32
security.SetVolumeReadTokenProvider(func(ctx context.Context, fileId string) string {
return fmt.Sprintf("tok-%d", atomic.AddInt32(&seq, 1))
})
defer security.SetVolumeReadTokenProvider(nil)
buffer := make([]byte, 3)
n, err := RetriedFetchChunkData(context.Background(), buffer, []string{server.URL + "/3,abc"}, nil, false, isFullChunk, 0, "3,abc", nil)
if err != nil {
t.Fatalf("fetch: %v", err)
}
if n != 3 || string(buffer) != "abc" {
t.Fatalf("read %d bytes %q, want 3 abc", n, string(buffer))
}
mu.Lock()
defer mu.Unlock()
if len(seenAuth) < 2 {
t.Fatalf("expected a retry, got %d attempt(s): %v", len(seenAuth), seenAuth)
}
if seenAuth[0] == seenAuth[1] {
t.Fatalf("token was not reselected per attempt: both attempts sent %q", seenAuth[0])
}
if seenAuth[0] == "" || seenAuth[1] == "" {
t.Fatalf("expected a bearer token on each attempt, got %v", seenAuth)
}
})
}
}