@google-cloud/spanner 6.3.0 → 6.5.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 +22 -0
- package/README.md +3 -0
- package/build/protos/google/spanner/v1/result_set.proto +12 -0
- package/build/protos/google/spanner/v1/transaction.proto +39 -10
- package/build/protos/protos.d.ts +22 -0
- package/build/protos/protos.js +99 -3
- package/build/protos/protos.json +19 -1
- package/build/src/backup.d.ts +12 -12
- package/build/src/codec.d.ts +11 -1
- package/build/src/codec.js +34 -1
- package/build/src/common.d.ts +4 -4
- package/build/src/database.d.ts +30 -30
- package/build/src/database.js +10 -10
- package/build/src/index.d.ts +33 -15
- package/build/src/index.js +47 -26
- package/build/src/instance-config.d.ts +10 -10
- package/build/src/instance.d.ts +30 -30
- package/build/src/partial-result-stream.d.ts +1 -1
- package/build/src/session-pool.d.ts +64 -81
- package/build/src/session-pool.js +122 -251
- package/build/src/session.d.ts +8 -9
- package/build/src/table.d.ts +20 -20
- package/build/src/transaction-runner.d.ts +1 -1
- package/build/src/transaction-runner.js +4 -1
- package/build/src/transaction.d.ts +34 -11
- package/build/src/transaction.js +102 -13
- package/package.json +3 -3
|
@@ -31,7 +31,6 @@ const DEFAULTS = {
|
|
|
31
31
|
max: 100,
|
|
32
32
|
maxIdle: 1,
|
|
33
33
|
min: 25,
|
|
34
|
-
writes: 0,
|
|
35
34
|
incStep: 25,
|
|
36
35
|
};
|
|
37
36
|
/**
|
|
@@ -146,44 +145,6 @@ exports.isProjectIdNotSetInEnvironmentError = isProjectIdNotSetInEnvironmentErro
|
|
|
146
145
|
* @extends {EventEmitter}
|
|
147
146
|
*/
|
|
148
147
|
class SessionPool extends events_1.EventEmitter {
|
|
149
|
-
/**
|
|
150
|
-
* @constructor
|
|
151
|
-
* @param {Database} database The DB instance.
|
|
152
|
-
* @param {SessionPoolOptions} [options] Configuration options.
|
|
153
|
-
*/
|
|
154
|
-
constructor(database, options) {
|
|
155
|
-
super();
|
|
156
|
-
this._pending = 0;
|
|
157
|
-
this._pendingPrepare = 0;
|
|
158
|
-
this._numInProcessPrepare = 0;
|
|
159
|
-
if (options && options.min && options.max && options.min > options.max) {
|
|
160
|
-
throw new TypeError('Min sessions may not be greater than max sessions.');
|
|
161
|
-
}
|
|
162
|
-
this.isOpen = false;
|
|
163
|
-
this.database = database;
|
|
164
|
-
this.options = Object.assign({}, DEFAULTS, options);
|
|
165
|
-
this.options.min = Math.min(this.options.min, this.options.max);
|
|
166
|
-
const { writes } = this.options;
|
|
167
|
-
if (writes < 0 || writes > 1) {
|
|
168
|
-
throw new TypeError('Write percentage should be represented as a float between 0.0 and 1.0.');
|
|
169
|
-
}
|
|
170
|
-
this._inventory = {
|
|
171
|
-
["readonly" /* types.ReadOnly */]: [],
|
|
172
|
-
["readwrite" /* types.ReadWrite */]: [],
|
|
173
|
-
borrowed: new Set(),
|
|
174
|
-
};
|
|
175
|
-
this._waiters = {
|
|
176
|
-
["readonly" /* types.ReadOnly */]: 0,
|
|
177
|
-
["readwrite" /* types.ReadWrite */]: 0,
|
|
178
|
-
};
|
|
179
|
-
this._requests = new p_queue_1.default({
|
|
180
|
-
concurrency: this.options.concurrency,
|
|
181
|
-
});
|
|
182
|
-
this._acquires = new p_queue_1.default({
|
|
183
|
-
concurrency: 1,
|
|
184
|
-
});
|
|
185
|
-
this._traces = new Map();
|
|
186
|
-
}
|
|
187
148
|
/**
|
|
188
149
|
* Formats stack trace objects into Node-like stack trace.
|
|
189
150
|
*
|
|
@@ -205,24 +166,13 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
205
166
|
* @type {number}
|
|
206
167
|
*/
|
|
207
168
|
get available() {
|
|
208
|
-
|
|
209
|
-
const writes = this._inventory["readwrite" /* types.ReadWrite */];
|
|
210
|
-
return reads.length + writes.length;
|
|
169
|
+
return this._inventory.sessions.length;
|
|
211
170
|
}
|
|
212
|
-
/**
|
|
213
|
-
*
|
|
214
|
-
* @type {number}
|
|
171
|
+
/** @deprecated Starting from v6.5.0 the same session can be reused for
|
|
172
|
+
* different types of transactions.
|
|
215
173
|
*/
|
|
216
174
|
get currentWriteFraction() {
|
|
217
|
-
|
|
218
|
-
// There are no sessions in the pool. Define the current write fraction as
|
|
219
|
-
// 0.5. That means that if the user has configured a write fraction >= 0.5
|
|
220
|
-
// the first session to be created will be a write session, while it will
|
|
221
|
-
// otherwise be a read-only session.
|
|
222
|
-
return 0.5;
|
|
223
|
-
}
|
|
224
|
-
const writes = this._inventory["readwrite" /* types.ReadWrite */].length + this.pendingPrepare;
|
|
225
|
-
return writes / (this.available + this.pendingPrepare);
|
|
175
|
+
return 0;
|
|
226
176
|
}
|
|
227
177
|
/**
|
|
228
178
|
* Total number of borrowed sessions.
|
|
@@ -238,14 +188,9 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
238
188
|
get isFull() {
|
|
239
189
|
return this.size >= this.options.max;
|
|
240
190
|
}
|
|
241
|
-
/**
|
|
242
|
-
* Total number of read sessions.
|
|
243
|
-
* @type {number}
|
|
244
|
-
*/
|
|
191
|
+
/** @deprecated Use `size()` instead. */
|
|
245
192
|
get reads() {
|
|
246
|
-
|
|
247
|
-
const borrowed = [...this._inventory.borrowed].filter(session => session.type === "readonly" /* types.ReadOnly */).length;
|
|
248
|
-
return available + borrowed;
|
|
193
|
+
return this.size;
|
|
249
194
|
}
|
|
250
195
|
/**
|
|
251
196
|
* Total size of pool.
|
|
@@ -254,52 +199,64 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
254
199
|
get size() {
|
|
255
200
|
return this.available + this.borrowed;
|
|
256
201
|
}
|
|
257
|
-
/**
|
|
258
|
-
* Total number of write sessions.
|
|
259
|
-
* @type {number}
|
|
260
|
-
*/
|
|
202
|
+
/** @deprecated Use `size()` instead. */
|
|
261
203
|
get writes() {
|
|
262
|
-
|
|
263
|
-
const borrowed = [...this._inventory.borrowed].filter(session => session.type === "readwrite" /* types.ReadWrite */).length;
|
|
264
|
-
return available + borrowed;
|
|
204
|
+
return this.size;
|
|
265
205
|
}
|
|
266
|
-
/**
|
|
267
|
-
* Number of sessions currently being prepared for a read/write transaction
|
|
268
|
-
* before being released into the pool. This number does not include the
|
|
269
|
-
* number of sessions being prepared for a read/write transaction that have
|
|
270
|
-
* already been checked out of the pool.
|
|
271
|
-
* @type {number}
|
|
272
|
-
*/
|
|
206
|
+
/** @deprecated Starting v6.5.0 the pending prepare state is obsolete. */
|
|
273
207
|
get pendingPrepare() {
|
|
274
|
-
return
|
|
208
|
+
return 0;
|
|
275
209
|
}
|
|
276
210
|
/**
|
|
277
211
|
* Number of sessions being created or prepared for a read/write transaction.
|
|
278
212
|
* @type {number}
|
|
279
213
|
*/
|
|
280
214
|
get totalPending() {
|
|
281
|
-
return this._pending
|
|
215
|
+
return this._pending;
|
|
282
216
|
}
|
|
283
|
-
/**
|
|
284
|
-
* Current number of waiters for a read-only session.
|
|
285
|
-
* @type {number}
|
|
286
|
-
*/
|
|
217
|
+
/** @deprecated Use totalWaiters instead. */
|
|
287
218
|
get numReadWaiters() {
|
|
288
|
-
return this.
|
|
219
|
+
return this.totalWaiters;
|
|
289
220
|
}
|
|
290
|
-
/**
|
|
291
|
-
* Current number of waiters for a read/write session.
|
|
292
|
-
* @type {number}
|
|
293
|
-
*/
|
|
221
|
+
/** @deprecated Use totalWaiters instead. */
|
|
294
222
|
get numWriteWaiters() {
|
|
295
|
-
return this.
|
|
223
|
+
return this.totalWaiters;
|
|
296
224
|
}
|
|
297
225
|
/**
|
|
298
226
|
* Sum of read and write waiters.
|
|
299
227
|
* @type {number}
|
|
300
228
|
*/
|
|
301
229
|
get totalWaiters() {
|
|
302
|
-
return this.
|
|
230
|
+
return this._waiters;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* @constructor
|
|
234
|
+
* @param {Database} database The DB instance.
|
|
235
|
+
* @param {SessionPoolOptions} [options] Configuration options.
|
|
236
|
+
*/
|
|
237
|
+
constructor(database, options) {
|
|
238
|
+
super();
|
|
239
|
+
this._pending = 0;
|
|
240
|
+
this._waiters = 0;
|
|
241
|
+
if (options && options.min && options.max && options.min > options.max) {
|
|
242
|
+
throw new TypeError('Min sessions may not be greater than max sessions.');
|
|
243
|
+
}
|
|
244
|
+
this.isOpen = false;
|
|
245
|
+
this.database = database;
|
|
246
|
+
this.options = Object.assign({}, DEFAULTS, options);
|
|
247
|
+
this.options.min = Math.min(this.options.min, this.options.max);
|
|
248
|
+
this._inventory = {
|
|
249
|
+
sessions: [],
|
|
250
|
+
borrowed: new Set(),
|
|
251
|
+
};
|
|
252
|
+
this._waiters = 0;
|
|
253
|
+
this._requests = new p_queue_1.default({
|
|
254
|
+
concurrency: this.options.concurrency,
|
|
255
|
+
});
|
|
256
|
+
this._acquires = new p_queue_1.default({
|
|
257
|
+
concurrency: 1,
|
|
258
|
+
});
|
|
259
|
+
this._traces = new Map();
|
|
303
260
|
}
|
|
304
261
|
/**
|
|
305
262
|
* Closes and the pool.
|
|
@@ -309,8 +266,7 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
309
266
|
*/
|
|
310
267
|
close(callback) {
|
|
311
268
|
const sessions = [
|
|
312
|
-
...this._inventory
|
|
313
|
-
...this._inventory["readwrite" /* types.ReadWrite */],
|
|
269
|
+
...this._inventory.sessions,
|
|
314
270
|
...this._inventory.borrowed,
|
|
315
271
|
];
|
|
316
272
|
this.isOpen = false;
|
|
@@ -320,8 +276,7 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
320
276
|
this._requests.onIdle().then(() => {
|
|
321
277
|
const leaks = this._getLeaks();
|
|
322
278
|
let error;
|
|
323
|
-
this._inventory
|
|
324
|
-
this._inventory["readwrite" /* types.ReadWrite */] = [];
|
|
279
|
+
this._inventory.sessions = [];
|
|
325
280
|
this._inventory.borrowed.clear();
|
|
326
281
|
if (leaks.length) {
|
|
327
282
|
error = new SessionLeakError(leaks);
|
|
@@ -332,18 +287,28 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
332
287
|
/**
|
|
333
288
|
* Retrieve a read session.
|
|
334
289
|
*
|
|
290
|
+
* @deprecated Use getSession instead.
|
|
335
291
|
* @param {GetReadSessionCallback} callback The callback function.
|
|
336
292
|
*/
|
|
337
293
|
getReadSession(callback) {
|
|
338
|
-
this.
|
|
294
|
+
this.getSession((error, session) => callback(error, session));
|
|
339
295
|
}
|
|
340
296
|
/**
|
|
341
297
|
* Retrieve a read/write session.
|
|
342
298
|
*
|
|
299
|
+
* @deprecated use getSession instead.
|
|
343
300
|
* @param {GetWriteSessionCallback} callback The callback function.
|
|
344
301
|
*/
|
|
345
302
|
getWriteSession(callback) {
|
|
346
|
-
this.
|
|
303
|
+
this.getSession(callback);
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Retrieve a session.
|
|
307
|
+
*
|
|
308
|
+
* @param {GetSessionCallback} callback The callback function.
|
|
309
|
+
*/
|
|
310
|
+
getSession(callback) {
|
|
311
|
+
this._acquire().then(session => callback(null, session, session.txn), callback);
|
|
347
312
|
}
|
|
348
313
|
/**
|
|
349
314
|
* Opens the pool, filling it to the configured number of read and write
|
|
@@ -371,14 +336,14 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
371
336
|
});
|
|
372
337
|
}
|
|
373
338
|
/**
|
|
374
|
-
* Releases session back into the pool.
|
|
375
|
-
* will also prepare a new transaction before releasing it.
|
|
339
|
+
* Releases session back into the pool.
|
|
376
340
|
*
|
|
377
341
|
* @throws {Error} For unknown sessions.
|
|
378
342
|
* @emits SessionPool#available
|
|
379
343
|
* @emits SessionPool#error
|
|
380
|
-
* @
|
|
381
|
-
* @
|
|
344
|
+
* @fires SessionPool#session-available
|
|
345
|
+
* @fires @deprecated SessionPool#readonly-available
|
|
346
|
+
* @fires @deprecated SessionPool#readwrite-available
|
|
382
347
|
* @param {Session} session The session to release.
|
|
383
348
|
*/
|
|
384
349
|
release(session) {
|
|
@@ -395,45 +360,23 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
395
360
|
return;
|
|
396
361
|
}
|
|
397
362
|
session.lastError = undefined;
|
|
398
|
-
// Release it into the pool as a read-only session in the following cases:
|
|
399
|
-
// 1. There are more waiters than there are sessions available. Releasing it
|
|
400
|
-
// into the pool will ensure that a waiter will be unblocked as soon as
|
|
401
|
-
// possible.
|
|
402
|
-
// 2. The user has not set a write fraction, but this session has been used
|
|
403
|
-
// as a read/write session. This is an indication that the application
|
|
404
|
-
// needs read/write sessions, and the pool should try to keep that number
|
|
405
|
-
// of read/write sessions dynamically.
|
|
406
|
-
// 3. The user has set a write fraction and that fraction has been reached.
|
|
407
|
-
const shouldBeWrite = (session.type === "readwrite" /* types.ReadWrite */ && this.options.writes === 0) ||
|
|
408
|
-
(this.options.writes > 0 &&
|
|
409
|
-
this.currentWriteFraction < this.options.writes);
|
|
410
|
-
if (this.totalWaiters > this.available || !shouldBeWrite) {
|
|
411
|
-
session.type = "readonly" /* types.ReadOnly */;
|
|
412
|
-
this._release(session);
|
|
413
|
-
return;
|
|
414
|
-
}
|
|
415
363
|
// Delete the trace associated with this session to mark the session as checked
|
|
416
364
|
// back into the pool. This will prevent the session to be marked as leaked if
|
|
417
365
|
// the pool is closed while the session is being prepared.
|
|
418
366
|
this._traces.delete(session.id);
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
.then(() => {
|
|
424
|
-
this._pendingPrepare--;
|
|
425
|
-
this._release(session);
|
|
426
|
-
});
|
|
367
|
+
// Release it into the pool as a session if there are more waiters than
|
|
368
|
+
// there are sessions available. Releasing it will unblock a waiter as soon
|
|
369
|
+
// as possible.
|
|
370
|
+
this._release(session);
|
|
427
371
|
}
|
|
428
372
|
/**
|
|
429
373
|
* Attempts to borrow a session from the pool.
|
|
430
374
|
*
|
|
431
375
|
* @private
|
|
432
376
|
*
|
|
433
|
-
* @param {string} type The desired type to borrow.
|
|
434
377
|
* @returns {Promise<Session>}
|
|
435
378
|
*/
|
|
436
|
-
async _acquire(
|
|
379
|
+
async _acquire() {
|
|
437
380
|
if (!this.isOpen) {
|
|
438
381
|
throw new google_gax_1.GoogleError("Database is closed." /* errors.Closed */);
|
|
439
382
|
}
|
|
@@ -448,7 +391,7 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
448
391
|
if (elapsed >= timeout) {
|
|
449
392
|
throw new google_gax_1.GoogleError("Timeout occurred while acquiring session." /* errors.Timeout */);
|
|
450
393
|
}
|
|
451
|
-
const session = await this._getSession(
|
|
394
|
+
const session = await this._getSession(startTime);
|
|
452
395
|
if (this._isValidSession(session)) {
|
|
453
396
|
return session;
|
|
454
397
|
}
|
|
@@ -456,26 +399,7 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
456
399
|
return getSession();
|
|
457
400
|
};
|
|
458
401
|
const session = await this._acquires.add(getSession);
|
|
459
|
-
|
|
460
|
-
this._numInProcessPrepare++;
|
|
461
|
-
try {
|
|
462
|
-
await this._prepareTransaction(session);
|
|
463
|
-
}
|
|
464
|
-
catch (e) {
|
|
465
|
-
if (isSessionNotFoundError(e)) {
|
|
466
|
-
this._inventory.borrowed.delete(session);
|
|
467
|
-
}
|
|
468
|
-
else {
|
|
469
|
-
this._release(session);
|
|
470
|
-
}
|
|
471
|
-
throw e;
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
// Mark the session as the type that was requested. This ensures that the
|
|
475
|
-
// fraction of read/write sessions in the pool is kept aligned with the
|
|
476
|
-
// actual need if the user has not specified a write fraction in the session
|
|
477
|
-
// pool options.
|
|
478
|
-
session.type = type;
|
|
402
|
+
this._prepareTransaction(session);
|
|
479
403
|
this._traces.set(session.id, frames);
|
|
480
404
|
return session;
|
|
481
405
|
}
|
|
@@ -487,22 +411,19 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
487
411
|
* @param {Session} session The session object.
|
|
488
412
|
*/
|
|
489
413
|
_borrow(session) {
|
|
490
|
-
const
|
|
491
|
-
const index = this._inventory[type].indexOf(session);
|
|
414
|
+
const index = this._inventory.sessions.indexOf(session);
|
|
492
415
|
this._inventory.borrowed.add(session);
|
|
493
|
-
this._inventory
|
|
416
|
+
this._inventory.sessions.splice(index, 1);
|
|
494
417
|
}
|
|
495
418
|
/**
|
|
496
|
-
* Borrows the first session from
|
|
497
|
-
* actually contains a session of the desired type.
|
|
419
|
+
* Borrows the first session from the inventory.
|
|
498
420
|
*
|
|
499
421
|
* @private
|
|
500
422
|
*
|
|
501
|
-
* @param {string} type The desired session type.
|
|
502
423
|
* @return {Session}
|
|
503
424
|
*/
|
|
504
|
-
_borrowFrom(
|
|
505
|
-
const session = this._inventory
|
|
425
|
+
_borrowFrom() {
|
|
426
|
+
const session = this._inventory.sessions.pop();
|
|
506
427
|
this._inventory.borrowed.add(session);
|
|
507
428
|
return session;
|
|
508
429
|
}
|
|
@@ -511,67 +432,53 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
511
432
|
*
|
|
512
433
|
* @private
|
|
513
434
|
*
|
|
514
|
-
* @param {string} type The desired session type.
|
|
515
435
|
* @returns {Promise<Session>}
|
|
516
436
|
*/
|
|
517
|
-
_borrowNextAvailableSession(
|
|
518
|
-
|
|
519
|
-
if (type === "readonly" /* types.ReadOnly */ && hasReads) {
|
|
520
|
-
return this._borrowFrom("readonly" /* types.ReadOnly */);
|
|
521
|
-
}
|
|
522
|
-
const hasWrites = !!this._inventory["readwrite" /* types.ReadWrite */].length;
|
|
523
|
-
if (hasWrites) {
|
|
524
|
-
return this._borrowFrom("readwrite" /* types.ReadWrite */);
|
|
525
|
-
}
|
|
526
|
-
return this._borrowFrom("readonly" /* types.ReadOnly */);
|
|
437
|
+
_borrowNextAvailableSession() {
|
|
438
|
+
return this._borrowFrom();
|
|
527
439
|
}
|
|
528
440
|
/**
|
|
529
|
-
* Attempts to create a single session
|
|
441
|
+
* Attempts to create a single session.
|
|
530
442
|
*
|
|
531
443
|
* @private
|
|
532
444
|
*
|
|
533
|
-
* @param {string} type The desired type to create.
|
|
534
445
|
* @returns {Promise}
|
|
535
446
|
*/
|
|
536
|
-
_createSession(
|
|
537
|
-
|
|
538
|
-
const options = { [kind]: 1 };
|
|
539
|
-
return this._createSessions(options);
|
|
447
|
+
_createSession() {
|
|
448
|
+
return this._createSessions(1);
|
|
540
449
|
}
|
|
541
450
|
/**
|
|
542
|
-
* Batch creates sessions
|
|
451
|
+
* Batch creates sessions.
|
|
543
452
|
*
|
|
544
453
|
* @private
|
|
545
454
|
*
|
|
546
|
-
* @param {
|
|
455
|
+
* @param {number} [amount] Config specifying how many sessions to create.
|
|
547
456
|
* @returns {Promise}
|
|
548
457
|
* @emits SessionPool#createError
|
|
549
458
|
*/
|
|
550
|
-
async _createSessions(
|
|
459
|
+
async _createSessions(amount) {
|
|
551
460
|
const labels = this.options.labels;
|
|
552
|
-
|
|
553
|
-
if (needed <= 0) {
|
|
461
|
+
if (amount <= 0) {
|
|
554
462
|
return;
|
|
555
463
|
}
|
|
556
|
-
this._pending +=
|
|
464
|
+
this._pending += amount;
|
|
557
465
|
// while we can request as many sessions be created as we want, the backend
|
|
558
466
|
// will return at most 100 at a time, hence the need for a while loop.
|
|
559
|
-
while (
|
|
467
|
+
while (amount > 0) {
|
|
560
468
|
let sessions = null;
|
|
561
469
|
try {
|
|
562
470
|
[sessions] = await this.database.batchCreateSessions({
|
|
563
|
-
count:
|
|
471
|
+
count: amount,
|
|
564
472
|
labels,
|
|
565
473
|
});
|
|
566
|
-
|
|
474
|
+
amount -= sessions.length;
|
|
567
475
|
}
|
|
568
476
|
catch (e) {
|
|
569
|
-
this._pending -=
|
|
477
|
+
this._pending -= amount;
|
|
570
478
|
this.emit('createError', e);
|
|
571
479
|
throw e;
|
|
572
480
|
}
|
|
573
481
|
sessions.forEach((session) => {
|
|
574
|
-
session.type = writes-- > 0 ? "readwrite" /* types.ReadWrite */ : "readonly" /* types.ReadOnly */;
|
|
575
482
|
setImmediate(() => {
|
|
576
483
|
this._inventory.borrowed.add(session);
|
|
577
484
|
this._pending -= 1;
|
|
@@ -614,9 +521,8 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
614
521
|
if (!session) {
|
|
615
522
|
continue;
|
|
616
523
|
}
|
|
617
|
-
const
|
|
618
|
-
|
|
619
|
-
this._inventory[type].splice(index, 1);
|
|
524
|
+
const index = this._inventory.sessions.indexOf(session);
|
|
525
|
+
this._inventory.sessions.splice(index, 1);
|
|
620
526
|
this._destroy(session);
|
|
621
527
|
}
|
|
622
528
|
}
|
|
@@ -631,7 +537,7 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
631
537
|
return;
|
|
632
538
|
}
|
|
633
539
|
try {
|
|
634
|
-
await this._createSessions(
|
|
540
|
+
await this._createSessions(needed);
|
|
635
541
|
}
|
|
636
542
|
catch (e) {
|
|
637
543
|
this.emit('error', e);
|
|
@@ -646,10 +552,7 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
646
552
|
*/
|
|
647
553
|
_getIdleSessions() {
|
|
648
554
|
const idlesAfter = this.options.idlesAfter * 60000;
|
|
649
|
-
const sessions =
|
|
650
|
-
...this._inventory["readonly" /* types.ReadOnly */],
|
|
651
|
-
...this._inventory["readwrite" /* types.ReadWrite */],
|
|
652
|
-
];
|
|
555
|
+
const sessions = this._inventory.sessions;
|
|
653
556
|
return sessions.filter(session => {
|
|
654
557
|
return Date.now() - session.lastUsed >= idlesAfter;
|
|
655
558
|
});
|
|
@@ -663,48 +566,31 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
663
566
|
return [...this._traces.values()].map(SessionPool.formatTrace);
|
|
664
567
|
}
|
|
665
568
|
/**
|
|
666
|
-
* Returns true if the pool has a
|
|
667
|
-
* type, i.e. if a read-only session is requested, it returns true if the
|
|
668
|
-
* pool has a read-only or a read/write session. If a read/write session is
|
|
669
|
-
* requested, the method only returns true if the pool has a read/write
|
|
670
|
-
* session available.
|
|
671
|
-
* @param type The type of session.
|
|
569
|
+
* Returns true if the pool has a usable session.
|
|
672
570
|
* @private
|
|
673
571
|
*/
|
|
674
|
-
_hasSessionUsableFor(
|
|
675
|
-
return
|
|
676
|
-
this._inventory["readwrite" /* types.ReadWrite */].length > 0);
|
|
572
|
+
_hasSessionUsableFor() {
|
|
573
|
+
return this._inventory.sessions.length > 0;
|
|
677
574
|
}
|
|
678
575
|
/**
|
|
679
|
-
* Attempts to get a session
|
|
680
|
-
* may try to use a different type.
|
|
576
|
+
* Attempts to get a session.
|
|
681
577
|
*
|
|
682
578
|
* @private
|
|
683
579
|
*
|
|
684
|
-
* @param {string} type The desired session type.
|
|
685
580
|
* @param {number} startTime Timestamp to use when determining timeouts.
|
|
686
581
|
* @returns {Promise<Session>}
|
|
687
582
|
*/
|
|
688
|
-
async _getSession(
|
|
689
|
-
if (this._hasSessionUsableFor(
|
|
690
|
-
return this._borrowNextAvailableSession(
|
|
691
|
-
}
|
|
692
|
-
// If a read/write session is requested and the pool has a read-only session
|
|
693
|
-
// available, we should return that session unless there is a session
|
|
694
|
-
// currently being prepared for read/write that is not already claimed by
|
|
695
|
-
// another requester.
|
|
696
|
-
if (type === "readwrite" /* types.ReadWrite */ &&
|
|
697
|
-
this._hasSessionUsableFor("readonly" /* types.ReadOnly */) &&
|
|
698
|
-
this.numWriteWaiters >= this.pendingPrepare) {
|
|
699
|
-
return this._borrowNextAvailableSession(type);
|
|
583
|
+
async _getSession(startTime) {
|
|
584
|
+
if (this._hasSessionUsableFor()) {
|
|
585
|
+
return this._borrowNextAvailableSession();
|
|
700
586
|
}
|
|
701
587
|
if (this.isFull && this.options.fail) {
|
|
702
588
|
throw new SessionPoolExhaustedError(this._getLeaks());
|
|
703
589
|
}
|
|
704
590
|
let removeOnceCloseListener;
|
|
705
591
|
let removeListener;
|
|
706
|
-
// Wait for
|
|
707
|
-
const availableEvent =
|
|
592
|
+
// Wait for a session to become available.
|
|
593
|
+
const availableEvent = 'session-available';
|
|
708
594
|
const promises = [
|
|
709
595
|
new Promise((_, reject) => {
|
|
710
596
|
const onceCloseListener = () => reject(new google_gax_1.GoogleError("Database is closed." /* errors.Closed */));
|
|
@@ -730,23 +616,23 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
730
616
|
// Only create a new session if there are more waiters than sessions already
|
|
731
617
|
// being created. The current requester will be waiter number _numWaiters+1.
|
|
732
618
|
if (!this.isFull && this.totalPending <= this.totalWaiters) {
|
|
733
|
-
let
|
|
619
|
+
let amount = this.options.incStep
|
|
734
620
|
? this.options.incStep
|
|
735
621
|
: DEFAULTS.incStep;
|
|
736
622
|
// Create additional sessions if the configured minimum has not been reached.
|
|
737
623
|
const min = this.options.min ? this.options.min : 0;
|
|
738
|
-
if (this.size + this.totalPending +
|
|
739
|
-
|
|
624
|
+
if (this.size + this.totalPending + amount < min) {
|
|
625
|
+
amount = min - this.size - this.totalPending;
|
|
740
626
|
}
|
|
741
627
|
// Make sure we don't create more sessions than the pool should have.
|
|
742
|
-
if (
|
|
743
|
-
|
|
628
|
+
if (amount + this.size > this.options.max) {
|
|
629
|
+
amount = this.options.max - this.size;
|
|
744
630
|
}
|
|
745
|
-
if (
|
|
746
|
-
this._pending +=
|
|
631
|
+
if (amount > 0) {
|
|
632
|
+
this._pending += amount;
|
|
747
633
|
promises.push(new Promise((_, reject) => {
|
|
748
|
-
this._pending -=
|
|
749
|
-
this._createSessions(
|
|
634
|
+
this._pending -= amount;
|
|
635
|
+
this._createSessions(amount).catch(reject);
|
|
750
636
|
}));
|
|
751
637
|
}
|
|
752
638
|
}
|
|
@@ -756,17 +642,17 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
756
642
|
removeErrorListener = this.removeListener.bind(this, 'createError', reject);
|
|
757
643
|
}));
|
|
758
644
|
try {
|
|
759
|
-
this._waiters
|
|
645
|
+
this._waiters++;
|
|
760
646
|
await Promise.race(promises);
|
|
761
647
|
}
|
|
762
648
|
finally {
|
|
763
|
-
this._waiters
|
|
649
|
+
this._waiters--;
|
|
764
650
|
removeOnceCloseListener();
|
|
765
651
|
removeListener();
|
|
766
652
|
removeErrorListener();
|
|
767
653
|
removeTimeoutListener();
|
|
768
654
|
}
|
|
769
|
-
return this._borrowNextAvailableSession(
|
|
655
|
+
return this._borrowNextAvailableSession();
|
|
770
656
|
}
|
|
771
657
|
/**
|
|
772
658
|
* Checks to see whether or not session is expired.
|
|
@@ -822,11 +708,9 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
822
708
|
*
|
|
823
709
|
* @param {Session} session The session object.
|
|
824
710
|
* @param {object} options The transaction options.
|
|
825
|
-
* @returns {Promise}
|
|
826
711
|
*/
|
|
827
|
-
|
|
712
|
+
_prepareTransaction(session) {
|
|
828
713
|
const transaction = session.transaction(session.parent.queryOptions_);
|
|
829
|
-
await transaction.begin();
|
|
830
714
|
session.txn = transaction;
|
|
831
715
|
}
|
|
832
716
|
/**
|
|
@@ -835,32 +719,19 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
835
719
|
* @private
|
|
836
720
|
*
|
|
837
721
|
* @fires SessionPool#available
|
|
838
|
-
* @fires SessionPool#
|
|
839
|
-
* @fires SessionPool#
|
|
722
|
+
* @fires SessionPool#session-available
|
|
723
|
+
* @fires @deprecated SessionPool#readonly-available
|
|
724
|
+
* @fires @deprecated SessionPool#readwrite-available
|
|
840
725
|
* @param {Session} session The session object.
|
|
841
726
|
*/
|
|
842
727
|
_release(session) {
|
|
843
|
-
|
|
844
|
-
this._inventory[type].push(session);
|
|
728
|
+
this._inventory.sessions.push(session);
|
|
845
729
|
this._inventory.borrowed.delete(session);
|
|
846
730
|
this._traces.delete(session.id);
|
|
847
731
|
this.emit('available');
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
!this.numReadWaiters &&
|
|
852
|
-
this.numWriteWaiters) {
|
|
853
|
-
emitType = "readwrite" /* types.ReadWrite */;
|
|
854
|
-
}
|
|
855
|
-
else if (type === "readwrite" /* types.ReadWrite */ &&
|
|
856
|
-
!this.numWriteWaiters &&
|
|
857
|
-
this.numReadWaiters) {
|
|
858
|
-
emitType = "readonly" /* types.ReadOnly */;
|
|
859
|
-
}
|
|
860
|
-
else {
|
|
861
|
-
emitType = type;
|
|
862
|
-
}
|
|
863
|
-
this.emit(emitType + '-available');
|
|
732
|
+
this.emit('session-available');
|
|
733
|
+
this.emit('readonly-available');
|
|
734
|
+
this.emit('readwrite-available');
|
|
864
735
|
}
|
|
865
736
|
/**
|
|
866
737
|
* Starts housekeeping (pinging/evicting) of idle sessions.
|