-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(webapp): retry branch environment lookups that race replica lag #4857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
myftija
wants to merge
1
commit into
main
Choose a base branch
from
fix/preview-branch-auth-replica-race-tri-13546
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| area: webapp | ||
| type: fix | ||
| --- | ||
|
|
||
| Fixed a race that could make the first deploy of a newly created preview branch fail and eventually time out. Deploys to just-created branches now resolve reliably. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { setTimeout as sleep } from "node:timers/promises"; | ||
|
|
||
| export type ReplicaRetryOutcome = "replica_retry" | "primary" | "not_found"; | ||
|
|
||
| // Replica-lag guard: on a miss, retry the replica once with jitter, then let the primary decide. | ||
| export async function findWithReplicaRetry<T>({ | ||
| replicaFind, | ||
| primaryFind, | ||
| hasDedicatedReplica, | ||
| retryDelayMs, | ||
| onOutcome, | ||
| }: { | ||
| replicaFind: () => Promise<T | null>; | ||
| primaryFind: () => Promise<T | null>; | ||
| hasDedicatedReplica: boolean; | ||
| retryDelayMs: { min: number; max: number }; | ||
| onOutcome?: (outcome: ReplicaRetryOutcome) => void; | ||
| }): Promise<T | null> { | ||
| const report = (outcome: ReplicaRetryOutcome) => { | ||
| try { | ||
| onOutcome?.(outcome); | ||
| } catch {} | ||
| }; | ||
|
|
||
| const found = await replicaFind(); | ||
| if (found) { | ||
| return found; | ||
| } | ||
|
|
||
| // Without a dedicated replica both lookups hit the same database, so a retry can't help. | ||
| if (!hasDedicatedReplica) { | ||
| report("not_found"); | ||
| return null; | ||
| } | ||
|
|
||
| await sleep(retryDelayMs.min + Math.random() * Math.max(0, retryDelayMs.max - retryDelayMs.min)); | ||
|
|
||
| const retried = await replicaFind(); | ||
| if (retried) { | ||
| report("replica_retry"); | ||
| return retried; | ||
| } | ||
|
|
||
| const fromPrimary = await primaryFind(); | ||
| report(fromPrimary ? "primary" : "not_found"); | ||
| return fromPrimary; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { findWithReplicaRetry } from "~/services/replicaLagRetry.server"; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| const base = { hasDedicatedReplica: true, retryDelayMs: { min: 0, max: 0 } }; | ||
|
myftija marked this conversation as resolved.
|
||
|
|
||
| describe("findWithReplicaRetry", () => { | ||
| it("returns the first replica hit without retrying or touching the primary", async () => { | ||
| const replicaFind = vi.fn().mockResolvedValue({ id: "env_1" }); | ||
| const primaryFind = vi.fn(); | ||
| const onOutcome = vi.fn(); | ||
|
|
||
| const result = await findWithReplicaRetry({ ...base, replicaFind, primaryFind, onOutcome }); | ||
|
|
||
| expect(result).toEqual({ id: "env_1" }); | ||
| expect(replicaFind).toHaveBeenCalledTimes(1); | ||
| expect(primaryFind).not.toHaveBeenCalled(); | ||
| expect(onOutcome).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("recovers via a replica retry when the row appears on the second read", async () => { | ||
| const replicaFind = vi.fn().mockResolvedValueOnce(null).mockResolvedValueOnce({ id: "env_1" }); | ||
| const primaryFind = vi.fn(); | ||
| const onOutcome = vi.fn(); | ||
|
|
||
| const result = await findWithReplicaRetry({ ...base, replicaFind, primaryFind, onOutcome }); | ||
|
|
||
| expect(result).toEqual({ id: "env_1" }); | ||
| expect(replicaFind).toHaveBeenCalledTimes(2); | ||
| expect(primaryFind).not.toHaveBeenCalled(); | ||
| expect(onOutcome).toHaveBeenCalledWith("replica_retry"); | ||
| }); | ||
|
|
||
| it("falls back to the primary when the replica misses twice", async () => { | ||
| const replicaFind = vi.fn().mockResolvedValue(null); | ||
| const primaryFind = vi.fn().mockResolvedValue({ id: "env_1" }); | ||
| const onOutcome = vi.fn(); | ||
|
|
||
| const result = await findWithReplicaRetry({ ...base, replicaFind, primaryFind, onOutcome }); | ||
|
|
||
| expect(result).toEqual({ id: "env_1" }); | ||
| expect(replicaFind).toHaveBeenCalledTimes(2); | ||
| expect(primaryFind).toHaveBeenCalledTimes(1); | ||
| expect(onOutcome).toHaveBeenCalledWith("primary"); | ||
| }); | ||
|
|
||
| it("reports a genuine miss and returns null", async () => { | ||
| const replicaFind = vi.fn().mockResolvedValue(null); | ||
| const primaryFind = vi.fn().mockResolvedValue(null); | ||
| const onOutcome = vi.fn(); | ||
|
|
||
| const result = await findWithReplicaRetry({ ...base, replicaFind, primaryFind, onOutcome }); | ||
|
|
||
| expect(result).toBeNull(); | ||
| expect(onOutcome).toHaveBeenCalledWith("not_found"); | ||
| expect(onOutcome).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("does a single lookup when there is no dedicated replica", async () => { | ||
| const replicaFind = vi.fn().mockResolvedValue(null); | ||
| const primaryFind = vi.fn(); | ||
| const onOutcome = vi.fn(); | ||
|
|
||
| const result = await findWithReplicaRetry({ | ||
| ...base, | ||
| hasDedicatedReplica: false, | ||
| replicaFind, | ||
| primaryFind, | ||
| onOutcome, | ||
| }); | ||
|
|
||
| expect(result).toBeNull(); | ||
| expect(replicaFind).toHaveBeenCalledTimes(1); | ||
| expect(primaryFind).not.toHaveBeenCalled(); | ||
| expect(onOutcome).toHaveBeenCalledWith("not_found"); | ||
| }); | ||
|
|
||
| it("does not fail the lookup when the outcome callback throws", async () => { | ||
| const replicaFind = vi.fn().mockResolvedValueOnce(null).mockResolvedValueOnce({ id: "env_1" }); | ||
| const primaryFind = vi.fn(); | ||
| const onOutcome = vi.fn(() => { | ||
| throw new Error("meter unavailable"); | ||
| }); | ||
|
|
||
| const result = await findWithReplicaRetry({ ...base, replicaFind, primaryFind, onOutcome }); | ||
|
|
||
| expect(result).toEqual({ id: "env_1" }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.