@google-cloud/spanner 8.11.0 → 8.13.0
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/build/src/common-grpc/service.js +9 -10
- package/build/src/database.d.ts +33 -0
- package/build/src/database.js +161 -41
- package/build/src/instance-config.d.ts +1 -2
- package/build/src/instance-config.js +1 -1
- package/build/src/instance.d.ts +1 -2
- package/build/src/instrument.js +9 -5
- package/build/src/metrics/interceptor.js +4 -1
- package/build/src/metrics/spanner-metrics-exporter.js +3 -1
- package/build/src/multiplexed-session.d.ts +3 -0
- package/build/src/multiplexed-session.js +28 -2
- package/build/src/partial-result-stream.d.ts +43 -0
- package/build/src/partial-result-stream.js +303 -85
- package/build/src/session-factory.d.ts +4 -2
- package/build/src/session-factory.js +14 -3
- package/build/src/transaction.d.ts +64 -8
- package/build/src/transaction.js +429 -18
- package/package.json +5 -10
|
@@ -16,11 +16,13 @@
|
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.PartialResultStream = void 0;
|
|
19
|
+
exports.createFieldDecoders = createFieldDecoders;
|
|
20
|
+
exports.createJsonRow = createJsonRow;
|
|
21
|
+
exports.createRow = createRow;
|
|
22
|
+
exports.formatRow = formatRow;
|
|
23
|
+
exports.decodeRowsDirect = decodeRowsDirect;
|
|
19
24
|
exports.partialResultStream = partialResultStream;
|
|
20
25
|
const service_1 = require("./common-grpc/service");
|
|
21
|
-
const checkpointStream = require("checkpoint-stream");
|
|
22
|
-
const eventsIntercept = require("events-intercept");
|
|
23
|
-
const mergeStream = require("merge-stream");
|
|
24
26
|
const stream_1 = require("stream");
|
|
25
27
|
const streamEvents = require("stream-events");
|
|
26
28
|
const google_gax_1 = require("google-gax");
|
|
@@ -28,10 +30,135 @@ const transaction_runner_1 = require("./transaction-runner");
|
|
|
28
30
|
const codec_1 = require("./codec");
|
|
29
31
|
const spanner_api_1 = require("@google-cloud/spanner-api");
|
|
30
32
|
var google = spanner_api_1.protos.google;
|
|
31
|
-
const stream = require("stream");
|
|
32
33
|
const helper_1 = require("./helper");
|
|
33
34
|
const originalDecode = codec_1.codec.decode;
|
|
34
35
|
const originalConvertFieldsToJson = codec_1.codec.convertFieldsToJson;
|
|
36
|
+
/**
|
|
37
|
+
* Row implementation extending Array to provide a shared, non-enumerable
|
|
38
|
+
* toJSON method without per-row closures or Object.setPrototypeOf overhead.
|
|
39
|
+
*/
|
|
40
|
+
class RowImpl extends Array {
|
|
41
|
+
toJSON(options) {
|
|
42
|
+
return codec_1.codec.convertFieldsToJson(this, options);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
Object.defineProperty(RowImpl.prototype, 'constructor', {
|
|
46
|
+
value: Array,
|
|
47
|
+
writable: true,
|
|
48
|
+
configurable: true,
|
|
49
|
+
enumerable: false,
|
|
50
|
+
});
|
|
51
|
+
/**
|
|
52
|
+
* Creates an array of decoder functions for the specified struct fields.
|
|
53
|
+
*
|
|
54
|
+
* @private
|
|
55
|
+
*/
|
|
56
|
+
function createFieldDecoders(fields, options) {
|
|
57
|
+
const jsonMode = Boolean(options?.json);
|
|
58
|
+
const jsonOptions = options?.jsonOptions;
|
|
59
|
+
const columnsMetadata = options?.columnsMetadata;
|
|
60
|
+
return fields.map(({ name, type }) => {
|
|
61
|
+
const columnMetadata = columnsMetadata &&
|
|
62
|
+
name !== null &&
|
|
63
|
+
name !== undefined &&
|
|
64
|
+
Object.prototype.hasOwnProperty.call(columnsMetadata, name)
|
|
65
|
+
? columnsMetadata[name]
|
|
66
|
+
: undefined;
|
|
67
|
+
if (codec_1.codec.decode !== originalDecode) {
|
|
68
|
+
return (val) => codec_1.codec.decode(val, type, columnMetadata);
|
|
69
|
+
}
|
|
70
|
+
return codec_1.codec.getDecoder(type, columnMetadata, jsonMode ? jsonOptions || {} : undefined);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Directly creates a plain JSON object from row cell values, bypassing
|
|
75
|
+
* Struct, Row, and WrappedNumber class wrappers when possible.
|
|
76
|
+
*
|
|
77
|
+
* @private
|
|
78
|
+
*/
|
|
79
|
+
function createJsonRow(fields, decoders, values, includeNameless) {
|
|
80
|
+
const json = {};
|
|
81
|
+
const len = fields.length;
|
|
82
|
+
for (let i = 0; i < len; i++) {
|
|
83
|
+
const { name } = fields[i];
|
|
84
|
+
if (!name && !includeNameless) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const fieldName = name || `_${i}`;
|
|
88
|
+
try {
|
|
89
|
+
json[fieldName] = decoders[i](values[i]);
|
|
90
|
+
}
|
|
91
|
+
catch (e) {
|
|
92
|
+
e.message = [
|
|
93
|
+
`Serializing column "${fieldName}" encountered an error: ${e.message}`,
|
|
94
|
+
'Call row.toJSON({ wrapNumbers: true }) to receive a custom type.',
|
|
95
|
+
].join(' ');
|
|
96
|
+
throw e;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return json;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Converts an array of decoded cell values into a Row.
|
|
103
|
+
*
|
|
104
|
+
* @private
|
|
105
|
+
*/
|
|
106
|
+
function createRow(fields, decoders, values) {
|
|
107
|
+
const len = fields.length;
|
|
108
|
+
const row = new RowImpl(len);
|
|
109
|
+
for (let i = 0; i < len; i++) {
|
|
110
|
+
row[i] = {
|
|
111
|
+
name: fields[i].name,
|
|
112
|
+
value: decoders[i](values[i]),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
return row;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Formats raw row values into either a plain JSON object or a Row instance
|
|
119
|
+
* according to the provided RowOptions.
|
|
120
|
+
*
|
|
121
|
+
* @private
|
|
122
|
+
*/
|
|
123
|
+
function formatRow(fields, decoders, values, options) {
|
|
124
|
+
const jsonMode = Boolean(options?.json);
|
|
125
|
+
const jsonOptions = options?.jsonOptions;
|
|
126
|
+
const isJsonStubbed = codec_1.codec.convertFieldsToJson !== originalConvertFieldsToJson;
|
|
127
|
+
if (jsonMode && !isJsonStubbed) {
|
|
128
|
+
return createJsonRow(fields, decoders, values, Boolean(jsonOptions?.includeNameless));
|
|
129
|
+
}
|
|
130
|
+
const row = createRow(fields, decoders, values);
|
|
131
|
+
return jsonMode ? row.toJSON(jsonOptions) : row;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Directly decodes rows from a PartialResultSet without going through the stream
|
|
135
|
+
* pipeline. Used by the fast-path for queries returning small results in a single chunk.
|
|
136
|
+
*
|
|
137
|
+
* @private
|
|
138
|
+
*/
|
|
139
|
+
function decodeRowsDirect(chunk, options, existingFields, existingDecoders) {
|
|
140
|
+
const fields = existingFields ||
|
|
141
|
+
(chunk.metadata?.rowType?.fields ||
|
|
142
|
+
[]);
|
|
143
|
+
const numFields = fields.length;
|
|
144
|
+
const chunkValues = chunk.values || [];
|
|
145
|
+
const numValues = chunkValues.length;
|
|
146
|
+
if (numFields === 0 || numValues === 0) {
|
|
147
|
+
return [];
|
|
148
|
+
}
|
|
149
|
+
const rowCount = Math.floor(numValues / numFields);
|
|
150
|
+
const rows = new Array(rowCount);
|
|
151
|
+
const decoders = existingDecoders || createFieldDecoders(fields, options);
|
|
152
|
+
const rowValues = new Array(numFields);
|
|
153
|
+
for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
|
|
154
|
+
const offset = rowIndex * numFields;
|
|
155
|
+
for (let columnIndex = 0; columnIndex < numFields; columnIndex++) {
|
|
156
|
+
rowValues[columnIndex] = service_1.GrpcService.decodeValue_(chunkValues[offset + columnIndex]);
|
|
157
|
+
}
|
|
158
|
+
rows[rowIndex] = formatRow(fields, decoders, rowValues, options);
|
|
159
|
+
}
|
|
160
|
+
return rows;
|
|
161
|
+
}
|
|
35
162
|
/**
|
|
36
163
|
* The PartialResultStream transforms partial result set objects into Row
|
|
37
164
|
* objects.
|
|
@@ -50,11 +177,13 @@ class PartialResultStream extends stream_1.Transform {
|
|
|
50
177
|
_pendingValueForResume;
|
|
51
178
|
_values;
|
|
52
179
|
_numPushFailed = 0;
|
|
180
|
+
_isFirstChunk = true;
|
|
53
181
|
constructor(options = {}) {
|
|
54
182
|
super({ objectMode: true });
|
|
55
183
|
this._destroyed = false;
|
|
56
184
|
this._options = Object.assign({ maxResumeRetries: 20 }, options);
|
|
57
185
|
this._values = [];
|
|
186
|
+
this._isFirstChunk = true;
|
|
58
187
|
}
|
|
59
188
|
/**
|
|
60
189
|
* Destroys the stream.
|
|
@@ -91,18 +220,7 @@ class PartialResultStream extends stream_1.Transform {
|
|
|
91
220
|
if (!this._fields && chunk.metadata) {
|
|
92
221
|
this._fields = chunk.metadata.rowType
|
|
93
222
|
.fields;
|
|
94
|
-
this._decoders = this._fields
|
|
95
|
-
const columnMetadata = this._options.columnsMetadata &&
|
|
96
|
-
name !== null &&
|
|
97
|
-
name !== undefined &&
|
|
98
|
-
Object.prototype.hasOwnProperty.call(this._options.columnsMetadata, name)
|
|
99
|
-
? this._options.columnsMetadata[name]
|
|
100
|
-
: undefined;
|
|
101
|
-
if (codec_1.codec.decode !== originalDecode) {
|
|
102
|
-
return val => codec_1.codec.decode(val, type, columnMetadata);
|
|
103
|
-
}
|
|
104
|
-
return codec_1.codec.getDecoder(type, columnMetadata, this._options.json ? this._options.jsonOptions || {} : undefined);
|
|
105
|
-
});
|
|
223
|
+
this._decoders = createFieldDecoders(this._fields, this._options);
|
|
106
224
|
}
|
|
107
225
|
let res = true;
|
|
108
226
|
if (!(0, helper_1.isEmpty)(chunk.values)) {
|
|
@@ -116,6 +234,11 @@ class PartialResultStream extends stream_1.Transform {
|
|
|
116
234
|
}
|
|
117
235
|
if (chunk.last) {
|
|
118
236
|
this.push(null);
|
|
237
|
+
// Calling next() notifies Node's stream machinery that processing of this
|
|
238
|
+
// chunk is complete on the Writable side of this Transform stream.
|
|
239
|
+
// This is a local, synchronous callback to Node's internal buffer; it does
|
|
240
|
+
// not block the event loop or wait for upstream network I/O or gRPC trailers.
|
|
241
|
+
next();
|
|
119
242
|
return;
|
|
120
243
|
}
|
|
121
244
|
if (res) {
|
|
@@ -171,6 +294,12 @@ class PartialResultStream extends stream_1.Transform {
|
|
|
171
294
|
* @param {object} chunk The partial result set.
|
|
172
295
|
*/
|
|
173
296
|
_addChunk(chunk) {
|
|
297
|
+
const isFirstChunk = this._isFirstChunk;
|
|
298
|
+
this._isFirstChunk = false;
|
|
299
|
+
// Fast path for single-chunk stream responses:
|
|
300
|
+
if (isFirstChunk && chunk.last && !chunk.chunkedValue) {
|
|
301
|
+
return this._addSingleChunk(chunk);
|
|
302
|
+
}
|
|
174
303
|
const chunkValues = chunk.values;
|
|
175
304
|
const numValues = chunkValues.length;
|
|
176
305
|
const values = new Array(numValues);
|
|
@@ -206,6 +335,26 @@ class PartialResultStream extends stream_1.Transform {
|
|
|
206
335
|
}
|
|
207
336
|
return res;
|
|
208
337
|
}
|
|
338
|
+
/**
|
|
339
|
+
* Fast-path handler for single-chunk stream responses. Decodes rows directly
|
|
340
|
+
* and pushes them into the stream without incremental chunk buffering.
|
|
341
|
+
*
|
|
342
|
+
* @private
|
|
343
|
+
* @param {google.spanner.v1.PartialResultSet} chunk The partial result set.
|
|
344
|
+
* @returns {boolean} Whether the stream can accept more data.
|
|
345
|
+
*/
|
|
346
|
+
_addSingleChunk(chunk) {
|
|
347
|
+
const rows = decodeRowsDirect(chunk, this._options, this._fields, this._decoders);
|
|
348
|
+
let canAcceptMore = true;
|
|
349
|
+
for (let i = 0; i < rows.length; i++) {
|
|
350
|
+
const accepted = this.push(rows[i]);
|
|
351
|
+
if (!accepted && canAcceptMore) {
|
|
352
|
+
canAcceptMore = false;
|
|
353
|
+
this.emit('paused');
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return canAcceptMore;
|
|
357
|
+
}
|
|
209
358
|
/**
|
|
210
359
|
* Manages complete values, pushing a completed row into the stream once all
|
|
211
360
|
* values have been received.
|
|
@@ -221,15 +370,7 @@ class PartialResultStream extends stream_1.Transform {
|
|
|
221
370
|
return true;
|
|
222
371
|
}
|
|
223
372
|
this._values = [];
|
|
224
|
-
|
|
225
|
-
if (this._options.json && !isJsonStubbed) {
|
|
226
|
-
return this.push(this._createJsonRow(values));
|
|
227
|
-
}
|
|
228
|
-
const row = this._createRow(values);
|
|
229
|
-
if (this._options.json) {
|
|
230
|
-
return this.push(row.toJSON(this._options.jsonOptions));
|
|
231
|
-
}
|
|
232
|
-
return this.push(row);
|
|
373
|
+
return this.push(formatRow(this._fields, this._decoders, values, this._options));
|
|
233
374
|
}
|
|
234
375
|
/**
|
|
235
376
|
* Directly creates a plain JSON object from row cell values, bypassing
|
|
@@ -241,29 +382,7 @@ class PartialResultStream extends stream_1.Transform {
|
|
|
241
382
|
* @returns {Json} The plain JavaScript object representing the row.
|
|
242
383
|
*/
|
|
243
384
|
_createJsonRow(values) {
|
|
244
|
-
|
|
245
|
-
const fields = this._fields;
|
|
246
|
-
const decoders = this._decoders;
|
|
247
|
-
const len = fields.length;
|
|
248
|
-
const includeNameless = !!this._options.jsonOptions?.includeNameless;
|
|
249
|
-
for (let i = 0; i < len; i++) {
|
|
250
|
-
const { name } = fields[i];
|
|
251
|
-
if (!name && !includeNameless) {
|
|
252
|
-
continue;
|
|
253
|
-
}
|
|
254
|
-
const fieldName = name ? name : `_${i}`;
|
|
255
|
-
try {
|
|
256
|
-
json[fieldName] = decoders[i](values[i]);
|
|
257
|
-
}
|
|
258
|
-
catch (e) {
|
|
259
|
-
e.message = [
|
|
260
|
-
`Serializing column "${fieldName}" encountered an error: ${e.message}`,
|
|
261
|
-
'Call row.toJSON({ wrapNumbers: true }) to receive a custom type.',
|
|
262
|
-
].join(' ');
|
|
263
|
-
throw e;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
return json;
|
|
385
|
+
return createJsonRow(this._fields, this._decoders, values, Boolean(this._options.jsonOptions?.includeNameless));
|
|
267
386
|
}
|
|
268
387
|
/**
|
|
269
388
|
* Converts an array of values into a row.
|
|
@@ -274,22 +393,7 @@ class PartialResultStream extends stream_1.Transform {
|
|
|
274
393
|
* @returns {Row}
|
|
275
394
|
*/
|
|
276
395
|
_createRow(values) {
|
|
277
|
-
|
|
278
|
-
const fields = new Array(len);
|
|
279
|
-
const decoders = this._decoders;
|
|
280
|
-
const classFields = this._fields;
|
|
281
|
-
for (let i = 0; i < len; i++) {
|
|
282
|
-
fields[i] = {
|
|
283
|
-
name: classFields[i].name,
|
|
284
|
-
value: decoders[i](values[i]),
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
Object.defineProperty(fields, 'toJSON', {
|
|
288
|
-
value: (options) => {
|
|
289
|
-
return codec_1.codec.convertFieldsToJson(fields, options);
|
|
290
|
-
},
|
|
291
|
-
});
|
|
292
|
-
return fields;
|
|
396
|
+
return createRow(this._fields, this._decoders, values);
|
|
293
397
|
}
|
|
294
398
|
/**
|
|
295
399
|
* Attempts to merge chunked values together.
|
|
@@ -344,6 +448,83 @@ class PartialResultStream extends stream_1.Transform {
|
|
|
344
448
|
}
|
|
345
449
|
}
|
|
346
450
|
exports.PartialResultStream = PartialResultStream;
|
|
451
|
+
/**
|
|
452
|
+
* A custom Transform stream that buffers PartialResultSet chunks and flushes them
|
|
453
|
+
* asynchronously to prevent blocking the event loop.
|
|
454
|
+
*
|
|
455
|
+
* It holds chunks in a queue until a "checkpoint" is reached (as determined by
|
|
456
|
+
* `isCheckpointFn`) or until the queue exceeds `maxQueued` items.
|
|
457
|
+
*
|
|
458
|
+
* @private
|
|
459
|
+
*/
|
|
460
|
+
class CheckpointStream extends stream_1.Transform {
|
|
461
|
+
queue = [];
|
|
462
|
+
maxQueued;
|
|
463
|
+
isCheckpointFn;
|
|
464
|
+
constructor(options) {
|
|
465
|
+
super({ objectMode: true });
|
|
466
|
+
this.maxQueued = options.maxQueued ?? 10;
|
|
467
|
+
this.isCheckpointFn = options.isCheckpointFn;
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Buffers chunks and flushes queue synchronously on checkpoints or when max limit is reached.
|
|
471
|
+
*
|
|
472
|
+
* @param {google.spanner.v1.PartialResultSet} chunk The chunk to transform.
|
|
473
|
+
* @param {string} enc Encoding (unused).
|
|
474
|
+
* @param {Function} callback Callback to signal completion of transformation.
|
|
475
|
+
*/
|
|
476
|
+
_transform(chunk, enc, callback) {
|
|
477
|
+
this.queue.push(chunk);
|
|
478
|
+
const isCheckpoint = this.isCheckpointFn(chunk);
|
|
479
|
+
let shouldFlush = false;
|
|
480
|
+
if (isCheckpoint) {
|
|
481
|
+
this.emit('checkpoint', chunk);
|
|
482
|
+
shouldFlush = true;
|
|
483
|
+
}
|
|
484
|
+
else if (this.queue.length > this.maxQueued) {
|
|
485
|
+
shouldFlush = true;
|
|
486
|
+
}
|
|
487
|
+
if (!shouldFlush) {
|
|
488
|
+
return callback();
|
|
489
|
+
}
|
|
490
|
+
this._flushQueue();
|
|
491
|
+
callback();
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Flushes queued chunks synchronously to prevent state races on retry/reset.
|
|
495
|
+
*
|
|
496
|
+
* @private
|
|
497
|
+
*/
|
|
498
|
+
_flushQueue() {
|
|
499
|
+
while (this.queue.length > 0 && !this.destroyed) {
|
|
500
|
+
this.push(this.queue.shift());
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Flushes remaining queued chunks before destroying the stream with the provided error.
|
|
505
|
+
*
|
|
506
|
+
* @param {Error} err The error to destroy the stream with.
|
|
507
|
+
*/
|
|
508
|
+
flushAndDestroy(err) {
|
|
509
|
+
this._flushQueue();
|
|
510
|
+
this.destroy(err);
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Clears the queue without flushing, useful when retrying and discarding partial data.
|
|
514
|
+
*/
|
|
515
|
+
reset() {
|
|
516
|
+
this.queue = [];
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Flushes all remaining queued chunks when the stream ends.
|
|
520
|
+
*
|
|
521
|
+
* @param {Function} callback Callback to call when flushing is complete.
|
|
522
|
+
*/
|
|
523
|
+
_flush(callback) {
|
|
524
|
+
this._flushQueue();
|
|
525
|
+
callback();
|
|
526
|
+
}
|
|
527
|
+
}
|
|
347
528
|
/**
|
|
348
529
|
* Rows returned from queries may be chunked, requiring them to be stitched
|
|
349
530
|
* together. This function returns a stream that will properly assemble these
|
|
@@ -365,26 +546,33 @@ function partialResultStream(requestFn, options) {
|
|
|
365
546
|
const maxQueued = 10;
|
|
366
547
|
let lastResumeToken;
|
|
367
548
|
let lastRequestStream;
|
|
549
|
+
let errorListener;
|
|
368
550
|
const startTime = Date.now();
|
|
369
551
|
const timeout = options?.gaxOptions?.timeout ?? Infinity;
|
|
370
|
-
//
|
|
552
|
+
// requestsStream allows multiple streams to be connected into one. This is good;
|
|
371
553
|
// if we need to retry a request and pipe more data to the user's stream.
|
|
372
554
|
// We also add an additional stream that can be used to flush any remaining
|
|
373
555
|
// items in the checkpoint stream that have been received, and that did not
|
|
374
556
|
// contain a resume token.
|
|
375
|
-
const requestsStream =
|
|
376
|
-
const flushStream = new
|
|
377
|
-
|
|
557
|
+
const requestsStream = new stream_1.PassThrough({ objectMode: true });
|
|
558
|
+
const flushStream = new stream_1.PassThrough({ objectMode: true });
|
|
559
|
+
flushStream.pipe(requestsStream);
|
|
378
560
|
const partialRSStream = new PartialResultStream(options);
|
|
379
561
|
const userStream = streamEvents(partialRSStream);
|
|
380
562
|
// We keep track of the number of PartialResultSets that did not include a
|
|
381
563
|
// resume token, as that is an indication whether it is safe to retry the
|
|
382
564
|
// stream halfway.
|
|
383
565
|
let withoutCheckpointCount = 0;
|
|
384
|
-
|
|
566
|
+
let receivedLast = false;
|
|
567
|
+
const batchAndSplitOnTokenStream = new CheckpointStream({
|
|
385
568
|
maxQueued,
|
|
386
569
|
isCheckpointFn: (chunk) => {
|
|
387
|
-
|
|
570
|
+
if (chunk.last) {
|
|
571
|
+
receivedLast = true;
|
|
572
|
+
destroyRequestStream();
|
|
573
|
+
requestsStream.end();
|
|
574
|
+
}
|
|
575
|
+
const withCheckpoint = _hasResumeToken(chunk) || Boolean(chunk.last);
|
|
388
576
|
if (withCheckpoint) {
|
|
389
577
|
withoutCheckpointCount = 0;
|
|
390
578
|
}
|
|
@@ -397,30 +585,65 @@ function partialResultStream(requestFn, options) {
|
|
|
397
585
|
// This listener ensures that the last request that executed successfully
|
|
398
586
|
// after one or more retries will end the requestsStream.
|
|
399
587
|
const endListener = () => {
|
|
588
|
+
if (receivedLast) {
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
400
591
|
setImmediate(() => {
|
|
592
|
+
if (receivedLast) {
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
401
595
|
// Push a fake PartialResultSet without any values but with a resume token
|
|
402
596
|
// into the stream to ensure that the checkpoint stream is emptied, and
|
|
403
597
|
// then push `null` to end the stream.
|
|
404
598
|
flushStream.push({ resumeToken: '_' });
|
|
405
599
|
flushStream.push(null);
|
|
406
|
-
requestsStream.end();
|
|
407
600
|
});
|
|
408
601
|
};
|
|
602
|
+
const destroyRequestStream = () => {
|
|
603
|
+
if (lastRequestStream) {
|
|
604
|
+
const streamToClean = lastRequestStream;
|
|
605
|
+
lastRequestStream = undefined;
|
|
606
|
+
streamToClean.removeListener('end', endListener);
|
|
607
|
+
if (errorListener) {
|
|
608
|
+
streamToClean.removeListener('error', errorListener);
|
|
609
|
+
}
|
|
610
|
+
streamToClean.on('error', () => { });
|
|
611
|
+
streamToClean.unpipe(requestsStream);
|
|
612
|
+
if (receivedLast) {
|
|
613
|
+
// Query completed successfully. Do not cancel the gRPC call; allow it
|
|
614
|
+
// to drain remaining trailers/EOF in the background so it is not marked
|
|
615
|
+
// CANCELLED by Spanner or Cloud Monitoring.
|
|
616
|
+
streamToClean.resume();
|
|
617
|
+
}
|
|
618
|
+
else {
|
|
619
|
+
streamToClean.destroy();
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
};
|
|
409
623
|
const makeRequest = () => {
|
|
410
624
|
if ((0, helper_1.isDefined)(lastResumeToken) && lastResumeToken.length > 0) {
|
|
411
625
|
partialRSStream._resetPendingValues();
|
|
412
626
|
}
|
|
413
627
|
lastRequestStream = requestFn(lastResumeToken);
|
|
414
628
|
lastRequestStream.on('end', endListener);
|
|
415
|
-
|
|
629
|
+
errorListener = (err) => {
|
|
630
|
+
if (receivedLast) {
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
destroyRequestStream();
|
|
634
|
+
setImmediate(() => retry(err));
|
|
635
|
+
};
|
|
636
|
+
lastRequestStream.on('error', errorListener);
|
|
637
|
+
lastRequestStream.pipe(requestsStream, { end: false });
|
|
416
638
|
};
|
|
417
639
|
const retry = (err) => {
|
|
640
|
+
destroyRequestStream();
|
|
418
641
|
const elapsed = Date.now() - startTime;
|
|
419
642
|
if (elapsed >= timeout) {
|
|
420
643
|
// The timeout has reached so this will flush any rows the
|
|
421
644
|
// checkpoint stream has queued. After that, we will destroy the
|
|
422
645
|
// user's stream with the Deadline exceeded error.
|
|
423
|
-
setImmediate(() => batchAndSplitOnTokenStream.
|
|
646
|
+
setImmediate(() => batchAndSplitOnTokenStream.flushAndDestroy(new transaction_runner_1.DeadlineError(err)));
|
|
424
647
|
return;
|
|
425
648
|
}
|
|
426
649
|
if (!(err.code &&
|
|
@@ -431,13 +654,9 @@ function partialResultStream(requestFn, options) {
|
|
|
431
654
|
// This is not a retryable error so this will flush any rows the
|
|
432
655
|
// checkpoint stream has queued. After that, we will destroy the
|
|
433
656
|
// user's stream with the same error.
|
|
434
|
-
setImmediate(() => batchAndSplitOnTokenStream.
|
|
657
|
+
setImmediate(() => batchAndSplitOnTokenStream.flushAndDestroy(err));
|
|
435
658
|
return;
|
|
436
659
|
}
|
|
437
|
-
if (lastRequestStream) {
|
|
438
|
-
lastRequestStream.removeListener('end', endListener);
|
|
439
|
-
lastRequestStream.destroy();
|
|
440
|
-
}
|
|
441
660
|
// Delay the retry until all the values that are already in the stream
|
|
442
661
|
// pipeline have been handled. This ensures that the checkpoint stream is
|
|
443
662
|
// reset to the correct point. Calling .reset() directly here could cause
|
|
@@ -450,13 +669,12 @@ function partialResultStream(requestFn, options) {
|
|
|
450
669
|
});
|
|
451
670
|
};
|
|
452
671
|
userStream.once('reading', makeRequest);
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
setImmediate(() => retry(err)));
|
|
672
|
+
userStream.once('close', () => {
|
|
673
|
+
destroyRequestStream();
|
|
674
|
+
requestsStream.destroy();
|
|
675
|
+
flushStream.destroy();
|
|
676
|
+
batchAndSplitOnTokenStream.destroy();
|
|
677
|
+
});
|
|
460
678
|
return (requestsStream
|
|
461
679
|
.pipe(batchAndSplitOnTokenStream)
|
|
462
680
|
// If we get this error, the checkpoint stream has flushed any rows
|
|
@@ -21,8 +21,10 @@ declare const common: any;
|
|
|
21
21
|
/**
|
|
22
22
|
* @callback GetSessionCallback
|
|
23
23
|
* @param {?Error} error Request error, if any.
|
|
24
|
-
* @param {Session} session The
|
|
25
|
-
* @param {Transaction} transaction The transaction object.
|
|
24
|
+
* @param {Session} [session] The session object.
|
|
25
|
+
* @param {Transaction} [transaction] The transaction object, if applicable.
|
|
26
|
+
* Omitted for read-only multiplexed session acquisitions to prevent throwaway
|
|
27
|
+
* allocations. Provided by getSessionForReadWrite or when using regular session pools.
|
|
26
28
|
*/
|
|
27
29
|
export interface GetSessionCallback {
|
|
28
30
|
(err: Error | null, session?: Session | null, transaction?: Transaction | null): void;
|
|
@@ -108,9 +108,20 @@ class SessionFactory extends common.GrpcServiceObject {
|
|
|
108
108
|
* @param {GetSessionCallback} callback The callback function.
|
|
109
109
|
*/
|
|
110
110
|
getSessionForReadWrite(callback) {
|
|
111
|
-
this.isMultiplexedRW
|
|
112
|
-
|
|
113
|
-
|
|
111
|
+
if (this.isMultiplexedRW) {
|
|
112
|
+
this.getSession((error, session, transaction) => {
|
|
113
|
+
if (error || !session) {
|
|
114
|
+
callback(error ?? new Error('No session found'), null);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const database = session.parent;
|
|
118
|
+
const activeTransaction = transaction ?? session.transaction(database.queryOptions_);
|
|
119
|
+
callback(null, session, activeTransaction);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
this.pool_.getSession(callback);
|
|
124
|
+
}
|
|
114
125
|
}
|
|
115
126
|
/**
|
|
116
127
|
* Returns the regular session pool object.
|