-
Notifications
You must be signed in to change notification settings - Fork 108
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
Reducing cache-control for scheduled transactions. #9327
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: mgoelswirlds <[email protected]>
Signed-off-by: mgoelswirlds <[email protected]>
hedera-mirror-rest/__tests__/specs/transactions/{id}/no-params.json
Outdated
Show resolved
Hide resolved
…edule-transactions
…edule-transactions
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #9327 +/- ##
=========================================
Coverage 92.68% 92.68%
Complexity 6943 6943
=========================================
Files 896 896
Lines 29553 29576 +23
Branches 3745 3751 +6
=========================================
+ Hits 27390 27412 +22
Misses 1399 1399
- Partials 764 765 +1 ☔ View full report in Codecov by Sentry. |
Signed-off-by: mgoelswirlds <[email protected]>
...ra-mirror-rest/__tests__/specs/transactions/{id}/scheduled-transactions/scheduled-false.json
Outdated
Show resolved
Hide resolved
Signed-off-by: mgoelswirlds <[email protected]>
Signed-off-by: mgoelswirlds <[email protected]>
import { | ||
requestPathLabel, | ||
requestStartTime, | ||
responseContentType, |
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.
We should update network supply to use the new response headers and remove responseContentType
to make it consistent and not have two ways to possibly set content type.
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.
I did try changing this. Works for network supply but many other tests fail complaining about the Invalid media type
. Looking further into what needs to be resolved here.
I still need to address the new comments on the PR @steven-sheehy |
...irror-rest/__tests__/specs/transactions/{id}/scheduled-create-non-scheduled-transaction.json
Outdated
Show resolved
Hide resolved
…edule-transactions
Signed-off-by: mgoelswirlds <[email protected]>
Removing the use of responseHeadersLabel. Signed-off-by: mgoelswirlds <[email protected]>
Signed-off-by: mgoelswirlds <[email protected]>
Signed-off-by: mgoelswirlds <[email protected]>
Signed-off-by: mgoelswirlds <[email protected]>
Signed-off-by: mgoelswirlds <[email protected]>
Signed-off-by: mgoelswirlds <[email protected]>
Signed-off-by: mgoelswirlds <[email protected]>
expect(mockResponse.set).toHaveBeenNthCalledWith(1, headers.default); | ||
expect(mockResponse.set).toHaveBeenNthCalledWith(2, headers.path[mockRequest.route.path]); | ||
expect(mockResponse.set).toHaveBeenNthCalledWith(3, 'Content-Type', 'application/json; charset=utf-8'); | ||
expect(mockResponse.set).toHaveBeenCalledWith({'Content-type': 'application/json; charset=utf-8'},headers.path[mockRequest.route.path]); |
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.
I have spent a lot of time trying to fix these 2 tests with this new approach. But have not been successful.
Would like to get the other parta approved and continue working on these on the side.
@@ -363,8 +363,13 @@ const getSuccessProtoIds = () => { | |||
]; | |||
}; | |||
|
|||
const isSuccessful = (result) => { | |||
return getSuccessProtoIds().includes(result); |
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.
For performance, we should calculate getSuccessProtoIds() once and store as a static. Prior to this method, its other uses were separately cached so it wasn't a concern. Now it's called in a loop inside our highest used API.
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.
I have added a constant which calls getSuccessProtoIds()
once.
|
||
const code = res.locals.statusCode; | ||
const contentType = res.locals[responseContentType] || APPLICATION_JSON; | ||
const contentType = mergedHeaders['Content-type']; |
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.
Use CONTENT_TYPE_HEADER
here and other places that put responseHeadersLabel
const contentType = mergedHeaders['Content-type']; | ||
|
||
const linksNext = res.locals.responseData.links?.next; | ||
res.status(code); | ||
res.set(CONTENT_TYPE_HEADER, contentType); |
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.
We shouldn't need to set content-type header since it should've already been set above via res.set(mergedHeaders).
const contentType = mergedHeaders['Content-type']; | |
const linksNext = res.locals.responseData.links?.next; | |
res.status(code); | |
res.set(CONTENT_TYPE_HEADER, contentType); | |
const linksNext = res.locals.responseData.links?.next; | |
res.status(code); |
const mergedHeaders = { | ||
...headers.default, | ||
...(headers.path[path] ?? {}), | ||
...(res.locals[responseHeadersLabel] ?? {}), | ||
}; | ||
res.set(mergedHeaders); |
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.
Have to be careful since HTTP headers are case insensitive but this mergedHeaders
object is not. However, express set() should be case insensitive. We should have a unit test that has different mixed case for default, path and responseHeadersLabel and ensure they are all merged into one entry returned via res.get()
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.
I tried adding a unit test, but that would need to work with mocking the result and then there seemed no point to hardcode what set and get would do.
This needs fixing but this was the tentative test:
test('should merge headers in a case insesitive manner', async () => {
const path = '/api/v1/transactions/:transactionId';
headers.path[path] = { "Cache-control": "public, max-age=1",
"content-Type": "application/json; charset=utf-8" };
mockResponse.locals[responseHeadersLabel] = { "cache-Control": "public, max-age=1",
"Content-type": 'text/plain; charset=utf-8' };
await responseHandler(mockRequest, mockResponse, null);
expect(mockResponse.get('content-type')).toHaveLastReturnedWith(mockResponse.locals[responseHeadersLabel]['Content-type']);
});
The network/supply spec tests do test the case insensitiveness to some extend atleast for headers.default
and res.locals[responseHeadersLabel]
If that is acceptable , I can work on a future PR to add a more comprehensive test for this .
hedera-mirror-rest/transactions.js
Outdated
function getTransactionsByIdOrHashCacheControlHeader(transactionsRows, isTransactionHash, scheduled) { | ||
if (scheduled || isTransactionHash) { |
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.
As I mentioned previously.
function getTransactionsByIdOrHashCacheControlHeader(transactionsRows, isTransactionHash, scheduled) { | |
if (scheduled || isTransactionHash) { | |
function getTransactionsByIdOrHashCacheControlHeader(transactionsRows) { | |
if (transactionsRows.length < 2) { |
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.
not entirely true. it's possible to have scheduled=false
query param and return multiple transactions for the same transaction id. Then we can have a successful schedulecreate in the response with all others failed, ideally we should use the longer max-age because the existence of the scheduled=?
query param.
However given that such scenario happens rarely, and using a shorter max-age doesn't hurt function, agree we can go with the simpler approach.
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.
I missed that this scheduled variable represents the query param exists and not that it exists and is set to true, hence my comment. We can keep the scheduled check but omit isTransactionHash and just use list size for that.
@@ -87,7 +87,8 @@ hedera: | |||
response: | |||
compression: true | |||
headers: | |||
default: { "cache-control": "public, max-age=1" } | |||
default: { "cache-control": "public, max-age=1", | |||
"Content-type": "application/json; charset=utf-8"} |
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.
Use consistent casing.
"Content-type": "application/json; charset=utf-8"} | |
"content-type": "application/json; charset=utf-8"} |
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.
Actually other places in the code so you "Content-type"
but will change it here.
hedera-mirror-rest/transactions.js
Outdated
if (transaction.type === scheduleCreateProtoId) { | ||
if (TransactionResult.isSuccessful(transaction.result)) { |
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.
We can combine these two if statements since it's not like a schedule create can have scheduled=true
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.
ok.
hedera-mirror-rest/transactions.js
Outdated
if (successScheduleCreateTimestamp !== undefined | ||
&& (utils.nowInNs() - successScheduleCreateTimestamp) < maxTransactionConsensusTimestampRangeNs ) { |
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.
nit: Readability
if (successScheduleCreateTimestamp !== undefined | |
&& (utils.nowInNs() - successScheduleCreateTimestamp) < maxTransactionConsensusTimestampRangeNs ) { | |
const elapsed = utils.nowInNs() - successScheduleCreateTimestamp; | |
if (successScheduleCreateTimestamp && elapsed < maxTransactionConsensusTimestampRangeNs ) { |
const isTransactionHash = isValidTransactionHash(transactionIdOrHash); | ||
let scheduledParamExists = false; |
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.
Think updates to this method can be undone with my other comment.
Signed-off-by: mgoelswirlds <[email protected]>
…edule-transactions
Signed-off-by: mgoelswirlds <[email protected]>
Signed-off-by: mgoelswirlds <[email protected]>
Signed-off-by: mgoelswirlds <[email protected]>
Signed-off-by: mgoelswirlds <[email protected]>
Signed-off-by: mgoelswirlds <[email protected]>
Signed-off-by: mgoelswirlds <[email protected]>
Quality Gate passedIssues Measures |
import {NotFoundError} from '../errors'; | ||
import {JSONStringify} from '../utils'; | ||
|
||
const { | ||
response: {headers}, | ||
} = config; | ||
|
||
const CONTENT_TYPE_HEADER = 'Content-Type'; | ||
const CONTENT_TYPE_HEADER = 'content-type'; |
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.
why have the same constant defined in two places?
I approved it since I'm off on Monday so don't unnecessarily block the PR. There is still one minor change I asked for. |
Description:
Reduce cache for the transactions endpoint with scheduled transactions.
This PR modifies
ScheduleCreate
transactionscontent-type
is set for the/network/supply
endpointRelated issue(s):
Fixes #9174
Notes for reviewer:
Checklist