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

perf: enhanced caching test #461

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "account-lookup-service",
"description": "Account Lookup Service is used to validate Party and Participant lookups.",
"version": "14.2.3",
"version": "14.3.0-snapshot.0",
"license": "Apache-2.0",
"author": "ModusBox",
"contributors": [
Expand Down Expand Up @@ -99,6 +99,7 @@
"knex": "2.5.1",
"mustache": "4.2.0",
"mysql": "2.18.1",
"node-cache": "^5.1.2",
"npm-run-all": "4.1.5",
"parse-strings-in-object": "2.0.0",
"rc": "1.2.8",
Expand Down
64 changes: 41 additions & 23 deletions src/models/oracle/facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ const Enums = require('@mojaloop/central-services-shared').Enum
const ErrorHandler = require('@mojaloop/central-services-error-handling')
const Config = require('../../lib/config')
const Metrics = require('@mojaloop/central-services-metrics')
// Ref: https://github.com/node-cache/node-cache
const NodeCache = require('node-cache')
const confNodeCacheEnabled = process.env?.NODE_CACHE_ENABLED || false
const confNodeCachestdTTL = process.env?.NODE_CACHE_STDTTL || 100
const confNodeCacheCheckPeriod = process.env?.NODE_CACHE_CHECKPERIOD || 120
const oracleCache = new NodeCache({
stdTTL: confNodeCachestdTTL,
checkperiod: confNodeCacheCheckPeriod,
deleteOnExpire: true,
useClones: false
})

/**
* @function oracleRequest
Expand All @@ -56,31 +67,38 @@ exports.oracleRequest = async (headers, method, params = {}, query = {}, payload
const currency = (payload && payload.currency) ? payload.currency : (query && query.currency) ? query.currency : undefined
const partySubIdOrType = (params && params.SubId) ? params.SubId : (query && query.partySubIdOrType) ? query.partySubIdOrType : undefined
const isGetRequest = method.toUpperCase() === Enums.Http.RestMethods.GET
if (currency && partySubIdOrType && isGetRequest) {
url = await _getOracleEndpointByTypeCurrencyAndSubId(partyIdType, partyIdentifier, currency, partySubIdOrType)
} else if (currency && isGetRequest) {
url = await _getOracleEndpointByTypeAndCurrency(partyIdType, partyIdentifier, currency)
} else if (partySubIdOrType && isGetRequest) {
url = await _getOracleEndpointByTypeAndSubId(partyIdType, partyIdentifier, partySubIdOrType)
const oracleCacheKey = `getOracle-${partyIdType}-${partyIdentifier}-${currency}-${partySubIdOrType}-${isGetRequest}`
let resp = confNodeCacheEnabled ? oracleCache.get(oracleCacheKey) : null
if (resp) {
return resp
} else {
url = await _getOracleEndpointByType(partyIdType, partyIdentifier)
if (partySubIdOrType) {
payload = { ...payload, partySubIdOrType }
if (currency && partySubIdOrType && isGetRequest) {
url = await _getOracleEndpointByTypeCurrencyAndSubId(partyIdType, partyIdentifier, currency, partySubIdOrType)
} else if (currency && isGetRequest) {
url = await _getOracleEndpointByTypeAndCurrency(partyIdType, partyIdentifier, currency)
} else if (partySubIdOrType && isGetRequest) {
url = await _getOracleEndpointByTypeAndSubId(partyIdType, partyIdentifier, partySubIdOrType)
} else {
url = await _getOracleEndpointByType(partyIdType, partyIdentifier)
if (partySubIdOrType) {
payload = { ...payload, partySubIdOrType }
}
}
Logger.debug(`Oracle endpoints: ${url}`)
const histTimerEnd = Metrics.getHistogram(
'egress_oracleRequest',
'Egress: oracleRequest',
['success']
).startTimer()
try {
resp = await request.sendRequest(url, headers, headers[Enums.Http.Headers.FSPIOP.SOURCE], headers[Enums.Http.Headers.FSPIOP.DESTINATION] || Enums.Http.Headers.FSPIOP.SWITCH.value, method.toUpperCase(), payload || undefined)
oracleCache.set(oracleCacheKey, resp)
histTimerEnd({ success: true })
return resp
} catch (err) {
histTimerEnd({ success: false })
throw err
}
}
Logger.debug(`Oracle endpoints: ${url}`)
const histTimerEnd = Metrics.getHistogram(
'egress_oracleRequest',
'Egress: oracleRequest',
['success']
).startTimer()
try {
const resp = await request.sendRequest(url, headers, headers[Enums.Http.Headers.FSPIOP.SOURCE], headers[Enums.Http.Headers.FSPIOP.DESTINATION] || Enums.Http.Headers.FSPIOP.SWITCH.value, method.toUpperCase(), payload || undefined)
histTimerEnd({ success: true })
return resp
} catch (err) {
histTimerEnd({ success: false })
throw err
}
} catch (err) {
Logger.error(err)
Expand Down
55 changes: 41 additions & 14 deletions src/models/participantEndpoint/facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ const Metrics = require('@mojaloop/central-services-metrics')
const Config = require('../../lib/config')
const uriRegex = /(?:^.*)(\/(participants|parties|quotes|transfers)(\/.*)*)$/

// Ref: https://github.com/node-cache/node-cache
const NodeCache = require('node-cache')
const confNodeCachestdTTL = process.env?.NODE_CACHE_STDTTL || 100
const confNodeCacheCheckPeriod = process.env?.NODE_CACHE_CHECKPERIOD || 120
const participantCache = new NodeCache({
stdTTL: confNodeCachestdTTL,
checkperiod: confNodeCacheCheckPeriod,
deleteOnExpire: true,
useClones: false
})

/**
* @module src/models/participantEndpoint/facade
*/
Expand Down Expand Up @@ -108,24 +119,40 @@ exports.validateParticipant = async (fsp, span = undefined) => {
const histTimerEnd = Metrics.getHistogram(
'egress_validateParticipant',
'Egress: Validate participant',
['success']
['success', 'cachehit']
).startTimer()
const participantCacheKey = `validateParticipant-${fsp}`
let resp = participantCache.get(participantCacheKey)
let cachehit = false
try {
const requestedParticipantUrl = Mustache.render(Config.SWITCH_ENDPOINT + Enums.EndPoints.FspEndpointTemplates.PARTICIPANTS_GET, { fsp })
Logger.debug(`validateParticipant url: ${requestedParticipantUrl}`)
const resp = await Util.Request.sendRequest(
requestedParticipantUrl,
Util.Http.SwitchDefaultHeaders(Enums.Http.Headers.FSPIOP.SWITCH.value, Enums.Http.HeaderResources.PARTICIPANTS, Enums.Http.Headers.FSPIOP.SWITCH.value),
Enums.Http.Headers.FSPIOP.SWITCH.value,
Enums.Http.Headers.FSPIOP.SWITCH.value,
Enums.Http.RestMethods.GET,
null,
Enums.Http.ResponseTypes.JSON,
span)
histTimerEnd({ success: true })
if (resp) {
// resp = participantCache.get(participantCacheKey)
console.log('CACHE HIT!')
cachehit = true
} else {
console.log('CACHE MISS!')
const requestedParticipantUrl = Mustache.render(Config.SWITCH_ENDPOINT + Enums.EndPoints.FspEndpointTemplates.PARTICIPANTS_GET, { fsp })
Logger.debug(`validateParticipant url: ${requestedParticipantUrl}`)
// const resp = await Util.Request.sendRequest(
resp = await Util.Request.sendRequest(
requestedParticipantUrl,
Util.Http.SwitchDefaultHeaders(Enums.Http.Headers.FSPIOP.SWITCH.value, Enums.Http.HeaderResources.PARTICIPANTS, Enums.Http.Headers.FSPIOP.SWITCH.value),
Enums.Http.Headers.FSPIOP.SWITCH.value,
Enums.Http.Headers.FSPIOP.SWITCH.value,
Enums.Http.RestMethods.GET,
null,
Enums.Http.ResponseTypes.JSON,
span)
// let isValidated = false
// if (resp) {
// isValidated = true
// }
participantCache.set(participantCacheKey, resp)
}
histTimerEnd({ success: true, cachehit })
return resp
} catch (err) {
histTimerEnd({ success: false })
histTimerEnd({ success: false, cachehit })
Logger.error(err)
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
Expand Down