Skip to content

Commit b8db315

Browse files
committed
feat: add provision config drain mode support and related tests
- Add drain mode support in provision config implementation - Implement disable/enable function invocation for fast scale-to-zero - Add E2E test files for provision drain mode verification - Update dependencies (@alicloud/fc20230330 and ts-jest) - Add test case in CI script for nodejs provision config mode=drain
1 parent 67de068 commit b8db315

8 files changed

Lines changed: 107 additions & 42 deletions

File tree

__tests__/e2e/ci-mac-linux.sh

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,38 @@ s remove -y -t ./go/s.yaml
7777
rm -rf ./go/code/target
7878
cd ..
7979

80-
echo "test nodejs runtime with auto ..."
80+
81+
echo "test nodejs runtime with provision config mode=drain ..."
8182
cd nodejs
82-
export fc_component_function_name=nodejs18-$(uname)-$(uname -m)-$RANDSTR
83+
export fc_component_function_name=nodejs18-provision-drain-$(uname)-$(uname -m)-$RANDSTR
84+
s deploy -y -t s_provision_drain.yaml
85+
s invoke -e '{"hello":"fc nodejs provision config mode=drain"}' -t s_provision_drain.yaml
86+
s info -y -t s_provision_drain.yaml
87+
s remove -y -t s_provision_drain.yaml
88+
89+
echo "test nodejs runtime with auto ..."
90+
export fc_component_function_name=nodejs18-nas-auto-$(uname)-$(uname -m)-$RANDSTR
8391
s deploy -y -t s_auto.yaml
8492
s invoke -e '{"hello":"fc nodejs with auto"}' -t s_auto.yaml
8593
s info -y -t s_auto.yaml
8694
s remove -y -t s_auto.yaml
8795

8896
echo "test nodejs runtime with oss config auto ..."
89-
export fc_component_function_name=nodejs18-$(uname)-$(uname -m)-$RANDSTR
97+
export fc_component_function_name=nodejs18-oss-auto-$(uname)-$(uname -m)-$RANDSTR
9098
s deploy -y -t ./s_oss_config_auto.yaml
9199
s invoke -e '{"hello":"fc nodejs with oss config auto"}' -t ./s_oss_config_auto.yaml
92100
s info -y -t ./s_oss_config_auto.yaml
93101
s remove -y -t ./s_oss_config_auto.yaml
94102

95103
echo "test nodejs runtime with more vpc and nas auto ..."
96-
export fc_component_function_name=nodejs16-$(uname)-$(uname -m)-$RANDSTR
104+
export fc_component_function_name=nodejs16-multi-nas-auto-$(uname)-$(uname -m)-$RANDSTR
97105
s deploy -y -t ./s_lock_auto.yaml
98106
s invoke -e '{"hello":"fc nodejs with more vpc and nas auto"}' -t ./s_lock_auto.yaml
99107
s info -y -t ./s_lock_auto.yaml
100108
s remove -y -t ./s_lock_auto.yaml
101109

