Skip to content

Commit 337266d

Browse files
authored
Merge pull request midwayjs#5 from yanglong1010/alibaba-dragonwell-support
fix oss and nas upload bug
2 parents 044a0f0 + b4d63f4 commit 337266d

File tree

4 files changed

+136
-86
lines changed

4 files changed

+136
-86
lines changed

resources/AccelerationHelper.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ public class AccelerationHelper implements HttpRequestHandler {
2525
private static final String QUICKSTART = "/code/quickstart.sh";
2626
private static final String DUMP = "dump";
2727
private static final String SIZE = "size";
28-
private static final String OSS_UPLOAD = "ossUpload";
29-
private static final String NAS_UPLOAD = "nasUpload";
3028
private static final String KEY_NAS_FILEPATH = "nasFilePath";
3129

3230
private static final String OSSUTIL64 = "/code/ossutil64";
@@ -62,30 +60,27 @@ public String _handleRequest(HttpServletRequest request) throws Exception {
6260
if (DUMP.equals(map.get(KEY_TYPE))) {
6361
String srpath = map.get(KEY_SRPATH);
6462
data = dumpByJcmd(srpath, filePath);
65-
return data;
66-
}
67-
68-
if (OSS_UPLOAD.equals(map.get(KEY_TYPE))) {
69-
String accessKeyId = map.get(KEY_ACCESS_KEY_ID);
70-
String accessKeySecret = map.get(KEY_ACCESS_KEY_SECRET);
71-
String endpoint = map.get(KEY_ENDPOINT);
72-
String bucket = map.get(KEY_BUCKET);
73-
try {
74-
return doOSSUpload(filePath, accessKeyId, accessKeySecret, endpoint, bucket);
75-
} catch (Exception e) {
76-
e.printStackTrace();
77-
return e.getMessage();
78-
}
79-
}
80-
81-
if (NAS_UPLOAD.equals(map.get(KEY_TYPE))) {
82-
String nasFilePath = map.get(KEY_NAS_FILEPATH);
83-
try {
84-
return doNASUpload(filePath, nasFilePath);
85-
} catch (Exception e) {
86-
e.printStackTrace();
87-
return e.getMessage();
63+
if (map.containsKey(KEY_BUCKET)) {
64+
String accessKeyId = map.get(KEY_ACCESS_KEY_ID);
65+
String accessKeySecret = map.get(KEY_ACCESS_KEY_SECRET);
66+
String endpoint = map.get(KEY_ENDPOINT);
67+
String bucket = map.get(KEY_BUCKET);
68+
try {
69+
return doOSSUpload(filePath, accessKeyId, accessKeySecret, endpoint, bucket);
70+
} catch (Exception e) {
71+
e.printStackTrace();
72+
return e.getMessage();
73+
}
74+
} else if (map.containsKey(KEY_NAS_FILEPATH)) {
75+
String nasFilePath = map.get(KEY_NAS_FILEPATH);
76+
try {
77+
return doNASUpload(filePath, nasFilePath);
78+
} catch (Exception e) {
79+
e.printStackTrace();
80+
return e.getMessage();
81+
}
8882
}
83+
return data;
8984
}
9085

9186
File file = new File(filePath);

src/common.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
export const OSS = 'oss';
22
export const NAS = 'nas';
33
export const STREAM = 'stream';
4+
export const SRPATH = '/code/runtime.data.share';
5+
export const QUICK_START = '/code/quickstart.sh';
6+
export const ARTIFACT_DIR = 'target/artifact';
7+
export const OSS_UTIL_URL = 'https://gosspublic.alicdn.com/ossutil/1.7.9/ossutil64';
48

59
const prefix = '[acceleration adapter] ';
610

src/javaIndex.ts

Lines changed: 77 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import * as YAML from 'js-yaml';
2121
import * as uuid from 'uuid-1345';
2222
import * as tar from 'tar';
2323
import * as child_process from 'child_process'
24-
import {error, info, NAS, OSS} from "./common";
24+
import {error, info, NAS, OSS, OSS_UTIL_URL, QUICK_START} from "./common";
2525
import * as path from "path";
2626
import * as OSSClient from 'ali-oss';
2727
import got from 'got';
@@ -59,6 +59,7 @@ export class JavaStartupAcceleration {
5959
role;
6060
logConfig;
6161
sharedDirName;
62+
tmpSrpath;
6263
srpath;
6364
downloader;
6465
uploader;
@@ -71,11 +72,16 @@ export class JavaStartupAcceleration {
7172
timeout;
7273
initTimeout;
7374
maxMemory;
75+
tmpBucketName;
76+
enable;
77+
serviceName;
78+
functionName;
79+
funcEnvVars;
7480

7581
constructor(pwd: string, config) {
7682
const { region, fcEndpoint, access, runtime, initializer, credential, role, logConfig, sharedDirName, downloader,
7783
uploader, ossUtilUrl, ossBucket, ossKey, ossEndpoint, vpcConfig, nasConfig, srpath, maxMemory, timeout,
78-
initTimeout } = config;
84+
initTimeout, enable, serviceName, functionName, funcEnvVars } = config;
7985
this.region = region;
8086
this.runtime = runtime;
8187
this.initializer = initializer;
@@ -88,40 +94,79 @@ export class JavaStartupAcceleration {
8894
this.logConfig = logConfig;
8995
this.fcEndpoint = fcEndpoint;
9096
this.sharedDirName = sharedDirName;
91-
this.srpath = join(TMP_PATH, sharedDirName);
97+
this.tmpSrpath = join(TMP_PATH, sharedDirName);
98+
this.srpath = srpath;
9299
this.downloader = downloader;
93100
if (ossEndpoint) {
94101
this.ossEndpoint = ossEndpoint;
95102
} else {
96103
this.ossEndpoint = 'oss-${FC_REGION}-internal.aliyuncs.com'.replace('${FC_REGION}', this.region);
97104
}
98-
this.ossUtilUrl = ossUtilUrl;
105+
if (ossUtilUrl) {
106+
this.ossUtilUrl = ossUtilUrl;
107+
} else {
108+
this.ossUtilUrl = OSS_UTIL_URL;
109+
}
110+
99111
this.uploader = uploader;
100112
this.ossBucket = ossBucket;
101113
this.ossKey = ossKey;
102114
this.vpcConfig = vpcConfig;
103115
this.nasConfig = nasConfig;
104116

105117
if (this.uploader == NAS) {
106-
this.srpath = srpath;
118+
this.tmpSrpath = srpath;
107119
} else {
108-
this.srpath = SRPATH;
120+
this.tmpSrpath = SRPATH;
109121
}
110122
this.maxMemory = maxMemory;
111123
this.timeout = timeout;
112124
this.initTimeout = initTimeout;
125+
this.tmpBucketName = `tmp-acceleration-${uuid.v1()}`;
126+
this.enable = enable;
127+
this.serviceName = serviceName;
128+
this.functionName = functionName;
129+
this.funcEnvVars = funcEnvVars;
113130
}
114131

115132
public async gen() {
116-
info("acceleration function shared dir: " + this.srpath);
133+
if (this.enable) {
134+
await this.enableQuickStart();
135+
info("quickstart enabled");
136+
return;
137+
}
138+
info("acceleration function shared dir: " + this.tmpSrpath);
117139
info("local temp dir: " + tmpDir);
140+
info("use [" + this.downloader + "] to download acceleration files to local")
141+
info("use [" + this.uploader + "] to upload acceleration files to fc production")
118142
if (this.downloader == OSS) {
119143
info("oss endpoint: " + this.ossEndpoint)
120144
}
121145
await this.genDump();
122146
info("completed");
123147
}
124148

149+
async enableQuickStart() {
150+
info("function environment variables:" + JSON.stringify(this.funcEnvVars));
151+
if (this.funcEnvVars) {
152+
this.funcEnvVars['BOOTSTRAP_WRAPPER'] = QUICK_START;
153+
this.funcEnvVars['SRPATH'] = this.srpath;
154+
} else {
155+
this.funcEnvVars = {
156+
'BOOTSTRAP_WRAPPER': QUICK_START,
157+
'SRPATH': this.srpath
158+
}
159+
}
160+
const client = await this.getFCClient();
161+
let res = await client.updateFunction(
162+
this.serviceName,
163+
this.functionName,
164+
{
165+
environmentVariables: this.funcEnvVars,
166+
});
167+
info('update function result: ' + JSON.stringify(res));
168+
}
169+
125170
async genDump() {
126171
await ensureDir(tmpDir);
127172
const fcClient = await this.getFCClient();
@@ -159,6 +204,8 @@ export class JavaStartupAcceleration {
159204
if (this.uploader == OSS) {
160205
await this.createZipAndUploadToOSS();
161206
}
207+
} catch (e) {
208+
error(e.message);
162209
} finally {
163210
/* delete local temp files */
164211
await remove(tmpDir);
@@ -176,13 +223,24 @@ export class JavaStartupAcceleration {
176223

177224
if (this.uploader == NAS) {
178225
archiveFile = '';
179-
let command = 's nas command rm -rf ' + this.srpath;
226+
let command = 's nas command rm -rf ' + this.tmpSrpath;
180227
info("clear srctl path before invoking assistant function: [" + command + "]");
181228
child_process.execSync(command);
182229
}
183230

184231
info("invoking assistant function to dump acceleration files");
185-
let body = 'srpath=' + this.srpath + ';type=dump;file=' + archiveFile + ";method=jcmd";
232+
let body = 'srpath=' + this.tmpSrpath + ';type=dump;file=' + archiveFile + ";method=jcmd";
233+
if (this.downloader == OSS) {
234+
const {ak, secret } = await this.getConfig();
235+
body += ';accessKeyId=' + ak + ';' +
236+
'accessKeySecret=' + secret + ';' +
237+
'endpoint=' + this.ossEndpoint + ';' +
238+
'bucket=' + this.tmpBucketName;
239+
} else if (this.downloader == NAS && this.uploader != NAS) {
240+
let nasFilePath = join(this.nasConfig.mountPoints[0].mountDir, ARCHIVE_NAME);
241+
body += ';nasFilePath=' + nasFilePath + ';';
242+
}
243+
186244
let result = await fcClient.post(`/proxy/${tmpServiceName}/${tmpFunctionName}/action`, body, null);
187245
let data = result.data;
188246
info("server messages: " + data)
@@ -198,10 +256,9 @@ export class JavaStartupAcceleration {
198256
await ensureDir(sharedDir);
199257
let localFile = join(sharedDir, ARCHIVE_NAME);
200258
if (this.downloader == OSS) {
201-
const {ak, secret } = await this.getConfig();
202-
await this.downloadByOSS(fcClient, tmpServiceName, tmpFunctionName, ak, secret, this.ossEndpoint, localFile);
259+
await this.downloadByOSS(localFile);
203260
} else if (this.downloader == NAS) {
204-
await this.downloadByNAS(fcClient, tmpServiceName, tmpFunctionName, localFile);
261+
await this.downloadByNAS(localFile);
205262
} else {
206263
await JavaStartupAcceleration.download(fcClient, tmpServiceName, tmpFunctionName, localFile);
207264
}
@@ -225,8 +282,8 @@ export class JavaStartupAcceleration {
225282
initializationTimeout: this.initTimeout, // unit second
226283
environmentVariables: {
227284
DISABLE_JAVA11_QUICKSTART: 'true',
228-
BOOTSTRAP_WRAPPER: '/code/quickstart.sh',
229-
SRPATH: this.srpath
285+
BOOTSTRAP_WRAPPER: QUICK_START,
286+
SRPATH: this.tmpSrpath
230287
}
231288
});
232289
info("assistant function created")
@@ -411,25 +468,8 @@ export class JavaStartupAcceleration {
411468
return true;
412469
}
413470

414-
private async downloadByOSS(fcClient, tmpServiceName: string, tmpFunctionName: string,
415-
accessKeyId: string, accessKeySecret: string, endpoint: string, localFile: string) {
416-
const bucketName = `tmp-acceleration-${uuid.v1()}`;
417-
const payload =
418-
'type=ossUpload;' +
419-
'file=' + ARCHIVE_PATH + ';' +
420-
'accessKeyId=' + accessKeyId + ';' +
421-
'accessKeySecret=' + accessKeySecret + ';' +
422-
'endpoint=' + endpoint + ';' +
423-
'bucket=' + bucketName
424-
let result = await fcClient.post(`/proxy/${tmpServiceName}/${tmpFunctionName}/action`, payload, null);
425-
let data = result.data;
426-
info("oss upload result: " + data);
427-
428-
if (data.indexOf('success') != 0) {
429-
throw new Error("upload acceleration file to oss error");
430-
}
431-
432-
let client = await this.getOSSClient(bucketName);
471+
private async downloadByOSS(localFile: string) {
472+
let client = await this.getOSSClient(this.tmpBucketName);
433473

434474
try {
435475
await client.get(ARCHIVE_NAME, localFile);
@@ -440,28 +480,19 @@ export class JavaStartupAcceleration {
440480

441481
let list = await client.list();
442482
if (list.length > 0) {
443-
throw new Error('oss bucket [' + bucketName + '] is not empty');
483+
throw new Error('oss bucket [' + this.tmpBucketName + '] is not empty');
444484
}
445485

446-
await client.deleteBucket(bucketName);
447-
info('oss bucket [' + bucketName + '] deleted');
486+
await client.deleteBucket(this.tmpBucketName);
487+
info('oss bucket [' + this.tmpBucketName + '] deleted');
448488
} catch (e) {
449489
error('oss operation error:' + e.message);
450490
throw e;
451491
}
452492
}
453493

454-
private async downloadByNAS(fcClient, tmpServiceName: string, tmpFunctionName: string, localFile: string) {
494+
private async downloadByNAS(localFile: string) {
455495
let nasFilePath = join(this.nasConfig.mountPoints[0].mountDir, ARCHIVE_NAME);
456-
const payload = 'type=nasUpload;file=' + ARCHIVE_PATH + ';nasFilePath=' + nasFilePath;
457-
let result = await fcClient.post(`/proxy/${tmpServiceName}/${tmpFunctionName}/action`, payload, null);
458-
let data = result.data;
459-
info("nas upload result: " + data);
460-
461-
if (data.indexOf('success') != 0) {
462-
throw new Error("upload acceleration file to nas error: " + data);
463-
}
464-
465496
if (existsSync(localFile)) {
466497
info('before download from nas, remove existing file [' + localFile + ']')
467498
await remove(localFile);

0 commit comments

Comments
 (0)