forked from awslabs/aws-lambda-redshift-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
357 lines (319 loc) · 8.71 KB
/
common.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
/*
Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Amazon Software License (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/asl/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
var async = require('async');
require('./constants');
// function which creates a string representation of now suitable for use in S3
// paths
exports.getFormattedDate = function(date) {
if (!date) {
date = new Date();
}
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var day = date.getDate();
day = (day < 10 ? "0" : "") + day;
return year + "-" + month + "-" + day + "-" + hour + ":" + min + ":" + sec;
};
/* current time as seconds */
exports.now = function() {
return new Date().getTime() / 1000;
};
exports.readableTime = function(epochSeconds) {
var d = new Date(0);
d.setUTCSeconds(epochSeconds);
return exports.getFormattedDate(d);
};
exports.createTables = function(dynamoDB, callback) {
// processed files table spec
var pfKey = 'loadFile';
var processedFilesSpec = {
AttributeDefinitions : [ {
AttributeName : pfKey,
AttributeType : 'S'
} ],
KeySchema : [ {
AttributeName : pfKey,
KeyType : 'HASH'
} ],
TableName : filesTable,
ProvisionedThroughput : {
ReadCapacityUnits : 5,
WriteCapacityUnits : 5
}
};
var configKey = s3prefix;
var configSpec = {
AttributeDefinitions : [ {
AttributeName : configKey,
AttributeType : 'S'
} ],
KeySchema : [ {
AttributeName : configKey,
KeyType : 'HASH'
} ],
TableName : configTable,
ProvisionedThroughput : {
ReadCapacityUnits : 5,
WriteCapacityUnits : 5
}
};
var batchKey = batchId;
var inputLoc = s3prefix;
var batchSpec = {
AttributeDefinitions : [ {
AttributeName : batchKey,
AttributeType : 'S'
}, {
AttributeName : 'status',
AttributeType : 'S'
}, {
AttributeName : lastUpdate,
AttributeType : 'N'
}, {
AttributeName : inputLoc,
AttributeType : 'S'
} ],
KeySchema : [ {
AttributeName : inputLoc,
KeyType : 'HASH'
}, {
AttributeName : batchKey,
KeyType : 'RANGE'
} ],
TableName : batchTable,
ProvisionedThroughput : {
ReadCapacityUnits : 1,
WriteCapacityUnits : 5
},
GlobalSecondaryIndexes : [ {
IndexName : batchStatusGSI,
KeySchema : [ {
AttributeName : 'status',
KeyType : 'HASH'
}, {
AttributeName : lastUpdate,
KeyType : 'RANGE'
} ],
Projection : {
ProjectionType : 'ALL'
},
ProvisionedThroughput : {
ReadCapacityUnits : 1,
WriteCapacityUnits : 5
}
} ]
};
console.log("Creating Tables in Dynamo DB if Required");
dynamoDB.createTable(processedFilesSpec, function(err, data) {
if (err) {
if (err.code !== 'ResourceInUseException') {
console.log(Object.prototype.toString.call(err).toString());
console.log(err.toString());
process.exit(ERROR);
}
}
dynamoDB.createTable(batchSpec, function(err, data) {
if (err) {
if (err.code !== 'ResourceInUseException') {
console.log(err.toString());
process.exit(ERROR);
}
}
dynamoDB.createTable(configSpec, function(err, data) {
if (err) {
if (err.code !== 'ResourceInUseException') {
console.log(err.toString());
process.exit(ERROR);
}
}
// now write the config item after the table has been
// provisioned
if (callback) {
setTimeout(callback(), 5000);
}
});
});
});
};
exports.updateConfig = function(setRegion, dynamoDB, updateRequest, outerCallback) {
var tryNumber = 0;
var writeConfigRetryLimit = 100;
async.whilst(function() {
// retry until the try count is hit
return tryNumber < writeConfigRetryLimit;
}, function(callback) {
tryNumber++;
dynamoDB.updateItem(updateRequest, function(err, data) {
if (err) {
if (err.code === 'ResourceInUseException' || err.code === 'ResourceNotFoundException') {
console.log(err.code);
// retry if the table is in use after 1 second
setTimeout(callback(), 1000);
} else {
// some other error - fail
console.log(JSON.stringify(updateRequest));
console.log(err);
outerCallback(err);
}
} else {
// all OK - exit OK
if (data) {
console.log("Configuration for " + updateRequest.Key.s3Prefix.S + " updated in " + setRegion);
outerCallback(null);
}
}
});
}, function(error) {
// never called
});
};
exports.writeConfig = function(setRegion, dynamoDB, dynamoConfig, outerCallback) {
var tryNumber = 0;
var writeConfigRetryLimit = 100;
async.whilst(function() {
// retry until the try count is hit
return tryNumber < writeConfigRetryLimit;
}, function(callback) {
tryNumber++;
dynamoDB.putItem(dynamoConfig, function(err, data) {
if (err) {
if (err.code === 'ResourceInUseException' || err.code === 'ResourceNotFoundException') {
// retry if the table is in use after 1 second
setTimeout(callback(), 1000);
} else {
// some other error - fail
console.log(JSON.stringify(dynamoConfig));
console.log(JSON.stringify(err));
if (outerCallback)
outerCallback(err);
}
} else {
// all OK - exit OK
if (data) {
console.log("Configuration for " + dynamoConfig.Item.s3Prefix.S + " successfully written in "
+ setRegion);
if (outerCallback)
outerCallback(null);
}
}
});
}, function(error) {
// never called
});
};
exports.dropTables = function(dynamoDB, callback) {
// drop the config table
dynamoDB.deleteTable({
TableName : configTable
}, function(err, data) {
if (err && err.code !== 'ResourceNotFoundException') {
console.log(err);
process.exit(ERROR);
} else {
// drop the processed files table
dynamoDB.deleteTable({
TableName : filesTable
}, function(err, data) {
if (err && err.code !== 'ResourceNotFoundException') {
console.log(err);
process.exit(ERROR);
} else {
// drop the batches table
dynamoDB.deleteTable({
TableName : batchTable
}, function(err, data) {
if (err && err.code !== 'ResourceNotFoundException') {
console.log(err);
process.exit(ERROR);
}
console.log("All Configuration Tables Dropped");
// call the callback requested
if (callback) {
callback();
}
});
}
});
}
});
};
/* validate that the given value is a number, and if so return it */
exports.getIntValue = function(value, rl) {
if (!value || value === null) {
rl.close();
console.log('Null Value');
process.exit(INVALID_ARG);
} else {
var num = parseInt(value);
if (isNaN(num)) {
rl.close();
console.log('Value \'' + value + '\' is not a Number');
process.exit(INVALID_ARG);
} else {
return num;
}
}
};
exports.getBooleanValue = function(value) {
if (value) {
if ([ 'TRUE', '1', 'YES', 'Y' ].indexOf(value.toUpperCase()) > -1) {
return true;
} else {
return false;
}
} else {
return false;
}
};
/* validate that the provided value is not null/undefined */
exports.validateNotNull = function(value, message, rl) {
if (!value || value === null || value === '') {
rl.close();
console.log(message);
process.exit(INVALID_ARG);
}
};
/* turn blank lines read from STDIN to Null */
exports.blank = function(value) {
if (value === '') {
return null;
} else {
return value;
}
};
exports.validateArrayContains = function(array, value, rl) {
if (!(array.indexOf(value) > -1)) {
rl.close();
console.log('Value must be one of ' + array.toString());
process.exit(INVALID_ARG);
}
};
exports.createManifestInfo = function(config) {
// manifest file will be at the configuration location, with a fixed
// prefix and the date plus a random value for uniqueness across all
// executing functions
var dateName = exports.getFormattedDate();
var rand = Math.floor(Math.random() * 10000);
var manifestInfo = {
manifestBucket : config.manifestBucket.S,
manifestKey : config.manifestKey.S,
manifestName : 'manifest-' + dateName + '-' + rand
};
manifestInfo.manifestPrefix = manifestInfo.manifestKey + '/' + manifestInfo.manifestName;
manifestInfo.manifestPath = manifestInfo.manifestBucket + "/" + manifestInfo.manifestPrefix;
return manifestInfo;
};
exports.randomInt = function(low, high) {
return Math.floor(Math.random() * (high - low) + low);
};