102110
echo "test nodejs runtime with tags ..."
103-
export fc_component_function_name=nodejs16-$(uname)-$(uname -m)-$RANDSTR
111+
export fc_component_function_name=nodejs16-tags-$(uname)-$(uname -m)-$RANDSTR
104112
s deploy -y -t ./s_tags.yaml
105113
s deploy -y -t ./s_tags2.yaml
106114
s deploy -y -t ./s_tags3.yaml
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
exports.handler = async function (event, context) {
2+
console.log('event: \n' + event);
3+
return 'Hello World!';
4+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
edition: 3.0.0
2+
name: test-node-app
3+
access: quanxi
4+
5+
vars:
6+
region: ${env('REGION', 'cn-hongkong')}
7+
8+
resources:
9+
fcDemo: # 业务名称/模块名称
10+
component: ${env('fc_component_version', path('../../../'))}
11+
props: # 组件的属性值
12+
region: ${vars.region}
13+
functionName: fc3-event-test-drain-${env('fc_component_function_name', 'nodejs18')}
14+
runtime: ${env('fc_component_runtime', 'nodejs18')}
15+
code: ./hello-code
16+
handler: index.handler
17+
memorySize: 128
18+
timeout: 60
19+
20+
provisionConfig:
21+
defaultTarget: 0
22+
mode: drain

package-lock.json

Lines changed: 29 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"dependencies": {
2525
"@alicloud/devs20230714": "^2.4.6-alpha.2",
2626
"@alicloud/fc2": "^2.6.6",
27-
"@alicloud/fc20230330": "4.6.0",
27+
"@alicloud/fc20230330": "4.6.2",
2828
"@alicloud/pop-core": "^1.8.0",
2929
"@serverless-cd/srm-aliyun-pop-core": "^0.0.8-beta.1",
3030
"@serverless-cd/srm-aliyun-ram20150501": "^0.0.2-beta.9",
@@ -68,7 +68,7 @@
6868
"patch-package": "^8.0.0",
6969
"postinstall-prepare": "^2.0.0",
7070
"prettier": "^3.6.2",
71-
"ts-jest": "^29.4.4",
71+
"ts-jest": "^29.4.5",
7272
"ts-node": "^10.9.2",
7373
"typescript": "^4.4.2",
7474
"typescript-json-schema": "^0.65.1"

src/resources/fc/impl/client.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import FCClient, {
4848
PutScalingConfigInput,
4949
GetScalingConfigRequest,
5050
DeleteScalingConfigRequest,
51+
DisableFunctionInvocationRequest,
5152
} from '@alicloud/fc20230330';
5253
import { ICredentials } from '@serverless-devs/component-interface';
5354
import { RuntimeOptions } from '@alicloud/tea-util';
@@ -639,4 +640,25 @@ export default class FC_Client {
639640
);
640641
return body;
641642
}
643+
644+
async disableFunctionInvocation(
645+
functionName: string,
646+
abortOngoingRequest: boolean,
647+
reason: string,
648+
) {
649+
const request = new DisableFunctionInvocationRequest({
650+
reason,
651+
abortOngoingRequest,
652+
});
653+
const result = await this.fc20230330Client.disableFunctionInvocation(functionName, request);
654+
const { body } = result.toMap();
655+
logger.debug(`DisableFunction ${functionName} result body: ${JSON.stringify(body)}`);
656+
return body;
657+
}
658+
async enableFunctionInvocation(functionName: string) {
659+
const result = await this.fc20230330Client.enableFunctionInvocation(functionName);
660+
const { body } = result.toMap();
661+
logger.debug(`EnableFunction ${functionName} result body: ${JSON.stringify(body)}`);
662+
return body;
663+
}
642664
}

src/resources/fc/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,10 @@ export default class FC extends FC_Client {
493493
_.unset(body, 'ossMountConfig');
494494
}
495495

496+
if (_.isEmpty(body.polarFsConfig?.mountPoints)) {
497+
_.unset(body, 'polarFsConfig');
498+
}
499+
496500
if (_.isEmpty(body.tracingConfig)) {
497501
_.unset(body, 'tracingConfig');
498502
}

src/subCommands/deploy/impl/provision_config.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default class ProvisionConfig extends Base {
4848
if (this.needDeploy) {
4949
await this.fcSdk.putFunctionProvisionConfig(this.functionName, qualifier, localConfig);
5050

51-
if (this.ProvisionMode === 'sync') {
51+
if (this.ProvisionMode === 'sync' || this.ProvisionMode === 'drain') {
5252
await this.waitForProvisionReady(qualifier, localConfig);
5353
} else {
5454
logger.info(
@@ -86,7 +86,16 @@ export default class ProvisionConfig extends Base {
8686

8787
// 如果没有目标值或目标值为0,则无需等待
8888
if (!realTarget || realTarget <= 0) {
89-
return;
89+
if (this.ProvisionMode !== 'drain') {
90+
return;
91+
} else {
92+
logger.info(`disableFunctionInvocation ${this.functionName} ...`);
93+
await this.fcSdk.disableFunctionInvocation(this.functionName, true, 'Fast scale-to-zero');
94+
await sleep(5);
95+
logger.info(`enableFunctionInvocation ${this.functionName} ...`);
96+
await this.fcSdk.enableFunctionInvocation(this.functionName);
97+
return;
98+
}
9099
}
91100

92101
let getCurrentErrorCount = 0;
@@ -143,7 +152,6 @@ export default class ProvisionConfig extends Base {
143152
// eslint-disable-next-line no-await-in-loop
144153
await sleep(5);
145154
}
146-
147155
logger.warn(
148156
`Timeout waiting for provisionConfig of ${this.functionName}/${qualifier} to be ready`,
149157
);

0 commit comments

Comments
 (0)