refactor(core): Move instanceRole to InstanceSettings (no-changelog) (#10242)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-08-02 15:18:33 +02:00
committed by GitHub
parent 7056e50b00
commit 0faf46f4f8
12 changed files with 77 additions and 48 deletions

View File

@@ -55,7 +55,7 @@ export class License {
* This ensures the mains do not cause a 429 (too many requests) on license init.
*/
if (config.getEnv('multiMainSetup.enabled')) {
return autoRenewEnabled && config.getEnv('instanceRole') === 'leader';
return autoRenewEnabled && this.instanceSettings.isLeader;
}
return autoRenewEnabled;

View File

@@ -186,7 +186,7 @@ export class Start extends BaseCommand {
await this.initOrchestration();
this.logger.debug('Orchestration init complete');
if (!config.getEnv('license.autoRenewEnabled') && config.getEnv('instanceRole') === 'leader') {
if (!config.getEnv('license.autoRenewEnabled') && this.instanceSettings.isLeader) {
this.logger.warn(
'Automatic license renewal is disabled. The license will not renew automatically, and access to licensed features may be lost!',
);
@@ -210,7 +210,7 @@ export class Start extends BaseCommand {
async initOrchestration() {
if (config.getEnv('executions.mode') === 'regular') {
config.set('instanceRole', 'leader');
this.instanceSettings.markAsLeader();
return;
}

View File

@@ -759,12 +759,6 @@ export const schema = {
},
},
instanceRole: {
doc: 'Always `leader` in single-main setup. `leader` or `follower` in multi-main setup.',
format: ['unset', 'leader', 'follower'] as const,
default: 'unset', // only until Start.initOrchestration
},
multiMainSetup: {
enabled: {
doc: 'Whether to enable multi-main setup for queue mode (license required)',

View File

@@ -1,6 +1,7 @@
import Container from 'typedi';
import { stringify } from 'flatted';
import { randomInt } from 'n8n-workflow';
import { InstanceSettings } from 'n8n-core';
import { mockInstance } from '@test/mocking';
import { createWorkflow } from '@test-integration/db/workflows';
@@ -21,38 +22,37 @@ import { EventMessageNode } from '@/eventbus/EventMessageClasses/EventMessageNod
import { IN_PROGRESS_EXECUTION_DATA, OOM_WORKFLOW } from './constants';
import { setupMessages } from './utils';
import type { EventService } from '@/eventbus/event.service';
import type { EventMessageTypes as EventMessage } from '@/eventbus/EventMessageClasses';
import type { Logger } from '@/Logger';
describe('ExecutionRecoveryService', () => {
let push: Push;
const push = mockInstance(Push);
mockInstance(InternalHooks);
const instanceSettings = new InstanceSettings();
let executionRecoveryService: ExecutionRecoveryService;
let orchestrationService: OrchestrationService;
let executionRepository: ExecutionRepository;
beforeAll(async () => {
await testDb.init();
push = mockInstance(Push);
executionRepository = Container.get(ExecutionRepository);
orchestrationService = Container.get(OrchestrationService);
mockInstance(InternalHooks);
executionRecoveryService = new ExecutionRecoveryService(
mock<Logger>(),
mock(),
instanceSettings,
push,
executionRepository,
orchestrationService,
mock<EventService>(),
mock(),
);
});
beforeEach(() => {
config.set('instanceRole', 'leader');
instanceSettings.markAsLeader();
});
afterEach(async () => {
config.load(config.default);
jest.restoreAllMocks();
await testDb.truncate(['Execution', 'ExecutionData', 'Workflow']);
executionRecoveryService.shutdown();
@@ -69,7 +69,6 @@ describe('ExecutionRecoveryService', () => {
* Arrange
*/
config.set('executions.mode', 'queue');
jest.spyOn(orchestrationService, 'isLeader', 'get').mockReturnValue(true);
const scheduleSpy = jest.spyOn(executionRecoveryService, 'scheduleQueueRecovery');
/**
@@ -88,7 +87,7 @@ describe('ExecutionRecoveryService', () => {
* Arrange
*/
config.set('executions.mode', 'queue');
jest.spyOn(orchestrationService, 'isLeader', 'get').mockReturnValue(false);
instanceSettings.markAsFollower();
const scheduleSpy = jest.spyOn(executionRecoveryService, 'scheduleQueueRecovery');
/**
@@ -130,7 +129,7 @@ describe('ExecutionRecoveryService', () => {
/**
* Arrange
*/
config.set('instanceRole', 'follower');
instanceSettings.markAsFollower();
// @ts-expect-error Private method
const amendSpy = jest.spyOn(executionRecoveryService, 'amend');
const messages = setupMessages('123', 'Some workflow');

View File

@@ -5,6 +5,7 @@ import { ExecutionRepository } from '@db/repositories/execution.repository';
import { getWorkflowHooksMain } from '@/WorkflowExecuteAdditionalData'; // @TODO: Dependency cycle
import type { DateTime } from 'luxon';
import type { IRun, ITaskData } from 'n8n-workflow';
import { InstanceSettings } from 'n8n-core';
import type { EventMessageTypes } from '../eventbus/EventMessageClasses';
import type { IExecutionResponse } from '@/Interfaces';
import { NodeCrashedError } from '@/errors/node-crashed.error';
@@ -24,6 +25,7 @@ import { EventService } from '@/eventbus/event.service';
export class ExecutionRecoveryService {
constructor(
private readonly logger: Logger,
private readonly instanceSettings: InstanceSettings,
private readonly push: Push,
private readonly executionRepository: ExecutionRepository,
private readonly orchestrationService: OrchestrationService,
@@ -36,10 +38,10 @@ export class ExecutionRecoveryService {
init() {
if (config.getEnv('executions.mode') === 'regular') return;
const { isLeader, isMultiMainSetupEnabled } = this.orchestrationService;
const { isLeader } = this.instanceSettings;
if (isLeader) this.scheduleQueueRecovery();
const { isMultiMainSetupEnabled } = this.orchestrationService;
if (isMultiMainSetupEnabled) {
this.orchestrationService.multiMainSetup
.on('leader-takeover', () => this.scheduleQueueRecovery())
@@ -58,7 +60,7 @@ export class ExecutionRecoveryService {
* Recover key properties of a truncated execution using event logs.
*/
async recoverFromLogs(executionId: string, messages: EventMessageTypes[]) {
if (this.orchestrationService.isFollower) return;
if (this.instanceSettings.isFollower) return;
const amendedExecution = await this.amend(executionId, messages);
@@ -319,7 +321,7 @@ export class ExecutionRecoveryService {
private shouldScheduleQueueRecovery() {
return (
config.getEnv('executions.mode') === 'queue' &&
config.getEnv('instanceRole') === 'leader' &&
this.instanceSettings.isLeader &&
!this.isShuttingDown
);
}

View File

@@ -7,11 +7,13 @@ import type { RedisServiceBaseCommand, RedisServiceCommand } from './redis/Redis
import { RedisService } from './redis.service';
import { MultiMainSetup } from './orchestration/main/MultiMainSetup.ee';
import type { WorkflowActivateMode } from 'n8n-workflow';
import { InstanceSettings } from 'n8n-core';
@Service()
export class OrchestrationService {
constructor(
private readonly logger: Logger,
private readonly instanceSettings: InstanceSettings,
private readonly redisService: RedisService,
readonly multiMainSetup: MultiMainSetup,
) {}
@@ -43,12 +45,14 @@ export class OrchestrationService {
return config.getEnv('redis.queueModeId');
}
/** @deprecated use InstanceSettings.isLeader */
get isLeader() {
return config.getEnv('instanceRole') === 'leader';
return this.instanceSettings.isLeader;
}
/** @deprecated use InstanceSettings.isFollower */
get isFollower() {
return config.getEnv('instanceRole') !== 'leader';
return this.instanceSettings.isFollower;
}
sanityCheck() {
@@ -63,7 +67,7 @@ export class OrchestrationService {
if (this.isMultiMainSetupEnabled) {
await this.multiMainSetup.init();
} else {
config.set('instanceRole', 'leader');
this.instanceSettings.markAsLeader();
}
this.isInitialized = true;

View File

@@ -1,6 +1,7 @@
import config from '@/config';
import { Service } from 'typedi';
import { TIME } from '@/constants';
import { InstanceSettings } from 'n8n-core';
import { ErrorReporterProxy as EventReporter } from 'n8n-workflow';
import { Logger } from '@/Logger';
import { RedisServicePubSubPublisher } from '@/services/redis/RedisServicePubSubPublisher';
@@ -16,6 +17,7 @@ type MultiMainEvents = {
export class MultiMainSetup extends TypedEmitter<MultiMainEvents> {
constructor(
private readonly logger: Logger,
private readonly instanceSettings: InstanceSettings,
private readonly redisPublisher: RedisServicePubSubPublisher,
private readonly redisClientService: RedisClientService,
) {
@@ -50,7 +52,7 @@ export class MultiMainSetup extends TypedEmitter<MultiMainEvents> {
async shutdown() {
clearInterval(this.leaderCheckInterval);
const isLeader = config.getEnv('instanceRole') === 'leader';
const { isLeader } = this.instanceSettings;
if (isLeader) await this.redisPublisher.clear(this.leaderKey);
}
@@ -69,8 +71,8 @@ export class MultiMainSetup extends TypedEmitter<MultiMainEvents> {
if (leaderId && leaderId !== this.instanceId) {
this.logger.debug(`[Instance ID ${this.instanceId}] Leader is other instance "${leaderId}"`);
if (config.getEnv('instanceRole') === 'leader') {
config.set('instanceRole', 'follower');
if (this.instanceSettings.isLeader) {
this.instanceSettings.markAsFollower();
this.emit('leader-stepdown'); // lost leadership - stop triggers, pollers, pruning, wait-tracking, queue recovery
@@ -85,7 +87,7 @@ export class MultiMainSetup extends TypedEmitter<MultiMainEvents> {
`[Instance ID ${this.instanceId}] Leadership vacant, attempting to become leader...`,
);
config.set('instanceRole', 'follower');
this.instanceSettings.markAsFollower();
/**
* Lost leadership - stop triggers, pollers, pruning, wait tracking, license renewal, queue recovery
@@ -106,7 +108,7 @@ export class MultiMainSetup extends TypedEmitter<MultiMainEvents> {
if (keySetSuccessfully) {
this.logger.debug(`[Instance ID ${this.instanceId}] Leader is now this instance`);
config.set('instanceRole', 'leader');
this.instanceSettings.markAsLeader();
await this.redisPublisher.setExpiration(this.leaderKey, this.leaderKeyTtl);
@@ -115,7 +117,7 @@ export class MultiMainSetup extends TypedEmitter<MultiMainEvents> {
*/
this.emit('leader-takeover');
} else {
config.set('instanceRole', 'follower');
this.instanceSettings.markAsFollower();
}
}

View File

@@ -1,5 +1,5 @@
import { Service } from 'typedi';
import { BinaryDataService } from 'n8n-core';
import { BinaryDataService, InstanceSettings } from 'n8n-core';
import { inTest, TIME } from '@/constants';
import config from '@/config';
import { ExecutionRepository } from '@db/repositories/execution.repository';
@@ -25,6 +25,7 @@ export class PruningService {
constructor(
private readonly logger: Logger,
private readonly instanceSettings: InstanceSettings,
private readonly executionRepository: ExecutionRepository,
private readonly binaryDataService: BinaryDataService,
private readonly orchestrationService: OrchestrationService,
@@ -56,7 +57,7 @@ export class PruningService {
if (
config.getEnv('multiMainSetup.enabled') &&
config.getEnv('generic.instanceType') === 'main' &&
config.getEnv('instanceRole') === 'follower'
this.instanceSettings.isFollower
) {
return false;
}