From 439ef02496da0bb8dc42469af29a47b2ed14dadf Mon Sep 17 00:00:00 2001 From: Gabriel20xx Date: Fri, 11 Sep 2026 11:56:55 +0200 Subject: [PATCH] Sync shared snapshot from 097442f --- .../carriernodeclient/carriernodeclient.go | 80 ++++++++++++------- .../carriernodeclient_test.go | 47 ++++++++++- 2 files changed, 96 insertions(+), 31 deletions(-) diff --git a/shared/backend/carriernodeclient/carriernodeclient.go b/shared/backend/carriernodeclient/carriernodeclient.go index 3306a01..f1bfc05 100644 --- a/shared/backend/carriernodeclient/carriernodeclient.go +++ b/shared/backend/carriernodeclient/carriernodeclient.go @@ -77,12 +77,12 @@ func New(internalServiceSecret string, baseURL func(ctx context.Context) string) // The candidates are used differently depending on the operation: // - ListFiles (discovery) queries every instance and merges the results, // so files living on distinct storage backends are all found. -// - Read operations tied to one file (Probe, FetchToScratch, ProbeMedia) -// try each instance in order and use the first one that actually has the -// file, so a replica standing in for a downed primary (or a file that -// simply lives on a non-primary storage backend) is still found. -// - Writes/streaming that don't originate from a discovered file -// (Proxy, PushThumbnail, DeleteThumbnail, PushOutput, PushInput, +// - Read operations tied to one file (Proxy, Probe, FetchToScratch, +// ProbeMedia) try each instance in order and use the first one that +// actually has the file, so playback/thumbnails/metadata for a file +// living on a non-primary storage backend still work, and a replica +// standing in for a downed primary is still found. +// - Writes (PushThumbnail, DeleteThumbnail, PushOutput, PushInput, // PushCopy, UploadDownload, StoreDownload) always target the primary // instance (the first candidate), matching single-instance behavior. func NewMulti(internalServiceSecret string, baseURLs func(ctx context.Context) []string) *Client { @@ -131,33 +131,55 @@ func (c *Client) newRequest(ctx context.Context, baseURL, method, path string, b // Proxy streams a CarrierNode response straight through to the original // client, preserving range/conditional-request semantics so video scrubbing -// keeps working without reimplementing http.ServeContent on this side. -// Always targets the primary instance - see NewMulti's doc comment. +// keeps working without reimplementing http.ServeContent on this side. Every +// configured instance is tried in order (see NewMulti's doc comment); the +// first one that doesn't answer with an error is the one actually streamed, +// so a file living on a non-primary instance still plays and a downed +// primary doesn't block a replica from serving it. The last candidate's +// response (success or error) is always the one forwarded, matching +// single-instance Proxy's behavior of passing even an error status through +// to the caller rather than swallowing it. func (c *Client) Proxy(w http.ResponseWriter, r *http.Request, carrierNodePath string) { - request, err := c.newRequest(r.Context(), c.primaryBaseURL(r.Context()), r.Method, carrierNodePath, nil) - if err != nil { - writeError(w, http.StatusBadGateway, "media service unavailable") - return + candidates := c.candidateBaseURLs(r.Context()) + if len(candidates) == 0 { + candidates = []string{""} } - for _, header := range forwardRequestHeaders { - if value := r.Header.Get(header); value != "" { - request.Header.Set(header, value) + for i, baseURL := range candidates { + last := i == len(candidates)-1 + request, err := c.newRequest(r.Context(), baseURL, r.Method, carrierNodePath, nil) + if err != nil { + if last { + writeError(w, http.StatusBadGateway, "media service unavailable") + } + continue } - } - response, err := httpClient.Do(request) - if err != nil { - writeError(w, http.StatusBadGateway, "media service unavailable") - return - } - defer response.Body.Close() - for _, header := range forwardResponseHeaders { - if value := response.Header.Get(header); value != "" { - w.Header().Set(header, value) + for _, header := range forwardRequestHeaders { + if value := r.Header.Get(header); value != "" { + request.Header.Set(header, value) + } } - } - w.WriteHeader(response.StatusCode) - if r.Method != http.MethodHead { - _, _ = io.Copy(w, response.Body) + response, err := httpClient.Do(request) + if err != nil { + if last { + writeError(w, http.StatusBadGateway, "media service unavailable") + } + continue + } + if response.StatusCode >= 400 && !last { + response.Body.Close() + continue + } + defer response.Body.Close() + for _, header := range forwardResponseHeaders { + if value := response.Header.Get(header); value != "" { + w.Header().Set(header, value) + } + } + w.WriteHeader(response.StatusCode) + if r.Method != http.MethodHead { + _, _ = io.Copy(w, response.Body) + } + return } } diff --git a/shared/backend/carriernodeclient/carriernodeclient_test.go b/shared/backend/carriernodeclient/carriernodeclient_test.go index 0d3150b..9190baa 100644 --- a/shared/backend/carriernodeclient/carriernodeclient_test.go +++ b/shared/backend/carriernodeclient/carriernodeclient_test.go @@ -105,7 +105,7 @@ func TestProbeExistsFalseWhenNoInstanceHasFile(t *testing.T) { } } -func TestProxyAlwaysTargetsPrimaryInstance(t *testing.T) { +func TestProxyPrefersPrimaryInstanceWhenItHasTheFile(t *testing.T) { var primaryHit, secondaryHit bool primary := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { primaryHit = true @@ -128,6 +128,49 @@ func TestProxyAlwaysTargetsPrimaryInstance(t *testing.T) { t.Fatal("Proxy did not hit the primary instance") } if secondaryHit { - t.Fatal("Proxy hit the secondary instance, want primary-only") + t.Fatal("Proxy hit the secondary instance even though the primary had the file") + } +} + +func TestProxyFallsBackToSecondaryInstanceWhenPrimaryMisses(t *testing.T) { + primary := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + })) + defer primary.Close() + secondary := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "video/mp4") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte("video bytes")) + })) + defer secondary.Close() + + client := NewMulti("", func(ctx context.Context) []string { return []string{primary.URL, secondary.URL} }) + + req := httptest.NewRequest(http.MethodGet, "/media/a.mp4", nil) + rec := httptest.NewRecorder() + client.Proxy(rec, req, "/media/a.mp4") + + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want 200 from the secondary instance", rec.Code) + } + if rec.Body.String() != "video bytes" { + t.Fatalf("body = %q, want secondary instance's bytes", rec.Body.String()) + } +} + +func TestProxyForwardsLastInstanceErrorWhenNoneHaveTheFile(t *testing.T) { + notFound := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + })) + defer notFound.Close() + + client := NewMulti("", func(ctx context.Context) []string { return []string{notFound.URL, notFound.URL} }) + + req := httptest.NewRequest(http.MethodGet, "/media/missing.mp4", nil) + rec := httptest.NewRecorder() + client.Proxy(rec, req, "/media/missing.mp4") + + if rec.Code != http.StatusNotFound { + t.Fatalf("status = %d, want 404 forwarded from the last instance", rec.Code) } }