Skip to content

Commit

Permalink
refactor(core): Make instance role clearer (no-changelog) (#10188)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov authored Jul 30, 2024
1 parent 47c7904 commit 99dc56c
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/License.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('multiMainSetup.instanceType') === 'leader';
return autoRenewEnabled && config.getEnv('instanceRole') === 'leader';
}

return autoRenewEnabled;
Expand Down
7 changes: 2 additions & 5 deletions packages/cli/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,7 @@ export class Start extends BaseCommand {
await this.initOrchestration();
this.logger.debug('Orchestration init complete');

if (
!config.getEnv('license.autoRenewEnabled') &&
config.getEnv('multiMainSetup.instanceType') === 'leader'
) {
if (!config.getEnv('license.autoRenewEnabled') && config.getEnv('instanceRole') === 'leader') {
this.logger.warn(
'Automatic license renewal is disabled. The license will not renew automatically, and access to licensed features may be lost!',
);
Expand All @@ -211,7 +208,7 @@ export class Start extends BaseCommand {

async initOrchestration() {
if (config.getEnv('executions.mode') === 'regular') {
config.set('multiMainSetup.instanceType', 'leader');
config.set('instanceRole', 'leader');
return;
}

Expand Down
11 changes: 6 additions & 5 deletions packages/cli/src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -883,12 +883,13 @@ 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: {
instanceType: {
doc: 'Type of instance in multi-main setup',
format: ['unset', 'leader', 'follower'] as const,
default: 'unset', // only until first leader key check
},
enabled: {
doc: 'Whether to enable multi-main setup for queue mode (license required)',
format: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('ExecutionRecoveryService', () => {
});

beforeEach(() => {
config.set('multiMainSetup.instanceType', 'leader');
config.set('instanceRole', 'leader');
});

afterEach(async () => {
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('ExecutionRecoveryService', () => {
/**
* Arrange
*/
config.set('multiMainSetup.instanceType', 'follower');
config.set('instanceRole', 'follower');
// @ts-expect-error Private method
const amendSpy = jest.spyOn(executionRecoveryService, 'amend');
const messages = setupMessages('123', 'Some workflow');
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/executions/execution-recovery.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export class ExecutionRecoveryService {
private shouldScheduleQueueRecovery() {
return (
config.getEnv('executions.mode') === 'queue' &&
config.getEnv('multiMainSetup.instanceType') === 'leader' &&
config.getEnv('instanceRole') === 'leader' &&
!this.isShuttingDown
);
}
Expand Down
9 changes: 3 additions & 6 deletions packages/cli/src/services/orchestration.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@ export class OrchestrationService {
return config.getEnv('redis.queueModeId');
}

/**
* Whether this instance is the leader in a multi-main setup. Always `false` in single-main setup.
*/
get isLeader() {
return config.getEnv('multiMainSetup.instanceType') === 'leader';
return config.getEnv('instanceRole') === 'leader';
}

get isFollower() {
return config.getEnv('multiMainSetup.instanceType') !== 'leader';
return config.getEnv('instanceRole') !== 'leader';
}

sanityCheck() {
Expand All @@ -66,7 +63,7 @@ export class OrchestrationService {
if (this.isMultiMainSetupEnabled) {
await this.multiMainSetup.init();
} else {
config.set('multiMainSetup.instanceType', 'leader');
config.set('instanceRole', 'leader');
}

this.isInitialized = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class MultiMainSetup extends EventEmitter {
async shutdown() {
clearInterval(this.leaderCheckInterval);

const isLeader = config.getEnv('multiMainSetup.instanceType') === 'leader';
const isLeader = config.getEnv('instanceRole') === 'leader';

if (isLeader) await this.redisPublisher.clear(this.leaderKey);
}
Expand All @@ -64,8 +64,8 @@ export class MultiMainSetup extends EventEmitter {
if (leaderId && leaderId !== this.instanceId) {
this.logger.debug(`[Instance ID ${this.instanceId}] Leader is other instance "${leaderId}"`);

if (config.getEnv('multiMainSetup.instanceType') === 'leader') {
config.set('multiMainSetup.instanceType', 'follower');
if (config.getEnv('instanceRole') === 'leader') {
config.set('instanceRole', 'follower');

this.emit('leader-stepdown'); // lost leadership - stop triggers, pollers, pruning, wait-tracking, queue recovery

Expand All @@ -80,7 +80,7 @@ export class MultiMainSetup extends EventEmitter {
`[Instance ID ${this.instanceId}] Leadership vacant, attempting to become leader...`,
);

config.set('multiMainSetup.instanceType', 'follower');
config.set('instanceRole', 'follower');

/**
* Lost leadership - stop triggers, pollers, pruning, wait tracking, license renewal, queue recovery
Expand All @@ -101,7 +101,7 @@ export class MultiMainSetup extends EventEmitter {
if (keySetSuccessfully) {
this.logger.debug(`[Instance ID ${this.instanceId}] Leader is now this instance`);

config.set('multiMainSetup.instanceType', 'leader');
config.set('instanceRole', 'leader');

await this.redisPublisher.setExpiration(this.leaderKey, this.leaderKeyTtl);

Expand All @@ -110,7 +110,7 @@ export class MultiMainSetup extends EventEmitter {
*/
this.emit('leader-takeover');
} else {
config.set('multiMainSetup.instanceType', 'follower');
config.set('instanceRole', 'follower');
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/services/pruning.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class PruningService {
if (
config.getEnv('multiMainSetup.enabled') &&
config.getEnv('generic.instanceType') === 'main' &&
config.getEnv('multiMainSetup.instanceType') === 'follower'
config.getEnv('instanceRole') === 'follower'
) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/test/unit/services/orchestration.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe('Orchestration Service', () => {

describe('shouldAddWebhooks', () => {
beforeEach(() => {
config.set('multiMainSetup.instanceType', 'leader');
config.set('instanceRole', 'leader');
});
test('should return true for init', () => {
// We want to ensure that webhooks are populated on init
Expand All @@ -169,7 +169,7 @@ describe('Orchestration Service', () => {
});

test('should return false for update or activate when not leader', () => {
config.set('multiMainSetup.instanceType', 'follower');
config.set('instanceRole', 'follower');
const modes = ['update', 'activate'] as WorkflowActivateMode[];
for (const mode of modes) {
const result = os.shouldAddWebhooks(mode);
Expand Down

0 comments on commit 99dc56c

Please sign in to comment.