forked from awslabs/aws-lambda-redshift-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
1252 lines (1147 loc) · 36.6 KB
/
index.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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 debug = false;
var pjson = require('./package.json');
var region = process.env['AWS_REGION'];
if (!region || region === null || region === "") {
region = "us-east-1";
console.log("AWS Lambda Redshift Database Loader using default region " + region);
}
var aws = require('aws-sdk');
aws.config.update({
region : region
});
var s3 = new aws.S3({
apiVersion : '2006-03-01',
region : region
});
var dynamoDB = new aws.DynamoDB({
apiVersion : '2012-08-10',
region : region
});
var sns = new aws.SNS({
apiVersion : '2010-03-31',
region : region
});
require('./constants');
var kmsCrypto = require('./kmsCrypto');
kmsCrypto.setRegion(region);
var common = require('./common');
var async = require('async');
var uuid = require('node-uuid');
var pg = require('pg');
var upgrade = require('./upgrades');
// main function for AWS Lambda
exports.handler = function(event, context) {
/** runtime functions * */
/*
* Function which performs all version upgrades over time - must be able to
* do a forward migration from any version to 'current' at all times!
*/
exports.upgradeConfig = function(s3Info, currentConfig, callback) {
// v 1.x to 2.x upgrade for multi-cluster loaders
if (currentConfig.version !== pjson.version) {
upgrade.upgradeAll(dynamoDB, s3Info, currentConfig, callback);
} else {
// no upgrade needed
callback(null, s3Info, currentConfig);
}
};
/* callback run when we find a configuration for load in Dynamo DB */
exports.foundConfig = function(s3Info, err, data) {
if (err) {
console.log(err);
var msg = 'Error getting Redshift Configuration for ' + s3Info.prefix + ' from Dynamo DB ';
console.log(msg);
context.done(error, msg);
}
if (!data || !data.Item) {
// finish with no exception - where this file sits
// in the S3
// structure is not configured for redshift loads
console.log("No Configuration Found for " + s3Info.prefix);
context.done(null, null);
} else {
console.log("Found Redshift Load Configuration for " + s3Info.prefix);
var config = data.Item;
var thisBatchId = config.currentBatch.S;
// run all configuration upgrades required
exports.upgradeConfig(s3Info, config, function(err, s3Info, config) {
if (err) {
console.log(err);
context.done(error, err);
} else {
if (config.filenameFilterRegex) {
if (s3Info.key.match(config.filenameFilterRegex.S)) {
exports.checkFileProcessed(config, thisBatchId, s3Info);
} else {
console.log('Object ' + s3Info.key + ' excluded by filename filter \'' + config.filenameFilterRegex.S + '\'');
// scan the current batch to decide
// if it needs to be
// flushed due to batch timeout
exports.processPendingBatch(config, thisBatchId, s3Info);
}
} else {
// no filter, so we'll load the data
exports.checkFileProcessed(config, thisBatchId, s3Info);
}
}
});
}
};
/*
* function to add a file to the pending batch set and then call the success
* callback
*/
exports.checkFileProcessed = function(config, thisBatchId, s3Info) {
var itemEntry = s3Info.bucket + '/' + s3Info.key;
// perform the idempotency check for the file before we put it
// into
// a manifest
var fileEntry = {
Item : {
loadFile : {
S : itemEntry
}
},
Expected : {
loadFile : {
Exists : false
}
},
TableName : filesTable
};
// add the file to the processed list
dynamoDB.putItem(fileEntry, function(err, data) {
if (err) {
// the conditional check failed so the file has already
// been processed
if (err.code == conditionCheckFailed) {
console.log("File " + itemEntry + " Already Processed");
context.done(null, null);
} else {
var msg = "Error " + err.code + " for " + fileEntry;
console.log(msg);
failBatch(msg, config, thisBatchId, s3Info, undefined);
}
} else {
if (!data) {
var msg = "Idempotency Check on " + fileEntry + " failed";
console.log(msg);
exports.failBatch(msg, config, thisBatchId, s3Info, undefined);
} else {
// add was OK - proceed with adding the entry to the
// pending batch
exports.addFileToPendingBatch(config, thisBatchId, s3Info, itemEntry);
}
}
});
};
/**
* Function run to add a file to the existing open batch. This will
* repeatedly try to write and if unsuccessful it will requery the batch ID
* on the configuration
*/
exports.addFileToPendingBatch = function(config, thisBatchId, s3Info, itemEntry) {
console.log("Adding Pending Batch Entry for " + itemEntry);
var proceed = false;
var asyncError = undefined;
var addFileRetryLimit = 100;
var tryNumber = 0;
async
.whilst(
function() {
// return OK if the proceed flag has
// been set, or if we've hit the
// retry count
return !proceed && tryNumber < addFileRetryLimit;
},
function(callback) {
tryNumber++;
// build the reference to the
// pending batch, with an
// atomic add of the current file
var item = {
Key : {
batchId : {
S : thisBatchId
},
s3Prefix : {
S : s3Info.prefix
}
},
TableName : batchTable,
UpdateExpression : "add entries :entry set #stat = :open, lastUpdate = :updateTime",
ExpressionAttributeNames : {
"#stat" : 'status'
},
ExpressionAttributeValues : {
":entry" : {
SS : [ itemEntry ]
},
":updateTime" : {
N : '' + common.now(),
},
":open" : {
S : open
}
},
/*
* current batch can't be locked
*/
ConditionExpression : "#stat = :open or attribute_not_exists(#stat)"
};
// add the file to the pending batch
var configReloads = 0;
dynamoDB.updateItem(item, function(err, data) {
if (err) {
if (err.code === conditionCheckFailed) {
/*
* the batch I have a reference to was
* locked so reload the current batch ID
* from the config
*/
var configReloadRequest = {
Key : {
s3Prefix : {
S : s3Info.prefix
}
},
TableName : configTable,
ConsistentRead : true
};
dynamoDB.getItem(configReloadRequest, function(err, data) {
configReloads++;
if (err) {
if (err === provisionedThroughputExceeded) {
console.log("Provisioned Throughput Exceeded on reload of " + configTable + " due to locked batch write");
callback();
} else {
console.log(err);
callback(err);
}
} else {
/*
* reset the batch ID to the
* current marked batch
*/
thisBatchId = data.Item.currentBatch.S;
/*
* we've not set proceed to
* true, so async will retry
*/
console.log("Reload of Configuration Complete after attempting to write to Locked Batch " + thisBatchId + ". Attempt "
+ configReloads);
/*
* we can call into the callback
* immediately, as we probably
* just missed the pending batch
* processor's rotate of the
* configuration batch ID
*/
callback();
}
});
} else {
asyncError = err;
proceed = true;
callback();
}
} else {
/*
* no error - the file was added to the
* batch, so mark the operation as OK so
* async will not retry
*/
proceed = true;
callback();
}
});
},
function(err) {
if (err) {
// throw presented errors
console.log(err);
context.done(error, err);
} else {
if (asyncError) {
/*
* throw errors which were encountered
* during the async calls
*/
console.log(asyncError);
context.done(error, asyncError);
} else {
if (!proceed) {
/*
* process what happened if the
* iterative request to write to the
* open pending batch timed out
*
* TODO Can we force a rotation of the
* current batch at this point?
*/
var e = "Unable to write "
+ itemEntry
+ " in "
+ addFileRetryLimit
+ " attempts. Failing further processing to Batch "
+ thisBatchId
+ " which may be stuck in '"
+ locked
+ "' state. If so, unlock the back using `node unlockBatch.js <batch ID>`, delete the processed file marker with `node processedFiles.js -d <filename>`, and then re-store the file in S3";
console.log(e);
exports.sendSNS(config.failureTopicARN.S, "Lambda Redshift Loader unable to write to Open Pending Batch", e, function() {
context.done(error, e);
}, function(err) {
console.log(err);
context.done(error, "Unable to Send SNS Notification");
});
} else {
// the add of the file was successful,
// so we
exports.linkProcessedFileToBatch(itemEntry, thisBatchId);
// which is async, so may fail but we'll
// still sweep
// the pending batch
exports.processPendingBatch(config, thisBatchId, s3Info);
}
}
}
});
};
/**
* Function which will link the deduplication table entry for the file to
* the batch into which the file was finally added
*/
exports.linkProcessedFileToBatch = function(itemEntry, batchId) {
var updateProcessedFile = {
Key : {
loadFile : {
S : itemEntry
}
},
TableName : filesTable,
AttributeUpdates : {
batchId : {
Action : 'PUT',
Value : {
S : batchId
}
}
}
};
dynamoDB.updateItem(updateProcessedFile, function(err, data) {
// because this is an async call which doesn't affect
// process flow, we'll just log the error and do nothing with the OK
// response
if (err) {
console.log(err);
}
});
};
/**
* Function which links the manifest name used to load redshift onto the
* batch table entry
*/
exports.addManifestToBatch = function(config, thisBatchId, s3Info, manifestInfo) {
// build the reference to the pending batch, with an atomic
// add of the current file
var item = {
Key : {
batchId : {
S : thisBatchId
},
s3Prefix : {
S : s3Info.prefix
}
},
TableName : batchTable,
AttributeUpdates : {
manifestFile : {
Action : 'PUT',
Value : {
S : manifestInfo.manifestPath
}
},
lastUpdate : {
Action : 'PUT',
Value : {
N : '' + common.now()
}
}
}
};
dynamoDB.updateItem(item, function(err, data) {
if (err) {
console.log(err);
} else {
console.log("Linked Manifest " + manifestInfo.manifestName + " to Batch " + thisBatchId);
}
});
};
/**
* Function to process the current pending batch, and create a batch load
* process if required on the basis of size or timeout
*/
exports.processPendingBatch = function(config, thisBatchId, s3Info) {
// make the request for the current batch
var currentBatchRequest = {
Key : {
batchId : {
S : thisBatchId
},
s3Prefix : {
S : s3Info.prefix
}
},
TableName : batchTable,
ConsistentRead : true
};
dynamoDB.getItem(currentBatchRequest, function(err, data) {
if (err) {
if (err === provisionedThroughputExceeded) {
console.log("Provisioned Throughput Exceeded on read of " + batchTable);
callback();
} else {
console.log(err);
context.done(error, err);
}
} else if (!data || !data.Item) {
var msg = "No open pending Batch " + thisBatchId;
console.log(msg);
context.done(null, msg);
} else {
// check whether the current batch is bigger than the
// configured max size, or older than configured max age
var lastUpdateTime = data.Item.lastUpdate.N;
var pendingEntries = data.Item.entries.SS;
var doProcessBatch = false;
if (pendingEntries.length >= parseInt(config.batchSize.N)) {
console.log("Batch Size " + config.batchSize.N + " reached");
doProcessBatch = true;
}
if (config.batchTimeoutSecs && config.batchTimeoutSecs.N) {
if (common.now() - lastUpdateTime > parseInt(config.batchTimeoutSecs.N) && pendingEntries.length > 0) {
console.log("Batch Size " + config.batchSize.N + " not reached but reached Age " + config.batchTimeoutSecs.N + " seconds");
doProcessBatch = true;
}
}
if (doProcessBatch) {
// set the current batch to locked status
var updateCurrentBatchStatus = {
Key : {
batchId : {
S : thisBatchId,
},
s3Prefix : {
S : s3Info.prefix
}
},
TableName : batchTable,
AttributeUpdates : {
status : {
Action : 'PUT',
Value : {
S : locked
}
},
lastUpdate : {
Action : 'PUT',
Value : {
N : '' + common.now()
}
}
},
/*
* the batch to be processed has to be 'open', otherwise
* we'll have multiple processes all handling a single
* batch
*/
Expected : {
status : {
AttributeValueList : [ {
S : open
} ],
ComparisonOperator : 'EQ'
}
},
/*
* add the ALL_NEW return values so we have the most up
* to date version of the entries string set
*/
ReturnValues : "ALL_NEW"
};
dynamoDB.updateItem(updateCurrentBatchStatus, function(err, data) {
if (err) {
if (err.code === conditionCheckFailed) {
/*
* some other Lambda function has locked the
* batch - this is OK and we'll just exit
* quietly
*/
context.done(null, null);
} else if (err.code === provisionedThroughputExceeded) {
console.log("Provisioned Throughput Exceeded on " + batchTable + " while trying to lock Batch");
context.done(error, err);
} else {
console.log("Unable to lock Batch " + thisBatchId);
context.done(error, err);
}
} else {
if (!data.Attributes) {
var e = "Unable to extract latest pending entries set from Locked batch";
console.log(e);
context.done(error, e);
} else {
/*
* grab the pending entries from the locked
* batch
*/
pendingEntries = data.Attributes.entries.SS;
/*
* assign the loaded configuration a new batch
* ID
*/
var allocateNewBatchRequest = {
Key : {
s3Prefix : {
S : s3Info.prefix
}
},
TableName : configTable,
AttributeUpdates : {
currentBatch : {
Action : 'PUT',
Value : {
S : uuid.v4()
}
},
lastBatchRotation : {
Action : 'PUT',
Value : {
S : common.getFormattedDate()
}
}
}
};
dynamoDB.updateItem(allocateNewBatchRequest, function(err, data) {
if (err) {
console.log("Error while allocating new Pending Batch ID");
console.log(err);
context.done(error, err);
} else {
// OK - let's create the manifest file
exports.createManifest(config, thisBatchId, s3Info, pendingEntries);
}
});
}
}
});
} else {
console.log("No pending batch flush required");
context.done(null, null);
}
}
});
};
/**
* Function which will create the manifest for a given batch and entries
*/
exports.createManifest = function(config, thisBatchId, s3Info, batchEntries) {
console.log("Creating Manifest for Batch " + thisBatchId);
var manifestInfo = common.createManifestInfo(config);
// create the manifest file for the file to be loaded
var manifestContents = {
entries : []
};
for (var i = 0; i < batchEntries.length; i++) {
manifestContents.entries.push({
/*
* fix url encoding for files with spaces. Space values come in
* from Lambda with '+' and plus values come in as %2B. Redshift
* wants the original S3 value
*/
url : 's3://' + batchEntries[i].replace('+', ' ').replace('%2B', '+'),
mandatory : true
});
}
var s3PutParams = {
Bucket : manifestInfo.manifestBucket,
Key : manifestInfo.manifestPrefix,
Body : JSON.stringify(manifestContents)
};
console.log("Writing manifest to " + manifestInfo.manifestBucket + "/" + manifestInfo.manifestPrefix);
/*
* save the manifest file to S3 and build the rest of the copy command
* in the callback letting us know that the manifest was created
* correctly
*/
s3.putObject(s3PutParams, exports.loadRedshiftWithManifest.bind(undefined, config, thisBatchId, s3Info, manifestInfo));
};
/**
* Function run when the Redshift manifest write completes succesfully
*/
exports.loadRedshiftWithManifest = function(config, thisBatchId, s3Info, manifestInfo, err, data) {
if (err) {
console.log("Error on Manifest Creation");
console.log(err);
exports.failBatch(err, config, thisBatchId, s3Info, manifestInfo);
} else {
console.log("Created Manifest " + manifestInfo.manifestPath + " Successfully");
// add the manifest file to the batch - this will NOT stop
// processing if it fails
exports.addManifestToBatch(config, thisBatchId, s3Info, manifestInfo);
// convert the config.loadClusters list into a format that
// looks like a native dynamo entry
clustersToLoad = [];
for (var i = 0; i < config.loadClusters.L.length; i++) {
clustersToLoad[clustersToLoad.length] = config.loadClusters.L[i].M;
}
console.log("Loading " + clustersToLoad.length + " Clusters");
// run all the cluster loaders in parallel
async.map(clustersToLoad, function(item, callback) {
// call the load cluster function, passing it the
// continuation callback
exports.loadCluster(config, thisBatchId, s3Info, manifestInfo, item, callback);
}, function(err, results) {
if (err) {
console.log(err);
}
// go through all the results - if they were all OK,
// then close the batch OK - otherwise fail
var allOK = true;
var loadState = {};
for (var i = 0; i < results.length; i++) {
if (!results[i] || results[i].status === ERROR) {
var allOK = false;
console.log("Cluster Load Failure " + results[i].error + " on Cluster " + results[i].cluster);
}
// log the response state for each cluster
loadState[results[i].cluster] = {
status : results[i].status,
error : results[i].error
};
}
var loadStateRequest = {
Key : {
batchId : {
S : thisBatchId,
},
s3Prefix : {
S : s3Info.prefix
}
},
TableName : batchTable,
AttributeUpdates : {
clusterLoadStatus : {
Action : 'PUT',
Value : {
S : JSON.stringify(loadState)
}
},
lastUpdate : {
Action : 'PUT',
Value : {
N : '' + common.now()
}
}
}
};
dynamoDB.updateItem(loadStateRequest, function(err, data) {
if (err) {
console.log("Error while attaching per-Cluster Load State");
exports.failBatch(err, config, thisBatchId, s3Info, manifestInfo);
} else {
if (allOK === true) {
// close the batch as OK
exports.closeBatch(null, config, thisBatchId, s3Info, manifestInfo, loadState);
} else {
// close the batch as failure
exports.failBatch(loadState, config, thisBatchId, s3Info, manifestInfo);
}
}
});
});
}
};
/**
* Function which loads a redshift cluster
*
*/
exports.loadCluster = function(config, thisBatchId, s3Info, manifestInfo, clusterInfo, callback) {
/* build the redshift copy command */
var copyCommand = '';
// set the statement timeout to 10 seconds less than the remaining
// execution time on the lambda function, or 60 seconds if we can't
// resolve the time remaining. fail the lambda function if we have less
// than 5 seconds remaining
var remainingMillis;
if (context) {
remainingMillis = context.getRemainingTimeInMillis() - 10000;
if (remainingMillis < 5000) {
exports.failBatch("Remaining duration of " + remainingMillis + ' insufficient to load cluster', config, thisBatchId, s3Info, manifestInfo);
} else {
copyCommand = 'set statement_timeout to ' + (remainingMillis) + ';\n';
}
} else {
copyCommand = 'set statement_timeout to 60000;\n';
}
var copyOptions = "manifest ";
// add the truncate option if requested
if (clusterInfo.truncateTarget && clusterInfo.truncateTarget.BOOL) {
copyCommand = 'truncate table ' + clusterInfo.targetTable.S + ';\n';
}
var encryptedItems = {};
var useLambdaCredentialsToLoad = true;
const
s3secretKeyMapEntry = "s3secretKey";
const
passwordKeyMapEntry = "clusterPassword";
const
symmetricKeyMapEntry = "symmetricKey";
if (config.secretKeyForS3) {
encryptedItems[s3secretKeyMapEntry] = kmsCrypto.stringToBuffer(config.secretKeyForS3.S);
useLambdaCredentialsToLoad = false;
}
if (debug) {
console.log("Loading Cluster " + clusterInfo.clusterEndpoint.S + " with " + (useLambdaCredentialsToLoad == true ? "Lambda" : "configured") + " credentials");
}
// add the cluster password
encryptedItems[passwordKeyMapEntry] = kmsCrypto.stringToBuffer(clusterInfo.connectPassword.S);
// add the master encryption key to the list of items to be decrypted,
// if there is one
if (config.masterSymmetricKey) {
encryptedItems[symmetricKeyMapEntry] = kmsCrypto.stringToBuffer(config.masterSymmetricKey.S);
}
// decrypt the encrypted items
kmsCrypto
.decryptMap(
encryptedItems,
function(err, decryptedConfigItems) {
if (err) {
callback(err, {
status : ERROR,
cluster : clusterInfo.clusterEndpoint.S
});
} else {
// create the credentials section
var credentials;
if (useLambdaCredentialsToLoad === true) {
credentials = 'aws_access_key_id=' + aws.config.credentials.accessKeyId + ';aws_secret_access_key=' + aws.config.credentials.secretAccessKey
+ ';token=' + aws.config.credentials.sessionToken;
} else {
credentials = 'aws_access_key_id=' + config.accessKeyForS3.S + ';aws_secret_access_key=' + decryptedConfigItems[s3secretKeyMapEntry].toString();
}
if (typeof clusterInfo.columnList === 'undefined') {
copyCommand = copyCommand + 'begin;\nCOPY ' + clusterInfo.targetTable.S + ' from \'s3://' + manifestInfo.manifestPath + '\'';
} else {
copyCommand = copyCommand + 'begin;\nCOPY ' + clusterInfo.targetTable.S + ' (' + clusterInfo.columnList.S + ') from \'s3://'
+ manifestInfo.manifestPath + '\'';
}
// add data formatting directives to copy
// options
if (config.dataFormat.S === 'CSV') {
// if removequotes or escape has been used
// in copy options, then we wont use the CSV
// formatter
if (!(config.copyOptions && (config.copyOptions.S.toUpperCase().indexOf('REMOVEQUOTES') > -1 || config.copyOptions.S.toUpperCase().indexOf(
'ESCAPE') > -1))) {
copyOptions = copyOptions + 'format csv ';
}
copyOptions = copyOptions + 'delimiter \'' + config.csvDelimiter.S + '\'\n';
} else if (config.dataFormat.S === 'JSON' || config.dataFormat.S === 'AVRO') {
copyOptions = copyOptions + ' format ' + config.dataFormat.S;
if (!(config.jsonPath === undefined || config.jsonPath === null)) {
copyOptions = copyOptions + ' \'' + config.jsonPath.S + '\' \n';
} else {
copyOptions = copyOptions + ' \'auto\' \n';
}
} else {
callback(null, {
status : ERROR,
error : 'Unsupported data format ' + config.dataFormat.S,
cluster : clusterInfo.clusterEndpoint.S
});
}
// add compression directives
if (config.compression !== undefined) {
copyOptions = copyOptions + ' ' + config.compression.S + '\n';
}
// add copy options
if (config.copyOptions !== undefined) {
copyOptions = copyOptions + config.copyOptions.S + '\n';
}
// add the encryption option to the copy
// command, and the master
// symmetric key clause to the credentials
if (config.masterSymmetricKey) {
copyOptions = copyOptions + "encrypted\n";
if (!decryptedConfigItems[symmetricKeyMapEntry]) {
credentials = credentials + ";master_symmetric_key=" + decryptedConfigItems[symmetricKeyMapEntry].toString();
} else {
console.log(JSON.stringify(decryptedConfigItems));
// we didn't get a decrypted symmetric
// key back
// so fail
callback(null, {
status : ERROR,
error : "KMS did not return a Decrypted Master Symmetric Key Value from: " + config.masterSymmetricKey.S,
cluster : clusterInfo.clusterEndpoint.S
});
}
}
// build the final copy command
copyCommand = copyCommand + " with credentials as \'" + credentials + "\' " + copyOptions + ";\ncommit;";
if (debug) {
console.log(copyCommand);
}
// build the connection string
var dbString = 'postgres://' + clusterInfo.connectUser.S + ":" + encodeURIComponent(decryptedConfigItems[passwordKeyMapEntry].toString()) + "@"
+ clusterInfo.clusterEndpoint.S + ":" + clusterInfo.clusterPort.N;
if (clusterInfo.clusterDB) {
dbString = dbString + '/' + clusterInfo.clusterDB.S;
}
console.log("Connecting to Database " + clusterInfo.clusterEndpoint.S + ":" + clusterInfo.clusterPort.N);
/*
* connect to database and run the copy command
*/
pg.connect(dbString, function(err, client, done) {
if (err) {
callback(null, {
status : ERROR,
error : err,
cluster : clusterInfo.clusterEndpoint.S
});
} else {
client.query(copyCommand, function(err, result) {
// release the client thread back to
// the pool
done();
// handle errors and cleanup
if (err) {
callback(null, {
status : ERROR,
error : err,
cluster : clusterInfo.clusterEndpoint.S
});
} else {
console.log("Load Complete");
callback(null, {
status : OK,
error : null,
cluster : clusterInfo.clusterEndpoint.S
});
}
});
}
});
}
});
};
/**
* Function which marks a batch as failed and sends notifications
* accordingly
*/
exports.failBatch = function(loadState, config, thisBatchId, s3Info, manifestInfo) {
console.log(loadState);
if (config.failedManifestKey && manifestInfo) {
// copy the manifest to the failed location
manifestInfo.failedManifestPrefix = manifestInfo.manifestPrefix.replace(manifestInfo.manifestKey + '/', config.failedManifestKey.S + '/');
manifestInfo.failedManifestPath = manifestInfo.manifestBucket + '/' + manifestInfo.failedManifestPrefix;
var copySpec = {
Bucket : manifestInfo.manifestBucket,
Key : manifestInfo.failedManifestPrefix,
CopySource : manifestInfo.manifestPath
};
s3.copyObject(copySpec, function(err, data) {
if (err) {
console.log(err);
exports.closeBatch(err, config, thisBatchId, s3Info, manifestInfo);
} else {
console.log('Created new Failed Manifest ' + manifestInfo.failedManifestPath);
// update the batch entry showing the failed
// manifest location
var manifestModification = {
Key : {
batchId : {
S : thisBatchId
},
s3Prefix : {
S : s3Info.prefix
}
},
TableName : batchTable,
AttributeUpdates : {
manifestFile : {
Action : 'PUT',
Value : {
S : manifestInfo.failedManifestPath
}
},
lastUpdate : {
Action : 'PUT',
Value : {
N : '' + common.now()
}
}
}
};
dynamoDB.updateItem(manifestModification, function(err, data) {
if (err) {
console.log(err);
exports.closeBatch(err, config, thisBatchId, s3Info, manifestInfo);
} else {
// close the batch with the original
// calling error
exports.closeBatch(loadState, config, thisBatchId, s3Info, manifestInfo);
}
});
}
});
} else {
console.log('Not requesting copy of Manifest to Failed S3 Location');
exports.closeBatch(loadState, config, thisBatchId, s3Info, manifestInfo);
}
};
/**
* Function which closes the batch to mark it as done, including
* notifications
*/
exports.closeBatch = function(batchError, config, thisBatchId, s3Info, manifestInfo) {
var batchEndStatus;
if (batchError && batchError !== null) {
batchEndStatus = error;
} else {