|
15 | 15 | import {promisifyAll} from '@google-cloud/promisify'; |
16 | 16 | import arrify = require('arrify'); |
17 | 17 | import {ServiceError} from 'google-gax'; |
18 | | -import {decorateStatus} from './decorateStatus'; |
| 18 | +import {BackoffSettings} from 'google-gax/build/src/gax'; |
19 | 19 | import {PassThrough, Transform} from 'stream'; |
20 | 20 |
|
21 | 21 | // eslint-disable-next-line @typescript-eslint/no-var-requires |
@@ -46,9 +46,16 @@ import {Duplex} from 'stream'; |
46 | 46 | // See protos/google/rpc/code.proto |
47 | 47 | // (4=DEADLINE_EXCEEDED, 10=ABORTED, 14=UNAVAILABLE) |
48 | 48 | const RETRYABLE_STATUS_CODES = new Set([4, 10, 14]); |
| 49 | +const IDEMPOTENT_RETRYABLE_STATUS_CODES = new Set([4, 14]); |
49 | 50 | // (1=CANCELLED) |
50 | 51 | const IGNORED_STATUS_CODES = new Set([1]); |
51 | 52 |
|
| 53 | +const DEFAULT_BACKOFF_SETTINGS: BackoffSettings = { |
| 54 | + initialRetryDelayMillis: 10, |
| 55 | + retryDelayMultiplier: 2, |
| 56 | + maxRetryDelayMillis: 60000, |
| 57 | +}; |
| 58 | + |
52 | 59 | /** |
53 | 60 | * @typedef {object} Policy |
54 | 61 | * @property {number} [version] Specifies the format of the policy. |
@@ -735,7 +742,8 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); |
735 | 742 | const rowsLimit = options.limit || 0; |
736 | 743 | const hasLimit = rowsLimit !== 0; |
737 | 744 | let rowsRead = 0; |
738 | | - let numRequestsMade = 0; |
| 745 | + let numConsecutiveErrors = 0; |
| 746 | + let retryTimer: NodeJS.Timeout | null; |
739 | 747 |
|
740 | 748 | rowKeys = options.keys || []; |
741 | 749 |
|
@@ -788,13 +796,20 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); |
788 | 796 | if (activeRequestStream) { |
789 | 797 | activeRequestStream.abort(); |
790 | 798 | } |
| 799 | + if (retryTimer) { |
| 800 | + clearTimeout(retryTimer); |
| 801 | + } |
791 | 802 | return end(); |
792 | 803 | }; |
793 | 804 |
|
794 | 805 | let chunkTransformer: ChunkTransformer; |
795 | 806 | let rowStream: Duplex; |
796 | 807 |
|
797 | 808 | const makeNewRequest = () => { |
| 809 | + // Avoid cancelling an expired timer if user |
| 810 | + // cancelled the stream in the middle of a retry |
| 811 | + retryTimer = null; |
| 812 | + |
798 | 813 | const lastRowKey = chunkTransformer ? chunkTransformer.lastRowKey : ''; |
799 | 814 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
800 | 815 | chunkTransformer = new ChunkTransformer({decode: options.decode} as any); |
@@ -805,7 +820,13 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); |
805 | 820 | } as google.bigtable.v2.IReadRowsRequest; |
806 | 821 |
|
807 | 822 | const retryOpts = { |
808 | | - currentRetryAttempt: numRequestsMade, |
| 823 | + currentRetryAttempt: numConsecutiveErrors, |
| 824 | + // Handling retries in this client. Specify the retry options to |
| 825 | + // make sure nothing is retried in retry-request. |
| 826 | + noResponseRetries: 0, |
| 827 | + shouldRetryFn: (_: any) => { |
| 828 | + return false; |
| 829 | + }, |
809 | 830 | }; |
810 | 831 |
|
811 | 832 | if (lastRowKey) { |
@@ -915,7 +936,6 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); |
915 | 936 | ) { |
916 | 937 | return next(); |
917 | 938 | } |
918 | | - numRequestsMade = 0; |
919 | 939 | rowsRead++; |
920 | 940 | const row = this.row(rowData.key); |
921 | 941 | row.data = rowData.data; |
@@ -936,20 +956,32 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); |
936 | 956 | userStream.end(); |
937 | 957 | return; |
938 | 958 | } |
| 959 | + numConsecutiveErrors++; |
939 | 960 | if ( |
940 | | - numRequestsMade <= maxRetries && |
| 961 | + numConsecutiveErrors <= maxRetries && |
941 | 962 | RETRYABLE_STATUS_CODES.has(error.code) |
942 | 963 | ) { |
943 | | - makeNewRequest(); |
| 964 | + const backOffSettings = |
| 965 | + options.gaxOptions?.retry?.backoffSettings || |
| 966 | + DEFAULT_BACKOFF_SETTINGS; |
| 967 | + const nextRetryDelay = getNextDelay( |
| 968 | + numConsecutiveErrors, |
| 969 | + backOffSettings |
| 970 | + ); |
| 971 | + retryTimer = setTimeout(makeNewRequest, nextRetryDelay); |
944 | 972 | } else { |
945 | 973 | userStream.emit('error', error); |
946 | 974 | } |
947 | 975 | }) |
| 976 | + .on('data', _ => { |
| 977 | + // Reset error count after a successful read so the backoff |
| 978 | + // time won't keep increasing when as stream had multiple errors |
| 979 | + numConsecutiveErrors = 0; |
| 980 | + }) |
948 | 981 | .on('end', () => { |
949 | 982 | activeRequestStream = null; |
950 | 983 | }); |
951 | 984 | rowStream.pipe(userStream); |
952 | | - numRequestsMade++; |
953 | 985 | }; |
954 | 986 |
|
955 | 987 | makeNewRequest(); |
@@ -1504,23 +1536,43 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); |
1504 | 1536 | ); |
1505 | 1537 | const mutationErrorsByEntryIndex = new Map(); |
1506 | 1538 |
|
1507 | | - const onBatchResponse = ( |
1508 | | - err: ServiceError | PartialFailureError | null |
1509 | | - ) => { |
1510 | | - // TODO: enable retries when the entire RPC fails |
1511 | | - if (err) { |
1512 | | - // The error happened before a request was even made, don't retry. |
| 1539 | + const isRetryable = (err: ServiceError | null) => { |
| 1540 | + // Don't retry if there are no more entries or retry attempts |
| 1541 | + if (pendingEntryIndices.size === 0 || numRequestsMade >= maxRetries + 1) { |
| 1542 | + return false; |
| 1543 | + } |
| 1544 | + // If the error is empty but there are still outstanding mutations, |
| 1545 | + // it means that there are retryable errors in the mutate response |
| 1546 | + // even when the RPC succeeded |
| 1547 | + return !err || IDEMPOTENT_RETRYABLE_STATUS_CODES.has(err.code); |
| 1548 | + }; |
| 1549 | + |
| 1550 | + const onBatchResponse = (err: ServiceError | null) => { |
| 1551 | + // Return if the error happened before a request was made |
| 1552 | + if (numRequestsMade === 0) { |
1513 | 1553 | callback(err); |
1514 | 1554 | return; |
1515 | 1555 | } |
1516 | | - if (pendingEntryIndices.size !== 0 && numRequestsMade <= maxRetries) { |
1517 | | - makeNextBatchRequest(); |
| 1556 | + |
| 1557 | + if (isRetryable(err)) { |
| 1558 | + const backOffSettings = |
| 1559 | + options.gaxOptions?.retry?.backoffSettings || |
| 1560 | + DEFAULT_BACKOFF_SETTINGS; |
| 1561 | + const nextDelay = getNextDelay(numRequestsMade, backOffSettings); |
| 1562 | + setTimeout(makeNextBatchRequest, nextDelay); |
1518 | 1563 | return; |
1519 | 1564 | } |
1520 | 1565 |
|
| 1566 | + // If there's no more pending mutations, set the error |
| 1567 | + // to null |
| 1568 | + if (pendingEntryIndices.size === 0) { |
| 1569 | + err = null; |
| 1570 | + } |
| 1571 | + |
1521 | 1572 | if (mutationErrorsByEntryIndex.size !== 0) { |
1522 | 1573 | const mutationErrors = Array.from(mutationErrorsByEntryIndex.values()); |
1523 | | - err = new PartialFailureError(mutationErrors); |
| 1574 | + callback(new PartialFailureError(mutationErrors, err)); |
| 1575 | + return; |
1524 | 1576 | } |
1525 | 1577 |
|
1526 | 1578 | callback(err); |
@@ -1541,6 +1593,12 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); |
1541 | 1593 |
|
1542 | 1594 | const retryOpts = { |
1543 | 1595 | currentRetryAttempt: numRequestsMade, |
| 1596 | + // Handling retries in this client. Specify the retry options to |
| 1597 | + // make sure nothing is retried in retry-request. |
| 1598 | + noResponseRetries: 0, |
| 1599 | + shouldRetryFn: (_: any) => { |
| 1600 | + return false; |
| 1601 | + }, |
1544 | 1602 | }; |
1545 | 1603 |
|
1546 | 1604 | this.bigtable |
@@ -1552,13 +1610,6 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); |
1552 | 1610 | retryOpts, |
1553 | 1611 | }) |
1554 | 1612 | .on('error', (err: ServiceError) => { |
1555 | | - // TODO: this check doesn't actually do anything, onBatchResponse |
1556 | | - // currently doesn't retry RPC errors, only entry failures |
1557 | | - if (numRequestsMade === 0) { |
1558 | | - callback(err); // Likely a "projectId not detected" error. |
1559 | | - return; |
1560 | | - } |
1561 | | - |
1562 | 1613 | onBatchResponse(err); |
1563 | 1614 | }) |
1564 | 1615 | .on('data', (obj: google.bigtable.v2.IMutateRowsResponse) => { |
@@ -1572,13 +1623,13 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); |
1572 | 1623 | mutationErrorsByEntryIndex.delete(originalEntriesIndex); |
1573 | 1624 | return; |
1574 | 1625 | } |
1575 | | - if (!RETRYABLE_STATUS_CODES.has(entry.status!.code!)) { |
| 1626 | + if (!IDEMPOTENT_RETRYABLE_STATUS_CODES.has(entry.status!.code!)) { |
1576 | 1627 | pendingEntryIndices.delete(originalEntriesIndex); |
1577 | 1628 | } |
1578 | | - const status = decorateStatus(entry.status); |
| 1629 | + const errorDetails = entry.status; |
1579 | 1630 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
1580 | | - (status as any).entry = originalEntry; |
1581 | | - mutationErrorsByEntryIndex.set(originalEntriesIndex, status); |
| 1631 | + (errorDetails as any).entry = originalEntry; |
| 1632 | + mutationErrorsByEntryIndex.set(originalEntriesIndex, errorDetails); |
1582 | 1633 | }); |
1583 | 1634 | }) |
1584 | 1635 | .on('end', onBatchResponse); |
@@ -1997,14 +2048,25 @@ promisifyAll(Table, { |
1997 | 2048 | exclude: ['family', 'row'], |
1998 | 2049 | }); |
1999 | 2050 |
|
| 2051 | +function getNextDelay(numConsecutiveErrors: number, config: BackoffSettings) { |
| 2052 | + // 0 - 100 ms jitter |
| 2053 | + const jitter = Math.floor(Math.random() * 100); |
| 2054 | + const calculatedNextRetryDelay = |
| 2055 | + config.initialRetryDelayMillis * |
| 2056 | + Math.pow(config.retryDelayMultiplier, numConsecutiveErrors) + |
| 2057 | + jitter; |
| 2058 | + |
| 2059 | + return Math.min(calculatedNextRetryDelay, config.maxRetryDelayMillis); |
| 2060 | +} |
| 2061 | + |
2000 | 2062 | export interface GoogleInnerError { |
2001 | 2063 | reason?: string; |
2002 | 2064 | message?: string; |
2003 | 2065 | } |
2004 | 2066 |
|
2005 | 2067 | export class PartialFailureError extends Error { |
2006 | 2068 | errors?: GoogleInnerError[]; |
2007 | | - constructor(errors: GoogleInnerError[]) { |
| 2069 | + constructor(errors: GoogleInnerError[], rpcError?: ServiceError | null) { |
2008 | 2070 | super(); |
2009 | 2071 | this.errors = errors; |
2010 | 2072 | this.name = 'PartialFailureError'; |
@@ -2017,5 +2079,8 @@ export class PartialFailureError extends Error { |
2017 | 2079 | messages.push('\n'); |
2018 | 2080 | } |
2019 | 2081 | this.message = messages.join('\n'); |
| 2082 | + if (rpcError) { |
| 2083 | + this.message += 'Request failed with: ' + rpcError.message; |
| 2084 | + } |
2020 | 2085 | } |
2021 | 2086 | } |