-
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 12 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,50 @@ | ||
import config from '../../src/config'; | ||
import { asMock } from '@dydxprotocol-indexer/dev'; | ||
import { createDBSnapshot, getMostRecentDBSnapshotIdentifier } 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(); | ||
}); | ||
|
||
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(); | ||
}); | ||
|
||
it('No existing snapshot', async () => { | ||
asMock(getMostRecentDBSnapshotIdentifier).mockImplementation( | ||
async () => Promise.resolve(undefined), | ||
); | ||
|
||
await takeFastSyncSnapshotTask(); | ||
|
||
expect(createDBSnapshot).toHaveBeenCalled(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,30 +16,177 @@ enum ExportTaskStatus { | |
COMPLETE = 'complete', | ||
} | ||
|
||
const S3_BUCKET_NAME = config.S3_BUCKET_ARN.split(':::')[1]; | ||
export const S3_BUCKET_NAME = config.S3_BUCKET_ARN.split(':::')[1]; | ||
export const S3_LOCATION_PREFIX = `s3://${S3_BUCKET_NAME}`; | ||
|
||
/** | ||
* Delete snapshots for the RDS instance older than the specified number of days. | ||
* Defaults to 7 days. | ||
* @param rds | ||
* @param daysOld | ||
*/ | ||
export async function deleteOldFastSyncSnapshots(rds: RDS, daysOld: number = 7): Promise<void> { | ||
try { | ||
const cutoffTime: number = new Date().getTime() - daysOld * 24 * 60 * 60 * 1000; | ||
let marker; | ||
do { | ||
const response: RDS.DBSnapshotMessage = await rds.describeDBSnapshots({ | ||
DBInstanceIdentifier: config.RDS_INSTANCE_NAME, | ||
MaxRecords: 20, // Maximum number of records per page | ||
Marker: marker, // Marker for pagination | ||
}).promise(); | ||
|
||
if (response.DBSnapshots === undefined) { | ||
logger.error({ | ||
at: `${atStart}deleteOldSnapshots`, | ||
message: `No DB snapshots found with identifier: ${config.RDS_INSTANCE_NAME}`, | ||
}); | ||
return; | ||
} | ||
|
||
// Filter for fast sync snapshots older than cutoffTime | ||
const oldFastSyncSnapshots = response.DBSnapshots.filter((snapshot) => { | ||
if (!snapshot.DBSnapshotIdentifier!.startsWith( | ||
config.FAST_SYNC_SNAPSHOT_IDENTIFIER_PREFIX, | ||
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: can we log an error here? Technically there shouldn't be anything that doesn't match our fast sync identifier right? 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. the rds automated daily snapshots will also be included here |
||
)) { | ||
return false; | ||
} | ||
const snapshotDate = snapshot.SnapshotCreateTime!.getTime(); | ||
return snapshotDate < cutoffTime; | ||
}); | ||
|
||
// Delete each old snapshot | ||
for (const snapshot of oldFastSyncSnapshots) { | ||
logger.info({ | ||
at: `${atStart}deleteOldSnapshots`, | ||
message: 'Deleting snapshot', | ||
snapshotIdentifier: snapshot.DBSnapshotIdentifier, | ||
}); | ||
const snapshotResult: RDS.Types.DeleteDBSnapshotResult = await rds.deleteDBSnapshot( | ||
{ DBSnapshotIdentifier: snapshot.DBSnapshotIdentifier! }, | ||
).promise(); | ||
logger.info({ | ||
at: `${atStart}deleteOldSnapshots`, | ||
message: 'Snapshot deleted', | ||
snapshotIdentifier: snapshotResult.DBSnapshot!.DBSnapshotIdentifier!, | ||
}); | ||
} | ||
|
||
marker = response.Marker; | ||
} while (marker); | ||
} catch (error) { | ||
logger.error({ | ||
at: `${atStart}deleteOldSnapshots`, | ||
message: 'Error deleting old snapshots', | ||
error, | ||
}); | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* @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 snapshotIdentifierPrefixInclude | ||
* @param snapshotIdentifierPrefixExclude - Exclude snapshots with snapshot identifier | ||
* that starts with snapshotIdentifierPrefixExclude | ||
*/ | ||
// TODO(CLOB-672): Verify this function returns the most recent DB snapshot. | ||
export async function getMostRecentDBSnapshotIdentifier(rds: RDS): Promise<string> { | ||
const awsResponse: RDS.DBSnapshotMessage = await rds.describeDBSnapshots({ | ||
DBInstanceIdentifier: config.RDS_INSTANCE_NAME, | ||
MaxRecords: 20, // this is the minimum | ||
}).promise(); | ||
export async function getMostRecentDBSnapshotIdentifier( | ||
rds: RDS, | ||
snapshotIdentifierPrefixInclude?: string, | ||
snapshotIdentifierPrefixExclude?: string, | ||
): Promise<string | undefined> { | ||
let snapshots: RDS.DBSnapshotList = []; | ||
let marker: string | undefined; | ||
|
||
do { | ||
const awsResponse: RDS.DBSnapshotMessage = await rds.describeDBSnapshots({ | ||
DBInstanceIdentifier: config.RDS_INSTANCE_NAME, | ||
MaxRecords: 20, // Maximum number of records per page | ||
Marker: marker, // Marker for pagination | ||
}).promise(); | ||
|
||
if (awsResponse.DBSnapshots === undefined) { | ||
throw Error(`No DB snapshots found with identifier: ${config.RDS_INSTANCE_NAME}`); | ||
} | ||
|
||
if (awsResponse.DBSnapshots === undefined) { | ||
throw Error(`No DB snapshots found with identifier: ${config.RDS_INSTANCE_NAME}`); | ||
snapshots = snapshots.concat(awsResponse.DBSnapshots); | ||
marker = awsResponse.Marker; | ||
} while (marker); | ||
|
||
// Filter snapshots based on include/exclude prefixes | ||
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)); | ||
} | ||
|
||
// Sort snapshots by creation time in descending order | ||
snapshots.sort((a, b) => b.SnapshotCreateTime!.getTime() - a.SnapshotCreateTime!.getTime()); | ||
|
||
logger.info({ | ||
at: `${atStart}getMostRecentDBSnapshotIdentifier`, | ||
message: 'Described snapshots for database', | ||
mostRecentSnapshot: awsResponse.DBSnapshots[awsResponse.DBSnapshots.length - 1], | ||
mostRecentSnapshot: snapshots[0], | ||
}); | ||
|
||
return awsResponse.DBSnapshots[awsResponse.DBSnapshots.length - 1].DBSnapshotIdentifier!; | ||
// Return the latest snapshot identifier | ||
return snapshots[0]?.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(); | ||
|
||
// Wait for the DB snapshot to become available with the specified waiter configuration | ||
await rds.waitFor('dBSnapshotAvailable', { | ||
DBSnapshotIdentifier: snapshotIdentifier, | ||
$waiter: { | ||
delay: 60, // 60 seconds delay between each request | ||
maxAttempts: 10, // Maximum of 10 attempts | ||
}, | ||
}).promise(); | ||
|
||
// Once it's available, retrieve its details | ||
const statusResponse = await rds.describeDBSnapshots( | ||
{ DBSnapshotIdentifier: snapshotIdentifier }, | ||
).promise(); | ||
|
||
const snapshot = statusResponse.DBSnapshots![0]; | ||
if (snapshot.Status === 'available') { | ||
return snapshot.DBSnapshotIdentifier!; | ||
} else { | ||
throw Error(`Snapshot is not in the available state: Status is ${snapshot.Status}`); | ||
} | ||
} catch (error) { | ||
logger.error({ | ||
at: `${atStart}createDBSnapshot`, | ||
message: 'Failed to create DB snapshot', | ||
error, | ||
snapshotIdentifier, | ||
}); | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { logger, stats } from '@dydxprotocol-indexer/base'; | ||
import RDS from 'aws-sdk/clients/rds'; | ||
import { DateTime } from 'luxon'; | ||
|
||
import config from '../config'; | ||
import { | ||
createDBSnapshot, | ||
deleteOldFastSyncSnapshots, | ||
getMostRecentDBSnapshotIdentifier, | ||
} from '../helpers/aws'; | ||
|
||
const statStart: string = `${config.SERVICE_NAME}.fast_sync_export_db_snapshot`; | ||
|
||
/** | ||
* Checks if the difference between two dates is less than a given interval. | ||
* | ||
* @param startDate | ||
* @param endDate | ||
* @param intervalMs | ||
*/ | ||
function isDifferenceLessThanInterval( | ||
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: move this under runTask so that the top level function is at the top of the file 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 |
||
startDate: string, | ||
endDate: string, | ||
intervalMs: number, | ||
): boolean { | ||
const parseDateString = (dateStr: string): Date => { | ||
const [year, month, day, hour, minute] = dateStr.split('-').map(Number); | ||
return new Date(year, month, day, hour, minute); | ||
}; | ||
|
||
// Parse the date strings | ||
const parsedDate1 = parseDateString(startDate); | ||
const parsedDate2 = parseDateString(endDate); | ||
|
||
// Calculate the difference in milliseconds | ||
const differenceInMilliseconds = Math.abs(parsedDate1.getTime() - parsedDate2.getTime()); | ||
|
||
// Compare with the interval | ||
return differenceInMilliseconds < intervalMs; | ||
} | ||
|
||
export default async function runTask(): Promise<void> { | ||
const at: string = 'fast-sync-export-db-snapshot#runTask'; | ||
logger.info({ at, message: 'Starting task.' }); | ||
|
||
const rds: RDS = new RDS(); | ||
|
||
const dateString: string = DateTime.utc().toFormat('yyyy-MM-dd-HH-mm'); | ||
const snapshotIdentifier: string = `${config.FAST_SYNC_SNAPSHOT_IDENTIFIER_PREFIX}-${config.RDS_INSTANCE_NAME}-${dateString}`; | ||
// check the time of the last snapshot | ||
const lastSnapshotIdentifier: string | undefined = await getMostRecentDBSnapshotIdentifier( | ||
rds, | ||
config.FAST_SYNC_SNAPSHOT_IDENTIFIER_PREFIX, | ||
); | ||
if (lastSnapshotIdentifier !== undefined) { | ||
const s3Date: string = lastSnapshotIdentifier.split(config.RDS_INSTANCE_NAME)[1].slice(1); | ||
if ( | ||
isDifferenceLessThanInterval( | ||
s3Date, | ||
dateString, | ||
config.LOOPS_INTERVAL_MS_TAKE_FAST_SYNC_SNAPSHOTS, | ||
) | ||
) { | ||
stats.increment(`${statStart}.existingDbSnapshot`, 1); | ||
logger.info({ | ||
at, | ||
message: 'Last fast sync db snapshot was taken less than the interval ago', | ||
interval: config.LOOPS_INTERVAL_MS_TAKE_FAST_SYNC_SNAPSHOTS, | ||
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: log the s3Date and dateString 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 |
||
}); | ||
return; | ||
} | ||
} | ||
// Create the DB snapshot | ||
const startSnapshot: number = Date.now(); | ||
const createdSnapshotIdentifier: string = await | ||
createDBSnapshot(rds, snapshotIdentifier, config.RDS_INSTANCE_NAME); | ||
logger.info({ at, message: 'Created DB snapshot.', snapshotIdentifier: createdSnapshotIdentifier }); | ||
stats.timing(`${statStart}.createDbSnapshot`, Date.now() - startSnapshot); | ||
const startDeleteOldSnapshot: number = Date.now(); | ||
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: separate into a separate function 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 |
||
// Delete old snapshots. | ||
await deleteOldFastSyncSnapshots(rds); | ||
stats.timing(`${statStart}.deleteOldSnapshots`, Date.now() - startDeleteOldSnapshot); | ||
} |
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