Skip to content

Commit

Permalink
feat(api7): add parallel backend request as experimental feature (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 authored Jan 24, 2025
1 parent 3a1e8ea commit 05a4b8c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 13 deletions.
4 changes: 2 additions & 2 deletions apps/cli/src/command/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as ADCSDK from '@api7/adc-sdk';
import { Command } from 'commander';
import dotenv from 'dotenv';

Expand All @@ -22,8 +23,7 @@ export const setupCommands = (): Command => {
.version('0.17.0', '-v, --version', 'display ADC version');

if (
process.env.ADC_EXPERIMENTAL_FEATURE_FLAGS &&
process.env.ADC_EXPERIMENTAL_FEATURE_FLAGS.includes('remote-state-file')
ADCSDK.utils.featureGateEnabled(ADCSDK.utils.featureGate.REMOTE_STATE_FILE)
) {
const desc =
'path of the remote state file, which will allow the ADC to skip the initial dump process and use the ADC configuration contained in the remote state file directly';
Expand Down
66 changes: 55 additions & 11 deletions libs/backend-api7/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,16 @@ export class BackendAPI7 implements ADCSDK.Backend {
this.getAPI7VersionTask(),
this.getResourceDefaultValueTask(),
this.getGatewayGroupIdTask(this.gatewayGroup),
...fetcher.allTask(),
...(ADCSDK.utils.featureGateEnabled(
ADCSDK.utils.featureGate.PARALLEL_BACKEND_REQUEST,
)
? [
{
task: (_, task) =>
task.newListr(fetcher.allTask(), { concurrent: true }),
},
]
: fetcher.allTask()),
],
{
//@ts-expect-error reorg renderer
Expand All @@ -222,16 +231,20 @@ export class BackendAPI7 implements ADCSDK.Backend {
this.getAPI7VersionTask(),
this.getGatewayGroupIdTask(this.gatewayGroup),
this.syncPreprocessEventsTask(),
{
task: (ctx, task) =>
task.newListr(
ctx.diff.map((event) =>
event.type === ADCSDK.EventType.DELETE
? operator.deleteResource(event)
: operator.updateResource(event),
),
),
},
ADCSDK.utils.featureGateEnabled(
ADCSDK.utils.featureGate.PARALLEL_BACKEND_REQUEST,
)
? this.groupSyncTasks(operator)
: {
task: (ctx, task) =>
task.newListr(
ctx.diff.map((event) =>
event.type === ADCSDK.EventType.DELETE
? operator.deleteResource(event)
: operator.updateResource(event),
),
),
},
operator.publishService(),
],
{
Expand Down Expand Up @@ -316,4 +329,35 @@ export class BackendAPI7 implements ADCSDK.Backend {
},
};
}

// Groups tasks so that they are grouped in parallel in order of same
// resource type and same operation type.
private groupSyncTasks(operator: Operator): ListrTask<OperateContext> {
return {
task: (ctx, task) => {
const groupedEvents = Object.values<Array<ADCSDK.Event>>(
ctx.diff.reduce((groups, event) => {
const key = `${event.resourceType}.${event.type}`;
(groups[key] = groups[key] || []).push(event);
return groups;
}, {}),
);

return task.newListr(
groupedEvents.map((events) => ({
task: (_, task) => {
return task.newListr(
events.map((event) =>
event.type === ADCSDK.EventType.DELETE
? operator.deleteResource(event)
: operator.updateResource(event),
),
{ concurrent: true },
);
},
})),
);
},
};
}
}
13 changes: 13 additions & 0 deletions libs/sdk/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,21 @@ class Logger {
}
}

enum featureGate {
REMOTE_STATE_FILE = 'remote-state-file',
PARALLEL_BACKEND_REQUEST = 'parallel-backend-request',
}
const featureGateEnabled = (key: string) => {
return (
process.env.ADC_EXPERIMENTAL_FEATURE_FLAGS &&
process.env.ADC_EXPERIMENTAL_FEATURE_FLAGS.includes(key)
);
};

export const utils = {
generateId,
recursiveOmitUndefined,
getLogger,
featureGate,
featureGateEnabled,
};

0 comments on commit 05a4b8c

Please sign in to comment.