Skip to content

Commit 730d0a0

Browse files
feat: retry multipart Bucket.upload (#1509)
* feat: retry multipart Bucket.upload * removed log statement * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md * removed unused variables * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent cf53a5e commit 730d0a0

File tree

2 files changed

+196
-14
lines changed

2 files changed

+196
-14
lines changed

src/bucket.ts

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import * as mime from 'mime-types';
3434
import * as path from 'path';
3535
import pLimit = require('p-limit');
3636
import {promisify} from 'util';
37+
import retry = require('async-retry');
3738

3839
// eslint-disable-next-line @typescript-eslint/no-var-requires
3940
const snakeize = require('snakeize');
@@ -3723,6 +3724,54 @@ class Bucket extends ServiceObject {
37233724
optionsOrCallback?: UploadOptions | UploadCallback,
37243725
callback?: UploadCallback
37253726
): Promise<UploadResponse> | void {
3727+
const upload = () => {
3728+
const isMultipart = options.resumable === false;
3729+
const returnValue = retry(
3730+
async (bail: (err: Error) => void) => {
3731+
await new Promise<void>((resolve, reject) => {
3732+
const writable = newFile.createWriteStream(options);
3733+
if (options.onUploadProgress) {
3734+
writable.on('progress', options.onUploadProgress);
3735+
}
3736+
fs.createReadStream(pathString)
3737+
.pipe(writable)
3738+
.on('error', err => {
3739+
if (
3740+
isMultipart &&
3741+
this.storage.retryOptions.autoRetry &&
3742+
this.storage.retryOptions.retryableErrorFn!(err)
3743+
) {
3744+
return reject(err);
3745+
} else {
3746+
return bail(err);
3747+
}
3748+
})
3749+
.on('finish', () => {
3750+
return resolve();
3751+
});
3752+
});
3753+
},
3754+
{
3755+
retries: this.storage.retryOptions.maxRetries,
3756+
factor: this.storage.retryOptions.retryDelayMultiplier,
3757+
maxTimeout: this.storage.retryOptions.maxRetryDelay! * 1000, //convert to milliseconds
3758+
maxRetryTime: this.storage.retryOptions.totalTimeout! * 1000, //convert to milliseconds
3759+
}
3760+
);
3761+
3762+
if (!callback) {
3763+
return returnValue;
3764+
} else {
3765+
return returnValue
3766+
.then(() => {
3767+
if (callback) {
3768+
return callback!(null, newFile, newFile.metadata);
3769+
}
3770+
})
3771+
.catch(callback);
3772+
}
3773+
};
3774+
37263775
// eslint-disable-next-line @typescript-eslint/no-explicit-any
37273776
if ((global as any)['GCLOUD_SANDBOX_ENV']) {
37283777
return;
@@ -3779,20 +3828,6 @@ class Bucket extends ServiceObject {
37793828
upload();
37803829
});
37813830
}
3782-
3783-
function upload() {
3784-
const writable = newFile.createWriteStream(options);
3785-
if (options.onUploadProgress) {
3786-
writable.on('progress', options.onUploadProgress);
3787-
}
3788-
fs.createReadStream(pathString)
3789-
.on('error', callback!)
3790-
.pipe(writable)
3791-
.on('error', callback!)
3792-
.on('finish', () => {
3793-
callback!(null, newFile, newFile.metadata);
3794-
});
3795-
}
37963831
}
37973832

