This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
/
exporter.js
385 lines (356 loc) · 12.9 KB
/
exporter.js
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
const http = require('http');
const https = require('https');
const util = require('util');
const async = require('async');
const log = require('./log.js');
const args = require('./args.js');
const options = require('./options.js');
const drivers = require('./drivers.js');
const cluster = require('./cluster.js');
/**
* An empty callback that doesn't pass on anything.
* @callback emptyCb
*/
/**
* A callback with support for receiving errors.
* @callback errorCb
* @param {string|string[]|Error|*} [err] The error to pass on or null if no error
*/
/**
* @callback dataCb
* @param {String|String[]|Error} [err]
* @param {*} [data]
*/
/**
* @typedef {Object} Statistics
* @property {SourceInfo} source General information about the source service
* @property {TargetInfo} target General information about the target service
* @property {Object} hits Statistical information about documents
* @property {number} hits.processed Statistical information about how many documents have been processed so far
* @property {number} hits.total Statistical information about how many documents there are in total
* @property {Object} memory Statistical information about memory usage
* @property {number} memory.peak Statistical information about maximum memory usage
* @property {number} memory.ratio Statistical information about memory usage ratio
*/
/**
* The environment object that will be passed on the the drivers for all operations.
* The properties available here are only a minimum set and can be extended by each driver however best suited.
*
* @constructor
* @property {Object} options Options that will be used to determine the export behavior
* @property {Object} options.log Options for log related operations
* @property {Statistics} statistics Statistics that are collected throughout the export process
*/
function Environment() {
this.options = {
log: {}
};
this.statistics = {
source: {
version: "0.0",
status: "Red",
docs: {
total: 0
}
},
target: {
version: "0.0",
status: "Red"
},
hits: {
processed: 0,
total: 0
},
memory: {
peak: 0,
ratio: 0
}
};
}
/**
* Stores the latest information about the systems memory usage. Is only used if memory.limit option is set.
* @type {Object}
*/
exports.memUsage = null;
/**
* Once the exporter starts this is where the global environment object is stored.
* @type {Environment}
*/
exports.env = null;
/**
* Status object that keeps track of where we are in the export process.
* @type {string}
*/
exports.status = "ready";
/**
* Queued up documents to be processed as soon as the target is ready.
* @type {Data[]}
*/
exports.queue = [];
/**
* A catch all exception handler that will try to print something useful before crashing.
*
* @param e
*/
exports.handleUncaughtExceptions = e => {
log.error('Caught exception in Main process: %s'.bold, util.format(e));
e instanceof Error && log.info(e.stack);
log.die(2);
};
/**
* Reads the options from either command line or file.
*
* @param {function} callback
*/
exports.readOptions = callback => {
options.read(optionTree => {
if (!optionTree) {
callback('options have been returned empty');
} else {
callback(null, optionTree);
}
});
};
/**
* Allows each driver to verify if the options supplied are sufficient.
* Once verified, the options are available in the environment.
*
* @param {errorCb} callback
* @param results The option tree from readOptions()
*/
exports.verifyOptions = (results, callback) => {
log.debug('Passing options to drivers for verification');
options.verify(results.readOptions, err => {
if (err && !err.length) {
err = null;
}
exports.env = new Environment();
exports.env.options = results.readOptions;
if (exports.env.options.network && exports.env.options.network.sockets) {
https.globalAgent.maxSockets = http.globalAgent.maxSockets = exports.env.options.network.sockets;
}
callback(err);
});
};
/**
* Calls the reset function on the source driver to get it ready for execution.
*
* @param {Object} results Ignored results object from async
* @param {errorCb} callback
*/
exports.resetSource = (results, callback) => {
async.retry(exports.env.options.errors.retry, callback => {
log.debug('Resetting source driver to begin operations');
let source = drivers.get(exports.env.options.drivers.source).driver;
source.reset(exports.env, callback);
}, callback);
};
/**
* Calls the reset function on the target driver to get it ready for execution.
*
* @param {Object} results Ignored results object from async
* @param {errorCb} callback
*/
exports.resetTarget = (results, callback) => {
async.retry(exports.env.options.errors.retry, callback => {
log.debug('Resetting target driver to begin operations');
let target = drivers.get(exports.env.options.drivers.target).driver;
target.reset(exports.env, callback);
}, callback);
};
/**
* Retrieve some basic statistics and status information from the source that allows to verify it's ready.
* The response includes an information about how many documents will be exported in total.
*
* @param {Object} results Ignored results object from async
* @param {errorCb} callback
*/
exports.getSourceStatistics = (results, callback) => {
async.retry(exports.env.options.errors.retry, callback => {
log.debug('Fetching source statistics before starting run');
let source = drivers.get(exports.env.options.drivers.source).driver;
source.getSourceStats(exports.env, (err, sourceStats) => {
exports.env.statistics.source = util._extend(exports.env.statistics.source, sourceStats);
callback(err);
});
}, callback);
};
/**
* Retrieve some basic statistics and status information from the target that allows to verify it's ready.
*
* @param {Object} results Ignored results object from async
* @param {errorCb} callback
*/
exports.getTargetStatistics = (results, callback) => {
async.retry(exports.env.options.errors.retry, callback => {
log.debug('Fetching target statistics before starting run');
let target = drivers.get(exports.env.options.drivers.target).driver;
target.getTargetStats(exports.env, (err, targetStats) => {
exports.env.statistics.target = util._extend(exports.env.statistics.target, targetStats);
callback(err);
});
}, callback);
};
/**
* Checks for some basic information such as if the source driver has any documents to be exported.
*
* @param {Object} results Ignored results object from async
* @param {errorCb} callback
*/
exports.checkSourceHealth = (results, callback) => {
log.debug("Checking source database health");
if (exports.env.statistics.source.status == "red") {
callback("The source database is experiencing and error and cannot proceed");
}
else if (exports.env.statistics.source.docs.total === 0) {
callback("The source driver has not reported any documents that can be exported. Not exporting.");
} else {
callback(null);
}
};
/**
* Checks for some basic information of the target driver.
*
* @param {Object} results Ignored results object from async
* @param {errorCb} callback
*/
exports.checkTargetHealth = (results, callback) => {
log.debug("Checking target database health");
if (exports.env.statistics.target.status == "red") {
callback("The target database is experiencing and error and cannot proceed");
} else {
callback(null);
}
};
/**
* Calls the source driver to retrieve the metadata.
*
* @param {Object} results Ignored results object from async
* @param {errorCb} callback
*/
exports.getMetadata = (results, callback) => {
if (!exports.env.options.run.mapping) {
return callback();
}
async.retry(exports.env.options.errors.retry, callback => {
if (exports.env.options.mapping) {
log.debug("Using mapping overridden through options");
callback(null, exports.env.options.mapping);
} else {
log.debug("Fetching mapping from source database");
let source = drivers.get(exports.env.options.drivers.source).driver;
source.getMeta(exports.env, callback);
}
// TODO validate metadata format
}, callback);
};
/**
* Send the retrieved metadata to the target driver to be stored.
*
* @param {Object} results Results object from async() that holds the getMetadata response
* @param {Metadata} results.getMetadata The metadata from the source driver
* @param {errorCb} callback
*/
exports.storeMetadata = (results, callback) => {
if (!exports.env.options.run.mapping) {
return callback();
}
async.retry(exports.env.options.errors.retry, callback => {
if (exports.env.options.run.test) {
log.info("Not storing meta data on target database because we're doing a test run.");
return callback();
}
let target = drivers.get(exports.env.options.drivers.target).driver;
let metadata = results.getMetadata;
target.putMeta(exports.env, metadata, err => {
if (err) {
log.error(err);
} else {
log.info("Mapping on target database is now ready");
}
callback(err);
});
}, callback);
};
/**
* Performs the actual transfer of data once all other functions have returned without any errors.
*
* @param {Object} results Ignored results object from async
* @param {errorCb} callback
*/
exports.transferData = (results, callback) => {
if (!exports.env.options.run.data) {
return callback();
}
let processed = 0;
let pointer = 0;
let total = exports.env.statistics.hits.total = exports.env.statistics.source.docs.total || 0;
let step = Math.min(exports.env.options.run.step, total);
let sourceConcurrent = drivers.get(exports.env.options.drivers.source).info.threadsafe;
let targetConcurrent = drivers.get(exports.env.options.drivers.target).info.threadsafe;
let concurrency = sourceConcurrent && targetConcurrent ? exports.env.options.run.concurrency : 1;
if (!sourceConcurrent || !targetConcurrent) {
log.debug('Concurrency has been disabled because at least one of the drivers doesn\'t support it');
}
let pump = cluster.run(exports.env, concurrency);
pump.onWorkDone(processedDocs => {
processed += processedDocs;
exports.env.statistics.hits.processed = processed || 0;
log.status('Processed %s of %s entries (%s%%)', processed, total, Math.round(processed / total * 100));
});
pump.onEnd(() => {
exports.status = "done";
log.clearStatus();
log.info('Processed %s entries (100%%)', total);
callback();
});
pump.onError(err => {
processed = total;
callback(err);
});
exports.status = "running";
log.info("Starting data export");
async.until(() => pointer >= total, callback => {
pump.work(pointer, step, () => {
pointer += step;
callback();
});
}, err => {
err && log.error(err);
log.debug('Worker loop finished with %s of %s entries processed (%s%%)', processed, total, Math.round(processed / total * 100));
});
};
/**
* This function ties everything together and performs all the operations from reading options to the actual export.
*
* @param {errorCb} callback
*/
exports.run = callback => {
async.auto({
readOptions: exports.readOptions,
verifyOptions: ["readOptions", exports.verifyOptions],
resetSource: ["verifyOptions", exports.resetSource],
resetTarget: ["verifyOptions", exports.resetTarget],
getSourceStatistics: ["resetSource", exports.getSourceStatistics],
getTargetStatistics: ["resetTarget", exports.getTargetStatistics],
checkSourceHealth: ["getSourceStatistics", exports.checkSourceHealth],
checkTargetHealth: ["getTargetStatistics", exports.checkTargetHealth],
getMetadata: ["checkSourceHealth", exports.getMetadata],
storeMetadata: ["checkTargetHealth", "getMetadata", exports.storeMetadata],
transferData: ["storeMetadata", exports.transferData]
}, callback);
};
if (require.main === module) {
process.on('uncaughtException', exports.handleUncaughtExceptions);
process.on('exit', () => exports.env && exports.env.statistics && args.printSummary(exports.env.statistics));
exports.run(err => {
if (err) {
if (isNaN(err)) {
log.error("The driver reported an error:", err);
log.die(4);
} else {
log.die(err);
}
}
});
}