From 9db437c8155f3563a33e22ed2896072a9f1fbdb0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 13 Oct 2021 11:47:32 +0200 Subject: Process slow followers in unicast job queue --- server/lib/activitypub/send/utils.ts | 40 ++++++++-- server/lib/activitypub/videos/refresh.ts | 6 +- server/lib/actor-follow-health-cache.ts | 86 ++++++++++++++++++++++ server/lib/files-cache/actor-follow-score-cache.ts | 75 ------------------- server/lib/files-cache/index.ts | 2 +- .../handlers/activitypub-http-broadcast.ts | 16 ++-- .../job-queue/handlers/activitypub-http-unicast.ts | 6 +- server/lib/schedulers/actor-follow-scheduler.ts | 14 ++-- 8 files changed, 143 insertions(+), 102 deletions(-) create mode 100644 server/lib/actor-follow-health-cache.ts delete mode 100644 server/lib/files-cache/actor-follow-score-cache.ts (limited to 'server/lib') diff --git a/server/lib/activitypub/send/utils.ts b/server/lib/activitypub/send/utils.ts index 7cd8030e1..7729703b8 100644 --- a/server/lib/activitypub/send/utils.ts +++ b/server/lib/activitypub/send/utils.ts @@ -1,4 +1,5 @@ import { Transaction } from 'sequelize' +import { ActorFollowHealthCache } from '@server/lib/actor-follow-health-cache' import { getServerActor } from '@server/models/application/application' import { ContextType } from '@shared/models/activitypub/context' import { Activity, ActivityAudience } from '../../../../shared/models/activitypub' @@ -119,16 +120,41 @@ async function broadcastToActors ( function broadcastTo (uris: string[], data: any, byActor: MActorId, contextType?: ContextType) { if (uris.length === 0) return undefined - logger.debug('Creating broadcast job.', { uris }) + const broadcastUris: string[] = [] + const unicastUris: string[] = [] - const payload = { - uris, - signatureActorId: byActor.id, - body: data, - contextType + // Bad URIs could be slow to respond, prefer to process them in a dedicated queue + for (const uri of uris) { + if (ActorFollowHealthCache.Instance.isBadInbox(uri)) { + unicastUris.push(uri) + } else { + broadcastUris.push(uri) + } + } + + logger.debug('Creating broadcast job.', { broadcastUris, unicastUris }) + + if (broadcastUris.length !== 0) { + const payload = { + uris: broadcastUris, + signatureActorId: byActor.id, + body: data, + contextType + } + + JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }) } - return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }) + for (const unicastUri of unicastUris) { + const payload = { + uri: unicastUri, + signatureActorId: byActor.id, + body: data, + contextType + } + + JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload }) + } } function unicastTo (data: any, byActor: MActorId, toActorUrl: string, contextType?: ContextType) { diff --git a/server/lib/activitypub/videos/refresh.ts b/server/lib/activitypub/videos/refresh.ts index 3af08acf4..9f952a218 100644 --- a/server/lib/activitypub/videos/refresh.ts +++ b/server/lib/activitypub/videos/refresh.ts @@ -1,10 +1,10 @@ import { logger, loggerTagsFactory } from '@server/helpers/logger' import { PeerTubeRequestError } from '@server/helpers/requests' -import { ActorFollowScoreCache } from '@server/lib/files-cache' import { VideoLoadByUrlType } from '@server/lib/model-loaders' import { VideoModel } from '@server/models/video/video' import { MVideoAccountLightBlacklistAllFiles, MVideoThumbnail } from '@server/types/models' import { HttpStatusCode } from '@shared/models' +import { ActorFollowHealthCache } from '../../actor-follow-health-cache' import { fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared' import { APVideoUpdater } from './updater' @@ -39,7 +39,7 @@ async function refreshVideoIfNeeded (options: { await syncVideoExternalAttributes(video, videoObject, options.syncParam) - ActorFollowScoreCache.Instance.addGoodServerId(video.VideoChannel.Actor.serverId) + ActorFollowHealthCache.Instance.addGoodServerId(video.VideoChannel.Actor.serverId) return video } catch (err) { @@ -53,7 +53,7 @@ async function refreshVideoIfNeeded (options: { logger.warn('Cannot refresh video %s.', options.video.url, { err, ...lTags() }) - ActorFollowScoreCache.Instance.addBadServerId(video.VideoChannel.Actor.serverId) + ActorFollowHealthCache.Instance.addBadServerId(video.VideoChannel.Actor.serverId) // Don't refresh in loop await video.setAsRefreshed() diff --git a/server/lib/actor-follow-health-cache.ts b/server/lib/actor-follow-health-cache.ts new file mode 100644 index 000000000..ab8cc98df --- /dev/null +++ b/server/lib/actor-follow-health-cache.ts @@ -0,0 +1,86 @@ +import { ACTOR_FOLLOW_SCORE } from '../initializers/constants' +import { logger } from '../helpers/logger' + +// Cache follows scores, instead of writing them too often in database +// Keep data in memory, we don't really need Redis here as we don't really care to loose some scores +class ActorFollowHealthCache { + + private static instance: ActorFollowHealthCache + + private pendingFollowsScore: { [ url: string ]: number } = {} + + private pendingBadServer = new Set() + private pendingGoodServer = new Set() + + private badInboxes = new Set() + + private constructor () {} + + static get Instance () { + return this.instance || (this.instance = new this()) + } + + updateActorFollowsHealth (goodInboxes: string[], badInboxes: string[]) { + this.badInboxes.clear() + + if (goodInboxes.length === 0 && badInboxes.length === 0) return + + logger.info( + 'Updating %d good actor follows and %d bad actor follows scores in cache.', + goodInboxes.length, badInboxes.length, { badInboxes } + ) + + for (const goodInbox of goodInboxes) { + if (this.pendingFollowsScore[goodInbox] === undefined) this.pendingFollowsScore[goodInbox] = 0 + + this.pendingFollowsScore[goodInbox] += ACTOR_FOLLOW_SCORE.BONUS + } + + for (const badInbox of badInboxes) { + if (this.pendingFollowsScore[badInbox] === undefined) this.pendingFollowsScore[badInbox] = 0 + + this.pendingFollowsScore[badInbox] += ACTOR_FOLLOW_SCORE.PENALTY + this.badInboxes.add(badInbox) + } + } + + isBadInbox (inboxUrl: string) { + return this.badInboxes.has(inboxUrl) + } + + addBadServerId (serverId: number) { + this.pendingBadServer.add(serverId) + } + + getBadFollowingServerIds () { + return Array.from(this.pendingBadServer) + } + + clearBadFollowingServerIds () { + this.pendingBadServer = new Set() + } + + addGoodServerId (serverId: number) { + this.pendingGoodServer.add(serverId) + } + + getGoodFollowingServerIds () { + return Array.from(this.pendingGoodServer) + } + + clearGoodFollowingServerIds () { + this.pendingGoodServer = new Set() + } + + getPendingFollowsScore () { + return this.pendingFollowsScore + } + + clearPendingFollowsScore () { + this.pendingFollowsScore = {} + } +} + +export { + ActorFollowHealthCache +} diff --git a/server/lib/files-cache/actor-follow-score-cache.ts b/server/lib/files-cache/actor-follow-score-cache.ts deleted file mode 100644 index 465080e72..000000000 --- a/server/lib/files-cache/actor-follow-score-cache.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { ACTOR_FOLLOW_SCORE } from '../../initializers/constants' -import { logger } from '../../helpers/logger' - -// Cache follows scores, instead of writing them too often in database -// Keep data in memory, we don't really need Redis here as we don't really care to loose some scores -class ActorFollowScoreCache { - - private static instance: ActorFollowScoreCache - private pendingFollowsScore: { [ url: string ]: number } = {} - private pendingBadServer = new Set() - private pendingGoodServer = new Set() - - private constructor () {} - - static get Instance () { - return this.instance || (this.instance = new this()) - } - - updateActorFollowsScore (goodInboxes: string[], badInboxes: string[]) { - if (goodInboxes.length === 0 && badInboxes.length === 0) return - - logger.info( - 'Updating %d good actor follows and %d bad actor follows scores in cache.', - goodInboxes.length, badInboxes.length, { badInboxes } - ) - - for (const goodInbox of goodInboxes) { - if (this.pendingFollowsScore[goodInbox] === undefined) this.pendingFollowsScore[goodInbox] = 0 - - this.pendingFollowsScore[goodInbox] += ACTOR_FOLLOW_SCORE.BONUS - } - - for (const badInbox of badInboxes) { - if (this.pendingFollowsScore[badInbox] === undefined) this.pendingFollowsScore[badInbox] = 0 - - this.pendingFollowsScore[badInbox] += ACTOR_FOLLOW_SCORE.PENALTY - } - } - - addBadServerId (serverId: number) { - this.pendingBadServer.add(serverId) - } - - getBadFollowingServerIds () { - return Array.from(this.pendingBadServer) - } - - clearBadFollowingServerIds () { - this.pendingBadServer = new Set() - } - - addGoodServerId (serverId: number) { - this.pendingGoodServer.add(serverId) - } - - getGoodFollowingServerIds () { - return Array.from(this.pendingGoodServer) - } - - clearGoodFollowingServerIds () { - this.pendingGoodServer = new Set() - } - - getPendingFollowsScore () { - return this.pendingFollowsScore - } - - clearPendingFollowsScore () { - this.pendingFollowsScore = {} - } -} - -export { - ActorFollowScoreCache -} diff --git a/server/lib/files-cache/index.ts b/server/lib/files-cache/index.ts index e921d04a7..e5853f7d6 100644 --- a/server/lib/files-cache/index.ts +++ b/server/lib/files-cache/index.ts @@ -1,3 +1,3 @@ -export * from './actor-follow-score-cache' export * from './videos-preview-cache' export * from './videos-caption-cache' +export * from './videos-torrent-cache' diff --git a/server/lib/job-queue/handlers/activitypub-http-broadcast.ts b/server/lib/job-queue/handlers/activitypub-http-broadcast.ts index 9b0bb6574..fbf01d276 100644 --- a/server/lib/job-queue/handlers/activitypub-http-broadcast.ts +++ b/server/lib/job-queue/handlers/activitypub-http-broadcast.ts @@ -1,10 +1,10 @@ import { map } from 'bluebird' import { Job } from 'bull' +import { ActorFollowHealthCache } from '@server/lib/actor-follow-health-cache' import { ActivitypubHttpBroadcastPayload } from '@shared/models' import { logger } from '../../../helpers/logger' import { doRequest } from '../../../helpers/requests' import { BROADCAST_CONCURRENCY } from '../../../initializers/constants' -import { ActorFollowScoreCache } from '../../files-cache' import { buildGlobalHeaders, buildSignedRequestOptions, computeBody } from './utils/activitypub-http-utils' async function processActivityPubHttpBroadcast (job: Job) { @@ -25,13 +25,17 @@ async function processActivityPubHttpBroadcast (job: Job) { const badUrls: string[] = [] const goodUrls: string[] = [] - await map(payload.uris, uri => { - return doRequest(uri, options) - .then(() => goodUrls.push(uri)) - .catch(() => badUrls.push(uri)) + await map(payload.uris, async uri => { + try { + await doRequest(uri, options) + goodUrls.push(uri) + } catch (err) { + logger.debug('HTTP broadcast to %s failed.', uri, { err }) + badUrls.push(uri) + } }, { concurrency: BROADCAST_CONCURRENCY }) - return ActorFollowScoreCache.Instance.updateActorFollowsScore(goodUrls, badUrls) + return ActorFollowHealthCache.Instance.updateActorFollowsHealth(goodUrls, badUrls) } // --------------------------------------------------------------------------- diff --git a/server/lib/job-queue/handlers/activitypub-http-unicast.ts b/server/lib/job-queue/handlers/activitypub-http-unicast.ts index 9be50837f..673583d2b 100644 --- a/server/lib/job-queue/handlers/activitypub-http-unicast.ts +++ b/server/lib/job-queue/handlers/activitypub-http-unicast.ts @@ -2,7 +2,7 @@ import { Job } from 'bull' import { ActivitypubHttpUnicastPayload } from '@shared/models' import { logger } from '../../../helpers/logger' import { doRequest } from '../../../helpers/requests' -import { ActorFollowScoreCache } from '../../files-cache' +import { ActorFollowHealthCache } from '../../actor-follow-health-cache' import { buildGlobalHeaders, buildSignedRequestOptions, computeBody } from './utils/activitypub-http-utils' async function processActivityPubHttpUnicast (job: Job) { @@ -23,9 +23,9 @@ async function processActivityPubHttpUnicast (job: Job) { try { await doRequest(uri, options) - ActorFollowScoreCache.Instance.updateActorFollowsScore([ uri ], []) + ActorFollowHealthCache.Instance.updateActorFollowsHealth([ uri ], []) } catch (err) { - ActorFollowScoreCache.Instance.updateActorFollowsScore([], [ uri ]) + ActorFollowHealthCache.Instance.updateActorFollowsHealth([], [ uri ]) throw err } diff --git a/server/lib/schedulers/actor-follow-scheduler.ts b/server/lib/schedulers/actor-follow-scheduler.ts index 1b80316e9..a5a377573 100644 --- a/server/lib/schedulers/actor-follow-scheduler.ts +++ b/server/lib/schedulers/actor-follow-scheduler.ts @@ -2,7 +2,7 @@ import { isTestInstance } from '../../helpers/core-utils' import { logger } from '../../helpers/logger' import { ACTOR_FOLLOW_SCORE, SCHEDULER_INTERVALS_MS } from '../../initializers/constants' import { ActorFollowModel } from '../../models/actor/actor-follow' -import { ActorFollowScoreCache } from '../files-cache' +import { ActorFollowHealthCache } from '../actor-follow-health-cache' import { AbstractScheduler } from './abstract-scheduler' export class ActorFollowScheduler extends AbstractScheduler { @@ -22,13 +22,13 @@ export class ActorFollowScheduler extends AbstractScheduler { } private async processPendingScores () { - const pendingScores = ActorFollowScoreCache.Instance.getPendingFollowsScore() - const badServerIds = ActorFollowScoreCache.Instance.getBadFollowingServerIds() - const goodServerIds = ActorFollowScoreCache.Instance.getGoodFollowingServerIds() + const pendingScores = ActorFollowHealthCache.Instance.getPendingFollowsScore() + const badServerIds = ActorFollowHealthCache.Instance.getBadFollowingServerIds() + const goodServerIds = ActorFollowHealthCache.Instance.getGoodFollowingServerIds() - ActorFollowScoreCache.Instance.clearPendingFollowsScore() - ActorFollowScoreCache.Instance.clearBadFollowingServerIds() - ActorFollowScoreCache.Instance.clearGoodFollowingServerIds() + ActorFollowHealthCache.Instance.clearPendingFollowsScore() + ActorFollowHealthCache.Instance.clearBadFollowingServerIds() + ActorFollowHealthCache.Instance.clearGoodFollowingServerIds() for (const inbox of Object.keys(pendingScores)) { await ActorFollowModel.updateScore(inbox, pendingScores[inbox]) -- cgit v1.2.3