Skip to content

Commit

Permalink
fix: Fetch prev version snapshot if current one is missing (#1985)
Browse files Browse the repository at this point in the history
## Motivation

If the present DB version snapshot is missing, fetch the previous one. 
## Merge Checklist

_Choose all relevant options below by adding an `x` now or at any time
before submitting for review_

- [X] PR title adheres to the [conventional
commits](https://www.conventionalcommits.org/en/v1.0.0/) standard
- [X] PR has a
[changeset](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#35-adding-changesets)
- [ ] PR has been tagged with a change label(s) (i.e. documentation,
feature, bugfix, or chore)
- [ ] PR includes
[documentation](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#32-writing-docs)
if necessary.
- [X] All [commits have been
signed](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#22-signing-commits)

<!-- start pr-codex -->

---

## PR-Codex overview
This PR focuses on fixing a bug in the `@farcaster/hubble` package
related to fetching previous snapshots.

### Detailed summary
- Renamed migration test file and index for better clarity
- Increased max retries and retry delay for snapshot uploads
- Refactored snapshot retrieval logic to handle errors better

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your
question}`

<!-- end pr-codex -->
  • Loading branch information
adityapk00 authored May 3, 2024
1 parent 8cab5ca commit 0e342af
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-lizards-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farcaster/hubble": patch
---

fix: Fetch previous snapshot if current db one is not present
17 changes: 12 additions & 5 deletions apps/hubble/src/hubble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -971,18 +971,25 @@ export class Hub implements HubInterface {
let latestSnapshotKeyBase;
let latestChunks: string[] = [];
do {
const response = await axios.get(
`https://${s3Bucket}/${this.getSnapshotFolder(prevVersion)}/latest.json`,
const response = await ResultAsync.fromPromise(
axios.get(`https://${s3Bucket}/${this.getSnapshotFolder(prevVersion)}/latest.json`),
(e) => e,
);
const { keyBase, chunks } = response.data;

if (!keyBase) {
if (
response.isErr() ||
!response.value.data ||
!response.value.data.keyBase ||
!response.value.data.chunks
) {
log.error(
{ data: response.data, folder: this.getSnapshotFolder(prevVersion) },
{ response, folder: this.getSnapshotFolder(prevVersion) },
"No latest snapshot name found in latest.json",
);
prevVersion += 1;
} else {
const { keyBase, chunks } = response.value.data;

latestSnapshotKeyBase = keyBase as string;
latestChunks = chunks as string[];
break;
Expand Down
4 changes: 2 additions & 2 deletions apps/hubble/src/storage/db/migrations/11.fnameIndex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { FID_BYTES, RootPrefix } from "../types.js";
import { makeFidKey } from "../message.js";
import { ResultAsync } from "neverthrow";

const db = jestRocksDB("fnameUserNameProofByFid.migration.test");
const db = jestRocksDB("fnameFidIndex.migration.test");

const fid1 = Factories.Fid.build();
const fid2 = fid1 + 1;

describe("fnameUserNameProofByFid migration", () => {
describe("fnameFidIndex migration", () => {
beforeAll(async () => {});

test("should migrate the fname index properly", async () => {
Expand Down
6 changes: 3 additions & 3 deletions apps/hubble/src/utils/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ export const uploadToS3 = async (
}
};

const maxRetries = 3;
const retryDelayMs = 10 * 1000;
const maxRetries = 5;
const retryDelayMs = 1 * 60 * 1000;

async function uploadChunk(
s3: S3Client,
Expand All @@ -177,7 +177,7 @@ async function uploadChunk(
retries++;
if (retries < maxRetries) {
logger.warn({ key, retries, errMsg: (e as Error)?.message }, "Snapshot chunk upload failed. Retrying...");
await new Promise((resolve) => setTimeout(resolve, retryDelayMs));
await new Promise((resolve) => setTimeout(resolve, retryDelayMs * retries));
} else {
logger.error(
{ key, errMsg: (e as Error)?.message },
Expand Down

0 comments on commit 0e342af

Please sign in to comment.