37983833
makeAllFilesPublicPrivate_(

test/bucket.ts

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
import {AddAclOptions} from '../src/acl';
5252
import {Policy} from '../src/iam';
5353
import sinon = require('sinon');
54+
import {Transform} from 'stream';
5455

5556
class FakeFile {
5657
calledWith_: IArguments;
@@ -167,6 +168,14 @@ const fakeSigner = {
167168
URLSigner: () => {},
168169
};
169170

171+
class HTTPError extends Error {
172+
code: number;
173+
constructor(message: string, code: number) {
174+
super(message);
175+
this.code = code;
176+
}
177+
}
178+
170179
describe('Bucket', () => {
171180
// eslint-disable-next-line @typescript-eslint/no-explicit-any
172181
let Bucket: any;
@@ -175,6 +184,16 @@ describe('Bucket', () => {
175184

176185
const STORAGE = {
177186
createBucket: util.noop,
187+
retryOptions: {
188+
autoRetry: true,
189+
maxRetries: 3,
190+
retryDelayMultipier: 2,
191+
totalTimeout: 600,
192+
maxRetryDelay: 60,
193+
retryableErrorFn: (err: HTTPError) => {
194+
return err.code === 500;
195+
},
196+
},
178197
};
179198
const BUCKET_NAME = 'test-bucket';
180199

@@ -2477,6 +2496,134 @@ describe('Bucket', () => {
24772496
});
24782497
});
24792498

2499+
describe('multipart uploads', () => {
2500+
class DelayedStream500Error extends Transform {
2501+
retryCount: number;
2502+
constructor(retryCount: number) {
2503+
super();
2504+
this.retryCount = retryCount;
2505+
}
2506+
_transform(chunk: string | Buffer, _encoding: string, done: Function) {
2507+
this.push(chunk);
2508+
setTimeout(() => {
2509+
if (this.retryCount === 1) {
2510+
done(new HTTPError('first error', 500));
2511+
} else {
2512+
done();
2513+
}
2514+
}, 5);
2515+
}
2516+
}
2517+
2518+
beforeEach(() => {
2519+
fsStatOverride = (path: string, callback: Function) => {
2520+
callback(null, {size: 1}); // Small size to guarantee simple upload
2521+
};
2522+
});
2523+
2524+
it('should save with no errors', done => {
2525+
const fakeFile = new FakeFile(bucket, 'file-name');
2526+
const options = {destination: fakeFile, resumable: false};
2527+
fakeFile.createWriteStream = (options_: CreateWriteStreamOptions) => {
2528+
class DelayedStreamNoError extends Transform {
2529+
_transform(
2530+
chunk: string | Buffer,
2531+
_encoding: string,
2532+
done: Function
2533+
) {
2534+
this.push(chunk);
2535+
setTimeout(() => {
2536+
done();
2537+
}, 5);
2538+
}
2539+
}
2540+
assert.strictEqual(options_.resumable, false);
2541+
return new DelayedStreamNoError();
2542+
};
2543+
bucket.upload(filepath, options, (err: Error) => {
2544+
assert.ifError(err);
2545+
done();
2546+
});
2547+
});
2548+
2549+
it('should retry on first failure', done => {
2550+
const fakeFile = new FakeFile(bucket, 'file-name');
2551+
const options = {destination: fakeFile, resumable: false};
2552+
let retryCount = 0;
2553+
fakeFile.createWriteStream = (options_: CreateWriteStreamOptions) => {
2554+
setImmediate(() => {
2555+
assert.strictEqual(options_.resumable, false);
2556+
retryCount++;
2557+
done();
2558+
});
2559+
return new DelayedStream500Error(retryCount);
2560+
};
2561+
bucket.upload(filepath, options, (err: Error, file: FakeFile) => {
2562+
assert.ifError(err);
2563+
assert(file.isSameFile());
2564+
assert.deepStrictEqual(file.metadata, metadata);
2565+
assert.ok(retryCount === 2);
2566+
done();
2567+
});
2568+
});
2569+
2570+
it('should not retry if nonretryable error code', done => {
2571+
const fakeFile = new FakeFile(bucket, 'file-name');
2572+
const options = {destination: fakeFile, resumable: false};
2573+
let retryCount = 0;
2574+
fakeFile.createWriteStream = (options_: CreateWriteStreamOptions) => {
2575+
class DelayedStream403Error extends Transform {
2576+
_transform(
2577+
chunk: string | Buffer,
2578+
_encoding: string,
2579+
done: Function
2580+
) {
2581+
this.push(chunk);
2582+
setTimeout(() => {
2583+
retryCount++;
2584+
if (retryCount === 1) {
2585+
done(new HTTPError('first error', 403));
2586+
} else {
2587+
done();
2588+
}
2589+
}, 5);
2590+
}
2591+
}
2592+
setImmediate(() => {
2593+
assert.strictEqual(options_.resumable, false);
2594+
retryCount++;
2595+
done();
2596+
});
2597+
return new DelayedStream403Error();
2598+
};
2599+
2600+
bucket.upload(filepath, options, (err: Error) => {
2601+
assert.strictEqual(err.message, 'first error');
2602+
assert.ok(retryCount === 2);
2603+
done();
2604+
});
2605+
});
2606+
2607+
it('non-multipart upload should not retry', done => {
2608+
const fakeFile = new FakeFile(bucket, 'file-name');
2609+
const options = {destination: fakeFile, resumable: true};
2610+
let retryCount = 0;
2611+
fakeFile.createWriteStream = (options_: CreateWriteStreamOptions) => {
2612+
setImmediate(() => {
2613+
assert.strictEqual(options_.resumable, true);
2614+
retryCount++;
2615+
done();
2616+
});
2617+
return new DelayedStream500Error(retryCount);
2618+
};
2619+
bucket.upload(filepath, options, (err: Error) => {
2620+
assert.strictEqual(err.message, 'first error');
2621+
assert.ok(retryCount === 1);
2622+
done();
2623+
});
2624+
});
2625+
});
2626+
24802627
it('should allow overriding content type', done => {
24812628
const fakeFile = new FakeFile(bucket, 'file-name');
24822629
const metadata = {contentType: 'made-up-content-type'};

0 commit comments

Comments
 (0)