-
Notifications
You must be signed in to change notification settings - Fork 106
/
recoverOrphanedData.jobProcessor.ts
354 lines (326 loc) · 11.4 KB
/
recoverOrphanedData.jobProcessor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import type Logger from 'bunyan'
import type { Redis } from 'ioredis'
import type {
RecoverOrphanedDataJobParams,
RecoverOrphanedDataJobReturnValue
} from './types'
import type { DecoratedJobParams, DecoratedJobReturnValue } from '../types'
import axios from 'axios'
import { METRIC_NAMES } from '../../prometheusMonitoring/prometheus.constants'
import { makeGaugeSetToRecord } from '../stateMachineUtils'
import { StateMonitoringUser } from '../stateMonitoring/types'
import {
ORPHANED_DATA_NUM_USERS_PER_QUERY,
ORPHANED_DATA_NUM_USERS_TO_RECOVER_PER_BATCH,
ORPHAN_DATA_DELAY_BETWEEN_BATCHES_MS,
MAX_MS_TO_ISSUE_RECOVER_ORPHANED_DATA_REQUESTS
} from '../stateMachineConstants'
import { instrumentTracing, tracing } from '../../../tracer'
const { QUEUE_NAMES } = require('../stateMachineConstants')
const { getNodeUsers } = require('../stateMonitoring/stateMonitoringUtils')
const config = require('../../../config')
const redisClient: Redis = require('../../../redis')
const models = require('../../../models')
const asyncRetry = require('../../../utils/asyncRetry')
const Utils = require('../../../utils')
const WALLETS_ON_NODE_KEY = 'orphanedDataWalletsWithStateOnNode'
const WALLETS_WITH_NODE_IN_REPLICA_SET_KEY =
'orphanedDataWalletsWithNodeInReplicaSet'
const WALLETS_ORPHANED_KEY = 'oprhanedDataWallets'
const thisContentNodeEndpoint = config.get('creatorNodeEndpoint')
/**
* Processes a job to find users who have data on this node but who do not have this node in their replica set.
* This means their data is "orphaned,"" so the job also issues a request to each user's primary to
* merges their data back into the primary and then wipe it from this node.
*
* @param {Object} param job data
* @param {string} param.discoveryNodeEndpoint the endpoint of a Discovery Node to query
* @param {Object} param.logger the logger that can be filtered by jobName and jobId
*/
async function _recoverOrphanedData({
discoveryNodeEndpoint,
logger
}: DecoratedJobParams<RecoverOrphanedDataJobParams>): Promise<
DecoratedJobReturnValue<RecoverOrphanedDataJobReturnValue>
> {
const numWalletsOnNode = await _saveWalletsOnThisNodeToRedis(logger)
const numWalletsWithNodeInReplicaSet =
await _saveWalletsWithThisNodeInReplicaToRedis(
discoveryNodeEndpoint,
logger
)
const numWalletsWithOrphanedData = await _saveWalletsWithOrphanedDataToRedis(
logger
)
const requestsIssued = await _batchIssueReqsToRecoverOrphanedData(
numWalletsWithOrphanedData,
discoveryNodeEndpoint,
logger
)
return {
numWalletsOnNode,
numWalletsWithNodeInReplicaSet,
numWalletsWithOrphanedData,
jobsToEnqueue: {
// Enqueue another job to search for any new data that gets orphaned after this job finishes
[QUEUE_NAMES.RECOVER_ORPHANED_DATA]: [
{
parentSpanContext: tracing.currentSpanContext(),
discoveryNodeEndpoint
}
]
},
metricsToRecord: [
makeGaugeSetToRecord(
METRIC_NAMES.RECOVER_ORPHANED_DATA_WALLET_COUNTS_GAUGE,
numWalletsWithOrphanedData
),
makeGaugeSetToRecord(
METRIC_NAMES.RECOVER_ORPHANED_DATA_SYNC_COUNTS_GAUGE,
requestsIssued
)
]
}
}
/**
* Queries this node's db to find all users who have data on it and adds them to a redis set.
*/
const _saveWalletsOnThisNodeToRedis = async (logger: Logger) => {
await redisClient.del(WALLETS_ON_NODE_KEY)
type WalletSqlRow = {
walletPublicKey: string
cnodeUserUUID: string
}
let walletSqlRows: WalletSqlRow[] = []
// Make paginated SQL queries to find all wallets with data on this node.
// Table is indexed on column `cnodeUserUUID`
const numCNodeUsers = await models.CNodeUser.count()
let prevCnodeUserUUID = '00000000-0000-0000-0000-000000000000'
for (let i = 0; i < numCNodeUsers; i += ORPHANED_DATA_NUM_USERS_PER_QUERY) {
walletSqlRows = await models.CNodeUser.findAll({
attributes: ['walletPublicKey', 'cnodeUserUUID'],
order: [['cnodeUserUUID', 'ASC']],
where: {
cnodeUserUUID: {
[models.Sequelize.Op.gt]: prevCnodeUserUUID
}
},
limit: ORPHANED_DATA_NUM_USERS_PER_QUERY
})
if (walletSqlRows?.length) {
// Save the wallets to a redis set
const walletsOnThisNodeArr: string[] = []
walletSqlRows.forEach((row) =>
walletsOnThisNodeArr.push(row.walletPublicKey)
)
await redisClient.sadd(WALLETS_ON_NODE_KEY, walletsOnThisNodeArr)
// Move pagination cursor to the end
prevCnodeUserUUID = walletSqlRows[walletSqlRows.length - 1].cnodeUserUUID
}
}
const numWalletsOnNode = await redisClient.scard(WALLETS_ON_NODE_KEY)
logger.info(`Found ${numWalletsOnNode} wallets with data on this node`)
return numWalletsOnNode
}
/**
* Queries the given discovery node to find all users who have this content node as their primary or secondary.
* Adds them to a redis set.
*/
const _saveWalletsWithThisNodeInReplicaToRedis = async (
discoveryNodeEndpoint: string,
logger: Logger
) => {
await redisClient.del(WALLETS_WITH_NODE_IN_REPLICA_SET_KEY)
// Make paginated Discovery queries to find all wallets with this current node in their replica set (primary or secondary)
let prevUserId = 0
let batchOfUsers: StateMonitoringUser[] = []
do {
try {
batchOfUsers = await getNodeUsers(
discoveryNodeEndpoint,
thisContentNodeEndpoint,
prevUserId,
ORPHANED_DATA_NUM_USERS_PER_QUERY
)
if (batchOfUsers?.length) {
// Save the wallets to a redis set
const walletsWithNodeInReplicaSetArr: string[] = []
batchOfUsers.forEach((user) =>
walletsWithNodeInReplicaSetArr.push(user.wallet)
)
await redisClient.sadd(
WALLETS_WITH_NODE_IN_REPLICA_SET_KEY,
walletsWithNodeInReplicaSetArr
)
// Move pagination cursor to the end of the batch
prevUserId = batchOfUsers[batchOfUsers.length - 1].user_id
} else {
prevUserId = 0
}
} catch (e: any) {
logger.error(
`Error fetching batch of users from ${discoveryNodeEndpoint}: ${e.message}`
)
// `batchOfUsers?.length` will be 0, which will cause us to break out of this loop and not fetch all users.
// This will cause extra users to be marked as orphaned, which is okay because orphaned data
// recovery will short circuit later to avoid wiping state on any node in the user's replica set.
}
} while (
batchOfUsers?.length === ORPHANED_DATA_NUM_USERS_PER_QUERY &&
prevUserId !== 0
)
const numWalletsWithNodeInReplicaSet = await redisClient.scard(
WALLETS_WITH_NODE_IN_REPLICA_SET_KEY
)
logger.info(
`Found ${numWalletsWithNodeInReplicaSet} wallets with this node in their replica set`
)
return numWalletsWithNodeInReplicaSet
}
/**
* Finds wallets that are orphaned and adds them to a redis set.
* (Set of orphaned wallets) = (set of wallets on this node) - (set of wallets with this node in their replica set)
* @returns number of wallets that have data orphaned on this node
*/
const _saveWalletsWithOrphanedDataToRedis = async (logger: Logger) => {
const numWalletsOrphaned: number = await redisClient.sdiffstore(
WALLETS_ORPHANED_KEY,
WALLETS_ON_NODE_KEY,
WALLETS_WITH_NODE_IN_REPLICA_SET_KEY
)
logger.info(
`Found ${numWalletsOrphaned} wallets with data orphaned on this node`
)
return numWalletsOrphaned
}
/**
* Pops from redis set in batches and issues requests to move data that's orphaned on this node back to each user's primary.
* Each request will sync from this node to a primary and then "force wipe" this node:
* 1. Merge orphaned data from this node into the user's primary.
* 2. Wipe the user's data from this node.
* 3. *DON'T* resync from the primary to this node -- this is what non-modified forceResync would do -- and instead set forceWipe=true.
* @param {number} numWalletsWithOrphanedData number of users to issue requests to recover orphaned data for
* @param {string} discoveryNodeEndpoint the endpoint of a discovery node to make queries to
* @param {Logger} logger logger
* @return {number} number of requests successfully issued
*/
const _batchIssueReqsToRecoverOrphanedData = async (
numWalletsWithOrphanedData: number,
discoveryNodeEndpoint: string,
logger: Logger
): Promise<number> => {
const start = Date.now()
let requestsIssued = 0
for (
let i = 0;
i < numWalletsWithOrphanedData;
i += ORPHANED_DATA_NUM_USERS_TO_RECOVER_PER_BATCH
) {
const walletsWithOrphanedData = await redisClient.srandmember(
WALLETS_ORPHANED_KEY,
ORPHANED_DATA_NUM_USERS_TO_RECOVER_PER_BATCH
)
if (!walletsWithOrphanedData?.length) return requestsIssued
for (const wallet of walletsWithOrphanedData) {
const primaryEndpoint = await _getPrimaryForWallet(
wallet,
discoveryNodeEndpoint,
logger
)
if (!primaryEndpoint) continue
try {
await axios({
baseURL: primaryEndpoint,
url: '/merge_primary_and_secondary',
method: 'post',
params: {
wallet,
endpoint: thisContentNodeEndpoint,
forceWipe: true
}
})
requestsIssued++
await redisClient.srem(WALLETS_ORPHANED_KEY, wallet)
} catch (e: any) {
logger.error(
`Error issuing request to recover orphaned data: ${e.message}`
)
}
}
const elapsedMs = Date.now() - start
logger.info(
`Issued /merge_primary_and_secondary requests for ${requestsIssued}/${numWalletsWithOrphanedData} wallets.
Time elapsed: ${elapsedMs}/${MAX_MS_TO_ISSUE_RECOVER_ORPHANED_DATA_REQUESTS}`
)
if (elapsedMs >= MAX_MS_TO_ISSUE_RECOVER_ORPHANED_DATA_REQUESTS) {
logger.info(`Gracefully ending job after ${elapsedMs}ms`)
break
}
// Delay processing the next batch to avoid spamming requests
await Utils.timeout(ORPHAN_DATA_DELAY_BETWEEN_BATCHES_MS, false)
}
return requestsIssued
}
const _getPrimaryForWallet = async (
wallet: string,
discoveryNodeEndpoint: string,
logger: Logger
): Promise<string> => {
let user
try {
const resp = await asyncRetry({
logLabel: "fetch user's primary endpoint",
options: { retries: 3 },
asyncFn: async () => {
return axios({
method: 'get',
baseURL: discoveryNodeEndpoint,
url: 'users',
params: {
wallet
},
timeout: 2000
})
},
logger
})
user = resp?.data?.data?.[0]
} catch (e: any) {
logger.error(
`Error fetching user data for orphaned wallet ${wallet}: ${e.message}`
)
}
const replicaSet = user?.creator_node_endpoint?.split(',')
if (!replicaSet) {
logger.error(
`Couldn't find primary endpoint for wallet ${wallet}. User: ${JSON.stringify(
user || {}
)}`
)
return ''
}
return replicaSet[0]
}
async function recoverOrphanedData(
params: DecoratedJobParams<RecoverOrphanedDataJobParams>
) {
const { parentSpanContext } = params
const jobProcessor = instrumentTracing({
name: 'recoverOrphanedData.jobProcessor',
fn: _recoverOrphanedData,
options: {
links: parentSpanContext
? [
{
context: parentSpanContext
}
]
: [],
attributes: {
[tracing.CODE_FILEPATH]: __filename
}
}
})
return await jobProcessor(params)
}
export default recoverOrphanedData