Skip to content

Commit

Permalink
CUMULUS-3841: Increase fetchRules page size to improve performance. (#…
Browse files Browse the repository at this point in the history
…3779)

* Increase limit of records per page to be 100 from fetchRules function

* Pass pageSize as a parameter from fetchRules function

* minor code cleaning

* Adds change log entry

* Adds unit tests for fetchRules page size argument

* Improves fetchRules pageSize unit test

* Fixes linter errors

---------

Co-authored-by: Hailiang Zhang <[email protected]>
  • Loading branch information
2 people authored and Nnaga1 committed Aug 23, 2024
1 parent 2f9dbd6 commit 05912dd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ degraded execution table operations.
- **CUMULUS-3449**
- Updated `@cumulus/db` package and configure knex hook postProcessResponse to convert the return string
from columns ending with "cumulus_id" to number.
- **CUMULUS-3841**
- Increased `fetchRules` page size to default to 100 instead of 10. This improves overall query time when
fetching all rules such as in `sqsMessageConsumer`.

### Fixed

Expand Down
6 changes: 4 additions & 2 deletions packages/api/lib/rulesHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ const log = new Logger({ sender: '@cumulus/rulesHelpers' });
*
* @param {Object} params - function params
* @param {number} [params.pageNumber] - current page of API results
* @param {number} [params.pageSize] - database query page size used when calling listRules
* @param {Array<Object>} [params.rules] - partial rules Array
* @param {Object} [params.queryParams] - API query params, empty query returns all rules
* @returns {Promise<Array<Object>>} all matching rules
*/
async function fetchRules({ pageNumber = 1, rules = [], queryParams = {} }) {
const query = { ...queryParams, page: pageNumber };
async function fetchRules({ pageNumber = 1, pageSize = 100, rules = [], queryParams = {} }) {
const query = { ...queryParams, page: pageNumber, limit: pageSize };
const apiResponse = await listRules({
prefix: process.env.stackName,
query,
Expand All @@ -70,6 +71,7 @@ async function fetchRules({ pageNumber = 1, rules = [], queryParams = {} }) {
if (responseBody.results.length > 0) {
return fetchRules({
pageNumber: (pageNumber + 1),
pageSize,
rules: rules.concat(responseBody.results),
queryParams,
});
Expand Down
23 changes: 20 additions & 3 deletions packages/api/tests/lib/rules/test-rulesHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,37 @@ test.serial('fetchRules invokes API to fetch rules', async (t) => {
t.true(listRulesStub.calledOnce);
});

test.serial('fetchRules allows for configurable page size', async (t) => {
listRulesStub.onFirstCall().callsFake(({ query }) => {
t.is(query.limit, 100);
return { body: JSON.stringify({ results: [] }) };
});

listRulesStub.onSecondCall().callsFake(({ query }) => {
t.is(query.limit, 50);
return { body: JSON.stringify({ results: [] }) };
});

await rulesHelpers.fetchRules({});
await rulesHelpers.fetchRules({ pageSize: 50 });

t.true(listRulesStub.calledTwice);
});

test.serial('fetchRules pages through results until reaching an empty list', async (t) => {
const rule1 = { name: 'rule-1' };
const rule2 = { name: 'rule-2' };
const firstCallArgs = {
prefix: process.env.stackName,
query: { page: 1 },
query: { limit: 100, page: 1 },
};
const secondCallArgs = {
prefix: process.env.stackName,
query: { page: 2 },
query: { limit: 100, page: 2 },
};
const thirdCallArgs = {
prefix: process.env.stackName,
query: { page: 3 },
query: { limit: 100, page: 3 },
};
listRulesStub.onFirstCall().callsFake((params) => {
t.deepEqual(params, firstCallArgs);
Expand Down

0 comments on commit 05912dd

Please sign in to comment.