-
Notifications
You must be signed in to change notification settings - Fork 48
/
diff.js
293 lines (237 loc) · 10.8 KB
/
diff.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
var _ = require('underscore');
var queue = require('queue-async');
var Dyno = require('@mapbox/dyno');
var stream = require('stream');
var assert = require('assert');
module.exports = function(config, done) {
var primary = Dyno(config.primary);
var replica = Dyno(config.replica);
primary.tableName = config.primary.table;
replica.tableName = config.replica.table;
primary.name = 'primary';
replica.name = 'replica';
var log = config.log || console.log;
var scanOpts = Object.prototype.hasOwnProperty.call(config, 'segment') && config.segments ?
{ Segment: config.segment, TotalSegments: config.segments } : undefined;
var discrepancies = 0;
var itemsScanned = 0;
var itemsCompared = 0;
var start = Date.now();
function report() {
var elapsed = (Date.now() - start) / 1000;
var scanRate = Math.min(itemsScanned, (itemsScanned / elapsed).toFixed(2));
var compareRate = Math.min(itemsCompared, (itemsCompared / elapsed).toFixed(2));
log('[progress] Scan rate: %s items @ %s items/s | Compare rate: %s items/s', itemsScanned, scanRate, compareRate);
}
var reporter = setInterval(report, 60000).unref();
function Aggregate() {
var aggregation = new stream.Transform({ objectMode: true });
aggregation.records = [];
aggregation._transform = function(record, enc, callback) {
aggregation.records.push(record);
if (aggregation.records.length < 25) return callback();
aggregation.push(aggregation.records);
aggregation.records = [];
callback();
};
aggregation._flush = function(callback) {
if (aggregation.records.length) aggregation.push(aggregation.records);
callback();
};
return aggregation;
}
function Compare(readFrom, compareTo, keySchema, deleteMissing) {
var noItem = deleteMissing ? 'extraneous' : 'missing';
var comparison = new stream.Transform({ objectMode: true });
comparison.discrepancies = 0;
comparison._transform = function(records, enc, callback) {
var params = { RequestItems: {} };
params.RequestItems[readFrom.tableName] = { Keys: [] };
itemsScanned += records.length;
var recordKeys = records.reduce(function(recordKeys, record) {
var key = keySchema.reduce(function(key, attribute) {
key[attribute] = record[attribute];
return key;
}, {});
params.RequestItems[readFrom.tableName].Keys.push(key);
recordKeys.push(key);
return recordKeys;
}, []);
var indexedRecords = records.reduce(function(indexedRecords, record, i) {
indexedRecords[JSON.stringify(recordKeys[i])] = record;
return indexedRecords;
}, {});
if (config.backfill) {
Object.keys(indexedRecords).forEach(function(key) {
var record = indexedRecords[key];
log('[backfill] %s', key);
comparison.discrepancies++;
itemsCompared++;
comparison.push({ put: record });
});
return callback();
}
var items = [];
(function read(params) {
readFrom.batchGetItem(params, function(err, data) {
if (err) return callback(err);
items = items.concat(data.Responses[readFrom.tableName]);
if (Object.keys(data.UnprocessedKeys).length)
return read({ RequestItems: data.UnprocessedKeys });
gotAll();
})
})(params);
function gotAll() {
var itemKeys = items.reduce(function(itemKeys, item) {
itemKeys.push(keySchema.reduce(function(key, attribute) {
key[attribute] = item[attribute];
return key;
}, {}));
return itemKeys;
}, []);
var indexedItems = items.reduce(function(indexedItems, item, i) {
indexedItems[JSON.stringify(itemKeys[i])] = item;
return indexedItems;
}, {});
var q = queue();
// Find missing records -- scan gave us a key but the batch read did not find a match
recordKeys.forEach(function(key) {
var item = indexedItems[JSON.stringify(key)];
if (!item) {
q.defer(function(next) {
compareTo.getItem({ Key: key, ConsistentRead: true }, function(err, data) {
itemsCompared++;
if (err) return next(err);
var record = data.Item;
if (record) {
comparison.discrepancies++;
log('[%s] %j', noItem, key);
if (!config.repair) return next();
if (deleteMissing) comparison.push({ remove: key });
else comparison.push({ put: record });
}
next();
});
});
}
});
// Find differing records -- iterate through each item that we did find in the batch read
_(indexedItems).each(function(item, key) {
itemsCompared++;
var record = indexedRecords[key];
var recordString = Dyno.serialize(record);
var itemString = Dyno.serialize(item);
try { assert.deepEqual(JSON.parse(recordString), JSON.parse(itemString)); }
catch (notEqual) {
q.defer(function(next) {
compareTo.getItem({ Key: JSON.parse(key), ConsistentRead: true }, function(err, data) {
if (err) return next(err);
var record = data.Item;
var recordString = Dyno.serialize(record);
try { assert.deepEqual(JSON.parse(recordString), JSON.parse(itemString)); }
catch (notEqual) {
comparison.discrepancies++;
log('[different] %s', key);
if (!config.repair) return next();
comparison.push({ put: record });
}
next();
});
});
}
});
q.awaitAll(function(err) { callback(err); });
}
};
return comparison;
}
function Write() {
var writer = new stream.Writable({ objectMode: true, highWaterMark: 40 });
writer.params = { RequestItems: {} };
writer.params.RequestItems[replica.tableName] = [];
writer.pending = false;
writer._write = function(item, enc, callback) {
if (!item.put && !item.remove) {
return callback(new Error('Invalid item sent to writer: ' + JSON.stringify(item)));
}
var buffer = writer.params.RequestItems[replica.tableName];
buffer.push(item.put ? { PutRequest: { Item: item.put } } : { DeleteRequest: { Key: item.remove } });
if (buffer.length < 25) return callback();
(function write(params) {
writer.pending = true;
replica.batchWriteItem(params, function(err, data) {
writer.pending = false;
if (err) return callback(err);
if (data.UnprocessedKeys && Object.keys(data.UnprocessedKeys).length)
return write({ RequestItems: data.UnprocessedKeys });
writer.params.RequestItems[replica.tableName] = [];
callback();
});
})(writer.params);
};
var streamEnd = writer.end.bind(writer);
writer.end = function() {
if (writer.pending) return setImmediate(writer.end);
if (!writer.params.RequestItems[replica.tableName].length)
return streamEnd();
(function write(params) {
replica.batchWriteItem(params, function(err, data) {
if (err) return streamEnd(err);
if (data.UnprocessedKeys && Object.keys(data.UnprocessedKeys).length)
return write({ RequestItems: data.UnprocessedKeys });
streamEnd();
});
})(writer.params);
};
return writer;
}
primary.describeTable(function(err, description) {
if (err) return done(err);
var keySchema = _(description.Table.KeySchema).pluck('AttributeName');
scanPrimary(keySchema);
});
function scanPrimary(keySchema) {
var aggregate = Aggregate();
var compare = Compare(replica, primary, keySchema, false);
var write = Write();
log('Scanning primary table and comparing to replica');
primary.scanStream(scanOpts)
.on('error', finish)
.pipe(aggregate)
.on('error', finish)
.pipe(compare)
.on('error', finish)
.pipe(write)
.on('error', finish)
.on('finish', function() {
discrepancies += compare.discrepancies;
log('[discrepancies] %s', compare.discrepancies);
if (!config.backfill) return scanReplica(keySchema);
finish();
});
}
function scanReplica(keySchema) {
var aggregate = Aggregate();
var compare = Compare(primary, replica, keySchema, true);
var write = Write();
log('Scanning replica table and comparing to primary');
replica.scanStream(scanOpts)
.on('error', finish)
.pipe(aggregate)
.on('error', finish)
.pipe(compare)
.on('error', finish)
.pipe(write)
.on('error', finish)
.on('finish', function() {
discrepancies += compare.discrepancies;
log('[discrepancies] %s', compare.discrepancies);
finish();
});
}
function finish(err) {
clearInterval(reporter);
report();
done(err, discrepancies);
}
};