-
Notifications
You must be signed in to change notification settings - Fork 14
Config version limit #1202
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
Open
tathagat2241
wants to merge
8
commits into
main
Choose a base branch
from
config-version-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Config version limit #1202
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e797fca
feat: implement strict 500 version limit for configurations
9d9a695
feat: improve version cleanup logging and code readability
9e6dcb1
feat: simplify version cleanup and add one-time cleanup script
b468a81
feat: improve error handling in cleanup script
9fde4cc
Merge branch 'main' into feature/config-version-limit
f1a7cd2
changed max version value to retain.
b401b42
chore: update MAX_VERSIONS to 499 and fix unit tests
62685b1
kept max version to retain at 500.
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
191 changes: 191 additions & 0 deletions
191
packages/spacecat-shared-data-access/scripts/cleanup-old-configuration-versions.js
This file contains hidden or 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,191 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| /* | ||
| * Copyright 2024 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| /* eslint-disable no-console, import/no-named-as-default, max-len */ | ||
|
|
||
| /** | ||
| * ONE-TIME CLEANUP SCRIPT | ||
| * | ||
| * This script deletes old configuration versions to bring the total count down to 500. | ||
| * It keeps the newest 500 versions and deletes all older versions. | ||
| * | ||
| * ⚠️ WARNING: This script is for ONE-TIME use only and should NEVER be merged to main. | ||
| * Run this script in production BEFORE deploying the automatic version limit feature. | ||
| * | ||
| * Usage: | ||
| * DRY_RUN=true node scripts/cleanup-old-configuration-versions.js | ||
| * node scripts/cleanup-old-configuration-versions.js | ||
| * | ||
| * Environment Variables Required: | ||
| * - DYNAMO_TABLE_NAME: The DynamoDB table name | ||
| * - AWS_REGION: AWS region (default: us-east-1) | ||
| * - DRY_RUN: Set to 'true' to preview deletions without executing (optional) | ||
| */ | ||
|
|
||
| import createDataAccess from '../src/index.js'; | ||
|
|
||
| const MAX_VERSIONS = 500; | ||
| const BATCH_SIZE = 25; // DynamoDB batch write limit | ||
|
|
||
| /** | ||
| * Main cleanup function | ||
| */ | ||
| async function cleanupOldConfigVersions() { | ||
| const isDryRun = process.env.DRY_RUN === 'true'; | ||
|
|
||
| console.log('='.repeat(80)); | ||
| console.log('Configuration Version Cleanup Script'); | ||
| console.log('='.repeat(80)); | ||
| console.log(`Mode: ${isDryRun ? 'DRY RUN (preview only)' : 'LIVE DELETION'}`); | ||
| console.log(`Target: Keep ${MAX_VERSIONS} newest versions, delete the rest`); | ||
| console.log(`Table: ${process.env.DYNAMO_TABLE_NAME || 'NOT SET'}`); | ||
| console.log('='.repeat(80)); | ||
| console.log(''); | ||
|
|
||
| if (!process.env.DYNAMO_TABLE_NAME) { | ||
| console.error('ERROR: DYNAMO_TABLE_NAME environment variable is required'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| let dataAccess; | ||
|
|
||
| try { | ||
| // Initialize data access | ||
| console.log('Initializing data access...'); | ||
|
|
||
| // Create data access (reads from environment variables) | ||
| dataAccess = createDataAccess({ | ||
| log: console, | ||
| }); | ||
|
|
||
| // Verify Configuration entity is available | ||
| if (!dataAccess || !dataAccess.Configuration) { | ||
| console.error('ERROR: Configuration entity is not available'); | ||
| console.error(' Troubleshooting:'); | ||
| console.error(' 1. Ensure DYNAMO_TABLE_NAME is set correctly'); | ||
| console.error(' 2. Ensure AWS credentials are configured'); | ||
| console.error(' 3. Try running: npm install (to ensure all dependencies are installed)'); | ||
| console.error(''); | ||
| console.error(' Current environment:'); | ||
| console.error(` - DYNAMO_TABLE_NAME: ${process.env.DYNAMO_TABLE_NAME || 'NOT SET'}`); | ||
| console.error(` - AWS_REGION: ${process.env.AWS_REGION || 'NOT SET'}`); | ||
| console.error(` - AWS_ACCESS_KEY_ID: ${process.env.AWS_ACCESS_KEY_ID ? 'SET' : 'NOT SET'}`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const { Configuration } = dataAccess; | ||
|
|
||
| // Query all configuration versions | ||
| console.log('Querying all configuration versions...'); | ||
| const allConfigs = await Configuration.all({}, { order: 'desc' }); | ||
| console.log(`Found ${allConfigs.length} total configuration versions`); | ||
| console.log(''); | ||
|
|
||
| // Check if cleanup is needed | ||
| if (allConfigs.length <= MAX_VERSIONS) { | ||
| console.log(`No cleanup needed. Current count (${allConfigs.length}) is within limit (${MAX_VERSIONS})`); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| // Calculate versions to delete | ||
| const versionsToDelete = allConfigs.slice(MAX_VERSIONS); | ||
| const deleteCount = versionsToDelete.length; | ||
| const batchCount = Math.ceil(deleteCount / BATCH_SIZE); | ||
|
|
||
| console.log('Cleanup Summary:'); | ||
| console.log(` Current versions: ${allConfigs.length}`); | ||
| console.log(` Target versions: ${MAX_VERSIONS}`); | ||
| console.log(` To delete: ${deleteCount}`); | ||
| console.log(` Batch size: ${BATCH_SIZE}`); | ||
| console.log(` Total batches: ${batchCount}`); | ||
| console.log(''); | ||
|
|
||
| // Show sample of versions to be deleted | ||
| console.log('Sample of versions to be deleted (oldest first):'); | ||
| const sampleCount = Math.min(10, versionsToDelete.length); | ||
| versionsToDelete.slice(-sampleCount).reverse().forEach((config, index) => { | ||
| console.log(` ${index + 1}. Version ${config.getVersion()} - Created: ${config.getCreatedAt()}`); | ||
| }); | ||
| if (versionsToDelete.length > sampleCount) { | ||
| console.log(` ... and ${versionsToDelete.length - sampleCount} more versions`); | ||
| } | ||
| console.log(''); | ||
|
|
||
| // Show sample of versions to be kept | ||
| console.log('Sample of versions to be kept (newest first):'); | ||
| const keepSample = Math.min(5, MAX_VERSIONS); | ||
| allConfigs.slice(0, keepSample).forEach((config, index) => { | ||
| console.log(` ${index + 1}. Version ${config.getVersion()} - Created: ${config.getCreatedAt()}`); | ||
| }); | ||
| console.log(` ... and ${MAX_VERSIONS - keepSample} more versions`); | ||
| console.log(''); | ||
|
|
||
| if (isDryRun) { | ||
| console.log('🔍 DRY RUN MODE: No deletions will be performed'); | ||
| console.log(' Set DRY_RUN=false or remove it to actually delete versions'); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| // Confirm before deletion | ||
| console.log('WARNING: About to delete configuration versions!'); | ||
| console.log(' This operation cannot be undone.'); | ||
| console.log(' Press Ctrl+C within 10 seconds to cancel...'); | ||
| console.log(''); | ||
|
|
||
| await new Promise((resolve) => { | ||
| setTimeout(resolve, 10000); | ||
| }); | ||
|
|
||
| // Delete in batches | ||
| console.log('Starting deletion process...'); | ||
| console.log(''); | ||
|
|
||
| for (let i = 0; i < versionsToDelete.length; i += BATCH_SIZE) { | ||
| const batch = versionsToDelete.slice(i, i + BATCH_SIZE); | ||
| const batchNumber = Math.floor(i / BATCH_SIZE) + 1; | ||
| const ids = batch.map((config) => config.getId()); | ||
|
|
||
| console.log(` Batch ${batchNumber}/${batchCount}: Deleting ${ids.length} versions...`); | ||
|
|
||
| try { | ||
| // eslint-disable-next-line no-await-in-loop | ||
| await Configuration.removeByIds(ids); | ||
| console.log(` Batch ${batchNumber} deleted successfully`); | ||
| } catch (error) { | ||
| console.error(` Batch ${batchNumber} failed:`, error.message); | ||
| throw error; // Stop on first failure | ||
| } | ||
| } | ||
|
|
||
| console.log(''); | ||
| console.log('='.repeat(80)); | ||
| console.log('CLEANUP COMPLETED SUCCESSFULLY'); | ||
| console.log('='.repeat(80)); | ||
| console.log(` Deleted: ${deleteCount} versions`); | ||
| console.log(` Remaining: ${MAX_VERSIONS} versions`); | ||
| console.log(''); | ||
| } catch (error) { | ||
| console.error(''); | ||
| console.error('='.repeat(80)); | ||
| console.error('CLEANUP FAILED'); | ||
| console.error('='.repeat(80)); | ||
| console.error('Error:', error.message); | ||
| console.error('Stack:', error.stack); | ||
| console.error(''); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| // Run the cleanup | ||
| cleanupOldConfigVersions(); | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.