|
| 1 | +import { Meteor } from 'meteor/meteor'; |
| 2 | +import { MongoInternals } from 'meteor/mongo'; |
| 3 | +import { BigQuery } from '@google-cloud/bigquery'; |
| 4 | +import { createOrGetCollection } from './mongo'; |
| 5 | + |
| 6 | +const { |
| 7 | + debug, |
| 8 | + dataSyncsCollectionName = 'dataSyncs', |
| 9 | + mongoOplogReadBatchSize = 100, |
| 10 | + mongoDatabase, |
| 11 | + bigQueryAccountsServiceFileName, |
| 12 | + bigQueryProjectId, |
| 13 | + bigQueryDatabase, |
| 14 | +} = Meteor.settings || {}; |
| 15 | + |
| 16 | +const options = { |
| 17 | + // eslint-disable-next-line no-undef |
| 18 | + keyFilename: Assets.absoluteFilePath(bigQueryAccountsServiceFileName), |
| 19 | + projectId: bigQueryProjectId, |
| 20 | +}; |
| 21 | + |
| 22 | +const bigquery = new BigQuery(options); |
| 23 | + |
| 24 | +export const insertRows = async ({ table, rows }) => { |
| 25 | + try { |
| 26 | + await bigquery.dataset(bigQueryDatabase).table(table).insert(rows); |
| 27 | + } catch (e) { |
| 28 | + console.error('Error inserting rows to bigquery.', e); |
| 29 | + if (e.errors) { |
| 30 | + console.error( |
| 31 | + 'Error inserting rows to bigquery.', |
| 32 | + JSON.stringify(e.errors, null, 2) |
| 33 | + ); |
| 34 | + } |
| 35 | + throw e; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +const DataSyncsCollection = createOrGetCollection({ |
| 40 | + name: dataSyncsCollectionName, |
| 41 | +}); |
| 42 | + |
| 43 | +const getDataSync = async ({ collectionName }) => { |
| 44 | + const dataSync = await DataSyncsCollection.findOneAsync({ collectionName }); |
| 45 | + |
| 46 | + if (dataSync) { |
| 47 | + return dataSync; |
| 48 | + } |
| 49 | + return DataSyncsCollection.findOneAsync( |
| 50 | + await DataSyncsCollection.insertAsync({ |
| 51 | + collectionName, |
| 52 | + createdAt: new Date(), |
| 53 | + updatedAt: new Date(), |
| 54 | + }) |
| 55 | + ); |
| 56 | +}; |
| 57 | +const setDataSync = async ({ _id, ...rest }) => { |
| 58 | + await DataSyncsCollection.updateAsync( |
| 59 | + { _id }, |
| 60 | + { $set: { ...rest, updatedAt: new Date() } } |
| 61 | + ); |
| 62 | +}; |
| 63 | +export const runSync = async ({ name: collectionName, projection = {} }) => { |
| 64 | + if (!process.env.MONGO_URL) { |
| 65 | + console.error('runSync error: missing MONGO_URL.'); |
| 66 | + return; |
| 67 | + } |
| 68 | + if (!process.env.MONGO_OPLOG_URL) { |
| 69 | + console.error('runSync error: missing MONGO_OPLOG_URL.'); |
| 70 | + return; |
| 71 | + } |
| 72 | + if ( |
| 73 | + !mongoDatabase || |
| 74 | + !bigQueryAccountsServiceFileName || |
| 75 | + !bigQueryProjectId || |
| 76 | + !bigQueryDatabase |
| 77 | + ) { |
| 78 | + console.error( |
| 79 | + 'runSync error: missing settings. All these fields are required: mongoDatabase, bigQueryAccountsServiceFileName, bigQueryProjectId, and bigQueryDatabase.' |
| 80 | + ); |
| 81 | + return; |
| 82 | + } |
| 83 | + if (!collectionName) { |
| 84 | + console.error('runSync error: collectionName is required.'); |
| 85 | + return; |
| 86 | + } |
| 87 | + console.log('runSync'); |
| 88 | + |
| 89 | + const dataSync = await getDataSync({ collectionName }); |
| 90 | + const { lastSyncedDocumentWall, lastSyncedDocumentTs } = dataSync; |
| 91 | + const collection = createOrGetCollection({ name: collectionName }); |
| 92 | + |
| 93 | + const oplogDb = |
| 94 | + MongoInternals.defaultRemoteCollectionDriver().mongo._oplogHandle |
| 95 | + ._oplogTailConnection.db; |
| 96 | + |
| 97 | + const oplogCollection = oplogDb.collection('oplog.rs'); |
| 98 | + |
| 99 | + const query = { |
| 100 | + ...(lastSyncedDocumentWall && { wall: { $gte: lastSyncedDocumentWall } }), |
| 101 | + // as we are using equal we should ignore the last one synced last time to not duplicate |
| 102 | + ...(lastSyncedDocumentTs && { ts: { $ne: lastSyncedDocumentTs } }), |
| 103 | + op: { $in: ['u', 'i'] }, |
| 104 | + ns: `${mongoDatabase}.${collectionName}`, |
| 105 | + }; |
| 106 | + |
| 107 | + const findOptions = { |
| 108 | + limit: mongoOplogReadBatchSize, |
| 109 | + sort: { wall: 1 }, |
| 110 | + projection: { ts: 1, 'o2._id': 1, wall: 1 }, |
| 111 | + }; |
| 112 | + |
| 113 | + const startOplogFind = new Date(); |
| 114 | + const prefix = `runSync:${collectionName}:`; |
| 115 | + console.log( |
| 116 | + `${prefix}oplog find\n${JSON.stringify(query, null, 2)}\n${JSON.stringify( |
| 117 | + findOptions, |
| 118 | + null, |
| 119 | + 2 |
| 120 | + )}` |
| 121 | + ); |
| 122 | + |
| 123 | + const cursor = oplogCollection.find(query, findOptions); |
| 124 | + const data = await cursor.toArray(); |
| 125 | + const tsOplogMessage = `(${( |
| 126 | + new Date().getTime() - startOplogFind.getTime() |
| 127 | + ).toLocaleString()}ms):`; |
| 128 | + if (!data.length) { |
| 129 | + console.log(`${prefix}${tsOplogMessage}oplog no data`); |
| 130 | + return; |
| 131 | + } |
| 132 | + console.log(`${prefix}${tsOplogMessage}oplog data (${data.length})`); |
| 133 | + if (debug) { |
| 134 | + console.log(`${JSON.stringify(data, null, 2)}`); |
| 135 | + } |
| 136 | + const ids = data.map(({ o2 }) => o2._id); |
| 137 | + const rows = collection.find({ _id: { $in: ids } }, { projection }).fetch(); |
| 138 | + await insertRows({ |
| 139 | + table: collectionName, |
| 140 | + rows, |
| 141 | + }); |
| 142 | + const lastDatum = data[rows.length - 1]; |
| 143 | + |
| 144 | + const newDataSync = { |
| 145 | + ...dataSync, |
| 146 | + lastSyncedDocumentTs: lastDatum.ts, |
| 147 | + lastSyncedDocumentWall: lastDatum.wall, |
| 148 | + }; |
| 149 | + console.log(`${prefix}setDataSync\n${JSON.stringify(newDataSync, null, 2)}`); |
| 150 | + await setDataSync(newDataSync); |
| 151 | +}; |
0 commit comments