-
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
Changes from 8 commits
6c04868
ffe0b2f
f8515a4
8851dbb
697bc6a
64054d6
424ca36
62a00b2
8460623
a0898af
4de0f17
f5ad041
fa75ea8
8ca7474
90b2f58
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import config from '../../src/config'; | ||
import { asMock } from '@dydxprotocol-indexer/dev'; | ||
import { createDBSnapshot, getMostRecentDBSnapshotIdentifier, startExportTask } from '../../src/helpers/aws'; | ||
import takeFastSyncSnapshotTask from '../../src/tasks/take-fast-sync-snapshot'; | ||
import { DateTime } from 'luxon'; | ||
|
||
jest.mock('../../src/helpers/aws'); | ||
|
||
describe('fast-sync-export-db-snapshot', () => { | ||
const snapshotIdentifier: string = `${config.FAST_SYNC_SNAPSHOT_IDENTIFIER_PREFIX}-postgres-main-staging-2022-05-03-04-16`; | ||
beforeAll(() => { | ||
config.RDS_INSTANCE_NAME = 'postgres-main-staging'; | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
asMock(getMostRecentDBSnapshotIdentifier).mockImplementation( | ||
async () => Promise.resolve(snapshotIdentifier), | ||
); | ||
}); | ||
|
||
afterAll(jest.resetAllMocks); | ||
|
||
it('Last snapshot was taken more than interval ago', async () => { | ||
await takeFastSyncSnapshotTask(); | ||
|
||
expect(createDBSnapshot).toHaveBeenCalled(); | ||
expect(startExportTask).toHaveBeenCalled(); | ||
}); | ||
|
||
it('Last snapshot was taken less than interval ago', async () => { | ||
const timestamp: string = DateTime.utc().minus({ minutes: 1 }).toFormat('yyyy-MM-dd-HH-mm'); | ||
asMock(getMostRecentDBSnapshotIdentifier).mockImplementation( | ||
async () => Promise.resolve(`${config.FAST_SYNC_SNAPSHOT_IDENTIFIER_PREFIX}-postgres-main-staging-${timestamp}`), | ||
); | ||
|
||
await takeFastSyncSnapshotTask(); | ||
|
||
expect(createDBSnapshot).not.toHaveBeenCalled(); | ||
expect(startExportTask).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('No existing snapshot', async () => { | ||
asMock(getMostRecentDBSnapshotIdentifier).mockImplementation( | ||
async () => Promise.resolve(undefined), | ||
); | ||
|
||
await takeFastSyncSnapshotTask(); | ||
|
||
expect(createDBSnapshot).toHaveBeenCalled(); | ||
expect(startExportTask).toHaveBeenCalled(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,14 +16,24 @@ enum ExportTaskStatus { | |
COMPLETE = 'complete', | ||
} | ||
|
||
const S3_BUCKET_NAME = config.S3_BUCKET_ARN.split(':::')[1]; | ||
export const S3_LOCATION_PREFIX = `s3://${S3_BUCKET_NAME}`; | ||
export const RESEARCH_SNAPSHOT_S3_BUCKET_NAME = config.RESEARCH_SNAPSHOT_S3_BUCKET_ARN.split(':::')[1]; | ||
export const RESEARCH_SNAPSHOT_S3_LOCATION_PREFIX = `s3://${RESEARCH_SNAPSHOT_S3_BUCKET_NAME}`; | ||
export const FAST_SYNC_SNAPSHOT_S3_BUCKET_NAME = config.FAST_SYNC_SNAPSHOT_S3_BUCKET_ARN.split(':::')[1]; | ||
|
||
/** | ||
* @description Get most recent snapshot identifier for an RDS database. | ||
* @param rds - RDS client | ||
* @param snapshotIdentifierPrefixInclude - Only include snapshots with snapshot identifier | ||
* that starts with prefixInclude | ||
* @param snapshotIdentifierPrefixExclude - Only include snapshots with snapshot identifier | ||
* that does not start with prefixExclude | ||
*/ | ||
// TODO(CLOB-672): Verify this function returns the most recent DB snapshot. | ||
export async function getMostRecentDBSnapshotIdentifier(rds: RDS): Promise<string> { | ||
export async function getMostRecentDBSnapshotIdentifier( | ||
rds: RDS, | ||
snapshotIdentifierPrefixInclude?: string, | ||
snapshotIdentifierPrefixExclude?: string, | ||
): Promise<string | undefined> { | ||
const awsResponse: RDS.DBSnapshotMessage = await rds.describeDBSnapshots({ | ||
DBInstanceIdentifier: config.RDS_INSTANCE_NAME, | ||
MaxRecords: 20, // this is the minimum | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a bug (noted in TODO(CLOB-672)) where if there are more than 20 DB snapshots for the RDS instance, this method won't return the latest DB snapshot. The fix (which wasn't implemented yet) is to page through all the snapshots and then get the latest one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
@@ -33,21 +43,86 @@ export async function getMostRecentDBSnapshotIdentifier(rds: RDS): Promise<strin | |
throw Error(`No DB snapshots found with identifier: ${config.RDS_INSTANCE_NAME}`); | ||
} | ||
|
||
let snapshots: RDS.DBSnapshotList = awsResponse.DBSnapshots; | ||
// Only include snapshots with snapshot identifier that starts with prefixInclude | ||
if (snapshotIdentifierPrefixInclude !== undefined) { | ||
snapshots = snapshots | ||
.filter((snapshot) => snapshot.DBSnapshotIdentifier && | ||
snapshot.DBSnapshotIdentifier.startsWith(snapshotIdentifierPrefixInclude), | ||
); | ||
} | ||
if (snapshotIdentifierPrefixExclude !== undefined) { | ||
snapshots = snapshots | ||
.filter((snapshot) => snapshot.DBSnapshotIdentifier && | ||
!snapshot.DBSnapshotIdentifier.startsWith(snapshotIdentifierPrefixExclude), | ||
); | ||
} | ||
|
||
logger.info({ | ||
at: `${atStart}getMostRecentDBSnapshotIdentifier`, | ||
message: 'Described snapshots for database', | ||
mostRecentSnapshot: awsResponse.DBSnapshots[awsResponse.DBSnapshots.length - 1], | ||
mostRecentSnapshot: snapshots[snapshots.length - 1], | ||
}); | ||
|
||
return awsResponse.DBSnapshots[awsResponse.DBSnapshots.length - 1].DBSnapshotIdentifier!; | ||
return snapshots[snapshots.length - 1]?.DBSnapshotIdentifier; | ||
} | ||
|
||
/** | ||
* @description Create DB snapshot for an RDS database. Only returns when the | ||
* snapshot is available. | ||
*/ | ||
export async function createDBSnapshot( | ||
rds: RDS, | ||
snapshotIdentifier: string, | ||
dbInstanceIdentifier: string, | ||
): Promise<string> { | ||
const params = { | ||
DBInstanceIdentifier: dbInstanceIdentifier, | ||
DBSnapshotIdentifier: snapshotIdentifier, | ||
}; | ||
|
||
try { | ||
await rds.createDBSnapshot(params).promise(); | ||
// Polling function to check snapshot status. Only return when the snapshot is available. | ||
const waitForSnapshot = async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think you can use this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Add some max wait time where the task breaks out of this loop, and some logs indicating how long the task is waiting for the snapshot at some X interval to help with debugging if this loops somehow never terminates. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed to use waiter in AWS SDK, that sets a max time limit (retries * delay) |
||
const statusResponse = await rds.describeDBSnapshots( | ||
{ DBSnapshotIdentifier: snapshotIdentifier }, | ||
).promise(); | ||
const snapshot = statusResponse.DBSnapshots![0]; | ||
if (snapshot.Status === 'available') { | ||
return snapshot.DBSnapshotIdentifier!; | ||
} else if (snapshot.Status === 'failed') { | ||
throw Error(`Snapshot creation failed for identifier: ${snapshotIdentifier}`); | ||
} | ||
|
||
// Wait for 1 minute before checking again | ||
await new Promise((resolve) => setTimeout(resolve, 60000)); | ||
} | ||
}; | ||
|
||
return await waitForSnapshot(); | ||
} catch (error) { | ||
logger.error({ | ||
at: `${atStart}createDBSnapshot`, | ||
message: 'Failed to create DB snapshot', | ||
error, | ||
snapshotIdentifier, | ||
}); | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* @description Check if an S3 Object already exists. | ||
*/ | ||
export async function checkIfS3ObjectExists(s3: S3, s3Date: string): Promise<boolean> { | ||
export async function checkIfS3ObjectExists( | ||
s3: S3, | ||
s3Date: string, | ||
bucket: string, | ||
): Promise<boolean> { | ||
const at: string = `${atStart}checkIfS3ObjectExists`; | ||
const bucket: string = S3_BUCKET_NAME; | ||
const key: string = `${config.RDS_INSTANCE_NAME}-${s3Date}/export_info_${config.RDS_INSTANCE_NAME}-${s3Date}.json`; | ||
|
||
logger.info({ | ||
|
@@ -143,12 +218,17 @@ export async function checkIfExportJobToS3IsOngoing( | |
export async function startExportTask( | ||
rds: RDS, | ||
rdsExportIdentifier: string, | ||
bucket: string, | ||
isAutomatedSnapshot: boolean, | ||
): Promise<RDS.ExportTask> { | ||
// TODO: Add validation | ||
const sourceArnPrefix = `arn:aws:rds:${config.AWS_REGION}:${config.AWS_ACCOUNT_ID}:snapshot:rds:`; | ||
let sourceArnPrefix: string = `arn:aws:rds:${config.AWS_REGION}:${config.AWS_ACCOUNT_ID}:snapshot:`; | ||
if (isAutomatedSnapshot) { | ||
sourceArnPrefix = sourceArnPrefix.concat('rds:'); | ||
} | ||
const awsResponse: RDS.ExportTask = await rds.startExportTask({ | ||
ExportTaskIdentifier: rdsExportIdentifier, | ||
S3BucketName: S3_BUCKET_NAME, | ||
S3BucketName: bucket, | ||
KmsKeyId: config.KMS_KEY_ARN, | ||
IamRoleArn: config.ECS_TASK_ROLE_ARN, | ||
SourceArn: `${sourceArnPrefix}${rdsExportIdentifier}`, | ||
|
@@ -216,7 +296,7 @@ export async function startAthenaQuery( | |
Database: config.ATHENA_DATABASE_NAME, | ||
}, | ||
ResultConfiguration: { | ||
OutputLocation: `${S3_LOCATION_PREFIX}/output/${timestamp}`, | ||
OutputLocation: `${RESEARCH_SNAPSHOT_S3_LOCATION_PREFIX}/output/${timestamp}`, | ||
}, | ||
WorkGroup: config.ATHENA_WORKING_GROUP, | ||
}).promise(); | ||
|
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