-
Notifications
You must be signed in to change notification settings - Fork 103
/
manual-consumer.js
240 lines (225 loc) · 8.65 KB
/
manual-consumer.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
'use strict';
const awsServices = require('@cumulus/aws-client/services');
const log = require('@cumulus/common/log');
const messageConsumer = require('./message-consumer');
const Kinesis = awsServices.kinesis();
const tallyReducer = (acc, cur) => acc + cur;
/**
* This function will accept as valid input an event whose `endTimestamp` and `startTimestamp`
* fields must contain valid input to the `new Date()` constructor if they exist.
* They will then be populated into `process.env` as ISO strings.
*
* @param {Object} event - input object
*/
const configureTimestampEnvs = (event) => {
if (!process.env.endTimestamp && event.endTimestamp) {
const dateObj = new Date(event.endTimestamp);
if (Number.isNaN(dateObj.valueOf())) {
throw new TypeError(`endTimestamp ${event.endTimestamp} is not a valid input for new Date().`);
}
process.env.endTimestamp = dateObj.toISOString();
}
if (!process.env.startTimestamp && event.startTimestamp) {
const dateObj = new Date(event.startTimestamp);
if (Number.isNaN(dateObj.valueOf())) {
throw new TypeError(`startTimestamp ${event.startTimestamp} is not a valid input for new Date().`);
}
process.env.startTimestamp = dateObj.toISOString();
}
};
/**
* Set up params object for call to `Kinesis.getShardIterator()`.
* Creates timestamp params if `process.env.startTimestamp` is set.
*
* @param {string} stream - stream name
* @param {string} shardId - shard ID
* @returns {Object} `getShardIterator` params object
*/
const setupIteratorParams = (stream, shardId) => {
const params = {
StreamName: stream,
ShardId: shardId
};
if (process.env.startTimestamp) {
params.ShardIteratorType = 'AT_TIMESTAMP';
params.Timestamp = process.env.startTimestamp;
} else {
params.ShardIteratorType = 'TRIM_HORIZON';
}
return params;
};
/**
* Set up params object for call to `Kinesis.listShards()`.
* `streamCreationTimestamp` is required when multiple streams with the same
* name exist in the Kinesis API (e.g. deleted and current streams).
*
* @param {string} stream - kinesis stream name
* @param {Date|string|number} [streamCreationTimestamp] - Stream creation timestamp
* used to differentiate streams that have a name used by a previous stream.
* @returns {Object} `listShards` params object
*/
const setupListShardParams = (stream, streamCreationTimestamp) => {
const params = {
StreamName: stream
};
if (streamCreationTimestamp) params.StreamCreationTimestamp = new Date(streamCreationTimestamp);
return params;
};
/**
* Process a batch of kinesis records.
*
* @param {Array<Object>} records - list of kinesis records
* @returns {number} number of records successfully processed
*/
async function processRecordBatch(records) {
const results = await Promise.all(records.map(async (record) => {
if (new Date(record.ApproximateArrivalTimestamp) > new Date(process.env.endTimestamp)) {
return 'skip';
}
try {
await messageConsumer.processRecord({ kinesis: { data: record.Data } }, false);
return 'ok';
} catch (err) {
log.error(err);
return 'err';
}
}));
const { skip, err, ok } = results.reduce((acc, cur) => {
acc[cur] += 1;
return acc;
}, { skip: 0, err: 0, ok: 0 });
if (skip > 0) {
log.info(
`Skipped ${skip} of ${records.length} records in batch for arriving after endTimestamp`
);
}
if (err > 0) {
log.warn(`Failed to process ${err} of ${records.length} records in batch`);
}
return ok;
}
/**
* Recursively process all records within a shard between start and end timestamps.
* Starts at beginning of shard (TRIM_HORIZON) if no start timestamp is available.
*
* @param {Array<Promise>} recordPromiseList - list of promises from calls to processRecordBatch
* @param {string} shardIterator - ShardIterator Id
* @returns {Array<Promise>} list of promises from calls to processRecordBatch
*/
async function iterateOverShardRecursively(recordPromiseList, shardIterator) {
try {
const response = await Kinesis.getRecords({
ShardIterator: shardIterator
}).promise();
recordPromiseList.push(processRecordBatch(response.Records));
if (response.MillisBehindLatest === 0 || !response.NextShardIterator) return recordPromiseList;
const nextShardIterator = response.NextShardIterator;
return iterateOverShardRecursively(recordPromiseList, nextShardIterator);
} catch (error) {
log.error(error);
return recordPromiseList;
}
}
/**
* Handle shard by creating shardIterator and calling processShard.
*
* @param {string} stream - kinesis stream name
* @param {string} shardId - shard ID
* @returns {number} number of records successfully processed from shard
*/
async function processShard(stream, shardId) {
const iteratorParams = setupIteratorParams(stream, shardId);
try {
const shardIterator = (
await Kinesis.getShardIterator(iteratorParams).promise()
).ShardIterator;
const tallyList = await Promise.all(await iterateOverShardRecursively([], shardIterator));
const shardTally = tallyList.reduce(tallyReducer, 0);
return shardTally;
} catch (err) {
log.error(err);
return 0;
}
}
/**
* Recursively fetch all records within a kinesis stream and process them through
* message-consumer's processRecord function.
*
* @param {string} stream - Kinesis stream name
* @param {Array<Promise>} shardPromiseList - list of promises from calls to processShard
* @param {Object} params - listShards query params
* @returns {Array<Promise>} list of promises from calls to processShard
*/
async function iterateOverStreamRecursivelyToDispatchShards(stream, shardPromiseList, params) {
const listShardsResponse = (await Kinesis.listShards(params).promise().catch(log.error));
if (!listShardsResponse || !listShardsResponse.Shards || listShardsResponse.Shards.length === 0) {
log.error(`No shards found for params ${JSON.stringify(params)}.`);
return shardPromiseList;
}
log.info(`Processing records from ${listShardsResponse.Shards.length} shards..`);
const shardCalls = listShardsResponse.Shards.map(
(shard) => processShard(stream, shard.ShardId).catch(log.error)
);
shardPromiseList.push(...shardCalls);
if (!listShardsResponse.NextToken) {
return shardPromiseList;
}
const newParams = { NextToken: listShardsResponse.NextToken };
return iterateOverStreamRecursivelyToDispatchShards(stream, shardPromiseList, newParams);
}
/**
* Fetch all records within a kinesis stream and process them through
* message-consumer's processRecord function.
*
* @param {string} stream - kinesis stream name
* @param {Date|string|number} [streamCreationTimestamp] - Optional. Stream creation
* timestamp used to differentiate streams that have a name used by a previous stream.
* @returns {number} number of records successfully processed from stream
*/
async function processStream(stream, streamCreationTimestamp) {
const initialParams = setupListShardParams(stream, streamCreationTimestamp);
const streamPromiseList = await iterateOverStreamRecursivelyToDispatchShards(
stream, [], initialParams
);
const streamResults = await Promise.all(streamPromiseList);
const recordsProcessed = streamResults.reduce(tallyReducer, 0);
const outMsg = `Processed ${recordsProcessed} kinesis records from stream ${stream}`;
log.info(outMsg);
return outMsg;
}
/**
* Manual Consumer handler. Determines operation from input.
* Supports manually consuming:
* - Kinesis records.
*
* @param {Object} event - input params object
* @returns {string} String describing outcome
*/
async function handler(event) {
configureTimestampEnvs(event);
if (!process.env.CollectionsTable) process.env.CollectionsTable = event.CollectionsTable;
if (!process.env.RulesTable) process.env.RulesTable = event.RulesTable;
if (!process.env.ProvidersTable) process.env.ProvidersTable = event.ProvidersTable;
if (!process.env.stackName) process.env.stackName = event.stackName;
if (!process.env.system_bucket) process.env.system_bucket = event.system_bucket;
if (!process.env.FallbackTopicArn) process.env.FallbackTopicArn = event.FallbackTopicArn;
if (event.type === 'kinesis' && event.kinesisStream !== undefined) {
log.info(`Processing records from stream ${event.kinesisStream}`);
return processStream(event.kinesisStream, event.kinesisStreamCreationTimestamp);
}
const errMsg = 'Manual consumer could not determine expected operation'
+ ` from event ${JSON.stringify(event)}`;
log.fatal(errMsg);
return errMsg;
}
module.exports = {
configureTimestampEnvs,
handler,
iterateOverShardRecursively,
iterateOverStreamRecursivelyToDispatchShards,
processRecordBatch,
processShard,
processStream,
setupIteratorParams,
setupListShardParams
};