-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreplay.js
223 lines (195 loc) · 5.68 KB
/
replay.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
exports.command = 'replay [bucket] [prefix]'
exports.describe = 'Replay the events in [bucket] for [prefix]'
const _ = require('highland');
const lodash = require('lodash');
const now = require('moment')().utc();
exports.builder = {
bucket: {
alias: 'b',
describe: 'bucket containing the events'
},
stream: {
alias: 's',
describe: 'stream that delivered the events - root prefix'
},
prefix: {
alias: 'p',
default: `${now.format('YYYY')}/${now.format('MM')}/${now.format('DD')}/`
},
type: {
alias: 't',
default: '*'
},
function: {
alias: 'f',
describe: 'function to replay the events to'
},
qualifier: {
default: '$LATEST'
},
dry: {
default: true,
type: 'boolean'
},
batch: {
default: 25
},
parallel: {
default: 16
},
rate: {
default: 2
},
window: {
default: 500
},
region: {
alias: 'r',
default: process.env.AWS_REGION || 'us-east-1'
},
source: {
alias: 'sp',
description: 'source account profile',
default: process.env.AWS_PROFILE || 'default'
},
target: {
alias: 'tp',
description: 'target account profile',
default: process.env.AWS_PROFILE || 'default'
},
// TODO target format
format: {
alias: 'f',
choices: [
'kinesis', // events wrapped in kinesis records
'eventbridge', // event wrapped in cloudwatch event format, delimited by EOL
'raw' // raw event format, delimited by EOL
],
default: 'kinesis' // original
},
}
exports.handler = (argv) => {
const common = require('../../lib/common')(argv);
common.print(argv);
const filterByType = obj => argv.type === '*' || argv.type === obj.event.type;
let batched = [];
const batchWithSize = function(err, x, push, next) {
if (err) {
push(err);
next();
}
else if (x === nil) {
if (batched.length > 0) {
push(null, batched);
}
push(null, nil);
}
else {
// TODO format x as a Record (kinesis or dynamo) or cw event
let buf = Buffer.from(JSON.stringify({
Records: batched.concat(x)
}));
if (buf.length <= 100000) {
batched.push(x);
} else {
push(null, batched);
batched = [x];
}
next();
}
}
const replay = function(s) {
return s
.map(obj => common.s3.getEvents(obj))
.parallel(argv.parallel)
.filter(filterByType)
.map(obj => {
typeCount.total = (typeCount.total ? typeCount.total : 0) + 1;
typeCount[obj.event.type] = (typeCount[obj.event.type] ? typeCount[obj.event.type] : 0) + 1;
return obj.record;
})
.consume(batchWithSize)
.tap(batch => console.log('Batch Size: ', batch.length))
.map(batch => {
return {
records: batch
};
})
// .tap(common.print)
.ratelimit(argv.rate, argv.window)
.map(batch => common.lambda.invoke(batch))
.parallel(argv.parallel)
// .tap(common.print)
.reduce(0, (counter, resp) => {
console.log('Running Event Count: ', counter + resp.batch);
console.log('Running Error Count: ', errs.length);
console.log('Running Invoke Count: ', common.lambda.count);
return counter + resp.batch;
})
.tap(counter => eventCount += counter)
;
}
const errors = function(err, push) {
console.error(err);
console.error(err.stack);
if (err.obj) {
errs.push(err);
} else {
push(err);
}
}
let typeCount = {};
let eventCount = 0;
let errs = [];
const retryErrors = function(s) {
const errorStream = () => {
if (errs.length) console.log('Retrying: %s Error(s)', errs.length);
let errors = errs;
errs = [];
batched = [];
return _((push, next) => {
let err = errors.shift();
if (err) {
console.log('Retrying: %j', err);
push(null, err.obj);
next();
} else {
push(null, _.nil);
}
})
.through(replay)
.errors(err => {
errs.push(err);
console.error(err);
console.error(err.stack);
})
;
};
return s
.consume(function(err, x, push, next) {
if (err) {
push(err);
next();
}
else if (x === _.nil) {
next(errorStream());
}
else {
push(null, x);
next();
}
})
;
}
common.s3.paginate()
.through(replay)
.errors(errors)
.through(retryErrors)
.done(() => {
console.log('Event Count: %s', eventCount);
console.log('Error Count: %s', errs.length);
console.log('Invoke Count: %s', common.lambda.count);
console.log('Types: %s', JSON.stringify(typeCount, null, 2))
})
;
}