@google-cloud/spanner 6.8.0 → 6.9.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 CHANGED
@@ -4,6 +4,13 @@
4
4
 
5
5
  [1]: https://www.npmjs.com/package/nodejs-spanner?activeTab=versions
6
6
 
7
+ ## [6.9.0](https://github.com/googleapis/nodejs-spanner/compare/v6.8.0...v6.9.0) (2023-04-26)
8
+
9
+
10
+ ### Features
11
+
12
+ * Leader aware routing ([#1783](https://github.com/googleapis/nodejs-spanner/issues/1783)) ([0703f41](https://github.com/googleapis/nodejs-spanner/commit/0703f4160c4a0b4c9f9f716174daca110ab8e50f))
13
+
7
14
  ## [6.8.0](https://github.com/googleapis/nodejs-spanner/compare/v6.7.2...v6.8.0) (2023-04-06)
8
15
 
9
16
 
@@ -123,11 +123,16 @@ class BatchTransaction extends transaction_1.Snapshot {
123
123
  const reqOpts = Object.assign({}, query, transaction_1.Snapshot.encodeParams(query));
124
124
  delete reqOpts.gaxOptions;
125
125
  delete reqOpts.types;
126
+ const headers = {};
127
+ if (this._getSpanner().routeToLeaderEnabled) {
128
+ (0, common_1.addLeaderAwareRoutingHeader)(headers);
129
+ }
126
130
  this.createPartitions_({
127
131
  client: 'SpannerClient',
128
132
  method: 'partitionQuery',
129
133
  reqOpts,
130
134
  gaxOpts: query.gaxOptions,
135
+ headers: headers,
131
136
  }, callback);
132
137
  }
133
138
  /**
@@ -205,11 +210,16 @@ class BatchTransaction extends transaction_1.Snapshot {
205
210
  delete reqOpts.gaxOptions;
206
211
  delete reqOpts.keys;
207
212
  delete reqOpts.ranges;
213
+ const headers = {};
214
+ if (this._getSpanner().routeToLeaderEnabled) {
215
+ (0, common_1.addLeaderAwareRoutingHeader)(headers);
216
+ }
208
217
  this.createPartitions_({
209
218
  client: 'SpannerClient',
210
219
  method: 'partitionRead',
211
220
  reqOpts,
212
221
  gaxOpts: options.gaxOptions,
222
+ headers: headers,
213
223
  }, callback);
214
224
  }
215
225
  /**
@@ -45,3 +45,14 @@ export interface PagedOptionsWithFilter extends PagedOptions {
45
45
  * by the backend.
46
46
  */
47
47
  export declare const CLOUD_RESOURCE_HEADER = "google-cloud-resource-prefix";
48
+ /*!
49
+ * HTTP header to route the requests at Leader
50
+ */
51
+ export declare const LEADER_AWARE_ROUTING_HEADER = "x-goog-spanner-route-to-leader";
52
+ /**
53
+ * Add Leader aware routing header to existing header list.
54
+ * @param headers Existing header list.
55
+ */
56
+ export declare function addLeaderAwareRoutingHeader(headers: {
57
+ [k: string]: string;
58
+ }): void;
@@ -15,10 +15,22 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.CLOUD_RESOURCE_HEADER = void 0;
18
+ exports.addLeaderAwareRoutingHeader = exports.LEADER_AWARE_ROUTING_HEADER = exports.CLOUD_RESOURCE_HEADER = void 0;
19
19
  /*!
20
20
  * HTTP header for the resource prefix to improve routing
21
21
  * by the backend.
22
22
  */
23
23
  exports.CLOUD_RESOURCE_HEADER = 'google-cloud-resource-prefix';
24
+ /*!
25
+ * HTTP header to route the requests at Leader
26
+ */
27
+ exports.LEADER_AWARE_ROUTING_HEADER = 'x-goog-spanner-route-to-leader';
28
+ /**
29
+ * Add Leader aware routing header to existing header list.
30
+ * @param headers Existing header list.
31
+ */
32
+ function addLeaderAwareRoutingHeader(headers) {
33
+ headers[exports.LEADER_AWARE_ROUTING_HEADER] = 'true';
34
+ }
35
+ exports.addLeaderAwareRoutingHeader = addLeaderAwareRoutingHeader;
24
36
  //# sourceMappingURL=common.js.map
@@ -1864,6 +1864,14 @@ declare class Database extends common.GrpcServiceObject {
1864
1864
  * ```
1865
1865
  */
1866
1866
  static formatName_(instanceName: string, name: string): string;
1867
+ /**
1868
+ * Gets the Spanner object
1869
+ *
1870
+ * @private
1871
+ *
1872
+ * @returns {Spanner}
1873
+ */
1874
+ private _getSpanner;
1867
1875
  }
1868
1876
  /**
1869
1877
  * Reference to the {@link Database} class.
@@ -179,12 +179,16 @@ class Database extends common.GrpcServiceObject {
179
179
  sessionTemplate: { labels: labels, creatorRole: databaseRole },
180
180
  sessionCount: count,
181
181
  };
182
+ const headers = this.resourceHeader_;
183
+ if (this._getSpanner().routeToLeaderEnabled) {
184
+ (0, common_1.addLeaderAwareRoutingHeader)(headers);
185
+ }
182
186
  this.request({
183
187
  client: 'SpannerClient',
184
188
  method: 'batchCreateSessions',
185
189
  reqOpts,
186
190
  gaxOpts: options.gaxOptions,
187
- headers: this.resourceHeader_,
191
+ headers: headers,
188
192
  }, (err, resp) => {
189
193
  if (err) {
190
194
  callback(err, null, resp);
@@ -275,12 +279,16 @@ class Database extends common.GrpcServiceObject {
275
279
  }
276
280
  reqOpts.session.creatorRole =
277
281
  options.databaseRole || this.databaseRole || null;
282
+ const headers = this.resourceHeader_;
283
+ if (this._getSpanner().routeToLeaderEnabled) {
284
+ (0, common_1.addLeaderAwareRoutingHeader)(headers);
285
+ }
278
286
  this.request({
279
287
  client: 'SpannerClient',
280
288
  method: 'createSession',
281
289
  reqOpts,
282
290
  gaxOpts: options.gaxOptions,
283
- headers: this.resourceHeader_,
291
+ headers: headers,
284
292
  }, (err, resp) => {
285
293
  if (err) {
286
294
  callback(err, null, resp);
@@ -1177,6 +1185,16 @@ class Database extends common.GrpcServiceObject {
1177
1185
  const databaseName = name.split('/').pop();
1178
1186
  return instanceName + '/databases/' + databaseName;
1179
1187
  }
1188
+ /**
1189
+ * Gets the Spanner object
1190
+ *
1191
+ * @private
1192
+ *
1193
+ * @returns {Spanner}
1194
+ */
1195
+ _getSpanner() {
1196
+ return this.instance.parent;
1197
+ }
1180
1198
  }
1181
1199
  exports.Database = Database;
1182
1200
  /*! Developer Documentation
@@ -46,11 +46,17 @@ export type GetInstanceConfigCallback = NormalCallback<IInstanceConfig>;
46
46
  export type GetInstanceConfigOperationsOptions = PagedOptionsWithFilter;
47
47
  export type GetInstanceConfigOperationsResponse = PagedResponse<IOperation, instanceAdmin.spanner.admin.instance.v1.IListInstanceConfigOperationsResponse>;
48
48
  export type GetInstanceConfigOperationsCallback = PagedCallback<IOperation, instanceAdmin.spanner.admin.instance.v1.IListInstanceConfigOperationsResponse>;
49
+ /**
50
+ * Session pool configuration options.
51
+ * @property {boolean} [routeToLeaderEnabled=True] If set to false leader aware routing will be disabled.
52
+ * Disabling leader aware routing would route all requests in RW/PDML transactions to any region.
53
+ */
49
54
  export interface SpannerOptions extends GrpcClientOptions {
50
55
  apiEndpoint?: string;
51
56
  servicePath?: string;
52
57
  port?: number;
53
58
  sslCreds?: grpc.ChannelCredentials;
59
+ routeToLeaderEnabled?: boolean;
54
60
  }
55
61
  export interface RequestConfig {
56
62
  client: string;
@@ -154,6 +160,7 @@ declare class Spanner extends GrpcService {
154
160
  resourceHeader_: {
155
161
  [k: string]: string;
156
162
  };
163
+ routeToLeaderEnabled: boolean;
157
164
  /**
158
165
  * Placeholder used to auto populate a column with the commit timestamp.
159
166
  * This can only be used for timestamp columns that have set the option
@@ -173,6 +173,10 @@ class Spanner extends service_1.GrpcService {
173
173
  packageJson: require('../../package.json'),
174
174
  };
175
175
  super(config, options);
176
+ this.routeToLeaderEnabled = false;
177
+ if (options.routeToLeaderEnabled === true) {
178
+ this.routeToLeaderEnabled = true;
179
+ }
176
180
  this.options = options;
177
181
  this.auth = new google_auth_library_1.GoogleAuth(this.options);
178
182
  this.clients_ = new Map();
@@ -246,5 +246,13 @@ export declare class Session extends common.GrpcServiceObject {
246
246
  * ```
247
247
  */
248
248
  static formatName_(databaseName: string, name: string): string;
249
+ /**
250
+ * Gets the Spanner object
251
+ *
252
+ * @private
253
+ *
254
+ * @returns {Spanner}
255
+ */
256
+ private _getSpanner;
249
257
  }
250
258
  export {};
@@ -227,12 +227,16 @@ class Session extends common.GrpcServiceObject {
227
227
  const reqOpts = {
228
228
  name: this.formattedName_,
229
229
  };
230
+ const headers = this.resourceHeader_;
231
+ if (this._getSpanner().routeToLeaderEnabled) {
232
+ (0, common_1.addLeaderAwareRoutingHeader)(headers);
233
+ }
230
234
  return this.request({
231
235
  client: 'SpannerClient',
232
236
  method: 'getSession',
233
237
  reqOpts,
234
238
  gaxOpts,
235
- headers: this.resourceHeader_,
239
+ headers: headers,
236
240
  }, (err, resp) => {
237
241
  if (resp) {
238
242
  resp.databaseRole = resp.creatorRole;
@@ -322,6 +326,16 @@ class Session extends common.GrpcServiceObject {
322
326
  const sessionName = name.split('/').pop();
323
327
  return databaseName + '/sessions/' + sessionName;
324
328
  }
329
+ /**
330
+ * Gets the Spanner object
331
+ *
332
+ * @private
333
+ *
334
+ * @returns {Spanner}
335
+ */
336
+ _getSpanner() {
337
+ return this.parent.parent.parent;
338
+ }
325
339
  }
326
340
  exports.Session = Session;
327
341
  /*! Developer Documentation
@@ -30,6 +30,7 @@ import { NormalCallback } from './common';
30
30
  import { google } from '../protos/protos';
31
31
  import IQueryOptions = google.spanner.v1.ExecuteSqlRequest.IQueryOptions;
32
32
  import IRequestOptions = google.spanner.v1.IRequestOptions;
33
+ import { Spanner } from '.';
33
34
  export type Rows = Array<Row | Json>;
34
35
  export interface TimestampBounds {
35
36
  strong?: boolean;
@@ -854,6 +855,14 @@ export declare class Snapshot extends EventEmitter {
854
855
  * @private
855
856
  */
856
857
  private _wrapWithIdWaiter;
858
+ /**
859
+ * Gets the Spanner object
860
+ *
861
+ * @private
862
+ *
863
+ * @returns {Spanner}
864
+ */
865
+ protected _getSpanner(): Spanner;
857
866
  }
858
867
  /**
859
868
  * Never use DML class directly. Instead, it should be extended upon
@@ -163,12 +163,18 @@ class Snapshot extends events_1.EventEmitter {
163
163
  if (this.requestOptions) {
164
164
  reqOpts.requestOptions = this.requestOptions;
165
165
  }
166
+ const headers = this.resourceHeader_;
167
+ if (this._getSpanner().routeToLeaderEnabled &&
168
+ (this._options.readWrite !== undefined ||
169
+ this._options.partitionedDml !== undefined)) {
170
+ (0, common_1.addLeaderAwareRoutingHeader)(headers);
171
+ }
166
172
  this.request({
167
173
  client: 'SpannerClient',
168
174
  method: 'beginTransaction',
169
175
  reqOpts,
170
176
  gaxOpts,
171
- headers: this.resourceHeader_,
177
+ headers: headers,
172
178
  }, (err, resp) => {
173
179
  if (err) {
174
180
  callback(err, resp);
@@ -375,6 +381,12 @@ class Snapshot extends events_1.EventEmitter {
375
381
  table,
376
382
  keySet,
377
383
  });
384
+ const headers = this.resourceHeader_;
385
+ if (this._getSpanner().routeToLeaderEnabled &&
386
+ (this._options.readWrite !== undefined ||
387
+ this._options.partitionedDml !== undefined)) {
388
+ (0, common_1.addLeaderAwareRoutingHeader)(headers);
389
+ }
378
390
  const makeRequest = (resumeToken) => {
379
391
  if (this.id && transaction.begin) {
380
392
  delete transaction.begin;
@@ -385,7 +397,7 @@ class Snapshot extends events_1.EventEmitter {
385
397
  method: 'streamingRead',
386
398
  reqOpts: Object.assign({}, reqOpts, { resumeToken }),
387
399
  gaxOpts: gaxOptions,
388
- headers: this.resourceHeader_,
400
+ headers: headers,
389
401
  });
390
402
  };
391
403
  return (_c = (0, partial_result_stream_1.partialResultStream)(this._wrapWithIdWaiter(makeRequest), {
@@ -623,6 +635,12 @@ class Snapshot extends events_1.EventEmitter {
623
635
  paramTypes,
624
636
  });
625
637
  };
638
+ const headers = this.resourceHeader_;
639
+ if (this._getSpanner().routeToLeaderEnabled &&
640
+ (this._options.readWrite !== undefined ||
641
+ this._options.partitionedDml !== undefined)) {
642
+ (0, common_1.addLeaderAwareRoutingHeader)(headers);
643
+ }
626
644
  const makeRequest = (resumeToken) => {
627
645
  if (!reqOpts || (this.id && !reqOpts.transaction.id)) {
628
646
  try {
@@ -639,7 +657,7 @@ class Snapshot extends events_1.EventEmitter {
639
657
  method: 'executeStreamingSql',
640
658
  reqOpts: Object.assign({}, reqOpts, { resumeToken }),
641
659
  gaxOpts: gaxOptions,
642
- headers: this.resourceHeader_,
660
+ headers: headers,
643
661
  });
644
662
  };
645
663
  return (0, partial_result_stream_1.partialResultStream)(this._wrapWithIdWaiter(makeRequest), {
@@ -796,6 +814,16 @@ class Snapshot extends events_1.EventEmitter {
796
814
  .on('data', chunk => this._idWaiter.emit('data', chunk))
797
815
  .once('end', () => this._idWaiter.emit('end')));
798
816
  }
817
+ /**
818
+ * Gets the Spanner object
819
+ *
820
+ * @private
821
+ *
822
+ * @returns {Spanner}
823
+ */
824
+ _getSpanner() {
825
+ return this.session.parent.parent.parent;
826
+ }
799
827
  }
800
828
  exports.Snapshot = Snapshot;
801
829
  /*! Developer Documentation
@@ -965,12 +993,16 @@ class Transaction extends Dml {
965
993
  seqno: this._seqno++,
966
994
  statements,
967
995
  };
996
+ const headers = this.resourceHeader_;
997
+ if (this._getSpanner().routeToLeaderEnabled) {
998
+ (0, common_1.addLeaderAwareRoutingHeader)(headers);
999
+ }
968
1000
  this.request({
969
1001
  client: 'SpannerClient',
970
1002
  method: 'executeBatchDml',
971
1003
  reqOpts,
972
1004
  gaxOpts,
973
- headers: this.resourceHeader_,
1005
+ headers: headers,
974
1006
  }, (err, resp) => {
975
1007
  var _a;
976
1008
  let batchUpdateError;
@@ -1037,12 +1069,16 @@ class Transaction extends Dml {
1037
1069
  reqOpts.returnCommitStats = options.returnCommitStats;
1038
1070
  }
1039
1071
  reqOpts.requestOptions = Object.assign(requestOptions || {}, this.requestOptions);
1072
+ const headers = this.resourceHeader_;
1073
+ if (this._getSpanner().routeToLeaderEnabled) {
1074
+ (0, common_1.addLeaderAwareRoutingHeader)(headers);
1075
+ }
1040
1076
  this.request({
1041
1077
  client: 'SpannerClient',
1042
1078
  method: 'commit',
1043
1079
  reqOpts,
1044
1080
  gaxOpts: gaxOpts,
1045
- headers: this.resourceHeader_,
1081
+ headers: headers,
1046
1082
  }, (err, resp) => {
1047
1083
  this.end();
1048
1084
  if (resp && resp.commitTimestamp) {
@@ -1294,12 +1330,16 @@ class Transaction extends Dml {
1294
1330
  session,
1295
1331
  transactionId,
1296
1332
  };
1333
+ const headers = this.resourceHeader_;
1334
+ if (this._getSpanner().routeToLeaderEnabled) {
1335
+ (0, common_1.addLeaderAwareRoutingHeader)(headers);
1336
+ }
1297
1337
  this.request({
1298
1338
  client: 'SpannerClient',
1299
1339
  method: 'rollback',
1300
1340
  reqOpts,
1301
1341
  gaxOpts,
1302
- headers: this.resourceHeader_,
1342
+ headers: headers,
1303
1343
  }, (err) => {
1304
1344
  this.end();
1305
1345
  callback(err);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@google-cloud/spanner",
3
3
  "description": "Cloud Spanner Client Library for Node.js",
4
- "version": "6.8.0",
4
+ "version": "6.9.0",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Google Inc.",
7
7
  "engines": {