@google-cloud/spanner 7.18.0 → 7.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +1 -0
- package/build/protos/google/spanner/admin/database/v1/backup.proto +757 -0
- package/build/protos/google/spanner/admin/database/v1/backup_schedule.proto +230 -0
- package/build/protos/google/spanner/admin/database/v1/common.proto +132 -0
- package/build/protos/google/spanner/admin/database/v1/spanner_database_admin.proto +1209 -0
- package/build/protos/google/spanner/admin/instance/v1/common.proto +64 -0
- package/build/protos/google/spanner/admin/instance/v1/spanner_instance_admin.proto +2110 -0
- package/build/protos/google/spanner/executor/v1/cloud_executor.proto +1520 -0
- package/build/protos/google/spanner/v1/commit_response.proto +59 -0
- package/build/protos/google/spanner/v1/keys.proto +163 -0
- package/build/protos/google/spanner/v1/mutation.proto +107 -0
- package/build/protos/google/spanner/v1/query_plan.proto +128 -0
- package/build/protos/google/spanner/v1/result_set.proto +237 -0
- package/build/protos/google/spanner/v1/spanner.proto +1389 -0
- package/build/protos/google/spanner/v1/transaction.proto +575 -0
- package/build/protos/google/spanner/v1/type.proto +214 -0
- package/build/protos/protos.d.ts +39317 -0
- package/build/protos/protos.js +100360 -0
- package/build/protos/protos.json +10271 -0
- package/build/src/backup.d.ts +349 -0
- package/build/src/backup.js +246 -0
- package/build/src/batch-transaction.d.ts +247 -0
- package/build/src/batch-transaction.js +384 -0
- package/build/src/codec.d.ts +362 -0
- package/build/src/codec.js +791 -0
- package/build/src/common-grpc/service-object.d.ts +80 -0
- package/build/src/common-grpc/service-object.js +101 -0
- package/build/src/common-grpc/service.d.ts +445 -0
- package/build/src/common-grpc/service.js +826 -0
- package/build/src/common.d.ts +66 -0
- package/build/src/common.js +54 -0
- package/build/src/database.d.ts +2103 -0
- package/build/src/database.js +1655 -0
- package/build/src/extra_proto_list.json +1 -0
- package/build/src/helper.d.ts +45 -0
- package/build/src/helper.js +71 -0
- package/build/src/index.d.ts +1195 -0
- package/build/src/index.js +1059 -0
- package/build/src/instance-config.d.ts +294 -0
- package/build/src/instance-config.js +216 -0
- package/build/src/instance.d.ts +832 -0
- package/build/src/instance.js +781 -0
- package/build/src/instrument.d.ts +85 -0
- package/build/src/instrument.js +215 -0
- package/build/src/multiplexed-session.d.ts +136 -0
- package/build/src/multiplexed-session.js +197 -0
- package/build/src/partial-result-stream.d.ts +253 -0
- package/build/src/partial-result-stream.js +400 -0
- package/build/src/session-factory.d.ts +100 -0
- package/build/src/session-factory.js +93 -0
- package/build/src/session-pool.d.ts +471 -0
- package/build/src/session-pool.js +754 -0
- package/build/src/session.d.ts +258 -0
- package/build/src/session.js +348 -0
- package/build/src/spanner_grpc_config.json +92 -0
- package/build/src/table.d.ts +831 -0
- package/build/src/table.js +277 -0
- package/build/src/transaction-runner.d.ts +185 -0
- package/build/src/transaction-runner.js +282 -0
- package/build/src/transaction.d.ts +1691 -0
- package/build/src/transaction.js +2004 -0
- package/build/src/v1/database_admin_client.d.ts +2301 -0
- package/build/src/v1/database_admin_client.js +2286 -0
- package/build/src/v1/database_admin_client_config.json +160 -0
- package/build/src/v1/database_admin_proto_list.json +6 -0
- package/build/src/v1/gapic_metadata.json +27 -0
- package/build/src/v1/index.d.ts +4 -0
- package/build/src/v1/index.js +29 -0
- package/build/src/v1/instance_admin_client.d.ts +1985 -0
- package/build/src/v1/instance_admin_client.js +1768 -0
- package/build/src/v1/instance_admin_client_config.json +129 -0
- package/build/src/v1/instance_admin_proto_list.json +4 -0
- package/build/src/v1/spanner_client.d.ts +1250 -0
- package/build/src/v1/spanner_client.js +1086 -0
- package/build/src/v1/spanner_client_config.json +119 -0
- package/build/src/v1/spanner_executor_proxy_client.d.ts +418 -0
- package/build/src/v1/spanner_executor_proxy_client.js +706 -0
- package/build/src/v1/spanner_executor_proxy_client_config.json +30 -0
- package/build/src/v1/spanner_executor_proxy_proto_list.json +14 -0
- package/build/src/v1/spanner_proto_list.json +10 -0
- package/package.json +1 -1
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*!
|
|
3
|
+
* Copyright 2019 Google Inc. All Rights Reserved.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.AsyncTransactionRunner = exports.TransactionRunner = exports.Runner = exports.DeadlineError = void 0;
|
|
19
|
+
exports.isRetryableInternalError = isRetryableInternalError;
|
|
20
|
+
const promisify_1 = require("@google-cloud/promisify");
|
|
21
|
+
const google_gax_1 = require("google-gax");
|
|
22
|
+
const protobufjs_1 = require("protobufjs");
|
|
23
|
+
const through = require("through2");
|
|
24
|
+
const session_pool_1 = require("./session-pool");
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
26
|
+
const jsonProtos = require('../protos/protos.json');
|
|
27
|
+
const RETRY_INFO = 'google.rpc.retryinfo-bin';
|
|
28
|
+
const RETRYABLE = [google_gax_1.grpc.status.ABORTED];
|
|
29
|
+
// tslint:disable-next-line variable-name
|
|
30
|
+
const RetryInfo = protobufjs_1.Root.fromJSON(jsonProtos).lookup('google.rpc.RetryInfo');
|
|
31
|
+
/**
|
|
32
|
+
* Error class used to signal a Transaction timeout.
|
|
33
|
+
*
|
|
34
|
+
* @private
|
|
35
|
+
* @class
|
|
36
|
+
*
|
|
37
|
+
* @param {Error} [err] The last known retryable Error.
|
|
38
|
+
*/
|
|
39
|
+
class DeadlineError extends Error {
|
|
40
|
+
constructor(error) {
|
|
41
|
+
super('Deadline for Transaction exceeded.');
|
|
42
|
+
this.code = google_gax_1.grpc.status.DEADLINE_EXCEEDED;
|
|
43
|
+
this.details = (error === null || error === void 0 ? void 0 : error.details) || '';
|
|
44
|
+
this.metadata = (error === null || error === void 0 ? void 0 : error.metadata) || new google_gax_1.grpc.Metadata();
|
|
45
|
+
this.errors = [];
|
|
46
|
+
if (error) {
|
|
47
|
+
this.errors.push(error);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.DeadlineError = DeadlineError;
|
|
52
|
+
/**
|
|
53
|
+
* Base class for running/retrying Transactions.
|
|
54
|
+
*
|
|
55
|
+
* @private
|
|
56
|
+
* @class
|
|
57
|
+
* @abstract
|
|
58
|
+
*
|
|
59
|
+
* @param {Database} database The Database to pull Sessions/Transactions from.
|
|
60
|
+
* @param {RunTransactionOptions} [options] The runner options.
|
|
61
|
+
*/
|
|
62
|
+
class Runner {
|
|
63
|
+
constructor(session, transaction, options) {
|
|
64
|
+
this.attempts = 0;
|
|
65
|
+
this.session = session;
|
|
66
|
+
this.transaction = transaction;
|
|
67
|
+
this.transaction.useInRunner();
|
|
68
|
+
const defaults = { timeout: 3600000 };
|
|
69
|
+
this.options = Object.assign(defaults, options);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Attempts to retrieve the retry delay from the supplied error. If absent it
|
|
73
|
+
* will create one based on the number of attempts made thus far.
|
|
74
|
+
*
|
|
75
|
+
* @private
|
|
76
|
+
*
|
|
77
|
+
* @param {Error} err The service error.
|
|
78
|
+
* @returns {number} Delay in milliseconds.
|
|
79
|
+
*/
|
|
80
|
+
getNextDelay(err) {
|
|
81
|
+
const retryInfo = err.metadata && err.metadata.get(RETRY_INFO);
|
|
82
|
+
if (retryInfo && retryInfo.length) {
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
84
|
+
const { retryDelay } = RetryInfo.decode(retryInfo[0]);
|
|
85
|
+
let { seconds } = retryDelay;
|
|
86
|
+
if (typeof seconds !== 'number') {
|
|
87
|
+
seconds = seconds.toNumber();
|
|
88
|
+
}
|
|
89
|
+
const secondsInMs = Math.floor(seconds) * 1000;
|
|
90
|
+
const nanosInMs = Math.floor(retryDelay.nanos) / 1e6;
|
|
91
|
+
return secondsInMs + nanosInMs;
|
|
92
|
+
}
|
|
93
|
+
// A 'Session not found' error without any specific retry info should not
|
|
94
|
+
// cause any delay between retries.
|
|
95
|
+
if ((0, session_pool_1.isSessionNotFoundError)(err)) {
|
|
96
|
+
return 0;
|
|
97
|
+
}
|
|
98
|
+
// Max backoff should be 32 seconds.
|
|
99
|
+
return (Math.pow(2, Math.min(this.attempts, 5)) * 1000 +
|
|
100
|
+
Math.floor(Math.random() * 1000));
|
|
101
|
+
}
|
|
102
|
+
/** Returns whether the given error should cause a transaction retry. */
|
|
103
|
+
shouldRetry(err) {
|
|
104
|
+
return (RETRYABLE.includes(err.code) ||
|
|
105
|
+
(0, session_pool_1.isSessionNotFoundError)(err) ||
|
|
106
|
+
isRetryableInternalError(err));
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Retrieves a transaction to run against.
|
|
110
|
+
*
|
|
111
|
+
* @private
|
|
112
|
+
*
|
|
113
|
+
* @returns Promise<Transaction>
|
|
114
|
+
*/
|
|
115
|
+
async getTransaction() {
|
|
116
|
+
if (this.transaction) {
|
|
117
|
+
const transaction = this.transaction;
|
|
118
|
+
delete this.transaction;
|
|
119
|
+
return transaction;
|
|
120
|
+
}
|
|
121
|
+
const transaction = this.session.transaction(this.session.parent.queryOptions_);
|
|
122
|
+
if (this.options.optimisticLock) {
|
|
123
|
+
transaction.useOptimisticLock();
|
|
124
|
+
}
|
|
125
|
+
if (this.options.excludeTxnFromChangeStreams) {
|
|
126
|
+
transaction.excludeTxnFromChangeStreams();
|
|
127
|
+
}
|
|
128
|
+
if (this.attempts > 0) {
|
|
129
|
+
await transaction.begin();
|
|
130
|
+
}
|
|
131
|
+
return transaction;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* This function is responsible for getting transactions, running them and
|
|
135
|
+
* handling any errors, retrying if necessary.
|
|
136
|
+
*
|
|
137
|
+
* @private
|
|
138
|
+
*
|
|
139
|
+
* @returns {Promise}
|
|
140
|
+
*/
|
|
141
|
+
async run() {
|
|
142
|
+
const start = Date.now();
|
|
143
|
+
const timeout = this.options.timeout;
|
|
144
|
+
let lastError;
|
|
145
|
+
// The transaction runner should always execute at least one attempt before
|
|
146
|
+
// timing out.
|
|
147
|
+
while (this.attempts === 0 || Date.now() - start < timeout) {
|
|
148
|
+
const transaction = await this.getTransaction();
|
|
149
|
+
try {
|
|
150
|
+
return await this._run(transaction);
|
|
151
|
+
}
|
|
152
|
+
catch (e) {
|
|
153
|
+
this.session.lastError = e;
|
|
154
|
+
lastError = e;
|
|
155
|
+
}
|
|
156
|
+
// Note that if the error is a 'Session not found' error, it will be
|
|
157
|
+
// thrown here. We do this to bubble this error up to the caller who is
|
|
158
|
+
// responsible for retrying the transaction on a different session.
|
|
159
|
+
if (!RETRYABLE.includes(lastError.code) &&
|
|
160
|
+
!isRetryableInternalError(lastError)) {
|
|
161
|
+
throw lastError;
|
|
162
|
+
}
|
|
163
|
+
this.attempts += 1;
|
|
164
|
+
const delay = this.getNextDelay(lastError);
|
|
165
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
166
|
+
}
|
|
167
|
+
throw new DeadlineError(lastError);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
exports.Runner = Runner;
|
|
171
|
+
/**
|
|
172
|
+
* This class handles transactions expecting to be ran in callback mode.
|
|
173
|
+
*
|
|
174
|
+
* @private
|
|
175
|
+
* @class
|
|
176
|
+
*
|
|
177
|
+
* @param {Database} database The database to pull sessions/transactions from.
|
|
178
|
+
* @param {RunTransactionCallback} runFn The user supplied run function.
|
|
179
|
+
* @param {RunTransactionOptions} [options] Runner options.
|
|
180
|
+
*/
|
|
181
|
+
class TransactionRunner extends Runner {
|
|
182
|
+
constructor(session, transaction, runFn, options) {
|
|
183
|
+
super(session, transaction, options);
|
|
184
|
+
this.runFn = runFn;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Because the user has decided to use callback mode, we want to try and
|
|
188
|
+
* intercept any ABORTED or UNKNOWN errors and stop the current function
|
|
189
|
+
* execution.
|
|
190
|
+
*
|
|
191
|
+
* @private
|
|
192
|
+
*
|
|
193
|
+
* @param {Transaction} transaction The transaction to intercept errors for.
|
|
194
|
+
* @param {Function} reject Function to call when a retryable error is found.
|
|
195
|
+
*/
|
|
196
|
+
_interceptErrors(transaction, reject) {
|
|
197
|
+
const request = transaction.request;
|
|
198
|
+
transaction.request = (0, promisify_1.promisify)((config, callback) => {
|
|
199
|
+
request(config, (err, resp) => {
|
|
200
|
+
if (!err || !this.shouldRetry(err)) {
|
|
201
|
+
callback(err, resp);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
reject(err);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
const requestStream = transaction.requestStream;
|
|
208
|
+
transaction.requestStream = (config) => {
|
|
209
|
+
const proxyStream = through.obj();
|
|
210
|
+
const stream = requestStream(config);
|
|
211
|
+
stream
|
|
212
|
+
.on('error', (err) => {
|
|
213
|
+
if (!this.shouldRetry(err)) {
|
|
214
|
+
proxyStream.destroy(err);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
stream.unpipe(proxyStream);
|
|
218
|
+
reject(err);
|
|
219
|
+
})
|
|
220
|
+
.pipe(proxyStream);
|
|
221
|
+
return proxyStream;
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Creates a Promise that should resolve when the provided transaction has
|
|
226
|
+
* been committed or rolled back. Rejects if a retryable error occurs.
|
|
227
|
+
*
|
|
228
|
+
* @private
|
|
229
|
+
*
|
|
230
|
+
* @param {Transaction}
|
|
231
|
+
* @returns {Promise}
|
|
232
|
+
*/
|
|
233
|
+
_run(transaction) {
|
|
234
|
+
return new Promise((resolve, reject) => {
|
|
235
|
+
transaction.once('end', resolve);
|
|
236
|
+
this._interceptErrors(transaction, reject);
|
|
237
|
+
this.runFn(null, transaction);
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
exports.TransactionRunner = TransactionRunner;
|
|
242
|
+
/**
|
|
243
|
+
* This class handles transactions expecting to be ran in promise mode.
|
|
244
|
+
*
|
|
245
|
+
* @private
|
|
246
|
+
* @class
|
|
247
|
+
*
|
|
248
|
+
* @param {Database} database The database to pull sessions/transactions from.
|
|
249
|
+
* @param {AsyncRunTransactionCallback} runFn The user supplied run function.
|
|
250
|
+
* @param {RunTransactionOptions} [options] Runner options.
|
|
251
|
+
*/
|
|
252
|
+
class AsyncTransactionRunner extends Runner {
|
|
253
|
+
constructor(session, transaction, runFn, options) {
|
|
254
|
+
super(session, transaction, options);
|
|
255
|
+
this.runFn = runFn;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Since this is promise mode all we need to do is return the user function.
|
|
259
|
+
*
|
|
260
|
+
* @private
|
|
261
|
+
*
|
|
262
|
+
* @param {Transaction} transaction The transaction to be ran against.
|
|
263
|
+
* @returns {Promise}
|
|
264
|
+
*/
|
|
265
|
+
_run(transaction) {
|
|
266
|
+
return this.runFn(transaction);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
exports.AsyncTransactionRunner = AsyncTransactionRunner;
|
|
270
|
+
/**
|
|
271
|
+
* Checks whether the given error is a retryable internal error.
|
|
272
|
+
* @param error the error to check
|
|
273
|
+
* @return true if the error is a retryable internal error, and otherwise false.
|
|
274
|
+
*/
|
|
275
|
+
function isRetryableInternalError(err) {
|
|
276
|
+
return (err.code === google_gax_1.grpc.status.INTERNAL &&
|
|
277
|
+
(err.message.includes('Received unexpected EOS on DATA frame from server') ||
|
|
278
|
+
err.message.includes('RST_STREAM') ||
|
|
279
|
+
err.message.includes('HTTP/2 error code: INTERNAL_ERROR') ||
|
|
280
|
+
err.message.includes('Connection closed with unknown cause')));
|
|
281
|
+
}
|
|
282
|
+
//# sourceMappingURL=transaction-runner.js.map
|