-
Notifications
You must be signed in to change notification settings - Fork 430
Expand file tree
/
Copy pathmultiThreadCollectionGroupBug.test.ts
More file actions
521 lines (447 loc) · 18.1 KB
/
Copy pathmultiThreadCollectionGroupBug.test.ts
File metadata and controls
521 lines (447 loc) · 18.1 KB
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
import { runMultiThread } from "../src/run-multi-thread";
import * as admin from "firebase-admin";
import { CliConfig } from "../src/types";
import * as workerpool from "workerpool";
import { FIRESTORE_DEFAULT_DATABASE } from "../src/config";
// Mock Firebase Admin to avoid credential issues
jest.mock("firebase-admin", () => {
const mockFirestore = {
collection: jest.fn().mockReturnValue({
add: jest
.fn()
.mockResolvedValue({ id: "test-doc", path: "test/test-doc" }),
doc: jest.fn().mockReturnValue({
set: jest.fn().mockResolvedValue(undefined),
delete: jest.fn().mockResolvedValue(undefined),
}),
listDocuments: jest.fn().mockResolvedValue([]),
}),
collectionGroup: jest.fn().mockReturnValue({
orderBy: jest.fn().mockReturnThis(),
startAt: jest.fn().mockReturnThis(),
endBefore: jest.fn().mockReturnThis(),
getPartitions: jest.fn().mockReturnValue({
next: jest.fn().mockResolvedValue({ value: undefined, done: true }),
}),
}),
doc: jest.fn().mockImplementation((path) => {
// This is where the bug occurs - if path has odd number of components
const components = path.split("/");
if (components.length % 2 === 1) {
throw new Error(
`Value for argument "documentPath" must point to a document, but was "${path}". Your path does not contain an even number of components.`
);
}
return { path };
}),
FieldPath: {
documentId: jest.fn().mockReturnValue("__name__"),
},
};
return {
apps: [],
initializeApp: jest.fn(),
firestore: Object.assign(jest.fn().mockReturnValue(mockFirestore), {
FieldPath: {
documentId: jest.fn().mockReturnValue("__name__"),
},
FieldValue: {
serverTimestamp: jest
.fn()
.mockReturnValue({ _type: "serverTimestamp" }),
},
}),
credential: {
applicationDefault: jest.fn(),
},
};
});
const firestore = admin.firestore();
// Mock workerpool to capture the actual queries being sent to workers
let capturedQueries: any[] = [];
jest.mock("workerpool", () => ({
pool: jest.fn().mockReturnValue({
exec: jest.fn().mockImplementation((method, args) => {
// Capture the serialized query that would be sent to the worker
capturedQueries.push(args[0]);
return Promise.resolve(5); // Simulate processing 5 docs
}),
stats: jest.fn().mockReturnValue({ activeTasks: 0, pendingTasks: 0 }),
terminate: jest.fn().mockResolvedValue(undefined),
}),
}));
describe("Multi-threaded Collection Group Bug Reproduction", () => {
let testCollection = `orderbooks_${Date.now()}`;
let mockConfig: CliConfig;
beforeAll(async () => {
console.log(
"Setting up test data for collection group bug reproduction..."
);
// Create test data that matches the user's scenario
// orderbooks/{event_id}/snapshots structure
const eventIds = [
"0x28845a6abb595ae75aad6a50c7047660ff5552d4359ebcb00f6ffd05bb05c4c3",
"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
"0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
];
for (const eventId of eventIds) {
// Create multiple snapshots for each event
for (let i = 0; i < 3; i++) {
await firestore.collection(`orderbooks/${eventId}/snapshots`).add({
index: i,
eventId: eventId,
timestamp: admin.firestore.FieldValue.serverTimestamp(),
data: { price: 100 + i, volume: 1000 + i },
});
}
}
});
beforeEach(() => {
jest.clearAllMocks();
capturedQueries = [];
mockConfig = {
kind: "CONFIG",
projectId: "poly-trader",
bigQueryProjectId: "poly-trader",
sourceCollectionPath: "orderbooks/{event_id}/snapshots", // This is the problematic path
datasetId: "orderbooks",
tableId: "old_data",
batchSize: 100,
queryCollectionGroup: true, // This triggers the bug
datasetLocation: "us",
multiThreaded: true, // This triggers the bug
useNewSnapshotQuerySyntax: false,
useEmulator: false,
rawChangeLogName: "old_data_raw_changelog",
cursorPositionFile: "/tmp/test_cursor_position",
firestoreInstanceId: FIRESTORE_DEFAULT_DATABASE,
};
});
it("should reproduce the documentPath error in multi-threaded mode", async () => {
console.log("Running multi-threaded collection group import...");
try {
await runMultiThread(mockConfig);
// If we get here, the bug might not be reproduced
console.log("No error occurred - this might not reproduce the bug");
} catch (error) {
console.log("Error caught:", error.message);
// Check if the error matches the expected pattern
expect(error.message).toContain(
'Value for argument "documentPath" must point to a document'
);
expect(error.message).toContain(
"Your path does not contain an even number of components"
);
// Log the captured queries for debugging
console.log(
"Captured queries sent to workers:",
JSON.stringify(capturedQueries, null, 2)
);
// Verify that the captured queries contain problematic paths
const hasProblematicPaths = capturedQueries.some((query) => {
const startAtPath = query.startAt?.values?.[0]?.referenceValue;
const endAtPath = query.endAt?.values?.[0]?.referenceValue;
// Check if paths contain odd number of components (indicating document paths)
const startAtComponents = startAtPath?.split("/") || [];
const endAtComponents = endAtPath?.split("/") || [];
return (
startAtComponents.length % 2 === 1 || endAtComponents.length % 2 === 1
);
});
expect(hasProblematicPaths).toBe(true);
}
});
it("should work correctly in single-threaded mode", async () => {
// This test verifies that the same configuration works in single-threaded mode
const singleThreadConfig = {
...mockConfig,
multiThreaded: false,
};
// We can't directly test single-threaded mode here since it requires BigQuery setup
// But we can verify the configuration is valid
expect(singleThreadConfig.sourceCollectionPath).toBe(
"orderbooks/{event_id}/snapshots"
);
expect(singleThreadConfig.queryCollectionGroup).toBe(true);
expect(singleThreadConfig.multiThreaded).toBe(false);
});
it("should analyze the partition boundaries", async () => {
console.log("Analyzing partition boundaries...");
// Create a collection group query similar to what runMultiThread does
const collectionGroupQuery = firestore.collectionGroup("snapshots");
// Get partitions to see what boundaries are created
const partitions = collectionGroupQuery.getPartitions(100);
const partitionBoundaries: any[] = [];
// Mock the partition iteration since we can't actually iterate over AsyncIterable in tests
const mockPartitions = [
{
toQuery: () => ({
_queryOptions: {
startAt: {
values: [
{
referenceValue:
"projects/test/databases/(default)/documents/orderbooks/event1/snapshots/doc1",
},
],
},
endAt: {
values: [
{
referenceValue:
"projects/test/databases/(default)/documents/orderbooks/event2/snapshots/doc2",
},
],
},
},
}),
},
];
for (const partition of mockPartitions) {
const query = partition.toQuery();
partitionBoundaries.push({
startAt: query._queryOptions.startAt,
endAt: query._queryOptions.endAt,
});
}
console.log(
"Partition boundaries:",
JSON.stringify(partitionBoundaries, null, 2)
);
// Verify that partition boundaries contain document references
expect(partitionBoundaries.length).toBeGreaterThan(0);
// Check if any boundaries have problematic paths
const problematicBoundaries = partitionBoundaries.filter((boundary) => {
const startAtPath = boundary.startAt?.values?.[0]?.referenceValue;
const endAtPath = boundary.endAt?.values?.[0]?.referenceValue;
const startAtComponents = startAtPath?.split("/") || [];
const endAtComponents = endAtPath?.split("/") || [];
return (
startAtComponents.length % 2 === 1 || endAtComponents.length % 2 === 1
);
});
console.log("Problematic boundaries:", problematicBoundaries);
// This should identify the root cause of the bug
expect(problematicBoundaries.length).toBeGreaterThan(0);
});
it("should validate the proposed fix for document path extraction", async () => {
// This test validates that the fix correctly extracts document paths
// Helper function that implements the proposed fix
const extractDocumentPath = (resourceName: string): string => {
// Remove the Firestore resource prefix to get just the document path
const prefix = /^projects\/[^\/]+\/databases\/[^\/]+\/documents\//;
return resourceName.replace(prefix, "");
};
// Test cases with various path formats
const testCases = [
{
input:
"projects/poly-trader/databases/(default)/documents/orderbooks/0x28845a6abb595ae75aad6a50c7047660ff5552d4359ebcb00f6ffd05bb05c4c3/snapshots/E2DPYIb4JG7PQrT4LAIA",
expected:
"orderbooks/0x28845a6abb595ae75aad6a50c7047660ff5552d4359ebcb00f6ffd05bb05c4c3/snapshots/E2DPYIb4JG7PQrT4LAIA",
},
{
input:
"projects/test-project/databases/(default)/documents/collection/doc1",
expected: "collection/doc1",
},
{
input:
"projects/my-app/databases/staging/documents/users/user123/posts/post456",
expected: "users/user123/posts/post456",
},
];
for (const testCase of testCases) {
const result = extractDocumentPath(testCase.input);
console.log(`Input: ${testCase.input}`);
console.log(`Output: ${result}`);
console.log(`Expected: ${testCase.expected}`);
expect(result).toBe(testCase.expected);
// Verify the extracted path has even number of components
const components = result.split("/");
expect(components.length % 2).toBe(0);
// Verify that firestore.doc() would accept this path
expect(() => {
firestore.doc(result);
}).not.toThrow();
}
});
it("should demonstrate the fixed worker implementation", async () => {
// This test shows how the worker should be fixed
const extractDocumentPath = (resourceName: string): string => {
const prefix = /^projects\/[^\/]+\/databases\/[^\/]+\/documents\//;
return resourceName.replace(prefix, "");
};
// Mock the fixed processDocuments function
const processDocumentsFixed = async (
serializableQuery: any,
config: CliConfig
) => {
// Initialize query as in the original worker
let query = firestore
.collectionGroup("snapshots")
.orderBy(admin.firestore.FieldPath.documentId(), "asc");
// Apply the FIX when handling partition boundaries
if (serializableQuery.startAt?.values?.[0]?.referenceValue) {
const fullPath = serializableQuery.startAt.values[0].referenceValue;
const documentPath = extractDocumentPath(fullPath); // THE FIX
query = query.startAt(firestore.doc(documentPath));
}
if (serializableQuery.endAt?.values?.[0]?.referenceValue) {
const fullPath = serializableQuery.endAt.values[0].referenceValue;
const documentPath = extractDocumentPath(fullPath); // THE FIX
query = query.endBefore(firestore.doc(documentPath));
}
// Rest of the processing would continue here...
return 0; // Mock return value
};
// Test with a problematic serialized query
const problematicQuery = {
startAt: {
values: [
{
referenceValue:
"projects/poly-trader/databases/(default)/documents/orderbooks/0x28845a6abb595ae75aad6a50c7047660ff5552d4359ebcb00f6ffd05bb05c4c3/snapshots/E2DPYIb4JG7PQrT4LAIA",
},
],
},
endAt: {
values: [
{
referenceValue:
"projects/poly-trader/databases/(default)/documents/orderbooks/0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef/snapshots/ABC123DEF456",
},
],
},
};
// This should NOT throw with the fix applied
await expect(
processDocumentsFixed(problematicQuery, mockConfig)
).resolves.not.toThrow();
console.log("✅ Fixed processDocuments function executed successfully!");
});
it("should handle edge cases in document path extraction", async () => {
// Test edge cases that could occur in real-world scenarios
const extractDocumentPath = (resourceName: string): string => {
const prefix = /^projects\/[^\/]+\/databases\/[^\/]+\/documents\//;
if (!prefix.test(resourceName)) {
console.warn(
`Path doesn't match expected format, using as-is: ${resourceName}`
);
return resourceName;
}
const documentPath = resourceName.replace(prefix, "");
if (!documentPath) {
throw new Error(
`Invalid resource name: ${resourceName} - no document path after prefix`
);
}
return documentPath;
};
// Test case 1: Already extracted path (should return as-is)
const alreadyExtracted = "orderbooks/0x123/snapshots/ABC";
expect(() => {
const result = extractDocumentPath(alreadyExtracted);
expect(result).toBe(alreadyExtracted);
expect(result.split("/").length % 2).toBe(0); // Should be even
}).not.toThrow();
// Test case 2: Just the prefix with no document path (should throw)
const justPrefix = "projects/poly-trader/databases/(default)/documents/";
expect(() => {
extractDocumentPath(justPrefix);
}).toThrow(
"Invalid resource name: projects/poly-trader/databases/(default)/documents/ - no document path after prefix"
);
// Test case 3: Empty project ID (should return as-is and fail validation later)
const emptyProjectId = "projects//databases/(default)/documents/test/doc";
expect(() => {
const result = extractDocumentPath(emptyProjectId);
expect(result).toBe(emptyProjectId); // Returned as-is
// This would fail the even/odd check in the actual code
expect(result.split("/").length % 2).toBe(1); // Odd number
}).not.toThrow();
// Test case 4: Completely different format (should return as-is)
const differentFormat = "some/random/path/structure";
expect(() => {
const result = extractDocumentPath(differentFormat);
expect(result).toBe(differentFormat);
}).not.toThrow();
// Test case 5: Valid full path (should extract correctly)
const validFullPath =
"projects/poly-trader/databases/(default)/documents/orderbooks/0x123/snapshots/ABC";
expect(() => {
const result = extractDocumentPath(validFullPath);
expect(result).toBe("orderbooks/0x123/snapshots/ABC");
expect(result.split("/").length % 2).toBe(0); // Should be even
}).not.toThrow();
// Test case 6: Path with special characters in database name
const specialDbName =
"projects/my-app/databases/prod-db-123/documents/users/user1";
expect(() => {
const result = extractDocumentPath(specialDbName);
expect(result).toBe("users/user1");
expect(result.split("/").length % 2).toBe(0); // Should be even
}).not.toThrow();
console.log("✅ All edge cases handled correctly!");
});
it("should verify the fix works with real partition boundaries", async () => {
// This test verifies the fix with realistic partition boundary scenarios
const extractDocumentPath = (resourceName: string): string => {
const prefix = /^projects\/[^\/]+\/databases\/[^\/]+\/documents\//;
return resourceName.replace(prefix, "");
};
// Simulate real partition boundaries from getPartitions
const mockPartitionBoundaries = [
{
startAt:
"projects/poly-trader/databases/(default)/documents/orderbooks/0x1111/snapshots/AAA",
endAt:
"projects/poly-trader/databases/(default)/documents/orderbooks/0x2222/snapshots/BBB",
},
{
startAt:
"projects/poly-trader/databases/(default)/documents/orderbooks/0x3333/snapshots/CCC",
endAt:
"projects/poly-trader/databases/(default)/documents/orderbooks/0x4444/snapshots/DDD",
},
];
for (const boundary of mockPartitionBoundaries) {
// Apply the fix to extract document paths
const fixedStartPath = extractDocumentPath(boundary.startAt);
const fixedEndPath = extractDocumentPath(boundary.endAt);
console.log(`Original startAt: ${boundary.startAt}`);
console.log(`Fixed startAt: ${fixedStartPath}`);
console.log(`Original endAt: ${boundary.endAt}`);
console.log(`Fixed endAt: ${fixedEndPath}`);
// Verify the fixed paths work with firestore.doc()
expect(() => {
const query = firestore
.collectionGroup("snapshots")
.startAt(firestore.doc(fixedStartPath))
.endBefore(firestore.doc(fixedEndPath));
}).not.toThrow();
// Verify the fixed paths have even component counts
expect(fixedStartPath.split("/").length % 2).toBe(0);
expect(fixedEndPath.split("/").length % 2).toBe(0);
}
console.log(
"✅ All partition boundaries processed successfully with the fix!"
);
});
afterAll(async () => {
console.log("Cleaning up test data...");
// Clean up test collections
const eventIds = [
"0x28845a6abb595ae75aad6a50c7047660ff5552d4359ebcb00f6ffd05bb05c4c3",
"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
"0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
];
for (const eventId of eventIds) {
const collectionRef = firestore.collection(
`orderbooks/${eventId}/snapshots`
);
const docs = await collectionRef.listDocuments();
await Promise.all(docs.map((doc) => doc.delete()));
}
});
});