Skip to content
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

Earlier exit when failing to fetch blocks with workers #2256

Merged
merged 4 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/node-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Schema Migration support for Enums, Relations, Subscription (#2251)

### Fixed
- Fixed non-atomic schema migration execution (#2244)
- Testing Suites should run with unfinalizedBlocks `false` (#2258)

### Changed
- Improve error handling when fetching blocks (#2256)

## [7.2.1] - 2024-02-07
### Added
- Update `ParentProject` to use `untilBlock` as and alias for `block` (#2235)
Expand Down
22 changes: 17 additions & 5 deletions packages/node-core/src/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,30 @@ export abstract class ApiService<A = any, SA = any, B extends Array<any> = any[]
private timeouts: Record<string, NodeJS.Timeout | undefined> = {};

async fetchBlocks(heights: number[], numAttempts = MAX_RECONNECT_ATTEMPTS): Promise<B> {
return this.retryFetch(async () => {
// Get the latest fetch function from the provider
const apiInstance = this.connectionPoolService.api;
return apiInstance.fetchBlocks(heights);
}, numAttempts);
}

protected async retryFetch(fn: () => Promise<B>, numAttempts = MAX_RECONNECT_ATTEMPTS): Promise<B> {
let reconnectAttempts = 0;
let lastError: Error | null = null;
while (reconnectAttempts < numAttempts) {
try {
// Get the latest fetch function from the provider
const apiInstance = this.connectionPoolService.api;
return await apiInstance.fetchBlocks(heights);
return await fn();
} catch (e: any) {
logger.error(e, `Failed to fetch blocks ${heights[0]}...${heights[heights.length - 1]}`);

lastError = e;
reconnectAttempts++;
}
}
if (lastError !== null) {
logger.error(
`Maximum number of retries (${numAttempts}) reached. See the following error for the underlying reason.`
);
throw lastError;
}
throw new Error(`Maximum number of retries (${numAttempts}) reached.`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export abstract class BlockDispatcher<B, DS>
// Do nothing, fetching the block was flushed, this could be caused by forked blocks or dynamic datasources
return;
}
logger.warn(e, 'Failed to enqueue fetched block to process');
logger.error(e, 'Failed to enqueue fetched block to process');
process.exit(1);
});

Expand Down
1 change: 1 addition & 0 deletions packages/node-core/src/indexer/worker/worker.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export abstract class BaseWorkerService<
return;
}
logger.error(e, `Failed to fetch block ${height}`);
throw e;
}
}

Expand Down
23 changes: 6 additions & 17 deletions packages/node/src/indexer/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,27 +263,16 @@ export class ApiService
return `api.rpc.${ext?.section ?? '*'}.${ext?.method ?? '*'}`;
}

// Overrides the super function because of the specVer
async fetchBlocks(
heights: number[],
overallSpecVer?: number,
numAttempts = MAX_RECONNECT_ATTEMPTS,
): Promise<LightBlockContent[]> {
let reconnectAttempts = 0;
while (reconnectAttempts < numAttempts) {
try {
const apiInstance = this.connectionPoolService.api;
return await apiInstance.fetchBlocks(heights, overallSpecVer);
} catch (e: any) {
logger.error(
e,
`Failed to fetch blocks ${heights[0]}...${
heights[heights.length - 1]
}`,
);

reconnectAttempts++;
}
}
throw new Error(`Maximum number of retries (${numAttempts}) reached.`);
return this.retryFetch(async () => {
// Get the latest fetch function from the provider
const apiInstance = this.connectionPoolService.api;
return apiInstance.fetchBlocks(heights, overallSpecVer);
}, numAttempts);
}
}
Loading