-
Notifications
You must be signed in to change notification settings - Fork 101
/
session-pool.ts
876 lines (759 loc) · 21.8 KB
/
session-pool.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
/*!
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {EventEmitter} from 'events';
import * as is from 'is';
import PQueue from 'p-queue';
import trace = require('stack-trace');
import {Database} from './database';
import {Session, types} from './session';
import {Transaction} from './transaction';
import {NormalCallback} from './common';
/**
* @callback SessionPoolCloseCallback
* @param {?Error} error Closing error, if any.
*/
export interface SessionPoolCloseCallback {
(error?: SessionLeakError): void;
}
/**
* @callback GetReadSessionCallback
* @param {?Error} error Request error, if any.
* @param {Session} session The read-only session.
*/
export type GetReadSessionCallback = NormalCallback<Session>;
/**
* @callback GetWriteSessionCallback
* @param {?Error} error Request error, if any.
* @param {Session} session The read-write session.
* @param {Transaction} transaction The transaction object.
*/
export interface GetWriteSessionCallback {
(
err: Error | null,
session?: Session | null,
transaction?: Transaction | null
): void;
}
/**
* Interface for implementing custom session pooling logic, it should extend the
* {@link https://nodejs.org/api/events.html|EventEmitter} class and emit any
* asynchronous errors via an error event.
*
* @interface SessionPoolInterface
* @extends external:{@link https://nodejs.org/api/events.html|EventEmitter}
*/
/**
* @constructs SessionPoolInterface
* @param {Database} database The database to create a pool for.
*/
export interface SessionPoolInterface extends EventEmitter {
/**
* Will be called via {@link Database#close}. Indicates that the pool should
* perform any necessary teardown actions to its resources.
*
* @name SessionPoolInterface#close
* @param {SessionPoolCloseCallback} callback The callback function.
*/
close(callback: SessionPoolCloseCallback): void;
/**
* Will be called by the Database object, should be used to start creating
* sessions/etc.
*
* @name SessionPoolInterface#open
*/
open(): void;
/**
* When called returns a read-only session.
*
* @name SessionPoolInterface#getReadSession
* @param {GetReadSessionCallback} callback The callback function.
*/
getReadSession(callback: GetReadSessionCallback): void;
/**
* When called returns a read-write session with prepared transaction.
*
* @name SessionPoolInterface#getWriteSession
* @param {GetWriteSessionCallback} callback The callback function.
*/
getWriteSession(callback: GetWriteSessionCallback): void;
/**
* To be called when releasing a session back into the pool.
*
* @name SessionPoolInterface#release
* @param {Session} session The session to be released.
*/
release(session: Session): void;
}
/**
* Session pool configuration options.
*
* @typedef {object} SessionPoolOptions
* @property {number} [acquireTimeout=Infinity] Time in milliseconds before
* giving up trying to acquire a session. If the specified value is
* `Infinity`, a timeout will not occur.
* @property {number} [concurrency=10] How many concurrent requests the pool is
* allowed to make.
* @property {boolean} [fail=false] If set to true, an error will be thrown when
* there are no available sessions for a request.
* @property {number} [idlesAfter=10] How long until a resource becomes idle, in
* minutes.
* @property {number} [keepAlive=50] How often to ping idle sessions, in
* minutes. Must be less than 1 hour.
* @property {Object<string, string>} [labels] Labels to apply to any session
* created by the pool.
* @property {number} [max=100] Maximum number of resources to create at any
* given time.
* @property {number} [maxIdle=1] Maximum number of idle resources to keep in
* the pool at any given time.
* @property {number} [min=0] Minimum number of resources to keep in the pool at
* any given time.
* @property {number} [writes=0.0] Percentage of sessions to be pre-allocated as
* write sessions represented as a float.
*/
export interface SessionPoolOptions {
acquireTimeout?: number;
concurrency?: number;
fail?: boolean;
idlesAfter?: number;
keepAlive?: number;
labels?: {[label: string]: string};
max?: number;
maxIdle?: number;
min?: number;
writes?: number;
}
const DEFAULTS: SessionPoolOptions = {
acquireTimeout: Infinity,
concurrency: Infinity,
fail: false,
idlesAfter: 10,
keepAlive: 30,
labels: {},
max: 100,
maxIdle: 1,
min: 0,
writes: 0,
};
/**
* Error to be thrown when attempting to release unknown resources.
*
* @private
*/
export class ReleaseError extends Error {
resource: unknown;
constructor(resource: unknown) {
super('Unable to release unknown resource.');
this.resource = resource;
}
}
/**
* Error to be thrown when session leaks are detected.
*
* @private
*/
export class SessionLeakError extends Error {
messages: string[];
constructor(leaks: string[]) {
super(`${leaks.length} session leak(s) detected.`);
this.messages = leaks;
}
}
/**
* enum to capture errors that can appear from multiple places
*/
const enum errors {
Closed = 'Database is closed.',
Timeout = 'Timeout occurred while acquiring session.',
}
interface SessionInventory {
[types.ReadOnly]: Session[];
[types.ReadWrite]: Session[];
borrowed: Set<Session>;
}
/**
* Class used to manage connections to Spanner.
*
* **You don't need to use this class directly, connections will be handled for
* you.**
*
* @class
* @extends {EventEmitter}
*/
export class SessionPool extends EventEmitter implements SessionPoolInterface {
database: Database;
isOpen: boolean;
options: SessionPoolOptions;
_acquires: PQueue;
_evictHandle!: NodeJS.Timer;
_inventory: SessionInventory;
_onClose!: Promise<void>;
_pingHandle!: NodeJS.Timer;
_requests: PQueue;
_traces: Map<string, trace.StackFrame[]>;
/**
* Formats stack trace objects into Node-like stack trace.
*
* @param {object[]} trace The trace object.
* @return {string}
*/
static formatTrace(frames: trace.StackFrame[]): string {
const stack = frames.map(frame => {
const name = frame.getFunctionName() || frame.getMethodName();
const file = frame.getFileName();
const lineno = frame.getLineNumber();
const columnno = frame.getColumnNumber();
return ` at ${name} (${file}:${lineno}:${columnno})`;
});
return `Session leak detected!\n${stack.join('\n')}`;
}
/**
* Total number of available sessions.
* @type {number}
*/
get available(): number {
const reads = this._inventory[types.ReadOnly];
const writes = this._inventory[types.ReadWrite];
return reads.length + writes.length;
}
/**
* Total number of borrowed sessions.
* @type {number}
*/
get borrowed(): number {
return this._inventory.borrowed.size;
}
/**
* Flag to determine if Pool is full.
* @type {boolean}
*/
get isFull(): boolean {
return this.size >= this.options.max!;
}
/**
* Total number of read sessions.
* @type {number}
*/
get reads(): number {
const available = this._inventory[types.ReadOnly].length;
const borrowed = [...this._inventory.borrowed].filter(
session => session.type === types.ReadOnly
).length;
return available + borrowed;
}
/**
* Total size of pool.
* @type {number}
*/
get size(): number {
return this.available + this.borrowed;
}
/**
* Total number of write sessions.
* @type {number}
*/
get writes(): number {
const available = this._inventory[types.ReadWrite].length;
const borrowed = [...this._inventory.borrowed].filter(
session => session.type === types.ReadWrite
).length;
return available + borrowed;
}
/**
* @constructor
* @param {Database} database The DB instance.
* @param {SessionPoolOptions} [options] Configuration options.
*/
constructor(database: Database, options?: SessionPoolOptions) {
super();
this.isOpen = false;
this.database = database;
this.options = Object.assign({}, DEFAULTS, options);
const {writes} = this.options;
if (writes! < 0 || writes! > 1) {
throw new TypeError(
'Write percentage should be represented as a float between 0.0 and 1.0.'
);
}
this._inventory = {
[types.ReadOnly]: [],
[types.ReadWrite]: [],
borrowed: new Set(),
};
this._requests = new PQueue({
concurrency: this.options.concurrency!,
});
this._acquires = new PQueue({
concurrency: 1,
});
this._traces = new Map();
}
/**
* Closes and the pool.
*
* @emits SessionPool#close
* @param {SessionPoolCloseCallback} callback The callback function.
*/
close(callback: SessionPoolCloseCallback): void {
const sessions: Session[] = [
...this._inventory[types.ReadOnly],
...this._inventory[types.ReadWrite],
...this._inventory.borrowed,
];
this.isOpen = false;
this._stopHouseKeeping();
this.emit('close');
sessions.forEach(session => this._destroy(session));
this._requests.onIdle().then(() => {
const leaks = this._getLeaks();
let error;
this._inventory[types.ReadOnly] = [];
this._inventory[types.ReadWrite] = [];
this._inventory.borrowed.clear();
if (leaks.length) {
error = new SessionLeakError(leaks);
}
callback(error);
});
}
/**
* Retrieve a read session.
*
* @param {GetReadSessionCallback} callback The callback function.
*/
getReadSession(callback: GetReadSessionCallback): void {
this._acquire(types.ReadOnly).then(
session => callback(null, session),
callback
);
}
/**
* Retrieve a read/write session.
*
* @param {GetWriteSessionCallback} callback The callback function.
*/
getWriteSession(callback: GetWriteSessionCallback): void {
this._acquire(types.ReadWrite).then(
session => callback(null, session, session.txn!),
callback
);
}
/**
* Opens the pool, filling it to the configured number of read and write
* sessions.
*
* @emits SessionPool#open
* @return {Promise}
*/
open(): void {
this._onClose = new Promise(resolve => this.once('close', resolve));
this._startHouseKeeping();
this.isOpen = true;
this.emit('open');
this._fill();
}
/**
* Releases session back into the pool. If the session is a write session it
* will also prepare a new transaction before releasing it.
*
* @throws {Error} For unknown sessions.
* @emits SessionPool#available
* @emits SessionPool#error
* @param {Session} session The session to release.
*/
release(session: Session): void {
if (!this._inventory.borrowed.has(session)) {
throw new ReleaseError(session);
}
delete session.txn;
session.lastUsed = Date.now();
if (session.type === types.ReadOnly) {
this._release(session);
return;
}
this._prepareTransaction(session)
.catch(() => (session.type = types.ReadOnly))
.then(() => this._release(session));
}
/**
* Attempts to borrow a session from the pool.
*
* @private
*
* @param {string} type The desired type to borrow.
* @returns {Promise<Session>}
*/
async _acquire(type: types): Promise<Session> {
if (!this.isOpen) {
throw new Error(errors.Closed);
}
const startTime = Date.now();
const timeout = this.options.acquireTimeout;
// wrapping this logic in a function to call recursively if the session
// we end up with is already dead
const getSession = async (): Promise<Session> => {
const elapsed = Date.now() - startTime;
if (elapsed >= timeout!) {
throw new Error(errors.Timeout);
}
const session = await this._getSession(type, startTime);
if (this._isValidSession(session)) {
return session;
}
this._inventory.borrowed.delete(session);
return getSession();
};
const session = await this._acquires.add(getSession);
if (type === types.ReadWrite && session.type === types.ReadOnly) {
try {
await this._prepareTransaction(session);
} catch (e) {
this._release(session);
throw e;
}
}
this._traces.set(session.id, trace.get());
return session;
}
/**
* Moves a session into the borrowed group.
*
* @private
*
* @param {Session} session The session object.
*/
_borrow(session: Session): void {
const type = session.type!;
const index = this._inventory[type].indexOf(session);
this._inventory.borrowed.add(session);
this._inventory[type].splice(index, 1);
}
/**
* Borrows session from specific group.
*
* @private
*
* @param {string} type The desired session type.
* @return {Session}
*/
_borrowFrom(type: types): Session {
const session = this._inventory[type][0];
this._borrow(session);
return session;
}
/**
* Grabs the next available session.
*
* @private
*
* @param {string} type The desired session type.
* @returns {Promise<Session>}
*/
_borrowNextAvailableSession(type: types): Session {
const hasReads = !!this._inventory[types.ReadOnly].length;
if (type === types.ReadOnly && hasReads) {
return this._borrowFrom(types.ReadOnly);
}
const hasWrites = !!this._inventory[types.ReadWrite].length;
if (hasWrites) {
return this._borrowFrom(types.ReadWrite);
}
return this._borrowFrom(types.ReadOnly);
}
/**
* Attempts to create a session of a certain type.
*
* @private
*
* @param {string} type The desired type to create.
* @returns {Promise}
*/
async _createSession(type: types): Promise<void> {
const session = this.database.session();
const labels = this.options.labels!;
this._inventory.borrowed.add(session);
const createSession = async (): Promise<void> => {
await session.create({labels});
if (type === types.ReadWrite) {
try {
await this._prepareTransaction(session);
} catch (e) {
type = types.ReadOnly;
}
}
};
try {
await this._requests.add(createSession);
} catch (e) {
this._inventory.borrowed.delete(session);
throw e;
}
session.type = type;
session.lastUsed = Date.now();
this._inventory[type].push(session);
this._inventory.borrowed.delete(session);
}
/**
* Attempts to create a session but emits any errors that occur.
*
* @private
*
* @emits SessionPool#available
* @emits SessionPool#error
* @param {string} type The desired type to create.
* @returns {Promise}
*/
async _createSessionInBackground(type: types): Promise<void> {
try {
await this._createSession(type);
this.emit('available');
} catch (e) {
this.emit('error', e);
}
}
/**
* Attempts to delete a session, optionally creating a new one of the same
* type if the pool is still open and we're under the configured min value.
*
* @private
*
* @fires SessoinPool#error
* @param {Session} session The session to delete.
* @returns {Promise}
*/
async _destroy(session: Session): Promise<void> {
try {
await this._requests.add(() => session.delete());
} catch (e) {
this.emit('error', e);
}
}
/**
* Deletes idle sessions that exceed the maxIdle configuration.
*
* @private
*/
_evictIdleSessions(): void {
const {maxIdle, min} = this.options;
const size = this.size;
const idle = this._getIdleSessions();
let count = idle.length;
let evicted = 0;
while (count-- > maxIdle! && size - evicted++ > min!) {
const session = idle.pop();
if (!session) {
continue;
}
const type = session.type!;
const index = this._inventory[type].indexOf(session);
this._inventory[type].splice(index, 1);
this._destroy(session);
}
}
/**
* Fills the pool with the minimum number of sessions.
*
* @return {Promise}
*/
async _fill(): Promise<void> {
const requests: Array<Promise<void>> = [];
const minReadWrite = Math.floor(this.options.min! * this.options.writes!);
const neededReadWrite = Math.max(minReadWrite - this.writes, 0);
for (let i = 0; i < neededReadWrite; i++) {
requests.push(this._createSessionInBackground(types.ReadWrite));
}
const minReadOnly = Math.ceil(this.options.min! - minReadWrite);
const neededReadOnly = Math.max(minReadOnly - this.reads, 0);
for (let i = 0; i < neededReadOnly; i++) {
requests.push(this._createSessionInBackground(types.ReadOnly));
}
await Promise.all(requests);
}
/**
* Retrieves a list of all the idle sessions.
*
* @private
*
* @returns {Session[]}
*/
_getIdleSessions(): Session[] {
const idlesAfter = this.options.idlesAfter! * 60000;
const sessions: Session[] = [
...this._inventory[types.ReadOnly],
...this._inventory[types.ReadWrite],
];
return sessions.filter(session => {
return Date.now() - session.lastUsed! >= idlesAfter;
});
}
/**
* Returns stack traces for sessions that have not been released.
*
* @return {string[]}
*/
_getLeaks(): string[] {
return [...this._traces.values()].map(SessionPool.formatTrace);
}
/**
* Attempts to get a session of a specific type. If the type is unavailable it
* may try to use a different type.
*
* @private
*
* @param {string} type The desired session type.
* @param {number} startTime Timestamp to use when determining timeouts.
* @returns {Promise<Session>}
*/
async _getSession(type: types, startTime: number): Promise<Session> {
if (this.available) {
return this._borrowNextAvailableSession(type);
}
if (this.options.fail!) {
throw new Error('No resources available.');
}
let removeListener: Function;
const promises = [
this._onClose.then(() => {
throw new Error(errors.Closed);
}),
new Promise(resolve => {
this.once('available', resolve);
removeListener = this.removeListener.bind(this, 'available', resolve);
}),
];
const timeout = this.options.acquireTimeout;
if (!is.infinite(timeout!)) {
const elapsed = Date.now() - startTime!;
const remaining = timeout! - elapsed;
promises.push(
new Promise((_, reject) => {
const error = new Error(errors.Timeout);
setTimeout(reject.bind(null, error), remaining);
})
);
}
if (!this.isFull) {
promises.push(
new Promise((resolve, reject) => {
this._createSession(type).then(() => this.emit('available'), reject);
})
);
}
try {
await Promise.race(promises);
} catch (e) {
removeListener!();
throw e;
}
return this._borrowNextAvailableSession(type);
}
/**
* Checks to see whether or not session is expired.
*
* @param {Session} session The session to check.
* @returns {boolean}
*/
_isValidSession(session: Session): boolean {
// unpinged sessions only stay good for 1 hour
const MAX_DURATION = 60000 * 60;
return Date.now() - session.lastUsed! < MAX_DURATION;
}
/**
* Pings an individual session.
*
* @private
*
* @param {Session} session The session to ping.
* @returns {Promise}
*/
async _ping(session: Session): Promise<void> {
this._borrow(session);
if (!this._isValidSession(session)) {
this._inventory.borrowed.delete(session);
return;
}
try {
await session.keepAlive();
this.release(session);
} catch (e) {
this._inventory.borrowed.delete(session);
this._destroy(session);
}
}
/**
* Makes a keep alive request to all the idle sessions.
*
* @private
*
* @returns {Promise}
*/
async _pingIdleSessions(): Promise<void> {
const sessions = this._getIdleSessions();
const pings = sessions.map(session => this._ping(session));
await Promise.all(pings);
return this._fill();
}
/**
* Creates a transaction for a session.
*
* @private
*
* @param {Session} session The session object.
* @param {object} options The transaction options.
* @returns {Promise}
*/
async _prepareTransaction(session: Session): Promise<void> {
const transaction = session.transaction();
await transaction.begin();
session.txn = transaction;
}
/**
* Releases a session back into the pool.
*
* @private
*
* @fires SessionPool#available
* @param {Session} session The session object.
*/
_release(session: Session): void {
const type = session.type!;
this._inventory[type].unshift(session);
this._inventory.borrowed.delete(session);
this._traces.delete(session.id);
this.emit('available');
}
/**
* Starts housekeeping (pinging/evicting) of idle sessions.
*
* @private
*/
_startHouseKeeping(): void {
const evictRate = this.options.idlesAfter! * 60000;
this._evictHandle = setInterval(() => this._evictIdleSessions(), evictRate);
this._evictHandle.unref();
const pingRate = this.options.keepAlive! * 60000;
this._pingHandle = setInterval(() => this._pingIdleSessions(), pingRate);
this._pingHandle.unref();
}
/**
* Stops housekeeping.
*
* @private
*/
_stopHouseKeeping(): void {
clearInterval(this._pingHandle);
clearInterval(this._evictHandle);
}
}