Skip to content

Commit c508d1b

Browse files
authored
change: pool.ts: add even more logging (#2426)
1 parent 4a6aafa commit c508d1b

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

dev/src/pool.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {GoogleError} from 'google-gax';
1818
import * as assert from 'assert';
1919

2020
import {logger} from './logger';
21-
import {Deferred, requestTag as generateClientId} from './util';
21+
import {Deferred, requestTag as generateTag} from './util';
2222

2323
export const CLIENT_TERMINATED_ERROR_MSG =
2424
'The client has already been terminated';
@@ -78,6 +78,11 @@ export class ClientPool<T extends object> {
7878
*/
7979
private readonly terminateDeferred = new Deferred<void>();
8080

81+
/**
82+
* A unique identifier for this object, for inclusion in log messages.
83+
*/
84+
private readonly instanceId = 'cpl' + generateTag();
85+
8186
/**
8287
* @param concurrentOperationLimit The number of operations that each client
8388
* can handle.
@@ -137,16 +142,16 @@ export class ClientPool<T extends object> {
137142
if (selectedClient) {
138143
const selectedClientId = this.clientIdByClient.get(selectedClient);
139144
logger(
140-
'ClientPool.acquire',
145+
`ClientPool[${this.instanceId}].acquire`,
141146
requestTag,
142147
'Re-using existing client [%s] with %s remaining operations',
143148
selectedClientId,
144149
this.concurrentOperationLimit - selectedClientRequestCount
145150
);
146151
} else {
147-
const newClientId = 'cli' + generateClientId();
152+
const newClientId = 'cli' + generateTag();
148153
logger(
149-
'ClientPool.acquire',
154+
`ClientPool[${this.instanceId}].acquire`,
150155
requestTag,
151156
'Creating a new client [%s] (requiresGrpc: %s)',
152157
newClientId,
@@ -190,7 +195,7 @@ export class ClientPool<T extends object> {
190195

191196
const gcDetermination = this.shouldGarbageCollectClient(client);
192197
logger(
193-
'ClientPool.release',
198+
`ClientPool[${this.instanceId}].release`,
194199
requestTag,
195200
'Releasing client [%s] (gc=%s)',
196201
clientId,
@@ -202,22 +207,23 @@ export class ClientPool<T extends object> {
202207
}
203208

204209
logger(
205-
'ClientPool.release',
210+
`ClientPool[${this.instanceId}].release`,
206211
requestTag,
207212
'Garbage collecting client [%s] (%s)',
208213
clientId,
209214
this.lazyLogStringForAllClientIds
210215
);
211216

212-
this.activeClients.delete(client);
217+
const activeClientDeleted = this.activeClients.delete(client);
213218
this.failedClients.delete(client);
214219
await this.clientDestructor(client);
215220

216221
logger(
217-
'ClientPool.release',
222+
`ClientPool[${this.instanceId}].release`,
218223
requestTag,
219-
'Garbage collected client [%s] (%s)',
224+
'Garbage collected client [%s] activeClientDeleted=%s (%s)',
220225
clientId,
226+
activeClientDeleted,
221227
this.lazyLogStringForAllClientIds
222228
);
223229
}
@@ -368,14 +374,20 @@ export class ClientPool<T extends object> {
368374
// Wait for all pending operations to complete before terminating.
369375
if (this.opCount > 0) {
370376
logger(
371-
'ClientPool.terminate',
377+
`ClientPool[${this.instanceId}].terminate`,
372378
/* requestTag= */ null,
373379
'Waiting for %s pending operations to complete before terminating (%s)',
374380
this.opCount,
375381
this.lazyLogStringForAllClientIds
376382
);
377383
await this.terminateDeferred.promise;
378384
}
385+
logger(
386+
`ClientPool[${this.instanceId}].terminate`,
387+
/* requestTag= */ null,
388+
'Closing all active clients (%s)',
389+
this.lazyLogStringForAllClientIds
390+
);
379391
for (const [client] of this.activeClients) {
380392
this.activeClients.delete(client);
381393
await this.clientDestructor(client);
@@ -389,12 +401,12 @@ export class ClientPool<T extends object> {
389401
* failed clients.
390402
*/
391403
class LazyLogStringForAllClientIds<T extends object> {
392-
private readonly activeClients: Map<T, unknown>;
404+
private readonly activeClients: Map<T, {activeRequestCount: number}>;
393405
private readonly failedClients: Set<T>;
394406
private readonly clientIdByClient: WeakMap<T, string>;
395407

396408
constructor(config: {
397-
activeClients: Map<T, unknown>;
409+
activeClients: Map<T, {activeRequestCount: number}>;
398410
failedClients: Set<T>;
399411
clientIdByClient: WeakMap<T, string>;
400412
}) {
@@ -404,22 +416,27 @@ class LazyLogStringForAllClientIds<T extends object> {
404416
}
405417

406418
toString(): string {
419+
const activeClientsDescription = Array.from(this.activeClients.entries())
420+
.map(
421+
([client, metadata]) =>
422+
`${this.clientIdByClient.get(client)}=${metadata.activeRequestCount}`
423+
)
424+
.sort()
425+
.join(', ');
426+
const failedClientsDescription = Array.from(this.failedClients)
427+
.map(client => `${this.clientIdByClient.get(client)}`)
428+
.sort()
429+
.join(', ');
430+
407431
return (
408432
`${this.activeClients.size} active clients: {` +
409-
this.logStringFromClientIds(this.activeClients.keys()) +
433+
activeClientsDescription +
410434
'}, ' +
411435
`${this.failedClients.size} failed clients: {` +
412-
this.logStringFromClientIds(this.failedClients) +
436+
failedClientsDescription +
413437
'}'
414438
);
415439
}
416-
417-
private logStringFromClientIds(clients: Iterable<T>): string {
418-
return Array.from(clients)
419-
.map(client => this.clientIdByClient.get(client) ?? '<unknown>')
420-
.sort()
421-
.join(', ');
422-
}
423440
}
424441

425442
/**

0 commit comments

Comments
 (0)