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

Feature/cumulus 2309 granule parallel deletes #2057

Merged
merged 66 commits into from Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
e27d812
CUMULUS-2309 tests for parllel granule delete
npauzenga Jan 26, 2021
599acfb
CUMULUS-2309 add delete method for base PG model and test
npauzenga Jan 26, 2021
4ca1786
CUMULUS-2309 update base delete() to accept a partial record instead …
npauzenga Jan 27, 2021
83b73c8
CUMULUS-2309 fix endpoint and tests to use test db instance
npauzenga Jan 27, 2021
ecb874a
CUMULUS-2309 fix granules tests for new PG deletes
npauzenga Jan 27, 2021
a9be97b
CUMULUS-2309 delete files from PG before deleting granule
npauzenga Jan 28, 2021
cb53c9d
CUMULUS-2309 add tests and transactions for deleting granule files
npauzenga Jan 29, 2021
a0f9fb0
CUMULUS-2309 add helper functions for S3 bucket creation/deletion
npauzenga Feb 1, 2021
e5e4e69
CUMULUS-2309 refactor granule deletes to use async/await and add tests
npauzenga Feb 2, 2021
640d1fb
CUMULUS-2309 add docs and update tests for failed granule deletes
npauzenga Feb 2, 2021
80baf1b
CUMULUS-2309 add granule and file delete tests
npauzenga Feb 2, 2021
b5e1da8
CUMULUS-2309 refactor tests to remove duplication
npauzenga Feb 2, 2021
5da3da0
Merge branch 'feature-rds' into feature/CUMULUS-2309-granule-parallel…
npauzenga Feb 3, 2021
81dca05
CUMULUS-2309 move s3 delete logic from file PG model to FileUtils
npauzenga Feb 5, 2021
8838e83
Merge branch 'feature/CUMULUS-2309-granule-parallel-deletes' of https…
npauzenga Feb 5, 2021
6f231e5
CUMULUS-2309 refactor granule/file deletes
npauzenga Feb 5, 2021
6394eb3
CUMULUS-2309 refactor granule delete logic out of endpoint into lib
npauzenga Feb 8, 2021
9ec1604
CUMULUS-2309 additional tests for failed granule delete transactions
npauzenga Feb 9, 2021
ca58993
CUMULUS-2309 update granule delete to async method
npauzenga Feb 9, 2021
4d1bfd4
CUMULUS-2309 address PR comments
npauzenga Feb 9, 2021
7cde92b
CUMULUS-2309 use shared S3 methods in tests
npauzenga Feb 9, 2021
7a2399d
CUMULUS-2309 address PR comments on base model test
npauzenga Feb 9, 2021
95d2083
CUMULUS-2309 update granule model tests
npauzenga Feb 9, 2021
5e69fad
CUMULUS-2309 refactor tests per PR comments
npauzenga Feb 9, 2021
78358d4
CUMULUS-2309 refactor base model exists function
npauzenga Feb 9, 2021
bcde23b
CUMULUS-2309 specify return type on Base Model get()
npauzenga Feb 9, 2021
492ac33
CUMULUS-2309 update test title per PR comments
npauzenga Feb 9, 2021
a1ebc69
CUMULUS-2309 add docs to PG Base Model
npauzenga Feb 9, 2021
7c6fb74
CUMULUS-2309 parameterize arguments to deleteGranuleAndFiles
npauzenga Feb 10, 2021
92061a3
Alpha sort arguments
npauzenga Feb 10, 2021
23299fa
Alpha sort arguments
npauzenga Feb 10, 2021
19d1a6c
CUMULUS-2309 alpha sort vars in tests
npauzenga Feb 10, 2021
d723024
CUMULUS-2309 refactor granule delete flow
npauzenga Feb 12, 2021
b1f5358
CUMULUS-2309 address PR comments
npauzenga Feb 12, 2021
158c8e5
Refactor test setup based on PR comment
npauzenga Feb 12, 2021
5755cf6
CUMULUS-2309 address PR comments
npauzenga Feb 12, 2021
c975861
CUMULUS-2309 address PR comments
npauzenga Feb 12, 2021
4f1ff53
CUMULUS-2309 refactor granule delete to allow a missing postgres granule
npauzenga Feb 12, 2021
c16e673
CUMULUS-2309 update test comment
npauzenga Feb 12, 2021
5894548
CUMULUS-2309 update base model delete test per PR comments
npauzenga Feb 12, 2021
697ef41
CUMULUS-2309 add search() method to base pg model
npauzenga Feb 12, 2021
6143876
CUMULUS-2309 update granule tests per PR comments
npauzenga Feb 12, 2021
db2bfb8
CUMULUS-2309 update tests per PR comments
npauzenga Feb 12, 2021
26afa2a
CUMULUS-2309 refactor tests per PR comments
npauzenga Feb 12, 2021
bbf2fb2
CUMULUS-2309 refactor granule delete library function to separate file
npauzenga Feb 15, 2021
16c3801
CUMULUS-2309 add tests to verify granules deleted from PG and Dynamo
npauzenga Feb 15, 2021
6be202d
CUMULUS-2309 add s3 bucket cleanup in granule delete tests
npauzenga Feb 15, 2021
30831e8
Merge branch 'feature-rds' into feature/CUMULUS-2309-granule-parallel…
npauzenga Feb 15, 2021
35dc237
CUMULUS-2309 remove unused params in granule delete test
npauzenga Feb 15, 2021
667e41a
CUMULUS-2309 address PR comments
npauzenga Feb 15, 2021
1f11eef
CUMULUS-2309 catch get errors when no record is found
npauzenga Feb 15, 2021
6a4ad3e
CUMULUS-2309 catch error from basepgmodel.get
npauzenga Feb 16, 2021
6154c8c
CUMULUS-2309 update catch for get requests to throw error if appropriate
npauzenga Feb 16, 2021
61b9f7a
CUMULUS-2309 move granule published check out of PG model
npauzenga Feb 16, 2021
2af8a4f
CUMULUS-2309 throw error in data migrations
npauzenga Feb 16, 2021
4bb208e
CUMULUS-2309 update comments
npauzenga Feb 16, 2021
f80bc77
CUMULUS-2309 remove delete() method from PG Granule model
npauzenga Feb 16, 2021
6133ec5
CUMULUS-2309 remove unused requires
npauzenga Feb 16, 2021
a83b7c7
CUMULUS-2309 fix test errors
npauzenga Feb 16, 2021
723c862
CUMULUS-2309 clean up granule delete tests
npauzenga Feb 16, 2021
94bf3c1
CUMULUS-2309 remove Dynamo model tests for deleting S3 files
npauzenga Feb 16, 2021
c6191fa
CUMULUS-2309 add logic for updating postgres record after removing gr…
npauzenga Feb 18, 2021
50639b4
Merge branch 'feature-rds' into feature/CUMULUS-2309-granule-parallel…
npauzenga Feb 18, 2021
38042e1
CUMULUS-2309 get the collection before getting the granule in removeF…
npauzenga Feb 19, 2021
27a6dc1
Merge branch 'feature/CUMULUS-2309-granule-parallel-deletes' of https…
npauzenga Feb 19, 2021
1eb64a7
CUMULUS-2309 use collection ID helper from message package
npauzenga Feb 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -43,6 +43,7 @@ This is for installation for Cumulus development. See the [Cumulus deployment i
- Docker (only required for testing)
- docker-compose (only required for testing `pip install docker-compose`)
- Python 3.6+
- [pipenv](https://pypi.org/project/pipenv/)
npauzenga marked this conversation as resolved.
Show resolved Hide resolved

Install the correct node version:

Expand Down
37 changes: 25 additions & 12 deletions packages/api/endpoints/granules.js
Expand Up @@ -10,7 +10,9 @@ const {
DeletePublishedGranule,
RecordDoesNotExist,
} = require('@cumulus/errors');
const { GranulePgModel, FilePgModel, getKnexClient } = require('@cumulus/db');

const { deleteGranuleAndFiles } = require('../lib/granules');
const { asyncOperationEndpointErrorHandler } = require('../app/middleware');
const Search = require('../es/search').Search;
const indexer = require('../es/indexer');
Expand Down Expand Up @@ -140,37 +142,48 @@ async function del(req, res) {
log.info(`granules.del ${granuleId}`);

const granuleModelClient = new models.Granule();
const granulePgModel = new GranulePgModel();
const filePgModel = new FilePgModel();

let granule;
const knex = await getKnexClient({ env: process.env });

let dynamoGranule;
let pgGranule;
try {
granule = await granuleModelClient.getRecord({ granuleId });
dynamoGranule = await granuleModelClient.getRecord({ granuleId });
pgGranule = await granulePgModel.get(knex, { granule_id: granuleId });
markdboyd marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
if (error instanceof RecordDoesNotExist) {
return res.boom.notFound(error);
}
throw error;
}

if (granule.detail) {
return res.boom.badRequest(granule);
// DEPRECATED this can be removed
npauzenga marked this conversation as resolved.
Show resolved Hide resolved
if (dynamoGranule.detail) {
return res.boom.badRequest(dynamoGranule);
}

try {
await granuleModelClient.delete(granule);
} catch (error) {
if (error instanceof DeletePublishedGranule) {
return res.boom.badRequest(error.message);
}
throw error;
if (pgGranule.published) {
Jkovarik marked this conversation as resolved.
Show resolved Hide resolved
throw new DeletePublishedGranule('You cannot delete a granule that is published to CMR. Remove it from CMR first');
}

await deleteGranuleAndFiles(
knex,
dynamoGranule,
pgGranule,
filePgModel,
granulePgModel,
granuleModelClient
);

if (inTestMode()) {
const esClient = await Search.es(process.env.ES_HOST);
await indexer.deleteRecord({
esClient,
id: granuleId,
type: 'granule',
parent: granule.collectionId,
parent: dynamoGranule.collectionId,
index: process.env.ES_INDEX,
ignore: [404],
});
Expand Down
3 changes: 2 additions & 1 deletion packages/api/lib/FileUtils.js
Expand Up @@ -7,9 +7,10 @@ const isInteger = require('lodash/isInteger');
const partial = require('lodash/partial');
const pick = require('lodash/pick');
const urljoin = require('url-join');
const { getObjectSize, parseS3Uri } = require('@cumulus/aws-client/S3');
const { getObjectSize, parseS3Uri, deleteS3Object } = require('@cumulus/aws-client/S3');
const { removeNilProperties } = require('@cumulus/common/util');
const Logger = require('@cumulus/logger');
const { UnparsableFileLocationError } = require('@cumulus/errors');
const schemas = require('../models/schemas');

const log = new Logger({ sender: 'api/lib/FileUtils' });
Expand Down
29 changes: 29 additions & 0 deletions packages/api/lib/granules.js
Expand Up @@ -3,6 +3,8 @@
const awsClients = require('@cumulus/aws-client/services');
const isInteger = require('lodash/isInteger');
const isNil = require('lodash/isNil');
const { deleteS3Object } = require('@cumulus/aws-client/S3');
const pMap = require('p-map');

const FileUtils = require('./FileUtils');

Expand Down Expand Up @@ -62,10 +64,37 @@ function getGranuleProductVolume(granuleFiles = []) {
.reduce((x, y) => x + y, 0);
}

// TODO default arguments
Jkovarik marked this conversation as resolved.
Show resolved Hide resolved
const deleteGranuleAndFiles = async (
Jkovarik marked this conversation as resolved.
Show resolved Hide resolved
knex,
dynamoGranule,
pgGranule,
filePgModel,
granulePgModel,
granuleModelClient
) => {
const files = await knex(filePgModel.tableName)
markdboyd marked this conversation as resolved.
Show resolved Hide resolved
.where({ granule_cumulus_id: pgGranule.cumulus_id });

await knex.transaction(async (trx) => {
markdboyd marked this conversation as resolved.
Show resolved Hide resolved
await pMap(
files,
(file) =>
knex.transaction(async (fileTrx) => {
await filePgModel.delete(fileTrx, { cumulus_id: file.cumulus_id });
await deleteS3Object(file.bucket, file.key);
markdboyd marked this conversation as resolved.
Show resolved Hide resolved
})
);
await granulePgModel.delete(trx, pgGranule);
await granuleModelClient.delete(dynamoGranule);
});
};

module.exports = {
translateGranule,
getExecutionProcessingTimeInfo,
getGranuleTimeToArchive,
getGranuleTimeToPreprocess,
getGranuleProductVolume,
deleteGranuleAndFiles,
};