-
Notifications
You must be signed in to change notification settings - Fork 133
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
[IND-552] add roundtable task to take fast sync Postgres snapshots every 4 hours #912
Merged
Merged
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6c04868
add roundtable task to take fast sync snapshots at shorter cadence
dydxwill ffe0b2f
lint
dydxwill f8515a4
Merge branch 'main' of https://github.com/dydxprotocol/v4-chain into …
dydxwill 8851dbb
Merge branch 'main' of https://github.com/dydxprotocol/v4-chain into …
dydxwill 697bc6a
fix
dydxwill 64054d6
latest
dydxwill 424ca36
latest
dydxwill 62a00b2
s3 export
dydxwill 8460623
fix
dydxwill a0898af
Merge branch 'main' of https://github.com/dydxprotocol/v4-chain into …
dydxwill 4de0f17
update cmt
dydxwill f5ad041
address cmts
dydxwill fa75ea8
lint
dydxwill 8ca7474
Merge branch 'main' of https://github.com/dydxprotocol/v4-chain into …
dydxwill 90b2f58
address cmts
dydxwill 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 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 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
48 changes: 48 additions & 0 deletions
48
indexer/services/roundtable/__tests__/tasks/take-fast-sync-snapshot.test.ts
This file contains 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,48 @@ | ||
import config from '../../src/config'; | ||
import { asMock } from '@dydxprotocol-indexer/dev'; | ||
import { | ||
checkIfExportJobToS3IsOngoing, | ||
checkIfS3ObjectExists, | ||
getMostRecentDBSnapshotIdentifier, | ||
startExportTask, | ||
} from '../../src/helpers/aws'; | ||
import takeFastSyncSnapshotTask from '../../src/tasks/take-fast-sync-snapshot'; | ||
|
||
jest.mock('../../src/helpers/aws'); | ||
|
||
describe('fast-sync-export-db-snapshot', () => { | ||
beforeAll(() => { | ||
config.RDS_INSTANCE_NAME = 'postgres-main-staging'; | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
asMock(getMostRecentDBSnapshotIdentifier).mockImplementation(async () => Promise.resolve('postgres-main-staging-2022-05-03-04-16')); | ||
}); | ||
|
||
afterAll(jest.resetAllMocks); | ||
|
||
it('s3Object exists', async () => { | ||
asMock(checkIfS3ObjectExists).mockImplementation(async () => Promise.resolve(true)); | ||
|
||
await takeFastSyncSnapshotTask(); | ||
|
||
expect(checkIfExportJobToS3IsOngoing).not.toHaveBeenCalled(); | ||
expect(startExportTask).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('export job in progress', async () => { | ||
asMock(checkIfExportJobToS3IsOngoing).mockImplementation( | ||
async () => Promise.resolve(true)); | ||
|
||
await takeFastSyncSnapshotTask(); | ||
|
||
expect(startExportTask).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('start export job', async () => { | ||
await takeFastSyncSnapshotTask(); | ||
|
||
expect(startExportTask).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains 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 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 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 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
98 changes: 98 additions & 0 deletions
98
indexer/services/roundtable/src/tasks/take-fast-sync-snapshot.ts
This file contains 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,98 @@ | ||
import { InfoObject, logger, stats } from '@dydxprotocol-indexer/base'; | ||
import RDS from 'aws-sdk/clients/rds'; | ||
import S3 from 'aws-sdk/clients/s3'; | ||
import { DateTime } from 'luxon'; | ||
|
||
import config from '../config'; | ||
import { | ||
checkIfExportJobToS3IsOngoing, | ||
checkIfS3ObjectExists, | ||
FAST_SYNC_SNAPSHOT_S3_BUCKET_NAME, | ||
getMostRecentDBSnapshotIdentifier, | ||
startExportTask, | ||
} from '../helpers/aws'; | ||
|
||
const statStart: string = `${config.SERVICE_NAME}.fast_sync_export_db_snapshot`; | ||
|
||
export default async function runTask(): Promise<void> { | ||
const at: string = 'fast-sync-export-db-snapshot#runTask'; | ||
|
||
const rds: RDS = new RDS(); | ||
|
||
// get most recent rds snapshot | ||
const startDescribe: number = Date.now(); | ||
const dateString: string = DateTime.utc().toFormat('yyyy-MM-dd'); | ||
const mostRecentSnapshot: string = await getMostRecentDBSnapshotIdentifier(rds); | ||
stats.timing(`${statStart}.describe_rds_snapshots`, Date.now() - startDescribe); | ||
|
||
// dev example: rds:dev-indexer-apne1-db-2023-06-25-18-34 | ||
const s3Date: string = mostRecentSnapshot.split(config.RDS_INSTANCE_NAME)[1].slice(1); | ||
const s3: S3 = new S3(); | ||
|
||
// check if s3 object exists | ||
const startS3Check: number = Date.now(); | ||
const s3ObjectExists: boolean = await checkIfS3ObjectExists( | ||
s3, | ||
s3Date, | ||
FAST_SYNC_SNAPSHOT_S3_BUCKET_NAME, | ||
); | ||
stats.timing(`${statStart}.checkS3Object`, Date.now() - startS3Check); | ||
|
||
const rdsExportIdentifier: string = `${config.RDS_INSTANCE_NAME}-fast-sync-${s3Date}`; | ||
|
||
// If the s3 object exists, return | ||
if (s3ObjectExists) { | ||
logger.info({ | ||
at, | ||
dateString, | ||
message: 'S3 object exists.', | ||
}); | ||
return; | ||
} | ||
|
||
// if we haven't created the object, check if it is being created | ||
const rdsExportCheck: number = Date.now(); | ||
const exportJobOngoing: boolean = await checkIfExportJobToS3IsOngoing(rds, rdsExportIdentifier); | ||
stats.timing(`${statStart}.checkRdsExport`, Date.now() - rdsExportCheck); | ||
|
||
if (exportJobOngoing) { | ||
logger.info({ | ||
at, | ||
dateString, | ||
message: 'Will wait for export job to finish', | ||
}); | ||
return; | ||
} | ||
// start Export Job if S3 Object does not exist | ||
const startExport: number = Date.now(); | ||
try { | ||
const exportData: RDS.ExportTask = await startExportTask( | ||
rds, | ||
rdsExportIdentifier, | ||
FAST_SYNC_SNAPSHOT_S3_BUCKET_NAME, | ||
); | ||
|
||
logger.info({ | ||
at, | ||
message: 'Started an export task', | ||
exportData, | ||
}); | ||
} catch (error) { // TODO handle this by finding the most recent snapshot earlier | ||
const message: InfoObject = { | ||
at, | ||
message: 'export to S3 failed', | ||
error, | ||
}; | ||
|
||
if (error.name === 'DBSnapshotNotFound') { | ||
stats.increment(`${statStart}.no_s3_snapshot`, 1); | ||
|
||
logger.info(message); | ||
return; | ||
} | ||
|
||
logger.error(message); | ||
} finally { | ||
stats.timing(`${statStart}.rdsSnapshotExport`, Date.now() - startExport); | ||
} | ||
} |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
take-fast-sync-snapshot.ts
file exists in the codebase, but theLOOPS_ENABLED_TAKE_FAST_SYNC_SNAPSHOTS
flag is not being used within it. This suggests that the flag may not be implemented correctly to control the execution of the snapshot task as intended by the PR objectives. It is recommended to verify the implementation of this flag in the snapshot task logic.Analysis chain
The addition of the
LOOPS_ENABLED_TAKE_FAST_SYNC_SNAPSHOTS
configuration flag is correct and aligns with the PR objectives. Ensure that this flag is used correctly throughout the codebase.Scripts Executed
The following scripts were executed for the analysis:
Script:
Length of output: 53
Script:
Length of output: 85
Script:
Length of output: 96