@google-cloud/spanner 7.14.0 → 7.16.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/CHANGELOG.md +36 -0
- package/README.md +14 -2
- package/build/protos/google/spanner/admin/instance/v1/common.proto +7 -0
- package/build/protos/google/spanner/admin/instance/v1/spanner_instance_admin.proto +139 -16
- package/build/protos/google/spanner/v1/commit_response.proto +9 -0
- package/build/protos/google/spanner/v1/result_set.proto +21 -0
- package/build/protos/google/spanner/v1/spanner.proto +39 -2
- package/build/protos/google/spanner/v1/transaction.proto +34 -0
- package/build/protos/google/spanner/v1/type.proto +43 -30
- package/build/protos/protos.d.ts +1908 -1452
- package/build/protos/protos.js +5095 -3724
- package/build/protos/protos.json +310 -129
- package/build/src/batch-transaction.d.ts +1 -0
- package/build/src/batch-transaction.js +90 -50
- package/build/src/database.d.ts +6 -2
- package/build/src/database.js +433 -235
- package/build/src/index.d.ts +5 -0
- package/build/src/index.js +4 -0
- package/build/src/instance.d.ts +2 -0
- package/build/src/instance.js +6 -1
- package/build/src/instrument.d.ts +84 -0
- package/build/src/instrument.js +212 -0
- package/build/src/session-pool.d.ts +2 -0
- package/build/src/session-pool.js +61 -23
- package/build/src/session.js +1 -7
- package/build/src/table.d.ts +3 -0
- package/build/src/table.js +34 -14
- package/build/src/transaction.d.ts +3 -0
- package/build/src/transaction.js +411 -194
- package/build/src/v1/spanner_client.d.ts +14 -0
- package/package.json +12 -3
package/build/src/transaction.js
CHANGED
|
@@ -25,10 +25,12 @@ const is = require("is");
|
|
|
25
25
|
const stream_1 = require("stream");
|
|
26
26
|
const codec_1 = require("./codec");
|
|
27
27
|
const partial_result_stream_1 = require("./partial-result-stream");
|
|
28
|
+
const instrument_1 = require("./instrument");
|
|
28
29
|
const protos_1 = require("../protos/protos");
|
|
29
30
|
const common_1 = require("./common");
|
|
30
31
|
const protos_2 = require("../protos/protos");
|
|
31
32
|
var ReadLockMode = protos_2.google.spanner.v1.TransactionOptions.ReadWrite.ReadLockMode;
|
|
33
|
+
const instrument_2 = require("./instrument");
|
|
32
34
|
const RETRY_INFO_TYPE = 'type.googleapis.com/google.rpc.retryinfo';
|
|
33
35
|
const RETRY_INFO_BIN = 'google.rpc.retryinfo-bin';
|
|
34
36
|
/**
|
|
@@ -140,11 +142,13 @@ class Snapshot extends events_1.EventEmitter {
|
|
|
140
142
|
this.requestStream = session.requestStream.bind(session);
|
|
141
143
|
const readOnly = Snapshot.encodeTimestampBounds(options || {});
|
|
142
144
|
this._options = { readOnly };
|
|
145
|
+
this._dbName = this.session.parent.formattedName_;
|
|
143
146
|
this.resourceHeader_ = {
|
|
144
|
-
[common_1.CLOUD_RESOURCE_HEADER]: this.
|
|
147
|
+
[common_1.CLOUD_RESOURCE_HEADER]: this._dbName,
|
|
145
148
|
};
|
|
146
149
|
this._waitingRequests = [];
|
|
147
150
|
this._inlineBeginStarted = false;
|
|
151
|
+
this._observabilityOptions = session._observabilityOptions;
|
|
148
152
|
}
|
|
149
153
|
begin(gaxOptionsOrCallback, cb) {
|
|
150
154
|
const gaxOpts = typeof gaxOptionsOrCallback === 'object' ? gaxOptionsOrCallback : {};
|
|
@@ -168,19 +172,28 @@ class Snapshot extends events_1.EventEmitter {
|
|
|
168
172
|
this._options.partitionedDml !== undefined)) {
|
|
169
173
|
(0, common_1.addLeaderAwareRoutingHeader)(headers);
|
|
170
174
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
175
|
+
const traceConfig = {
|
|
176
|
+
opts: this._observabilityOptions,
|
|
177
|
+
dbName: this._dbName,
|
|
178
|
+
};
|
|
179
|
+
return (0, instrument_2.startTrace)('Snapshot.begin', traceConfig, span => {
|
|
180
|
+
span.addEvent('Begin Transaction');
|
|
181
|
+
this.request({
|
|
182
|
+
client: 'SpannerClient',
|
|
183
|
+
method: 'beginTransaction',
|
|
184
|
+
reqOpts,
|
|
185
|
+
gaxOpts,
|
|
186
|
+
headers: headers,
|
|
187
|
+
}, (err, resp) => {
|
|
188
|
+
if (err) {
|
|
189
|
+
(0, instrument_2.setSpanError)(span, err);
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
this._update(resp);
|
|
193
|
+
}
|
|
194
|
+
span.end();
|
|
179
195
|
callback(err, resp);
|
|
180
|
-
|
|
181
|
-
}
|
|
182
|
-
this._update(resp);
|
|
183
|
-
callback(null, resp);
|
|
196
|
+
});
|
|
184
197
|
});
|
|
185
198
|
}
|
|
186
199
|
/**
|
|
@@ -354,7 +367,7 @@ class Snapshot extends events_1.EventEmitter {
|
|
|
354
367
|
* ```
|
|
355
368
|
*/
|
|
356
369
|
createReadStream(table, request = {}) {
|
|
357
|
-
var _a, _b
|
|
370
|
+
var _a, _b;
|
|
358
371
|
const { gaxOptions, json, jsonOptions, maxResumeRetries, requestOptions, columnsMetadata, } = request;
|
|
359
372
|
const keySet = Snapshot.encodeKeySet(request);
|
|
360
373
|
const transaction = {};
|
|
@@ -392,37 +405,83 @@ class Snapshot extends events_1.EventEmitter {
|
|
|
392
405
|
this._options.partitionedDml !== undefined)) {
|
|
393
406
|
(0, common_1.addLeaderAwareRoutingHeader)(headers);
|
|
394
407
|
}
|
|
395
|
-
const
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
}
|
|
400
|
-
return this.requestStream({
|
|
401
|
-
client: 'SpannerClient',
|
|
402
|
-
method: 'streamingRead',
|
|
403
|
-
reqOpts: Object.assign({}, reqOpts, { resumeToken }),
|
|
404
|
-
gaxOpts: gaxOptions,
|
|
405
|
-
headers: headers,
|
|
406
|
-
});
|
|
408
|
+
const traceConfig = {
|
|
409
|
+
tableName: table,
|
|
410
|
+
opts: this._observabilityOptions,
|
|
411
|
+
dbName: this._dbName,
|
|
407
412
|
};
|
|
408
|
-
return (
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
413
|
+
return (0, instrument_2.startTrace)('Snapshot.createReadStream', traceConfig, span => {
|
|
414
|
+
var _a;
|
|
415
|
+
let attempt = 0;
|
|
416
|
+
const makeRequest = (resumeToken) => {
|
|
417
|
+
if (this.id && transaction.begin) {
|
|
418
|
+
delete transaction.begin;
|
|
419
|
+
transaction.id = this.id;
|
|
420
|
+
}
|
|
421
|
+
attempt++;
|
|
422
|
+
if (!resumeToken) {
|
|
423
|
+
if (attempt === 1) {
|
|
424
|
+
span.addEvent('Starting stream');
|
|
425
|
+
}
|
|
426
|
+
else {
|
|
427
|
+
span.addEvent('Re-attempting start stream', { attempt: attempt });
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
span.addEvent('Resuming stream', {
|
|
432
|
+
resume_token: resumeToken.toString(),
|
|
433
|
+
attempt: attempt,
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
return this.requestStream({
|
|
437
|
+
client: 'SpannerClient',
|
|
438
|
+
method: 'streamingRead',
|
|
439
|
+
reqOpts: Object.assign({}, reqOpts, { resumeToken }),
|
|
440
|
+
gaxOpts: gaxOptions,
|
|
441
|
+
headers: headers,
|
|
442
|
+
});
|
|
443
|
+
};
|
|
444
|
+
const resultStream = (_a = (0, partial_result_stream_1.partialResultStream)(this._wrapWithIdWaiter(makeRequest), {
|
|
445
|
+
json,
|
|
446
|
+
jsonOptions,
|
|
447
|
+
maxResumeRetries,
|
|
448
|
+
columnsMetadata,
|
|
449
|
+
gaxOptions,
|
|
450
|
+
})) === null || _a === void 0 ? void 0 : _a.on('response', response => {
|
|
451
|
+
if (response.metadata && response.metadata.transaction && !this.id) {
|
|
452
|
+
this._update(response.metadata.transaction);
|
|
453
|
+
}
|
|
454
|
+
}).on('error', err => {
|
|
455
|
+
var _a;
|
|
456
|
+
(0, instrument_2.setSpanError)(span, err);
|
|
457
|
+
const wasAborted = isErrorAborted(err);
|
|
458
|
+
if (!this.id && this._useInRunner && !wasAborted) {
|
|
459
|
+
// TODO: resolve https://github.com/googleapis/nodejs-spanner/issues/2170
|
|
460
|
+
this.begin();
|
|
461
|
+
}
|
|
462
|
+
else {
|
|
463
|
+
if (wasAborted) {
|
|
464
|
+
span.addEvent('Stream broken. Not safe to retry', {
|
|
465
|
+
'transaction.id': (_a = this.id) === null || _a === void 0 ? void 0 : _a.toString(),
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
span.end();
|
|
470
|
+
}).on('end', err => {
|
|
471
|
+
if (err) {
|
|
472
|
+
(0, instrument_2.setSpanError)(span, err);
|
|
473
|
+
}
|
|
474
|
+
span.end();
|
|
475
|
+
});
|
|
476
|
+
if (resultStream instanceof stream_1.Stream) {
|
|
477
|
+
(0, stream_1.finished)(resultStream, err => {
|
|
478
|
+
if (err) {
|
|
479
|
+
(0, instrument_2.setSpanError)(span, err);
|
|
480
|
+
}
|
|
481
|
+
span.end();
|
|
482
|
+
});
|
|
425
483
|
}
|
|
484
|
+
return resultStream;
|
|
426
485
|
});
|
|
427
486
|
}
|
|
428
487
|
/**
|
|
@@ -489,28 +548,57 @@ class Snapshot extends events_1.EventEmitter {
|
|
|
489
548
|
request = requestOrCallback;
|
|
490
549
|
callback = cb;
|
|
491
550
|
}
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
.
|
|
495
|
-
.
|
|
551
|
+
const traceConfig = {
|
|
552
|
+
tableName: table,
|
|
553
|
+
opts: this._observabilityOptions,
|
|
554
|
+
dbName: this._dbName,
|
|
555
|
+
};
|
|
556
|
+
return (0, instrument_2.startTrace)('Snapshot.read', traceConfig, span => {
|
|
557
|
+
this.createReadStream(table, request)
|
|
558
|
+
.on('error', err => {
|
|
559
|
+
const e = err;
|
|
560
|
+
(0, instrument_2.setSpanError)(span, e);
|
|
561
|
+
span.end();
|
|
562
|
+
callback(e, null);
|
|
563
|
+
})
|
|
564
|
+
.on('data', row => rows.push(row))
|
|
565
|
+
.on('end', () => {
|
|
566
|
+
span.end();
|
|
567
|
+
callback(null, rows);
|
|
568
|
+
});
|
|
569
|
+
});
|
|
496
570
|
}
|
|
497
571
|
run(query, callback) {
|
|
498
572
|
const rows = [];
|
|
499
573
|
let stats;
|
|
500
574
|
let metadata;
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
.
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
575
|
+
const traceConfig = {
|
|
576
|
+
sql: query,
|
|
577
|
+
opts: this._observabilityOptions,
|
|
578
|
+
dbName: this._dbName,
|
|
579
|
+
};
|
|
580
|
+
(0, instrument_2.startTrace)('Snapshot.run', traceConfig, span => {
|
|
581
|
+
return this.runStream(query)
|
|
582
|
+
.on('error', (err, rows, stats, metadata) => {
|
|
583
|
+
(0, instrument_2.setSpanError)(span, err);
|
|
584
|
+
span.end();
|
|
585
|
+
callback(err, rows, stats, metadata);
|
|
586
|
+
})
|
|
587
|
+
.on('response', response => {
|
|
588
|
+
if (response.metadata) {
|
|
589
|
+
metadata = response.metadata;
|
|
590
|
+
if (metadata.transaction && !this.id) {
|
|
591
|
+
this._update(metadata.transaction);
|
|
592
|
+
}
|
|
508
593
|
}
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
594
|
+
})
|
|
595
|
+
.on('data', row => rows.push(row))
|
|
596
|
+
.on('stats', _stats => (stats = _stats))
|
|
597
|
+
.on('end', () => {
|
|
598
|
+
span.end();
|
|
599
|
+
callback(null, rows, stats, metadata);
|
|
600
|
+
});
|
|
601
|
+
});
|
|
514
602
|
}
|
|
515
603
|
/**
|
|
516
604
|
* ExecuteSql request options. This includes all standard ExecuteSqlRequest
|
|
@@ -658,45 +746,94 @@ class Snapshot extends events_1.EventEmitter {
|
|
|
658
746
|
this._options.partitionedDml !== undefined)) {
|
|
659
747
|
(0, common_1.addLeaderAwareRoutingHeader)(headers);
|
|
660
748
|
}
|
|
661
|
-
const
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
749
|
+
const traceConfig = {
|
|
750
|
+
opts: this._observabilityOptions,
|
|
751
|
+
dbName: this._dbName,
|
|
752
|
+
...query,
|
|
753
|
+
};
|
|
754
|
+
return (0, instrument_2.startTrace)('Snapshot.runStream', traceConfig, span => {
|
|
755
|
+
let attempt = 0;
|
|
756
|
+
const makeRequest = (resumeToken) => {
|
|
757
|
+
attempt++;
|
|
758
|
+
if (!resumeToken) {
|
|
759
|
+
if (attempt === 1) {
|
|
760
|
+
span.addEvent('Starting stream');
|
|
761
|
+
}
|
|
762
|
+
else {
|
|
763
|
+
span.addEvent('Re-attempting start stream', { attempt: attempt });
|
|
764
|
+
}
|
|
665
765
|
}
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
766
|
+
else {
|
|
767
|
+
span.addEvent('Resuming stream', {
|
|
768
|
+
resume_token: resumeToken.toString(),
|
|
769
|
+
attempt: attempt,
|
|
770
|
+
});
|
|
670
771
|
}
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
772
|
+
if (!reqOpts || (this.id && !reqOpts.transaction.id)) {
|
|
773
|
+
try {
|
|
774
|
+
sanitizeRequest();
|
|
775
|
+
}
|
|
776
|
+
catch (e) {
|
|
777
|
+
const errorStream = new stream_1.PassThrough();
|
|
778
|
+
(0, instrument_2.setSpanErrorAndException)(span, e);
|
|
779
|
+
span.end();
|
|
780
|
+
setImmediate(() => errorStream.destroy(e));
|
|
781
|
+
return errorStream;
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
return this.requestStream({
|
|
785
|
+
client: 'SpannerClient',
|
|
786
|
+
method: 'executeStreamingSql',
|
|
787
|
+
reqOpts: Object.assign({}, reqOpts, { resumeToken }),
|
|
788
|
+
gaxOpts: gaxOptions,
|
|
789
|
+
headers: headers,
|
|
790
|
+
});
|
|
791
|
+
};
|
|
792
|
+
const resultStream = (0, partial_result_stream_1.partialResultStream)(this._wrapWithIdWaiter(makeRequest), {
|
|
793
|
+
json,
|
|
794
|
+
jsonOptions,
|
|
795
|
+
maxResumeRetries,
|
|
796
|
+
columnsMetadata,
|
|
797
|
+
gaxOptions,
|
|
798
|
+
})
|
|
799
|
+
.on('response', response => {
|
|
800
|
+
if (response.metadata && response.metadata.transaction && !this.id) {
|
|
801
|
+
this._update(response.metadata.transaction);
|
|
802
|
+
}
|
|
803
|
+
})
|
|
804
|
+
.on('error', err => {
|
|
805
|
+
var _a;
|
|
806
|
+
(0, instrument_2.setSpanError)(span, err);
|
|
807
|
+
const wasAborted = isErrorAborted(err);
|
|
808
|
+
if (!this.id && this._useInRunner && !wasAborted) {
|
|
809
|
+
span.addEvent('Stream broken. Safe to retry');
|
|
810
|
+
// TODO: resolve https://github.com/googleapis/nodejs-spanner/issues/2170
|
|
811
|
+
this.begin();
|
|
812
|
+
}
|
|
813
|
+
else {
|
|
814
|
+
if (wasAborted) {
|
|
815
|
+
span.addEvent('Stream broken. Not safe to retry', {
|
|
816
|
+
'transaction.id': (_a = this.id) === null || _a === void 0 ? void 0 : _a.toString(),
|
|
817
|
+
});
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
span.end();
|
|
821
|
+
})
|
|
822
|
+
.on('end', err => {
|
|
823
|
+
if (err) {
|
|
824
|
+
(0, instrument_2.setSpanError)(span, err);
|
|
825
|
+
}
|
|
826
|
+
span.end();
|
|
678
827
|
});
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
})
|
|
687
|
-
.on('response', response => {
|
|
688
|
-
if (response.metadata && response.metadata.transaction && !this.id) {
|
|
689
|
-
this._update(response.metadata.transaction);
|
|
690
|
-
}
|
|
691
|
-
})
|
|
692
|
-
.on('error', err => {
|
|
693
|
-
const isServiceError = err && typeof err === 'object' && 'code' in err;
|
|
694
|
-
if (!this.id &&
|
|
695
|
-
this._useInRunner &&
|
|
696
|
-
!(isServiceError &&
|
|
697
|
-
err.code === google_gax_1.grpc.status.ABORTED)) {
|
|
698
|
-
this.begin();
|
|
828
|
+
if (resultStream instanceof stream_1.Stream) {
|
|
829
|
+
(0, stream_1.finished)(resultStream, err => {
|
|
830
|
+
if (err) {
|
|
831
|
+
(0, instrument_2.setSpanError)(span, err);
|
|
832
|
+
}
|
|
833
|
+
span.end();
|
|
834
|
+
});
|
|
699
835
|
}
|
|
836
|
+
return resultStream;
|
|
700
837
|
});
|
|
701
838
|
}
|
|
702
839
|
/**
|
|
@@ -825,6 +962,8 @@ class Snapshot extends events_1.EventEmitter {
|
|
|
825
962
|
const { id, readTimestamp } = resp;
|
|
826
963
|
this.id = id;
|
|
827
964
|
this.metadata = resp;
|
|
965
|
+
const span = (0, instrument_1.getActiveOrNoopSpan)();
|
|
966
|
+
span.addEvent('Transaction Creation Done', { id: this.id.toString() });
|
|
828
967
|
if (readTimestamp) {
|
|
829
968
|
this.readTimestampProto = readTimestamp;
|
|
830
969
|
this.readTimestamp = new precise_date_1.PreciseDate(readTimestamp);
|
|
@@ -898,12 +1037,23 @@ class Dml extends Snapshot {
|
|
|
898
1037
|
if (typeof query === 'string') {
|
|
899
1038
|
query = { sql: query };
|
|
900
1039
|
}
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
1040
|
+
const traceConfig = {
|
|
1041
|
+
opts: this._observabilityOptions,
|
|
1042
|
+
dbName: this._dbName,
|
|
1043
|
+
...query,
|
|
1044
|
+
};
|
|
1045
|
+
return (0, instrument_2.startTrace)('Dml.runUpdate', traceConfig, span => {
|
|
1046
|
+
this.run(query, (err, rows, stats) => {
|
|
1047
|
+
let rowCount = 0;
|
|
1048
|
+
if (stats && stats.rowCount) {
|
|
1049
|
+
rowCount = Math.floor(stats[stats.rowCount]);
|
|
1050
|
+
}
|
|
1051
|
+
if (err) {
|
|
1052
|
+
(0, instrument_2.setSpanError)(span, err);
|
|
1053
|
+
}
|
|
1054
|
+
span.end();
|
|
1055
|
+
callback(err, rowCount);
|
|
1056
|
+
});
|
|
907
1057
|
});
|
|
908
1058
|
}
|
|
909
1059
|
}
|
|
@@ -1049,41 +1199,51 @@ class Transaction extends Dml {
|
|
|
1049
1199
|
if (this._getSpanner().routeToLeaderEnabled) {
|
|
1050
1200
|
(0, common_1.addLeaderAwareRoutingHeader)(headers);
|
|
1051
1201
|
}
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1202
|
+
const traceConfig = {
|
|
1203
|
+
opts: this._observabilityOptions,
|
|
1204
|
+
dbName: this._dbName,
|
|
1205
|
+
};
|
|
1206
|
+
return (0, instrument_2.startTrace)('Transaction.batchUpdate', traceConfig, span => {
|
|
1207
|
+
this.request({
|
|
1208
|
+
client: 'SpannerClient',
|
|
1209
|
+
method: 'executeBatchDml',
|
|
1210
|
+
reqOpts,
|
|
1211
|
+
gaxOpts,
|
|
1212
|
+
headers: headers,
|
|
1213
|
+
}, (err, resp) => {
|
|
1214
|
+
var _a;
|
|
1215
|
+
let batchUpdateError;
|
|
1216
|
+
if (err) {
|
|
1217
|
+
const rowCounts = [];
|
|
1218
|
+
batchUpdateError = Object.assign(err, { rowCounts });
|
|
1219
|
+
(0, instrument_2.setSpanError)(span, batchUpdateError);
|
|
1220
|
+
span.end();
|
|
1221
|
+
callback(batchUpdateError, rowCounts, resp);
|
|
1222
|
+
return;
|
|
1071
1223
|
}
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
metadata: Transaction.extractKnownMetadata(status.details),
|
|
1083
|
-
rowCounts,
|
|
1224
|
+
const { resultSets, status } = resp;
|
|
1225
|
+
for (const resultSet of resultSets) {
|
|
1226
|
+
if (!this.id && ((_a = resultSet.metadata) === null || _a === void 0 ? void 0 : _a.transaction)) {
|
|
1227
|
+
this._update(resultSet.metadata.transaction);
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
const rowCounts = resultSets.map(({ stats }) => {
|
|
1231
|
+
return ((stats &&
|
|
1232
|
+
Number(stats[stats.rowCount])) ||
|
|
1233
|
+
0);
|
|
1084
1234
|
});
|
|
1085
|
-
|
|
1086
|
-
|
|
1235
|
+
if (status && status.code !== 0) {
|
|
1236
|
+
const error = new Error(status.message);
|
|
1237
|
+
batchUpdateError = Object.assign(error, {
|
|
1238
|
+
code: status.code,
|
|
1239
|
+
metadata: Transaction.extractKnownMetadata(status.details),
|
|
1240
|
+
rowCounts,
|
|
1241
|
+
});
|
|
1242
|
+
(0, instrument_2.setSpanError)(span, batchUpdateError);
|
|
1243
|
+
}
|
|
1244
|
+
span.end();
|
|
1245
|
+
callback(batchUpdateError, rowCounts, resp);
|
|
1246
|
+
});
|
|
1087
1247
|
});
|
|
1088
1248
|
}
|
|
1089
1249
|
static extractKnownMetadata(details) {
|
|
@@ -1116,43 +1276,70 @@ class Transaction extends Dml {
|
|
|
1116
1276
|
const session = this.session.formattedName_;
|
|
1117
1277
|
const requestOptions = options.requestOptions;
|
|
1118
1278
|
const reqOpts = { mutations, session, requestOptions };
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
if ('returnCommitStats' in options &&
|
|
1130
|
-
options.returnCommitStats) {
|
|
1131
|
-
reqOpts.returnCommitStats = options.returnCommitStats;
|
|
1132
|
-
}
|
|
1133
|
-
if ('maxCommitDelay' in options &&
|
|
1134
|
-
options.maxCommitDelay) {
|
|
1135
|
-
reqOpts.maxCommitDelay = options.maxCommitDelay;
|
|
1136
|
-
}
|
|
1137
|
-
reqOpts.requestOptions = Object.assign(requestOptions || {}, this.requestOptions);
|
|
1138
|
-
const headers = this.resourceHeader_;
|
|
1139
|
-
if (this._getSpanner().routeToLeaderEnabled) {
|
|
1140
|
-
(0, common_1.addLeaderAwareRoutingHeader)(headers);
|
|
1141
|
-
}
|
|
1142
|
-
this.request({
|
|
1143
|
-
client: 'SpannerClient',
|
|
1144
|
-
method: 'commit',
|
|
1145
|
-
reqOpts,
|
|
1146
|
-
gaxOpts: gaxOpts,
|
|
1147
|
-
headers: headers,
|
|
1148
|
-
}, (err, resp) => {
|
|
1149
|
-
this.end();
|
|
1150
|
-
if (resp && resp.commitTimestamp) {
|
|
1151
|
-
this.commitTimestampProto = resp.commitTimestamp;
|
|
1152
|
-
this.commitTimestamp = new precise_date_1.PreciseDate(resp.commitTimestamp);
|
|
1279
|
+
const traceConfig = {
|
|
1280
|
+
opts: this._observabilityOptions,
|
|
1281
|
+
dbName: this._dbName,
|
|
1282
|
+
};
|
|
1283
|
+
return (0, instrument_2.startTrace)('Transaction.commit', traceConfig, span => {
|
|
1284
|
+
if (this.id) {
|
|
1285
|
+
reqOpts.transactionId = this.id;
|
|
1286
|
+
}
|
|
1287
|
+
else if (!this._useInRunner) {
|
|
1288
|
+
reqOpts.singleUseTransaction = this._options;
|
|
1153
1289
|
}
|
|
1154
|
-
|
|
1155
|
-
|
|
1290
|
+
else {
|
|
1291
|
+
this.begin().then(() => {
|
|
1292
|
+
this.commit(options, (err, resp) => {
|
|
1293
|
+
if (err) {
|
|
1294
|
+
(0, instrument_2.setSpanError)(span, err);
|
|
1295
|
+
}
|
|
1296
|
+
span.end();
|
|
1297
|
+
callback(err, resp);
|
|
1298
|
+
});
|
|
1299
|
+
}, err => {
|
|
1300
|
+
(0, instrument_2.setSpanError)(span, err);
|
|
1301
|
+
span.end();
|
|
1302
|
+
callback(err, null);
|
|
1303
|
+
});
|
|
1304
|
+
return;
|
|
1305
|
+
}
|
|
1306
|
+
if ('returnCommitStats' in options &&
|
|
1307
|
+
options.returnCommitStats) {
|
|
1308
|
+
reqOpts.returnCommitStats = options.returnCommitStats;
|
|
1309
|
+
}
|
|
1310
|
+
if ('maxCommitDelay' in options &&
|
|
1311
|
+
options.maxCommitDelay) {
|
|
1312
|
+
reqOpts.maxCommitDelay = options.maxCommitDelay;
|
|
1313
|
+
}
|
|
1314
|
+
reqOpts.requestOptions = Object.assign(requestOptions || {}, this.requestOptions);
|
|
1315
|
+
const headers = this.resourceHeader_;
|
|
1316
|
+
if (this._getSpanner().routeToLeaderEnabled) {
|
|
1317
|
+
(0, common_1.addLeaderAwareRoutingHeader)(headers);
|
|
1318
|
+
}
|
|
1319
|
+
span.addEvent('Starting Commit');
|
|
1320
|
+
this.request({
|
|
1321
|
+
client: 'SpannerClient',
|
|
1322
|
+
method: 'commit',
|
|
1323
|
+
reqOpts,
|
|
1324
|
+
gaxOpts: gaxOpts,
|
|
1325
|
+
headers: headers,
|
|
1326
|
+
}, (err, resp) => {
|
|
1327
|
+
this.end();
|
|
1328
|
+
if (err) {
|
|
1329
|
+
span.addEvent('Commit failed');
|
|
1330
|
+
(0, instrument_2.setSpanError)(span, err);
|
|
1331
|
+
}
|
|
1332
|
+
else {
|
|
1333
|
+
span.addEvent('Commit Done');
|
|
1334
|
+
}
|
|
1335
|
+
if (resp && resp.commitTimestamp) {
|
|
1336
|
+
this.commitTimestampProto = resp.commitTimestamp;
|
|
1337
|
+
this.commitTimestamp = new precise_date_1.PreciseDate(resp.commitTimestamp);
|
|
1338
|
+
}
|
|
1339
|
+
err = Transaction.decorateCommitError(err, mutations);
|
|
1340
|
+
span.end();
|
|
1341
|
+
callback(err, resp);
|
|
1342
|
+
});
|
|
1156
1343
|
});
|
|
1157
1344
|
}
|
|
1158
1345
|
/**
|
|
@@ -1380,29 +1567,42 @@ class Transaction extends Dml {
|
|
|
1380
1567
|
rollback(gaxOptionsOrCallback, cb) {
|
|
1381
1568
|
const gaxOpts = typeof gaxOptionsOrCallback === 'object' ? gaxOptionsOrCallback : {};
|
|
1382
1569
|
const callback = typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb;
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
}
|
|
1387
|
-
const session = this.session.formattedName_;
|
|
1388
|
-
const transactionId = this.id;
|
|
1389
|
-
const reqOpts = {
|
|
1390
|
-
session,
|
|
1391
|
-
transactionId,
|
|
1570
|
+
const traceConfig = {
|
|
1571
|
+
opts: this._observabilityOptions,
|
|
1572
|
+
dbName: this._dbName,
|
|
1392
1573
|
};
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1574
|
+
return (0, instrument_2.startTrace)('Transaction.rollback', traceConfig, span => {
|
|
1575
|
+
if (!this.id) {
|
|
1576
|
+
const err = new Error('Transaction ID is unknown, nothing to rollback.');
|
|
1577
|
+
(0, instrument_2.setSpanError)(span, err);
|
|
1578
|
+
span.end();
|
|
1579
|
+
callback(err);
|
|
1580
|
+
return;
|
|
1581
|
+
}
|
|
1582
|
+
const session = this.session.formattedName_;
|
|
1583
|
+
const transactionId = this.id;
|
|
1584
|
+
const reqOpts = {
|
|
1585
|
+
session,
|
|
1586
|
+
transactionId,
|
|
1587
|
+
};
|
|
1588
|
+
const headers = this.resourceHeader_;
|
|
1589
|
+
if (this._getSpanner().routeToLeaderEnabled) {
|
|
1590
|
+
(0, common_1.addLeaderAwareRoutingHeader)(headers);
|
|
1591
|
+
}
|
|
1592
|
+
this.request({
|
|
1593
|
+
client: 'SpannerClient',
|
|
1594
|
+
method: 'rollback',
|
|
1595
|
+
reqOpts,
|
|
1596
|
+
gaxOpts,
|
|
1597
|
+
headers: headers,
|
|
1598
|
+
}, (err) => {
|
|
1599
|
+
if (err) {
|
|
1600
|
+
(0, instrument_2.setSpanError)(span, err);
|
|
1601
|
+
}
|
|
1602
|
+
span.end();
|
|
1603
|
+
this.end();
|
|
1604
|
+
callback(err);
|
|
1605
|
+
});
|
|
1406
1606
|
});
|
|
1407
1607
|
}
|
|
1408
1608
|
/**
|
|
@@ -1772,13 +1972,30 @@ class PartitionedDml extends Dml {
|
|
|
1772
1972
|
this._options.excludeTxnFromChangeStreams = true;
|
|
1773
1973
|
}
|
|
1774
1974
|
runUpdate(query, callback) {
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1975
|
+
const traceConfig = {
|
|
1976
|
+
sql: query,
|
|
1977
|
+
opts: this._observabilityOptions,
|
|
1978
|
+
dbName: this._dbName,
|
|
1979
|
+
};
|
|
1980
|
+
return (0, instrument_2.startTrace)('PartitionedDml.runUpdate', traceConfig, span => {
|
|
1981
|
+
super.runUpdate(query, (err, count) => {
|
|
1982
|
+
if (err) {
|
|
1983
|
+
(0, instrument_2.setSpanError)(span, err);
|
|
1984
|
+
}
|
|
1985
|
+
this.end();
|
|
1986
|
+
span.end();
|
|
1987
|
+
callback(err, count);
|
|
1988
|
+
});
|
|
1778
1989
|
});
|
|
1779
1990
|
}
|
|
1780
1991
|
}
|
|
1781
1992
|
exports.PartitionedDml = PartitionedDml;
|
|
1993
|
+
function isErrorAborted(err) {
|
|
1994
|
+
return (err &&
|
|
1995
|
+
typeof err === 'object' &&
|
|
1996
|
+
'code' in err &&
|
|
1997
|
+
err.code === google_gax_1.grpc.status.ABORTED);
|
|
1998
|
+
}
|
|
1782
1999
|
/*! Developer Documentation
|
|
1783
2000
|
*
|
|
1784
2001
|
* All async methods (except for streams) return a Promise in the event
|