import _ from "lodash"; import { getNS, stringify, throwUnsupportedParamError, timeoutOrResult } from "../common"; import { loggerFactory } from "../logger"; import { AgreementExecutor } from "./AgreementExecutor"; import { AllEqualConsensusExecutor, MedianConsensusExecutor } from "./ConsensusExecutor"; import { Executor } from "./Executor"; import { FallbackExecutor } from "./FallbackExecutor"; import { FnBox } from "./FnBox"; import { MultiAgreementExecutor } from "./MultiAgreementExecutor"; import { RaceExecutor } from "./RaceExecutor"; import { DEFAULT_CONFIG, ExecutionMode, MultiExecutorConfig, NestedMethodConfig } from "./config"; interface ProxyMeta { instances: T[]; config: MultiExecutorConfig; } const PROXY_META = new WeakMap>(); export class MultiExecutorFactory { private readonly logger = loggerFactory("multi-executor-proxy"); constructor( private readonly instances: T[], private readonly methodConfig: NestedMethodConfig, private readonly config: MultiExecutorConfig ) {} private getMethodMode(methodName: keyof T) { const mode = this.methodConfig[methodName]; if (mode instanceof Executor) { return mode; } if (typeof mode === "string") { return mode as ExecutionMode; } if (mode === undefined) { return this.config.defaultMode; } throw new Error(`Unexpected NestedMethodConfig for function ${methodName.toString()}`); } private getExecutor(mode: ExecutionMode | Executor): Executor { if (mode instanceof Executor) { return mode; } switch (mode) { case ExecutionMode.RACE: return new RaceExecutor(); case ExecutionMode.FALLBACK: return new FallbackExecutor(this.config.singleExecutionTimeoutMs); case ExecutionMode.CONSENSUS_MEDIAN: return new MedianConsensusExecutor( this.config.consensusQuorumRatio, this.config.singleExecutionTimeoutMs ); case ExecutionMode.CONSENSUS_ALL_EQUAL: return new AllEqualConsensusExecutor( this.config.consensusQuorumRatio, this.config.singleExecutionTimeoutMs ); case ExecutionMode.AGREEMENT: return new AgreementExecutor( this.config.agreementQuorumNumber, this.config.singleExecutionTimeoutMs ); case ExecutionMode.MULTI_AGREEMENT: // in this case we now R is an array return new MultiAgreementExecutor( this.config.agreementQuorumNumber, this.config.singleExecutionTimeoutMs, this.config.multiAgreementShouldResolveUnagreedToUndefined ) as unknown as Executor; default: return throwUnsupportedParamError(mode); } } createProxy(): T { // eslint-disable-next-line @typescript-eslint/no-this-alias -- add reason here, please const that = this; const proxy = new Proxy(this.instances[0], { get(target: T, prop: string | symbol): unknown { const key = prop as keyof T; const method = target[key]; if (Object(method) !== method) { return method; } if (typeof method !== "function") { return create( that.instances.map((instance) => instance[key] as object), that.methodConfig[key] ?? {}, that.config ); } if (method.constructor.name === "AsyncFunction" || method.toString().includes("Promise")) { return async function (...args: unknown[]): Promise { const functions = that.instances.map((instance, index) => MultiExecutorFactory.makeFnBox(String(key), that.config, index, instance, key, args) ); return await that.performExecuting(key, functions); }; } return function (...args: unknown[]): unknown { return (target[key] as (...args: unknown[]) => unknown).call(target, ...args); }; }, set(_target: T, prop: string | symbol): boolean { throw new Error( `Cannot assign ${String(prop)} on a multi-executor: the write would land on the first instance, whose call would re-enter the aggregate` ); }, }); PROXY_META.set(proxy, { instances: this.instances, config: this.config }); return proxy; } private static makeFnBox( name: string, config: MultiExecutorConfig, index: number, instance: T, key: keyof T, args: unknown[] ): FnBox { return { name, description: config.descriptions?.[index], index, delegate: config.delegate, fn: () => Promise.resolve((instance[key] as (...args: unknown[]) => unknown).call(instance, ...args)), }; } private async performExecuting(key: keyof T, functions: FnBox[]) { const mode = this.getMethodMode(key); this.logger.debug( `[${stringify(key)}] Executing ${getNS(functions.length, "promise")}` + ` with ${typeof mode === "string" ? mode : typeof mode}` + (this.config.allExecutionsTimeoutMs ? ` and totalExecutionTimeout: ${this.config.allExecutionsTimeoutMs} [ms]` : "") ); const result = this.getExecutor(mode).execute(functions); const value = await timeoutOrResult(result, this.config.allExecutionsTimeoutMs); this.logger.trace(`[${stringify(key)}] Returning ${stringify(value)}`); return value; } } export function create( instances: T[], methodConfig: NestedMethodConfig = {}, config: MultiExecutorConfig = DEFAULT_CONFIG ): T { if (!instances.length) { throw new Error("At least one instance is required"); } return new MultiExecutorFactory(instances, methodConfig, config).createProxy(); } export function getSubInstances(subject: T) { return getProxyMeta(subject)?.instances; } export function propagateProxyMeta( source: T, target: Target ) { const meta = getProxyMeta(source); if (meta) { PROXY_META.set(target, meta); } return target; } export function createForSubInstances( subject: T, callback: (arg: T) => U, methodConfig: NestedMethodConfig = {}, config: MultiExecutorConfig = DEFAULT_CONFIG ) { const meta = getProxyMeta(subject); const instances = meta ? meta.instances.map(callback) : [callback(subject)]; const mergedConfig = meta ? _.assign({}, meta.config, config) : config; return create(instances, methodConfig, mergedConfig); } function getProxyMeta(subject: T) { return PROXY_META.get(subject) as ProxyMeta | undefined; }