This repository has been archived by the owner on Sep 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
firehoser.js
226 lines (200 loc) · 7.51 KB
/
firehoser.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
'use strict';
var _ = require('lodash');
var AWS = require('aws-sdk');
var async = require('async');
var JaySchema = require('jayschema');
var moment = require('moment');
var schemaValidator = new JaySchema();
class DeliveryStream{
constructor(name, awsConfig=null, schema=null, retryInterval=1500, firehose=null, logger=null){
this.maxIngestion = 400;
this.maxDrains = 3;
this.maxRetries = 40;
this.name = name;
if (awsConfig !== null){
AWS.config.update(awsConfig);
}
this.schema = schema;
this.retryInterval = retryInterval;
this.firehose = firehose ? firehose : new AWS.Firehose({params: {DeliveryStreamName: name}});
this.log = logger ? logger : () => {};
}
validateRecord(record){
return schemaValidator.validate(record, this.schema);
}
validateRecords(records){
if (!this.schema){
return [records, []];
}
let validRecords = [];
let invalidRecords = [];
_.forEach(records, (record) => {
let validationErrors = this.validateRecord(record);
if (_.isEmpty(validationErrors)){
validRecords.push(record);
} else {
let ve = validationErrors[0];
invalidRecords.push({
type: "schema",
originalRecord: record,
description: buildSchemaErrorDescription(ve),
details: ve,
});
}
});
return [validRecords, invalidRecords];
}
formatRecord(record){
return {Data: record + '\n'};
}
putRecord(record){
return this.putRecords([record]);
}
putRecords(records){
var self = this;
this.log(`DeliveryStream.putRecords() called with ${records.length} records.`);
return new Promise((resolve, reject) => {
// Validate records against a schema, if necessary.
let [validRecords, invalidRecords] = this.validateRecords(records);
// Split the records into reasonably-sized chunks.
records = _.map(validRecords, this.formatRecord);
let chunks = _.chunk(records, this.maxIngestion);
let tasks = [];
for (let i=0; i < chunks.length; i++){
tasks.push(this.drain.bind(this, chunks[i]));
}
// Schedule the chunks all at the same time.
self.log(`Kicking off ${tasks.length} calls to drain() for ${records.length} records.`);
async.parallelLimit(tasks, this.maxDrains, function(err, results){
let allErrors = invalidRecords.concat(_.flatten(results));
if (err || !_.isEmpty(allErrors)){
return reject(allErrors);
}
return resolve();
});
});
}
drain(records, cb, numRetries=0){
var leftovers = [];
// var self = this;
this.log(`Draining ${records.length} records. Pass #${numRetries + 1}`);
this.firehose.putRecordBatch({Records: records}, (firehoseErr, resp)=>{
// Stuff broke!
if (firehoseErr){
return cb(null, {
type: "firehose",
description: "Internal aws-sdk error.",
details: firehoseErr,
originalRecord: null
});
}
// Not all records make it in, but firehose keeps on chugging!
if (resp.FailedPutCount > 0){
}
// Push errored records back into the next list.
for (let [orig, result] of _.zip(records, resp.RequestResponses)){
if (!_.isUndefined(result.ErrorCode)){
this.log(`Got ErrorCode ${result.ErrorCode} for record ${orig}`,'error');
leftovers.push({
type: "firehose",
description: result.ErrorMessage,
details: {
ErrorCode: result.ErrorCode,
ErrorMessage: result.ErrorMessage,
},
originalRecord: orig,
});
}
}
// Recurse!
if (leftovers.length && numRetries < this.maxRetries){
// We're about to recurse, let the child handle storing error details.
leftovers = _.map(leftovers, pickData);
return setTimeout(()=>{
this.drain(leftovers, cb, numRetries + 1);
}, this.retryInterval);
} else {
return cb(null, leftovers);
}
});
}
}
class JSONDeliveryStream extends DeliveryStream {
formatRecord(record){
return super.formatRecord(JSON.stringify(record));
}
}
class QueueableDeliveryStream extends DeliveryStream {
constructor(name, maxTime=30000, maxSize=500, ...args){
super(name, ...args);
this.queue = [];
this.timeout = null;
this.maxTime = maxTime;
this.maxSize = maxSize;
this.promise = null;
setInterval(this.drainQueue.bind(this), this.maxTime);
}
putRecords(records){
this.log(`QueueableDeliveryStream.putRecords() called with ${records.length} records.`);
this.queue.push(...records);
if (this.promise === null){
this.promise = new Promise((resolve, reject) => {
this.resolver = resolve;
this.rejecter = reject;
});
}
this.log(`queue size is: ${this.queue.length}, maxSize is: ${this.maxSize}.`);
if (this.queue.length >= this.maxSize){
// Queue's full!
this.log(`queue is full, draining immediately.`);
setImmediate(this.drainQueue.bind(this));
}
return this.promise;
}
drainQueue(){
this.log(`Countdown timer expired or queue limit reached.`);
this.log(`Time to drain the queue of ${this.queue.length} records.`);
let toQueue = this.queue.splice(0, this.queue.length);
if (!toQueue.length){
this.log(`No records in queue, not draining anything.`);
return;
}
super.putRecords(toQueue).then(this.resolver, this.rejecter).then(() => {
this.promise = null;
this.rejecter = null;
this.resolver = null;
});
}
}
class QueueableJSONDeliveryStream extends QueueableDeliveryStream {
formatRecord(record){
return super.formatRecord(JSON.stringify(record));
}
}
class PlainDeliveryStream extends DeliveryStream {
formatRecord(record){
return { Data: record };
}
}
function buildSchemaErrorDescription(ve){
if (ve.desc){
return ve.desc;
}
let field = ve.instanceContext.replace(/(#\/)|(#)/ig, "").replace(/\//g, ".")
return `${ve.kind || 'Error'} on '${field}'. Expected ${ve.constraintName} to be ${ve.constraintValue}, actual value was ${ve.testedValue}.`
}
function makeRedshiftTimestamp(input){
return moment(input).utc().format('YYYY-MM-DD HH:mm:ss')
}
function pickData(leftover){
return _.pick(leftover.originalRecord, ['Data']);
}
module.exports = {
DeliveryStream: DeliveryStream,
JSONDeliveryStream: JSONDeliveryStream,
QueueableDeliveryStream: QueueableDeliveryStream,
QueueableJSONDeliveryStream: QueueableJSONDeliveryStream,
PlainDeliveryStream: PlainDeliveryStream,
makeRedshiftTimestamp: makeRedshiftTimestamp,
pickData: pickData
};