Skip to content

Commit 9eac0d5

Browse files
committed
add more debugId logging right from Firestore
1 parent 45e0819 commit 9eac0d5

File tree

7 files changed

+104
-46
lines changed

7 files changed

+104
-46
lines changed

packages/firestore/src/api/database.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ import { LRU_MINIMUM_CACHE_SIZE_BYTES } from '../local/lru_garbage_collector_imp
6161
import { debugAssert } from '../util/assert';
6262
import { AsyncQueue } from '../util/async_queue';
6363
import { AsyncQueueImpl } from '../util/async_queue_impl';
64+
import { generateUniqueDebugId } from '../util/debug_uid';
6465
import { Code, FirestoreError } from '../util/error';
6566
import { cast } from '../util/input_validation';
66-
import { logWarn } from '../util/log';
67+
import { logDebug, logWarn } from '../util/log';
6768
import { Deferred } from '../util/promise';
6869

6970
import { LoadBundleTask } from './bundle';
@@ -94,6 +95,7 @@ export const CACHE_SIZE_UNLIMITED = LRU_COLLECTION_DISABLED;
9495
* Do not call this constructor directly. Instead, use {@link (getFirestore:1)}.
9596
*/
9697
export class Firestore extends LiteFirestore {
98+
readonly _debugId = `Firestore@${generateUniqueDebugId()}`;
9799
/**
98100
* Whether it's a {@link Firestore} or Firestore Lite instance.
99101
*/
@@ -204,10 +206,20 @@ export function initializeFirestore(
204206
void pingServer(settings.host);
205207
}
206208

207-
return provider.initialize({
209+
const db = provider.initialize({
208210
options: settings,
209211
instanceIdentifier: databaseId
210212
});
213+
214+
logDebug(
215+
`initializeFirestore(` +
216+
`app.name=${app.name ?? 'undefined'}, databaseId=${
217+
databaseId ?? 'undefined'
218+
}` +
219+
`) returns ${db._debugId}`
220+
);
221+
222+
return db;
211223
}
212224

213225
/**
@@ -269,6 +281,15 @@ export function getFirestore(
269281
connectFirestoreEmulator(db, ...emulator);
270282
}
271283
}
284+
285+
logDebug(
286+
`getFirestore(` +
287+
`app.name=${app.name ?? 'undefined'}, databaseId=${
288+
databaseId ?? 'undefined'
289+
}` +
290+
`) returns ${db._debugId}`
291+
);
292+
272293
return db;
273294
}
274295

@@ -323,6 +344,7 @@ export function configureFirestore(firestore: Firestore): void {
323344
firestore._componentsProvider &&
324345
buildComponentProvider(firestore._componentsProvider)
325346
);
347+
logDebug(`${firestore._firestoreClient} created by ${firestore._debugId}`);
326348
}
327349

328350
function buildComponentProvider(componentsProvider: {
@@ -500,6 +522,8 @@ export function clearIndexedDbPersistence(firestore: Firestore): Promise<void> {
500522
);
501523
}
502524

525+
logDebug(firestore._debugId, 'clearIndexedDbPersistence()');
526+
503527
const deferred = new Deferred<void>();
504528
firestore._queue.enqueueAndForgetEvenWhileRestricted(async () => {
505529
try {
@@ -531,6 +555,7 @@ export function clearIndexedDbPersistence(firestore: Firestore): Promise<void> {
531555
* acknowledged by the backend.
532556
*/
533557
export function waitForPendingWrites(firestore: Firestore): Promise<void> {
558+
logDebug(firestore._debugId, 'waitForPendingWrites()');
534559
firestore = cast(firestore, Firestore);
535560
const client = ensureFirestoreConfigured(firestore);
536561
return firestoreClientWaitForPendingWrites(client);
@@ -543,6 +568,7 @@ export function waitForPendingWrites(firestore: Firestore): Promise<void> {
543568
* @returns A `Promise` that is resolved once the network has been enabled.
544569
*/
545570
export function enableNetwork(firestore: Firestore): Promise<void> {
571+
logDebug(firestore._debugId, 'enableNetwork()');
546572
firestore = cast(firestore, Firestore);
547573
const client = ensureFirestoreConfigured(firestore);
548574
return firestoreClientEnableNetwork(client);
@@ -557,6 +583,7 @@ export function enableNetwork(firestore: Firestore): Promise<void> {
557583
* @returns A `Promise` that is resolved once the network has been disabled.
558584
*/
559585
export function disableNetwork(firestore: Firestore): Promise<void> {
586+
logDebug(firestore._debugId, 'disableNetwork()');
560587
firestore = cast(firestore, Firestore);
561588
const client = ensureFirestoreConfigured(firestore);
562589
return firestoreClientDisableNetwork(client);
@@ -585,6 +612,7 @@ export function disableNetwork(firestore: Firestore): Promise<void> {
585612
* terminated.
586613
*/
587614
export function terminate(firestore: Firestore): Promise<void> {
615+
logDebug(firestore._debugId, 'terminate()');
588616
_removeServiceInstance(
589617
firestore.app,
590618
'firestore',
@@ -608,6 +636,7 @@ export function loadBundle(
608636
firestore: Firestore,
609637
bundleData: ReadableStream<Uint8Array> | ArrayBuffer | string
610638
): LoadBundleTask {
639+
logDebug(firestore._debugId, 'loadBundle()');
611640
firestore = cast(firestore, Firestore);
612641
const client = ensureFirestoreConfigured(firestore);
613642
const resultTask = new LoadBundleTask();
@@ -636,6 +665,7 @@ export function namedQuery(
636665
firestore: Firestore,
637666
name: string
638667
): Promise<Query | null> {
668+
logDebug(firestore._debugId, 'namedQuery() name:', name);
639669
firestore = cast(firestore, Firestore);
640670
const client = ensureFirestoreConfigured(firestore);
641671
return firestoreClientGetNamedQuery(client, name).then(namedQuery => {

packages/firestore/src/core/component_provider.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ import {
5656
import { JsonProtoSerializer } from '../remote/serializer';
5757
import { hardAssert } from '../util/assert';
5858
import { AsyncQueue } from '../util/async_queue';
59+
import { generateUniqueDebugId } from '../util/debug_uid';
5960
import { Code, FirestoreError } from '../util/error';
61+
import { logDebug } from '../util/log';
6062

6163
import { DatabaseInfo } from './database_info';
6264
import { EventManager, newEventManager } from './event_manager';
@@ -96,6 +98,7 @@ export interface OfflineComponentProviderFactory {
9698
* cache. Implementations override `initialize()` to provide all components.
9799
*/
98100
export interface OfflineComponentProvider {
101+
readonly debugId: string;
99102
readonly kind: Kind;
100103
persistence: Persistence;
101104
sharedClientState: SharedClientState;
@@ -116,8 +119,13 @@ export interface OfflineComponentProvider {
116119
export class MemoryOfflineComponentProvider
117120
implements OfflineComponentProvider
118121
{
122+
readonly debugId: string;
119123
kind: Kind = 'memory';
120124

125+
constructor() {
126+
this.debugId = `MemoryOfflineComponentProvider@${generateUniqueDebugId()}`;
127+
}
128+
121129
static readonly provider: OfflineComponentProviderFactory = {
122130
build: () => new MemoryOfflineComponentProvider()
123131
};
@@ -171,7 +179,12 @@ export class MemoryOfflineComponentProvider
171179
}
172180

173181
createPersistence(cfg: ComponentConfiguration): Persistence {
174-
return new MemoryPersistence(MemoryEagerDelegate.factory, this.serializer);
182+
const persistence = new MemoryPersistence(
183+
MemoryEagerDelegate.factory,
184+
this.serializer
185+
);
186+
logDebug(`${persistence.debugId} created from ${this.debugId}`);
187+
return persistence;
175188
}
176189

177190
createSharedClientState(cfg: ComponentConfiguration): SharedClientState {
@@ -222,6 +235,7 @@ export class LruGcMemoryOfflineComponentProvider extends MemoryOfflineComponentP
222235
* Provides all components needed for Firestore with IndexedDB persistence.
223236
*/
224237
export class IndexedDbOfflineComponentProvider extends MemoryOfflineComponentProvider {
238+
readonly debugId: string;
225239
kind: Kind = 'persistent';
226240
persistence!: IndexedDbPersistence;
227241
sharedClientState!: SharedClientState;
@@ -236,6 +250,7 @@ export class IndexedDbOfflineComponentProvider extends MemoryOfflineComponentPro
236250
protected readonly forceOwnership: boolean | undefined
237251
) {
238252
super();
253+
this.debugId = `IndexedDbOfflineComponentProvider@${generateUniqueDebugId()}`;
239254
}
240255

241256
async initialize(cfg: ComponentConfiguration): Promise<void> {
@@ -301,7 +316,7 @@ export class IndexedDbOfflineComponentProvider extends MemoryOfflineComponentPro
301316
? LruParams.withCacheSize(this.cacheSizeBytes)
302317
: LruParams.DEFAULT;
303318

304-
return new IndexedDbPersistence(
319+
const persistence = new IndexedDbPersistence(
305320
this.synchronizeTabs,
306321
persistenceKey,
307322
cfg.clientId,
@@ -313,6 +328,10 @@ export class IndexedDbOfflineComponentProvider extends MemoryOfflineComponentPro
313328
this.sharedClientState,
314329
!!this.forceOwnership
315330
);
331+
332+
logDebug(`${persistence.debugId} created from ${this.debugId}`);
333+
334+
return persistence;
316335
}
317336

318337
createSharedClientState(cfg: ComponentConfiguration): SharedClientState {

packages/firestore/src/core/firestore_client.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import { AsyncQueue, wrapInUserErrorIfRecoverable } from '../util/async_queue';
5656
import { BundleReader, BundleReaderSync } from '../util/bundle_reader';
5757
import { newBundleReader } from '../util/bundle_reader_impl';
5858
import { newBundleReaderSync } from '../util/bundle_reader_sync_impl';
59+
import { generateUniqueDebugId } from '../util/debug_uid';
5960
import { Code, FirestoreError } from '../util/error';
6061
import { logDebug, logWarn } from '../util/log';
6162
import { AutoId } from '../util/misc';
@@ -98,7 +99,6 @@ import { TransactionRunner } from './transaction_runner';
9899
import { View } from './view';
99100
import { ViewSnapshot } from './view_snapshot';
100101

101-
const LOG_TAG = 'FirestoreClient';
102102
export const MAX_CONCURRENT_LIMBO_RESOLUTIONS = 100;
103103

104104
/** DOMException error code constants. */
@@ -112,6 +112,8 @@ const DOM_EXCEPTION_QUOTA_EXCEEDED = 22;
112112
* async queue that is shared by all of the other components in the system. //
113113
*/
114114
export class FirestoreClient {
115+
readonly debugId = `FirestoreClient@${generateUniqueDebugId()}`;
116+
115117
private user = User.UNAUTHENTICATED;
116118
private readonly clientId = AutoId.newId();
117119
private authCredentialListener: CredentialChangeListener<User> = () =>
@@ -148,12 +150,12 @@ export class FirestoreClient {
148150
) {
149151
this._uninitializedComponentsProvider = componentProvider;
150152
this.authCredentials.start(asyncQueue, async user => {
151-
logDebug(LOG_TAG, 'Received user=', user.uid);
153+
logDebug(this.debugId, 'Received user=', user.uid);
152154
await this.authCredentialListener(user);
153155
this.user = user;
154156
});
155157
this.appCheckCredentials.start(asyncQueue, newAppCheckToken => {
156-
logDebug(LOG_TAG, 'Received new app check token=', newAppCheckToken);
158+
logDebug(this.debugId, 'Received new app check token=', newAppCheckToken);
157159
return this.appCheckCredentialListener(newAppCheckToken, this.user);
158160
});
159161
}
@@ -216,7 +218,7 @@ export async function setOfflineComponentProvider(
216218
): Promise<void> {
217219
client.asyncQueue.verifyOperationInProgress();
218220

219-
logDebug(LOG_TAG, 'Initializing OfflineComponentProvider');
221+
logDebug(client.debugId, 'Initializing OfflineComponentProvider');
220222
const configuration = client.configuration;
221223
await offlineComponentProvider.initialize(configuration);
222224

@@ -260,7 +262,7 @@ export async function setOnlineComponentProvider(
260262

261263
const offlineComponents = await ensureOfflineComponents(client);
262264

263-
logDebug(LOG_TAG, 'Initializing OnlineComponentProvider');
265+
logDebug(client.debugId, 'Initializing OnlineComponentProvider');
264266
await onlineComponentProvider.initialize(
265267
offlineComponents,
266268
client.configuration
@@ -319,7 +321,7 @@ async function ensureOfflineComponents(
319321
): Promise<OfflineComponentProvider> {
320322
if (!client._offlineComponents) {
321323
if (client._uninitializedComponentsProvider) {
322-
logDebug(LOG_TAG, 'Using user provided OfflineComponentProvider');
324+
logDebug(client.debugId, 'Using user provided OfflineComponentProvider');
323325
try {
324326
await setOfflineComponentProvider(
325327
client,
@@ -341,12 +343,17 @@ async function ensureOfflineComponents(
341343
);
342344
}
343345
} else {
344-
logDebug(LOG_TAG, 'Using default OfflineComponentProvider');
346+
logDebug(client.debugId, 'Using default OfflineComponentProvider');
345347
await setOfflineComponentProvider(
346348
client,
347349
new LruGcMemoryOfflineComponentProvider(undefined)
348350
);
349351
}
352+
logDebug(
353+
`${client.debugId} using OfflineComponentProvider: ${
354+
client._offlineComponents!.debugId
355+
}`
356+
);
350357
}
351358

352359
return client._offlineComponents!;
@@ -357,13 +364,13 @@ export async function ensureOnlineComponents(
357364
): Promise<OnlineComponentProvider> {
358365
if (!client._onlineComponents) {
359366
if (client._uninitializedComponentsProvider) {
360-
logDebug(LOG_TAG, 'Using user provided OnlineComponentProvider');
367+
logDebug(client.debugId, 'Using user provided OnlineComponentProvider');
361368
await setOnlineComponentProvider(
362369
client,
363370
client._uninitializedComponentsProvider._online
364371
);
365372
} else {
366-
logDebug(LOG_TAG, 'Using default OnlineComponentProvider');
373+
logDebug(client.debugId, 'Using default OnlineComponentProvider');
367374
await setOnlineComponentProvider(client, new OnlineComponentProvider());
368375
}
369376
}

0 commit comments

Comments
 (0)