GeezerVison is showing github.com — fix: retry multipart uploads on File.save() (#1385) · googleapis/google-cloud-node@2d8701d
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: handwritten/storage/package.json +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 56 | 56 | "@google-cloud/paginator": "^3.0.0", | 57 | 57 | "@google-cloud/promisify": "^2.0.0", | 58 | 58 | "arrify": "^2.0.0", | | 59 | + "async-retry": "^1.3.1", | 59 | 60 | "compressible": "^2.0.12", | 60 | 61 | "date-and-time": "^0.14.2", | 61 | 62 | "duplexify": "^4.0.0", | | 79 | 80 | "@grpc/proto-loader": "^0.5.1", | 80 | 81 | "@microsoft/api-documenter": "^7.8.10", | 81 | 82 | "@microsoft/api-extractor": "^7.8.10", | | 83 | + "@types/async-retry": "^1.4.2", | 82 | 84 | "@types/compressible": "^2.0.0", | 83 | 85 | "@types/concat-stream": "^1.6.0", | 84 | 86 | "@types/configstore": "^4.0.0", | |
Collapse file Copy file name to clipboardExpand all lines: handwritten/storage/src/file.ts +39-9Lines changed: 39 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -64,6 +64,7 @@ import { | 64 | 64 | const duplexify: DuplexifyConstructor = require('duplexify'); | 65 | 65 | import {normalize, objectKeyToLowercase, unicodeJSONStringify} from './util'; | 66 | 66 | import {GaxiosError, Headers, request as gaxiosRequest} from 'gaxios'; | | 67 | +import retry = require('async-retry'); | 67 | 68 |
| 68 | 69 | export type GetExpirationDateResponse = [Date]; | 69 | 70 | export interface GetExpirationDateCallback { | @@ -3517,6 +3518,8 @@ class File extends ServiceObject<File> { | 3517 | 3518 | * Resumable uploads are automatically enabled and must be shut off explicitly | 3518 | 3519 | * by setting `options.resumable` to `false`. | 3519 | 3520 | * | | 3521 | + * Multipart uploads with retryable error codes will be retried 3 times with exponential backoff. | | 3522 | + * | 3520 | 3523 | * <p class="notice"> | 3521 | 3524 | * There is some overhead when using a resumable upload that can cause | 3522 | 3525 | * noticeable performance degradation while uploading a series of small | @@ -3560,15 +3563,42 @@ class File extends ServiceObject<File> { | 3560 | 3563 | typeof optionsOrCallback === 'function' ? optionsOrCallback : callback; | 3561 | 3564 | const options = | 3562 | 3565 | typeof optionsOrCallback === 'object' ? optionsOrCallback : {}; | 3563 | | - | 3564 | | - const writable = this.createWriteStream(options) | 3565 | | - .on('error', callback!) | 3566 | | - .on('finish', callback!); | 3567 | | - if (options.onUploadProgress) { | 3568 | | - writable.on('progress', options.onUploadProgress); | | 3566 | + const isMultipart = options.resumable === false; | | 3567 | + const returnValue = retry( | | 3568 | + async (bail: (err: Error) => void) => { | | 3569 | + await new Promise<void>((resolve, reject) => { | | 3570 | + const writable = this.createWriteStream(options) | | 3571 | + .on('error', err => { | | 3572 | + if (isMultipart && util.shouldRetryRequest(err)) { | | 3573 | + return reject(err); | | 3574 | + } else { | | 3575 | + return bail(err); | | 3576 | + } | | 3577 | + }) | | 3578 | + .on('finish', () => { | | 3579 | + return resolve(); | | 3580 | + }); | | 3581 | + if (options.onUploadProgress) { | | 3582 | + writable.on('progress', options.onUploadProgress); | | 3583 | + } | | 3584 | + writable.end(data); | | 3585 | + }); | | 3586 | + }, | | 3587 | + { | | 3588 | + retries: 3, | | 3589 | + } | | 3590 | + ); | | 3591 | + if (!callback) { | | 3592 | + return returnValue; | | 3593 | + } else { | | 3594 | + return returnValue | | 3595 | + .then(() => { | | 3596 | + if (callback) { | | 3597 | + return callback(); | | 3598 | + } | | 3599 | + }) | | 3600 | + .catch(callback); | 3569 | 3601 | } | 3570 | | - | 3571 | | - writable.end(data); | 3572 | 3602 | } | 3573 | 3603 | setStorageClass( | 3574 | 3604 | storageClass: string, | @@ -3816,7 +3846,7 @@ class File extends ServiceObject<File> { | 3816 | 3846 | * that a callback is omitted. | 3817 | 3847 | */ | 3818 | 3848 | promisifyAll(File, { | 3819 | | - exclude: ['publicUrl', 'request', 'setEncryptionKey'], | | 3849 | + exclude: ['publicUrl', 'request', 'save', 'setEncryptionKey'], | 3820 | 3850 | }); | 3821 | 3851 |
| 3822 | 3852 | /** | |
Collapse file Copy file name to clipboardExpand all lines: handwritten/storage/test/file.ts +139Lines changed: 139 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,6 +54,14 @@ import { | 54 | 54 | MoveOptions, | 55 | 55 | } from '../src/file'; | 56 | 56 |
| | 57 | +class HTTPError extends Error { | | 58 | + code: number; | | 59 | + constructor(message: string, code: number) { | | 60 | + super(message); | | 61 | + this.code = code; | | 62 | + } | | 63 | +} | | 64 | + | 57 | 65 | let promisified = false; | 58 | 66 | let makeWritableStreamOverride: Function | null; | 59 | 67 | let handleRespOverride: Function | null; | @@ -64,6 +72,9 @@ const fakeUtil = Object.assign({}, util, { | 64 | 72 | makeWritableStream(...args: Array<{}>) { | 65 | 73 | (makeWritableStreamOverride || util.makeWritableStream)(...args); | 66 | 74 | }, | | 75 | + shouldRetryRequest(err: HTTPError) { | | 76 | + return err.code === 500; | | 77 | + }, | 67 | 78 | }); | 68 | 79 |
| 69 | 80 | const fakePromisify = { | @@ -77,6 +88,7 @@ const fakePromisify = { | 77 | 88 | assert.deepStrictEqual(options.exclude, [ | 78 | 89 | 'publicUrl', | 79 | 90 | 'request', | | 91 | + 'save', | 80 | 92 | 'setEncryptionKey', | 81 | 93 | ]); | 82 | 94 | }, | @@ -3853,6 +3865,133 @@ describe('File', () => { | 3853 | 3865 |
| 3854 | 3866 | describe('save', () => { | 3855 | 3867 | const DATA = 'Data!'; | | 3868 | + const BUFFER_DATA = Buffer.from(DATA, 'utf8'); | | 3869 | + | | 3870 | + class DelayedStreamNoError extends Transform { | | 3871 | + _transform(chunk: string | Buffer, _encoding: string, done: Function) { | | 3872 | + this.push(chunk); | | 3873 | + setTimeout(() => { | | 3874 | + done(); | | 3875 | + }, 5); | | 3876 | + } | | 3877 | + } | | 3878 | + | | 3879 | + class DelayedStream500Error extends Transform { | | 3880 | + retryCount: number; | | 3881 | + constructor(retryCount: number) { | | 3882 | + super(); | | 3883 | + this.retryCount = retryCount; | | 3884 | + } | | 3885 | + _transform(chunk: string | Buffer, _encoding: string, done: Function) { | | 3886 | + this.push(chunk); | | 3887 | + setTimeout(() => { | | 3888 | + if (this.retryCount === 1) { | | 3889 | + done(new HTTPError('first error', 500)); | | 3890 | + } else { | | 3891 | + done(); | | 3892 | + } | | 3893 | + }, 5); | | 3894 | + } | | 3895 | + } | | 3896 | + | | 3897 | + describe('retry mulipart upload', () => { | | 3898 | + it('should save a string with no errors', async () => { | | 3899 | + const options = {resumable: false}; | | 3900 | + file.createWriteStream = () => { | | 3901 | + return new DelayedStreamNoError(); | | 3902 | + }; | | 3903 | + await file.save(DATA, options, assert.ifError); | | 3904 | + }); | | 3905 | + | | 3906 | + it('string upload should retry on first failure', async () => { | | 3907 | + const options = {resumable: false}; | | 3908 | + let retryCount = 0; | | 3909 | + file.createWriteStream = () => { | | 3910 | + retryCount++; | | 3911 | + return new DelayedStream500Error(retryCount); | | 3912 | + }; | | 3913 | + await file.save(DATA, options); | | 3914 | + assert.ok(retryCount === 2); | | 3915 | + }); | | 3916 | + | | 3917 | + it('string upload should not retry if nonretryable error code', async () => { | | 3918 | + const options = {resumable: false}; | | 3919 | + let retryCount = 0; | | 3920 | + file.createWriteStream = () => { | | 3921 | + class DelayedStream403Error extends Transform { | | 3922 | + _transform( | | 3923 | + chunk: string | Buffer, | | 3924 | + _encoding: string, | | 3925 | + done: Function | | 3926 | + ) { | | 3927 | + this.push(chunk); | | 3928 | + setTimeout(() => { | | 3929 | + retryCount++; | | 3930 | + if (retryCount === 1) { | | 3931 | + done(new HTTPError('first error', 403)); | | 3932 | + } else { | | 3933 | + done(); | | 3934 | + } | | 3935 | + }, 5); | | 3936 | + } | | 3937 | + } | | 3938 | + return new DelayedStream403Error(); | | 3939 | + }; | | 3940 | + try { | | 3941 | + await file.save(DATA, options); | | 3942 | + throw Error('unreachable'); | | 3943 | + } catch (e) { | | 3944 | + assert.strictEqual(e.message, 'first error'); | | 3945 | + } | | 3946 | + }); | | 3947 | + | | 3948 | + it('should save a buffer with no errors', async () => { | | 3949 | + const options = {resumable: false}; | | 3950 | + file.createWriteStream = () => { | | 3951 | + return new DelayedStreamNoError(); | | 3952 | + }; | | 3953 | + await file.save(DATA, options, assert.ifError); | | 3954 | + }); | | 3955 | + | | 3956 | + it('buffer upload should retry on first failure', async () => { | | 3957 | + const options = {resumable: false}; | | 3958 | + let retryCount = 0; | | 3959 | + file.createWriteStream = () => { | | 3960 | + retryCount++; | | 3961 | + return new DelayedStream500Error(retryCount); | | 3962 | + }; | | 3963 | + await file.save(BUFFER_DATA, options); | | 3964 | + assert.ok(retryCount === 2); | | 3965 | + }); | | 3966 | + | | 3967 | + it('non-multipart upload should not retry', async () => { | | 3968 | + const options = {resumable: true}; | | 3969 | + let retryCount = 0; | | 3970 | + file.createWriteStream = () => { | | 3971 | + retryCount++; | | 3972 | + return new DelayedStream500Error(retryCount); | | 3973 | + }; | | 3974 | + try { | | 3975 | + await file.save(DATA, options); | | 3976 | + throw Error('unreachable'); | | 3977 | + } catch (e) { | | 3978 | + assert.strictEqual(e.message, 'first error'); | | 3979 | + } | | 3980 | + }); | | 3981 | + }); | | 3982 | + | | 3983 | + it('should execute callback', async () => { | | 3984 | + const options = {resumable: true}; | | 3985 | + let retryCount = 0; | | 3986 | + file.createWriteStream = () => { | | 3987 | + retryCount++; | | 3988 | + return new DelayedStream500Error(retryCount); | | 3989 | + }; | | 3990 | + | | 3991 | + file.save(DATA, options, (err: HTTPError) => { | | 3992 | + assert.strictEqual(err.code, 500); | | 3993 | + }); | | 3994 | + }); | 3856 | 3995 |
| 3857 | 3996 | it('should accept an options object', done => { | 3858 | 3997 | const options = {}; | |
|