-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathindex.ts
508 lines (444 loc) · 15.4 KB
/
index.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
/*
* Copyright 2019 Google LLC
*
* 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
*
* https://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 * as bigquery from "@google-cloud/bigquery";
import * as firebase from "firebase-admin";
import * as traverse from "traverse";
import fetch from "node-fetch";
import {
RawChangelogSchema,
RawChangelogViewSchema,
documentIdField,
oldDataField,
documentPathParams,
} from "./schema";
import { latestConsistentSnapshotView } from "./snapshot";
import handleFailedTransactions from "./handleFailedTransactions";
import {
ChangeType,
FirestoreEventHistoryTracker,
FirestoreDocumentChangeEvent,
} from "../tracker";
import * as logs from "../logs";
import {
InsertRowsOptions,
TableMetadata,
} from "@google-cloud/bigquery/build/src/table";
import { Partitioning } from "./partitioning";
import { Clustering } from "./clustering";
import { tableRequiresUpdate, viewRequiresUpdate } from "./checkUpdates";
export { RawChangelogSchema, RawChangelogViewSchema } from "./schema";
export interface FirestoreBigQueryEventHistoryTrackerConfig {
datasetId: string;
tableId: string;
datasetLocation?: string | undefined;
transformFunction?: string | undefined;
timePartitioning?: string | undefined;
timePartitioningField?: string | undefined;
timePartitioningFieldType?: string | undefined;
timePartitioningFirestoreField?: string | undefined;
clustering: string[] | null;
wildcardIds?: boolean;
bqProjectId?: string | undefined;
backupTableId?: string | undefined;
useNewSnapshotQuerySyntax?: boolean;
}
/**
* An FirestoreEventHistoryTracker that exports data to BigQuery.
*
* When the first event is received, it creates necessary BigQuery resources:
* - Dataset: {@link FirestoreBigQueryEventHistoryTrackerConfig#datasetId}.
* - Table: Raw change log table {@link FirestoreBigQueryEventHistoryTracker#rawChangeLogTableName}.
* - View: Latest view {@link FirestoreBigQueryEventHistoryTracker#rawLatestView}.
* If any subsequent data export fails, it will attempt to reinitialize.
*/
export class FirestoreBigQueryEventHistoryTracker
implements FirestoreEventHistoryTracker
{
bq: bigquery.BigQuery;
_initialized: boolean = false;
constructor(public config: FirestoreBigQueryEventHistoryTrackerConfig) {
this.bq = new bigquery.BigQuery();
this.bq.projectId = config.bqProjectId || process.env.PROJECT_ID;
if (!this.config.datasetLocation) {
this.config.datasetLocation = "us";
}
}
async record(events: FirestoreDocumentChangeEvent[]) {
await this.initialize();
const partitionHandler = new Partitioning(this.config);
const rows = events.map((event) => {
const partitionValue = partitionHandler.getPartitionValue(event);
const { documentId, ...pathParams } = event.pathParams || {};
return {
insertId: event.eventId,
json: {
timestamp: event.timestamp,
event_id: event.eventId,
document_name: event.documentName,
document_id: event.documentId,
operation: ChangeType[event.operation],
data: JSON.stringify(this.serializeData(event.data)),
old_data: event.oldData
? JSON.stringify(this.serializeData(event.oldData))
: null,
...partitionValue,
...(this.config.wildcardIds &&
event.pathParams && { path_params: JSON.stringify(pathParams) }),
},
};
});
const transformedRows = await this.transformRows(rows);
await this.insertData(transformedRows);
}
private async transformRows(rows: any[]) {
if (this.config.transformFunction && this.config.transformFunction !== "") {
const response = await fetch(this.config.transformFunction, {
method: "post",
body: JSON.stringify({ data: rows }),
headers: { "Content-Type": "application/json" },
});
const responseJson = await response.json();
// To support callable functions, first check result.data
return responseJson?.result?.data ?? responseJson.data;
}
return rows;
}
serializeData(eventData: any) {
if (typeof eventData === "undefined") {
return undefined;
}
const data = traverse<traverse.Traverse<any>>(eventData).map(function (
property
) {
if (property && property.constructor) {
if (property.constructor.name === "Buffer") {
this.remove();
}
if (
property.constructor.name ===
firebase.firestore.DocumentReference.name
) {
this.update(property.path);
}
}
});
return data;
}
/**
* Check whether a failed operation is retryable or not.
* Reasons for retrying:
* 1) We added a new column to our schema. Sometimes BQ is not ready to stream insertion records immediately
* after adding a new column to an existing table (https://issuetracker.google.com/35905247)
*/
private async isRetryableInsertionError(e) {
let isRetryable = true;
const expectedErrors = [
{ message: "no such field.", location: documentIdField.name },
{ message: "no such field.", location: documentPathParams.name },
];
if (
e.response &&
e.response.insertErrors &&
e.response.insertErrors.errors
) {
const errors = e.response.insertErrors.errors;
errors?.forEach((error) => {
let isExpected = false;
expectedErrors?.forEach((expectedError) => {
if (
error.message === expectedError.message &&
error.location === expectedError.location
) {
isExpected = true;
}
});
if (!isExpected) {
isRetryable = false;
}
});
}
return isRetryable;
}
/**
* Tables can often take time to create and propagate.
* A half a second delay is added per check while the function
* continually re-checks until the referenced dataset and table become available.
*/
private async waitForInitialization() {
return new Promise((resolve) => {
let handle = setInterval(async () => {
try {
const dataset = this.bigqueryDataset();
const changelogName = this.rawChangeLogTableName();
const table = dataset.table(changelogName);
const [datasetExists] = await dataset.exists();
const [tableExists] = await table.exists();
if (datasetExists && tableExists) {
clearInterval(handle);
return resolve(table);
}
} catch (ex) {
clearInterval(handle);
logs.failedToInitializeWait(ex.message);
}
}, 5000);
});
}
/**
* Inserts rows of data into the BigQuery raw change log table.
*/
private async insertData(
rows: bigquery.RowMetadata[],
overrideOptions: InsertRowsOptions = {},
retry: boolean = true
) {
const options = {
skipInvalidRows: false,
ignoreUnknownValues: false,
raw: true,
...overrideOptions,
};
try {
const dataset = this.bigqueryDataset();
const table = dataset.table(this.rawChangeLogTableName());
logs.dataInserting(rows.length);
await table.insert(rows, options);
logs.dataInserted(rows.length);
} catch (e) {
if (retry && this.isRetryableInsertionError(e)) {
retry = false;
logs.dataInsertRetried(rows.length);
return this.insertData(
rows,
{ ...overrideOptions, ignoreUnknownValues: true },
retry
);
}
// Exceeded number of retries, save in failed collection
if (!retry && this.config.backupTableId) {
await handleFailedTransactions(rows, this.config, e);
}
// Reinitializing in case the destintation table is modified.
this._initialized = false;
logs.bigQueryTableInsertErrors(e.errors);
throw e;
}
}
/**
* Creates the BigQuery resources with the expected schema for {@link FirestoreEventHistoryTracker}.
* After the first invokation, it skips initialization assuming these resources are still there.
*/
private async initialize() {
try {
if (this._initialized) {
return;
}
await this.initializeDataset();
await this.initializeRawChangeLogTable();
await this.initializeLatestView();
this._initialized = true;
} catch (ex) {
await this.waitForInitialization();
this._initialized = true;
}
}
/**
* Creates the specified dataset if it doesn't already exists.
*/
private async initializeDataset() {
const dataset = this.bigqueryDataset();
const [datasetExists] = await dataset.exists();
if (datasetExists) {
logs.bigQueryDatasetExists(this.config.datasetId);
} else {
try {
logs.bigQueryDatasetCreating(this.config.datasetId);
await dataset.create();
logs.bigQueryDatasetCreated(this.config.datasetId);
} catch (ex) {
logs.tableCreationError(this.config.datasetId, ex.message);
}
}
return dataset;
}
/**
* Creates the raw change log table if it doesn't already exist.
*/
private async initializeRawChangeLogTable() {
const changelogName = this.rawChangeLogTableName();
const dataset = this.bigqueryDataset();
const table = dataset.table(changelogName);
const [tableExists] = await table.exists();
const partitioning = new Partitioning(this.config, table);
const clustering = new Clustering(this.config, table);
if (tableExists) {
logs.bigQueryTableAlreadyExists(table.id, dataset.id);
const [metadata] = await table.getMetadata();
const fields = metadata.schema ? metadata.schema.fields : [];
await clustering.updateClustering(metadata);
const documentIdColExists = fields.find(
(column) => column.name === "document_id"
);
const pathParamsColExists = fields.find(
(column) => column.name === "path_params"
);
const oldDataColExists = fields.find(
(column) => column.name === "old_data"
);
if (!oldDataColExists) {
fields.push(oldDataField);
logs.addNewColumn(this.rawChangeLogTableName(), oldDataField.name);
}
if (!documentIdColExists) {
fields.push(documentIdField);
logs.addNewColumn(this.rawChangeLogTableName(), documentIdField.name);
}
if (!pathParamsColExists && this.config.wildcardIds) {
fields.push(documentPathParams);
logs.addNewColumn(
this.rawChangeLogTableName(),
documentPathParams.name
);
}
/** Updated table metadata if required */
const shouldUpdate = await tableRequiresUpdate({
table,
config: this.config,
documentIdColExists,
pathParamsColExists,
oldDataColExists,
});
if (shouldUpdate) {
/** set partitioning */
await partitioning.addPartitioningToSchema(metadata.schema.fields);
/** update table metadata with changes. */
await table.setMetadata(metadata);
logs.updatingMetadata(this.rawChangeLogTableName(), {
config: this.config,
documentIdColExists,
pathParamsColExists,
oldDataColExists,
});
}
} else {
logs.bigQueryTableCreating(changelogName);
const schema = { fields: [...RawChangelogSchema.fields] };
if (this.config.wildcardIds) {
schema.fields.push(documentPathParams);
}
const options: TableMetadata = { friendlyName: changelogName, schema };
//Add partitioning
await partitioning.addPartitioningToSchema(schema.fields);
await partitioning.updateTableMetadata(options);
// Add clustering
await clustering.updateClustering(options);
try {
await table.create(options);
logs.bigQueryTableCreated(changelogName);
} catch (ex) {
logs.tableCreationError(changelogName, ex.message);
}
}
return table;
}
/**
* Creates the latest snapshot view, which returns only latest operations
* of all existing documents over the raw change log table.
*/
private async initializeLatestView() {
const dataset = this.bigqueryDataset();
const view = dataset.table(this.rawLatestView());
const [viewExists] = await view.exists();
const schema = RawChangelogViewSchema;
if (viewExists) {
logs.bigQueryViewAlreadyExists(view.id, dataset.id);
const [metadata] = await view.getMetadata();
// TODO: just casting this for now, needs properly fixing
const fields = (metadata.schema ? metadata.schema.fields : []) as {
name: string;
}[];
if (this.config.wildcardIds) {
schema.fields.push(documentPathParams);
}
const columnNames = fields.map((field) => field.name);
const documentIdColExists = columnNames.includes("document_id");
const pathParamsColExists = columnNames.includes("path_params");
const oldDataColExists = columnNames.includes("old_data");
/** If new view or opt-in to new query syntax **/
const updateView = viewRequiresUpdate({
metadata,
config: this.config,
documentIdColExists,
pathParamsColExists,
oldDataColExists,
});
if (updateView) {
metadata.view = latestConsistentSnapshotView({
datasetId: this.config.datasetId,
tableName: this.rawChangeLogTableName(),
schema,
useLegacyQuery: !this.config.useNewSnapshotQuerySyntax,
});
if (!documentIdColExists) {
logs.addNewColumn(this.rawLatestView(), documentIdField.name);
}
await view.setMetadata(metadata);
logs.updatingMetadata(this.rawLatestView(), {
config: this.config,
documentIdColExists,
pathParamsColExists,
oldDataColExists,
});
}
} else {
const schema = { fields: [...RawChangelogViewSchema.fields] };
if (this.config.wildcardIds) {
schema.fields.push(documentPathParams);
}
const latestSnapshot = latestConsistentSnapshotView({
datasetId: this.config.datasetId,
tableName: this.rawChangeLogTableName(),
schema,
bqProjectId: this.bq.projectId,
useLegacyQuery: !this.config.useNewSnapshotQuerySyntax,
});
logs.bigQueryViewCreating(this.rawLatestView(), latestSnapshot.query);
const options: TableMetadata = {
friendlyName: this.rawLatestView(),
view: latestSnapshot,
};
try {
await view.create(options);
await view.setMetadata({ schema: RawChangelogViewSchema });
logs.bigQueryViewCreated(this.rawLatestView());
} catch (ex) {
logs.tableCreationError(this.rawLatestView(), ex.message);
}
}
return view;
}
bigqueryDataset() {
return this.bq.dataset(this.config.datasetId, {
location: this.config.datasetLocation,
});
}
private rawChangeLogTableName(): string {
return `${this.config.tableId}_raw_changelog`;
}
private rawLatestView(): string {
return `${this.config.tableId}_raw_latest`;
}
}