Skip to content

Commit

Permalink
PrimarySyncFromSecondary() improvements to error handling and logging (
Browse files Browse the repository at this point in the history
  • Loading branch information
SidSethi authored Aug 17, 2022
1 parent 6b38bac commit 9f9be2d
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 194 deletions.
30 changes: 17 additions & 13 deletions creator-node/src/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,32 +508,34 @@ async function getOwnEndpoint({ libs }) {
}

/**
* Retrieves user replica set from discprov
* Retrieves user replica set endpoints from discprov
*
* Polls discprov conditionally as follows:
* - If blockNumber provided, polls discprov until it has indexed that blocknumber (for up to 200 seconds)
* - Else if ensurePrimary required, polls discprov until it has indexed myCnodeEndpoint (for up to 60 seconds)
* - Errors if retrieved primary does not match myCnodeEndpoint
* - If neither of above conditions are met, falls back to single discprov query without polling
*
* @param {string} wallet - wallet used to query discprov for user data
* @param {Object} serviceRegistry
* @param {Object} logger
* @param {string} wallet - wallet used to query discprov for user data
* @param {number} blockNumber - blocknumber of eth TX preceding CN call
* @param {string} myCnodeEndpoint - endpoint of this CN
* @param {boolean} ensurePrimary - determines if function should error if this CN is not primary
*
* @returns {Array} - array of strings of replica set
*/
async function getCreatorNodeEndpoints({
async function getUserReplicaSetEndpointsFromDiscovery({
wallet,
libs,
logger,
wallet,
blockNumber,
ensurePrimary,
myCnodeEndpoint
}) {
logger.info(`Starting getCreatorNodeEndpoints for wallet ${wallet}`)
logger.info(
`Starting getUserReplicaSetEndpointsFromDiscovery for wallet ${wallet}`
)
const start = Date.now()

let user = null
Expand All @@ -551,7 +553,7 @@ async function getCreatorNodeEndpoints({
let discprovBlockNumber = -1
for (let retry = 1; retry <= MaxRetries; retry++) {
logger.info(
`getCreatorNodeEndpoints retry #${retry}/${MaxRetries} || time from start: ${
`getUserReplicaSetEndpointsFromDiscovery retry #${retry}/${MaxRetries} || time from start: ${
Date.now() - start2
} discprovBlockNumber ${discprovBlockNumber} || blockNumber ${blockNumber}`
)
Expand Down Expand Up @@ -584,7 +586,7 @@ async function getCreatorNodeEndpoints({

await utils.timeout(RetryTimeout)
logger.info(
`getCreatorNodeEndpoints AFTER TIMEOUT retry #${retry}/${MaxRetries} || time from start: ${
`getUserReplicaSetEndpointsFromDiscovery AFTER TIMEOUT retry #${retry}/${MaxRetries} || time from start: ${
Date.now() - start2
} discprovBlockNumber ${discprovBlockNumber} || blockNumber ${blockNumber}`
)
Expand All @@ -609,7 +611,7 @@ async function getCreatorNodeEndpoints({
* Errors if retrieved primary does not match myCnodeEndpoint
*/
logger.info(
`getCreatorNodeEndpoints || no blockNumber passed, retrying until DN returns same endpoint`
`getUserReplicaSetEndpointsFromDiscovery || no blockNumber passed, retrying until DN returns same endpoint`
)

const start2 = Date.now()
Expand All @@ -621,7 +623,7 @@ async function getCreatorNodeEndpoints({
let returnedPrimaryEndpoint = null
for (let retry = 1; retry <= MaxRetries; retry++) {
logger.info(
`getCreatorNodeEndpoints retry #${retry}/${MaxRetries} || time from start: ${
`getUserReplicaSetEndpointsFromDiscovery retry #${retry}/${MaxRetries} || time from start: ${
Date.now() - start2
} myCnodeEndpoint ${myCnodeEndpoint}`
)
Expand Down Expand Up @@ -650,7 +652,7 @@ async function getCreatorNodeEndpoints({

await utils.timeout(RetryTimeout)
logger.info(
`getCreatorNodeEndpoints AFTER TIMEOUT retry #${retry}/${MaxRetries} || time from start: ${
`getUserReplicaSetEndpointsFromDiscovery AFTER TIMEOUT retry #${retry}/${MaxRetries} || time from start: ${
Date.now() - start2
} myCnodeEndpoint ${myCnodeEndpoint}`
)
Expand All @@ -674,7 +676,7 @@ async function getCreatorNodeEndpoints({
* If neither of above conditions are met, falls back to single discprov query without polling
*/
logger.info(
`getCreatorNodeEndpoints || ensurePrimary === false, fetching user without retries`
`getUserReplicaSetEndpointsFromDiscovery || ensurePrimary === false, fetching user without retries`
)
user = await libs.User.getUsers(1, 0, null, wallet)
}
Expand All @@ -692,7 +694,9 @@ async function getCreatorNodeEndpoints({
const endpoint = user[0].creator_node_endpoint
const userReplicaSet = endpoint ? endpoint.split(',') : []

logger.info(`getCreatorNodeEndpoints route time ${Date.now() - start}`)
logger.info(
`getUserReplicaSetEndpointsFromDiscovery route time ${Date.now() - start}`
)
return userReplicaSet
}

Expand Down Expand Up @@ -920,5 +924,5 @@ module.exports = {
ensureValidSPMiddleware,
issueAndWaitForSecondarySyncRequests,
getOwnEndpoint,
getCreatorNodeEndpoints
getUserReplicaSetEndpointsFromDiscovery
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const _ = require('lodash')
/**
* Queries to periodically keep the mapping of (Content Node endpoint -> SP ID)
* up to date.
*
* @dev Since this class holds all state in memory, it is not concurrency-compatible
*/
class ContentNodeInfoManager {
constructor() {
Expand Down
Loading

0 comments on commit 9f9be2d

Please sign in to comment.