Skip to content

Commit 4acaa4b

Browse files
committed
feat(api7): add parallel backend request as experimental feature
1 parent 3a1e8ea commit 4acaa4b

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

apps/cli/src/command/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as ADCSDK from '@api7/adc-sdk';
12
import { Command } from 'commander';
23
import dotenv from 'dotenv';
34

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

2425
if (
25-
process.env.ADC_EXPERIMENTAL_FEATURE_FLAGS &&
26-
process.env.ADC_EXPERIMENTAL_FEATURE_FLAGS.includes('remote-state-file')
26+
ADCSDK.utils.featureGateEnabled(ADCSDK.utils.featureGate.REMOTE_STATE_FILE)
2727
) {
2828
const desc =
2929
'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';

libs/backend-api7/src/index.ts

+55-11
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,16 @@ export class BackendAPI7 implements ADCSDK.Backend {
206206
this.getAPI7VersionTask(),
207207
this.getResourceDefaultValueTask(),
208208
this.getGatewayGroupIdTask(this.gatewayGroup),
209-
...fetcher.allTask(),
209+
...(ADCSDK.utils.featureGateEnabled(
210+
ADCSDK.utils.featureGate.PARALLEL_BACKEND_REQUEST,
211+
)
212+
? [
213+
{
214+
task: (_, task) =>
215+
task.newListr(fetcher.allTask(), { concurrent: true }),
216+
},
217+
]
218+
: fetcher.allTask()),
210219
],
211220
{
212221
//@ts-expect-error reorg renderer
@@ -222,16 +231,20 @@ export class BackendAPI7 implements ADCSDK.Backend {
222231
this.getAPI7VersionTask(),
223232
this.getGatewayGroupIdTask(this.gatewayGroup),
224233
this.syncPreprocessEventsTask(),
225-
{
226-
task: (ctx, task) =>
227-
task.newListr(
228-
ctx.diff.map((event) =>
229-
event.type === ADCSDK.EventType.DELETE
230-
? operator.deleteResource(event)
231-
: operator.updateResource(event),
232-
),
233-
),
234-
},
234+
ADCSDK.utils.featureGateEnabled(
235+
ADCSDK.utils.featureGate.PARALLEL_BACKEND_REQUEST,
236+
)
237+
? this.groupSyncTasks(operator)
238+
: {
239+
task: (ctx, task) =>
240+
task.newListr(
241+
ctx.diff.map((event) =>
242+
event.type === ADCSDK.EventType.DELETE
243+
? operator.deleteResource(event)
244+
: operator.updateResource(event),
245+
),
246+
),
247+
},
235248
operator.publishService(),
236249
],
237250
{
@@ -316,4 +329,35 @@ export class BackendAPI7 implements ADCSDK.Backend {
316329
},
317330
};
318331
}
332+
333+
// Groups tasks so that they are grouped in parallel in order of same
334+
// resource type and same operation type.
335+
private groupSyncTasks(operator: Operator): ListrTask<OperateContext> {
336+
return {
337+
task: (ctx, task) => {
338+
const groupedEvents = Object.values<Array<ADCSDK.Event>>(
339+
ctx.diff.reduce((groups, event) => {
340+
const key = `${event.resourceType}.${event.type}`;
341+
(groups[key] = groups[key] || []).push(event);
342+
return groups;
343+
}, {}),
344+
);
345+
346+
return task.newListr(
347+
groupedEvents.map((events) => ({
348+
task: (_, task) => {
349+
return task.newListr(
350+
events.map((event) =>
351+
event.type === ADCSDK.EventType.DELETE
352+
? operator.deleteResource(event)
353+
: operator.updateResource(event),
354+
),
355+
{ concurrent: true },
356+
);
357+
},
358+
})),
359+
);
360+
},
361+
};
362+
}
319363
}

libs/sdk/src/utils.ts

+13
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,21 @@ class Logger {
3838
}
3939
}
4040

41+
enum featureGate {
42+
REMOTE_STATE_FILE = 'remote-state-file',
43+
PARALLEL_BACKEND_REQUEST = 'parallel-backend-request',
44+
}
45+
const featureGateEnabled = (key: string) => {
46+
return (
47+
process.env.ADC_EXPERIMENTAL_FEATURE_FLAGS &&
48+
process.env.ADC_EXPERIMENTAL_FEATURE_FLAGS.includes(key)
49+
);
50+
};
51+
4152
export const utils = {
4253
generateId,
4354
recursiveOmitUndefined,
4455
getLogger,
56+
featureGate,
57+
featureGateEnabled,
4558
};

0 commit comments

Comments
 (0)