-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataPull.ecl
1136 lines (1053 loc) · 51.1 KB
/
DataPull.ecl
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
/**
* Provide delta copy functionality for files and superfiles that match
* one or more filename patterns.
*
* This is strictly a "pull" copy scheme where the intention is to make the
* local system (whatever is running this code) "mirror" the remote system,
* strictly for those files that match one or more of the given filename
* patterns. Care should be taken when specifying filename patterns, especially
* those with prefix and suffix wildcards (e.g. *fubar*). Any local file or
* superfile that matches a pattern is subject to modification or deletion,
* depending on whether that file exists on the remote system or not. It is
* easy to lose local files that way, by inadvertently referencing them with
* a filename pattern intended for something else.
*
* The full contents of superfiles will be copied as well, even if the subfiles
* do not match any of the filename patterns. Relatedly, superfile contents
* are modified if necessary, such as when the remote system lists different
* subfiles for a superfile that the local system already has. In that case,
* the code will copy any subfiles (if necessary) and alter the superfile
* relationships so they match the remote system.
*
* Regular files are copied only if necessary. If a file already exists in both
* the systems, it is examined for change (size, content or metadata) and
* copied only if a difference is found.
*
* Optional cluster name mapping is supported. This covers the case where a
* remote file may exist on a cluster with a name that doesn't exist on the
* local system. The most common example is probably 'thor' vs. 'mythor' --
* two common Thor cluster names that seem to pop up in simple configurations.
* The map indicates on which local cluster to put a new or modified file,
* file, given the name of the remote cluster.
*
* The code can be executed in "dry run" mode (which is the default). In this
* mode, every action that would normally be taken is compiled into a list of
* commands and then displayed in a workunit result. This gives you the
* opportunity to see what the code would do if only given the chance.
*
* This code must be executed on the hthor HPCC engine. If you try to execute
* it on a different engine then it will fail with an informative error.
*
* KNOWN LIMITATION: This code will not correctly process Roxie indexes that
* are in use on the local system and need to be modified, nor will it update
* local Roxie queries that need new data coming in from the remote system.
*
* Exported functions:
*
* - CollectFileInfo: Collects and analyzes file information
* - Go: Executes information from CollectFileInfo()
*
* Example code is provided in a comment block at the end of this file.
*/
IMPORT Std;
EXPORT DataPull := MODULE
// Action indicators
EXPORT SYNC_ACTION := ENUM
(
UNSIGNED1,
UNKNOWN = 0,
ADD = 1,
MODIFY = 2,
DELETE_FILE = 3,
DELETE_SUPERFILE = 4
);
// Map from one cluster to another, used to place a file on a local cluster
// when the local cluster's name does not match the remote cluster's name
EXPORT ClusterMapRec := RECORD
STRING remoteCluster;
STRING localCluster;
END;
// Collected information about files
EXPORT FileInfoRec := RECORD
UNSIGNED2 level;
Std.File.FsLogicalFileInfoRecord;
STRING fileCRC;
STRING formatCRC;
END;
// Collected information about superfile/subfile relationships
EXPORT SuperfileRelationshipRec := RECORD
STRING superFilePath;
STRING subFilePath;
BOOLEAN subFileIsSuperfile;
UNSIGNED2 level;
END;
// Information regarding a file that must be modified
EXPORT FileActionRec := RECORD
STRING path;
STRING sourceCluster;
BOOLEAN isSuperFile;
SYNC_ACTION syncAction;
END;
// Information regarding a superfile relationship that must be modified
EXPORT SuperFileActionRec := RECORD
STRING superFilePath;
STRING subFilePath;
SYNC_ACTION syncAction;
END;
// Summarization of actions compiled by processing FileActionRec and
// SuperFileActionRec data
EXPORT ActionSummaryRec := RECORD
STRING actionDescription;
UNSIGNED4 fileCount;
END;
//--------------------------------------------------------------------------
/**
* Default values used in exported function arguments
*/
SHARED DEFAULT_FILENAME_PATTERNS := ['*'];
/**
* Remove any prefixing tilde character from a path
*
* @param path An HPCC path
*
* @return Cleaned path.
*/
SHARED NoAbsPath(STRING path) := REGEXREPLACE('^~', path, '');
/**
* Ensure that a given path is absolute (contains a prefixing tilde).
*
* @param path An HPCC path
*
* @return Cleaned path.
*/
SHARED AbsPath(STRING path) := '~' + NoAbsPath(path);
/**
* Wrap the given string in apostrophes.
*
* @param s String to wrap.
*
* @return Wrapped string value.
*/
SHARED Quoted(STRING s) := '\'' + s + '\'';
/**
* Given a path, ensure it is absolute and then wrap it in apostrophes.
* Useful for displaying an absolute path.
*
* @param path An HPCC path
*
* @return Cleaned and quoted path.
*/
SHARED QuotedAbsPath(STRING path) := Quoted(AbsPath(path));
/**
* Test whether the given Dali address is the same as the local Dali
* address.
*
* @param dali The IP address of a Dali system as a string
*
* @return TRUE if the given Dali is the local Dali, FALSE otherwise.
*/
SHARED DaliIsRemote(STRING dali) := dali != Std.System.Thorlib.DaliServer();
/**
* Create a 'foreign' prefix that can be prepended to a path to make the
* path reference a file on another HPCC system.
*
* @param dali The IP address of a Dali system as a string
*
* @return The prefix needed to convert a local path to a foreign path.
*/
SHARED ForeignPrefix(STRING dali) := 'foreign::' + dali + '::';
/**
* Convert a local path to a foreign path to the given Dali.
*
* @param path An HPCC logical path
* @param dali The IP address of the remote Dali system
*
* @return The prefixed path.
*/
SHARED ForeignPath(STRING path, STRING dali) := '~' + ForeignPrefix(dali) + REGEXREPLACE('^~', path, '');
/**
* Ensure that the given path is foreign. If necessary, a foreign prefix
* will be prepended to path (if the given Dali is the local Dali then
* the path is merely converted to an absolute path)
*
* @param path An HPCC logical path
* @param dali The IP address of the remote Dali system
*
* @return The foreign path.
*/
SHARED EnsureForeignPath(STRING path, STRING dali) := MAP
(
DaliIsRemote(dali) AND Std.Str.StartsWith(path, '~foreign::') => path,
DaliIsRemote(dali) => ForeignPath(path, dali),
AbsPath(path)
);
/**
* Gather information on files and superfiles that match the given set of
* filename patterns.
*
* @param dali The IP address of the Dali to check
* for files
* @param patterns A set of filename patterns to match
* @param disableContentCheck If TRUE do no collect format or
* content CRC values
* @param asOfDate A date or datetime representing a cutoff;
* files modified prior to this value are
* ignored at both the remote and local
* clusters; use an empty string to denote
* no limit on date; if provided, the format
* must be either YYYY-MM-DD or
* YYYY-MM-DDTHH:MM:SS (note the T delimiter)
*
* @return A DATASET(FileInfoRec) containing the information. Note that
* both files and superfiles are gathered.
*/
SHARED GetInfoForFilesMatchingPatterns(STRING dali,
SET OF STRING patterns,
BOOLEAN disableContentCheck,
STRING asOfDate) := FUNCTION
// Gather files that match each given pattern; matched files will be
// in a child recordset
embeddedResults := PROJECT
(
NOFOLD(DATASET(patterns, {STRING aPattern})),
TRANSFORM
(
{
STRING foundWithPattern,
DATASET(Std.File.FsLogicalFileInfoRecord) infoList
},
thePattern := TRIM(LEFT.aPattern, LEFT, RIGHT);
SELF.foundWithPattern := thePattern,
SELF.infoList := Std.File.LogicalFileList
(
thePattern,
includenormal := TRUE,
includesuper := TRUE,
foreigndali := dali
)
)
);
// Filter out older results and flatten everything, then optionally
// append additional CRC information
flatResults := NORMALIZE
(
embeddedResults,
LEFT.infoList(modified = '' OR modified >= asOfDate),
TRANSFORM
(
FileInfoRec,
SELF.level := 1,
SELF.fileCRC := IF(~RIGHT.superfile AND ~disableContentCheck, Std.File.GetLogicalFileAttribute(EnsureForeignPath(RIGHT.name, dali), 'fileCrc'), ''),
SELF.formatCRC := IF(~RIGHT.superfile AND ~disableContentCheck, Std.File.GetLogicalFileAttribute(EnsureForeignPath(RIGHT.name, dali), 'formatCrc'), ''),
SELF := RIGHT
)
);
// A file can match more than one pattern, so dedup on name
dedupedResults := DEDUP(SORT(flatResults, name), name);
RETURN dedupedResults;
END;
/**
* Gather information on all subfiles referenced by superfiles. This is a
* recursive function and will drill down as far as it needs to go.
*
* @param fileInfoList A dataset containing previously-derived
* file info, as from
* GetInfoForFilesMatchingPatterns()
* @param dali The IP address of the Dali to check
* for files
* @param disableContentCheck If TRUE do no collect format or
* content CRC values
* @param asOfDate A date or datetime representing a cutoff;
* files modified prior to this value are
* ignored at both the remote and local
* clusters; use an empty string to denote
* no limit on date; if provided, the format
* must be either YYYY-MM-DD or
* YYYY-MM-DDTHH:MM:SS (note the T delimiter)
*
* @return A DATASET(FileInfoRec) containing the information. Note that
* both files and superfiles are gathered.
*
* @see GetInfoForFilesMatchingPatterns
*/
SHARED GetAllSubFiles(DATASET(FileInfoRec) fileInfoList,
STRING dali,
BOOLEAN disableContentCheck,
STRING asOfDate) := FUNCTION
// Loop that gathers information on the immediate children of superfiles;
// note that the ds argument will contain only superfiles
GetImmediateChildren(DATASET(SuperfileRelationshipRec) ds, UNSIGNED2 c) := FUNCTION
EmbeddedSubFileRec := RECORD
UNSIGNED2 level;
STRING superFilePath;
DATASET(Std.File.FsLogicalFileNameRecord) subFileNames;
END;
dedupedDS := DEDUP(SORT(ds, subFilePath), subFilePath);
// For each superfile, immediate children are stored in a child
// recordset
embeddedSubFileNames := PROJECT
(
dedupedDS,
TRANSFORM
(
EmbeddedSubFileRec,
SELF.level := c + 1,
SELF.superFilePath := LEFT.subFilePath,
SELF.subFileNames := Std.File.SuperFileContents(EnsureForeignPath(LEFT.subFilePath, dali), FALSE),
SELF := LEFT
)
);
FlatSubFileRec := RECORD
UNSIGNED2 level;
STRING superFilePath;
Std.File.FsLogicalFileNameRecord;
END;
// Flatten the results and remove the foreign path prefix (if any)
// from the name
flattenedSubFileNames := NORMALIZE
(
embeddedSubFileNames,
LEFT.subFileNames,
TRANSFORM
(
FlatSubFileRec,
SELF.name := REGEXREPLACE('^foreign::.+?::', RIGHT.name, ''),
SELF := LEFT
)
);
SubFileInfoRec := RECORD
UNSIGNED2 level;
STRING superFilePath;
DATASET(FileInfoRec) subFileInfo;
END;
// Collect the detailed information for each child path; note that
// are treating the full name as a "pattern" here
subFileInfoResults := PROJECT
(
flattenedSubFileNames,
TRANSFORM
(
SubFileInfoRec,
SELF.subFileInfo := GetInfoForFilesMatchingPatterns(dali, [LEFT.name], disableContentCheck, asOfDate),
SELF := LEFT
)
);
// Flatten those results and bang them into the same data structure
// as our ds argument
flatContentResults := NORMALIZE
(
subFileInfoResults,
LEFT.subFileInfo,
TRANSFORM
(
SuperfileRelationshipRec,
SELF.superFilePath := LEFT.superFilePath,
SELF.subFilePath := RIGHT.name,
SELF.subFileIsSuperfile := RIGHT.superfile,
SELF.level := LEFT.level
)
);
RETURN ds + flatContentResults;
END;
// Convert initial data into a structure our loop can use
initialDS := PROJECT
(
fileInfoList(superfile = TRUE),
TRANSFORM
(
SuperfileRelationshipRec,
SELF.superFilePath := '',
SELF.subFilePath := LEFT.name,
SELF.subFileIsSuperfile := LEFT.superfile,
SELF.level := 1
)
);
// Iteratively gather immediate children
loopResults := LOOP
(
initialDS,
LEFT.subFileIsSuperfile = TRUE AND LEFT.level = COUNTER,
GetImmediateChildren(ROWS(LEFT), COUNTER)
);
// Remove those results that have empty superfile paths (those from
// the initial loop dataset) and dedup the results
dedupedResults := DEDUP(SORT(loopResults(superFilePath != ''), superFilePath, subFilePath), superFilePath, subFilePath);
RETURN dedupedResults;
END;
/**
* Gather all information files referenced by the given Dali and matching
* at least one of the given filename patterns. If superfiles are
* gathered, their subfiles will also be (recursively) gathered. Note that
* gathered subfiles may not necessarily match any of the given filename
* patterns.
*
* @param dali The IP address of the Dali to check
* for files
* @param patterns A set of filename patterns to match
* @param disableContentCheck If TRUE do no collect format or
* content CRC values
* @param asOfDate A date or datetime representing a cutoff;
* files modified prior to this value are
* ignored at both the remote and local
* clusters; use an empty string to denote
* no limit on date; if provided, the format
* must be either YYYY-MM-DD or
* YYYY-MM-DDTHH:MM:SS (note the T delimiter)
*
* @return A MODULE containing file and superfile information
* within a DATASET(FileInfoRec) dataset, as well as
* superfile/subfile relationship data in a
* DATASET(SuperfileRelationshipRec) dataset.
*/
SHARED CollectFileInfoFromSystem(STRING dali,
SET OF STRING patterns,
BOOLEAN disableContentCheck,
STRING asOfDate) := FUNCTION
initialPatternResult := GetInfoForFilesMatchingPatterns(dali, patterns, disableContentCheck, asOfDate);
superSubResult := GetAllSubFiles(initialPatternResult, dali, disableContentCheck, asOfDate) : INDEPENDENT;
// We need to add file information for files found while gathering
// subfiles but were not included in the initial pattern match
unreportedSubFileNames := JOIN
(
superSubResult,
initialPatternResult,
LEFT.subFilePath = RIGHT.name,
TRANSFORM
(
{STRING subFilePath},
SELF.subFilePath := LEFT.subFilePath
),
LEFT ONLY
);
unreportedNameSet := NOTHOR(SET(unreportedSubFileNames, subFilePath));
unreportedSubFileInfo := GetInfoForFilesMatchingPatterns(dali, unreportedNameSet, disableContentCheck, asOfDate);
// Concatenate the additional files with the initial pattern result
allFileInfo := initialPatternResult + unreportedSubFileInfo : INDEPENDENT;
RETURN MODULE
EXPORT DATASET(FileInfoRec) files := allFileInfo;
EXPORT DATASET(SuperfileRelationshipRec) superFileRelationships := superSubResult;
END;
END;
/**
* Given file information gathered from both remote and local systems,
* create a single dataset describing the changes that need to be made to
* the local system in order to make the local system a mirror of the
* remote system.
*
* @param remoteFiles Dataset containing remote file information
* @param localFiles Dataset containing local file information
* @param doublecheckAdd If TRUE, perform a second check of the
* files that will be added to the local
* system to ensure they do not already exist;
* the check is by filename only, not contents;
* if FALSE, all such files will be copied
*
* @return DATASET(FileActionRec) containing information that inform
* actions to be taken.
*
* @see GenerateSuperFileActions
*/
SHARED GenerateFileActions(DATASET(FileInfoRec) remoteFiles, DATASET(FileInfoRec) localFiles, BOOLEAN doublecheckAdd) := FUNCTION
// Files and superfiles that exist only on the remote system; these
// will be added to the local system
onlyRemote0 := JOIN
(
remoteFiles,
localFiles,
LEFT.name = RIGHT.name,
TRANSFORM
(
FileActionRec,
SELF.path := LEFT.name,
SELF.sourceCluster := Std.Str.SplitWords(LEFT.cluster, ',')[1],
SELF.isSuperFile := LEFT.superfile,
SELF.syncAction := SYNC_ACTION.ADD
),
LEFT ONLY
);
onlyRemote := IF
(
doublecheckAdd,
onlyRemote0(isSuperFile OR (NOT Std.File.FileExists(AbsPath(path)))),
onlyRemote0
);
// Files and superfiles that exist only on the local system; these will
// be deleted from the local system
onlyLocal := JOIN
(
remoteFiles,
localFiles,
LEFT.name = RIGHT.name,
TRANSFORM
(
FileActionRec,
SELF.path := RIGHT.name,
SELF.sourceCluster := '',
SELF.isSuperFile := RIGHT.superfile,
SELF.syncAction := SYNC_ACTION.DELETE_FILE
),
RIGHT ONLY
);
// Regular files that exist on both systems that also appear to have
// different content; these will need to be copied from the remote
// system
common := JOIN
(
remoteFiles,
localFiles,
LEFT.name = RIGHT.name
AND (LEFT.size != RIGHT.size OR (LEFT.fileCRC != '' AND LEFT.fileCRC != RIGHT.fileCRC) OR (LEFT.formatCRC != '' AND LEFT.formatCRC != RIGHT.formatCRC))
AND LEFT.superfile = FALSE
AND RIGHT.superfile = FALSE,
TRANSFORM
(
FileActionRec,
SELF.path := LEFT.name,
SELF.sourceCluster := Std.Str.SplitWords(LEFT.cluster, ',')[1],
SELF.isSuperFile := LEFT.superfile,
SELF.syncAction := SYNC_ACTION.MODIFY
)
);
// Find cases where a particular path references a regular file on the
// remote system and a superfile on the local system; these files
// need to have two actions, one to copy the regular remote file and
// the second to delete the local superfile
commonSuperBecomesRegular1 := JOIN
(
remoteFiles,
localFiles,
LEFT.name = RIGHT.name
AND LEFT.superfile = FALSE
AND RIGHT.superfile = TRUE,
TRANSFORM
(
FileActionRec,
SELF.path := LEFT.name,
SELF.sourceCluster := Std.Str.SplitWords(LEFT.cluster, ',')[1],
SELF.isSuperFile := FALSE,
SELF.syncAction := SYNC_ACTION.ADD
)
);
commonSuperBecomesRegular2 := PROJECT
(
commonSuperBecomesRegular1,
TRANSFORM
(
RECORDOF(LEFT),
SELF.syncAction := SYNC_ACTION.DELETE_SUPERFILE,
SELF.isSuperFile := TRUE,
SELF := LEFT
)
);
// Find cases where a particular path references a superfile on the
// remote system and a regular file on the local system; these files
// need to have two actions, one to delete the regular local file and
// the second to add a local superfile
commonRegularBecomesSuper1 := JOIN
(
remoteFiles,
localFiles,
LEFT.name = RIGHT.name
AND LEFT.superfile = TRUE
AND RIGHT.superfile = FALSE,
TRANSFORM
(
FileActionRec,
SELF.path := LEFT.name,
SELF.sourceCluster := '',
SELF.isSuperFile := FALSE,
SELF.syncAction := SYNC_ACTION.DELETE_FILE
)
);
commonRegularBecomesSuper2 := PROJECT
(
commonRegularBecomesSuper1,
TRANSFORM
(
RECORDOF(LEFT),
SELF.syncAction := SYNC_ACTION.ADD,
SELF.isSuperFile := TRUE,
SELF := LEFT
)
);
RETURN onlyRemote + onlyLocal + common + commonSuperBecomesRegular1 + commonSuperBecomesRegular2 + commonRegularBecomesSuper1 + commonRegularBecomesSuper2;
END;
/**
* Given superfile relationship information gathered from both remote and
* local systems, create a single dataset describing the changes that need
* to be made to the local system in order to make the local system a
* mirror of the remote system.
*
* @param remoteFiles Dataset containing remote superfile information
* @param localFiles Dataset containing local superfile information
* @param fileActions A dataset describing the changes that will be
* applied at the file level, which is needed
* in order to find files that will be modified
* that are part of a superfile/subfile
* relationship (those files need to removed from
* their superfile prior to copy, then restored
* afterwards); OPTIONAL, defaults to an empty
* dataset
*
* @return DATASET(SuperFileActionRec) containing information that inform
* actions to be taken.
*
* @see GenerateFileActions
*/
SHARED GenerateSuperFileActions(DATASET(SuperfileRelationshipRec) remoteFiles,
DATASET(SuperfileRelationshipRec) localFiles,
DATASET(FileActionRec) fileActions = DATASET([], FileActionRec)) := FUNCTION
// Superfile/subfile relationships that exist only on the remote
// system; these will need to be instantiated on the local system
onlyRemote := JOIN
(
remoteFiles,
localFiles,
LEFT.superFilePath = RIGHT.superFilePath
AND LEFT.subFilePath = RIGHT.subFilePath,
TRANSFORM
(
SuperFileActionRec,
SELF.syncAction := SYNC_ACTION.ADD,
SELF := LEFT
),
LEFT ONLY
);
// Superfile/subfile relationships that exist only on the local
// system; these will need to be unlinked on the local system
onlyLocal := JOIN
(
remoteFiles,
localFiles,
LEFT.superFilePath = RIGHT.superFilePath
AND LEFT.subFilePath = RIGHT.subFilePath,
TRANSFORM
(
SuperFileActionRec,
SELF.syncAction := SYNC_ACTION.DELETE_FILE,
SELF := RIGHT
),
RIGHT ONLY
);
// Files that will be replaced that are part of a superfile
// relationship must be removed from the relationship before the
// copy, then added back after the copy
commonFiles := JOIN
(
remoteFiles,
localFiles,
LEFT.superFilePath = RIGHT.superFilePath
AND LEFT.subFilePath = RIGHT.subFilePath,
TRANSFORM
(
SuperFileActionRec,
SELF.syncAction := SYNC_ACTION.UNKNOWN, // Will be overridden
SELF := LEFT
)
);
toBeModifiedDelete := JOIN
(
commonFiles,
fileActions,
LEFT.subFilePath = RIGHT.path
AND RIGHT.syncAction = SYNC_ACTION.MODIFY,
TRANSFORM
(
SuperFileActionRec,
SELF.syncAction := SYNC_ACTION.DELETE_FILE,
SELF := LEFT
)
);
toBeModifiedAdd := PROJECT
(
toBeModifiedDelete,
TRANSFORM
(
RECORDOF(LEFT),
SELF.syncAction := SYNC_ACTION.ADD,
SELF := LEFT
)
);
allResults := onlyRemote + onlyLocal + toBeModifiedDelete + toBeModifiedAdd;
dedupedResults := DEDUP(SORT(allResults, superFilePath, subFilePath, syncAction), superFilePath, subFilePath, syncAction);
RETURN dedupedResults;
END;
/**
* Collect and analyze information from both remote and local systems,
* determine what changes are needed to make the local system mirror the
* remote system, and provide summarized results of those changes.
*
* @param dali The IP address of the remote Dali to check
* @param patterns A set of filename patterns to match;
* OPTIONAL, defaults to ['*'] which indicates
* all files
* @param disableContentCheck If TRUE, don't use a file's format or
* content CRC values to help determine
* if a file that already exists at the
* destination has been modified (the
* assumption will be that the file has NOT
* been modified); disabling this check can
* speed up the scanning operation, but source
* files that have been overwritten with new
* contents may not be copied; OPTIONAL,
* defaults to FALSE
* @param asOfDate A date or datetime representing a cutoff;
* files modified prior to this value are
* ignored at both the remote and local
* clusters; use an empty string to denote
* no limit on date; if provided, the format
* must be either YYYY-MM-DD or
* YYYY-MM-DDTHH:MM:SS (note the T delimiter);
* OPTIONAL, defaults to an empty string
*
* @return MODULE containing multiple attributes:
* engine The HPCC engine that is
* currently being used
* remoteFiles Remote files matching the
* filename patterns (plus any
* subfiles)
* remoteSuperFileRelationships Superfile/subfile relationships
* found on the remote system
* localFiles Local files matching the
* filename patterns (plus any
* subfiles)
* localSuperFileRelationships Superfile/subfile relationships
* found on the local system
* fileActions Dataset containing indicators
* of the actions that need to
* be taken on the local system
* for files
* fileActionSummary Human-readable summary of
* fileActions
* superFileActions Dataset containing indicators
* of the actions that need to
* be taken on the local system
* for superfile/subfile
* management
* superFileActionSummary Human-readable summary of
* superFileActions
*
* @see Go
*/
EXPORT CollectFileInfo(STRING dali,
SET OF STRING patterns = DEFAULT_FILENAME_PATTERNS,
BOOLEAN disableContentCheck = FALSE,
STRING asOfDate = '') := FUNCTION
remoteResults := CollectFileInfoFromSystem(dali, patterns, disableContentCheck, asOfDate);
localResults := CollectFileInfoFromSystem(Std.System.Thorlib.DaliServer(), patterns, disableContentCheck, asOfDate);
fileActionResults := GenerateFileActions(remoteResults.files, localResults.files, disableContentCheck);
fileActionSummaryStats := TABLE
(
fileActionResults,
{
syncAction,
UNSIGNED4 numFiles := COUNT(GROUP)
},
syncAction
);
fileActionSummary := PROJECT
(
fileActionSummaryStats,
TRANSFORM
(
ActionSummaryRec,
SELF.actionDescription := CASE
(
LEFT.syncAction,
SYNC_ACTION.ADD => 'Copy new file from remote (new)',
SYNC_ACTION.MODIFY => 'Copy modified file from remote',
SYNC_ACTION.DELETE_FILE => 'Delete local file',
SYNC_ACTION.DELETE_SUPERFILE => 'Delete local superfile',
ERROR('Unable to determine file action')
),
SELF.fileCount := LEFT.numFiles
)
);
superFileActionResults := GenerateSuperFileActions(remoteResults.superFileRelationships, localResults.superFileRelationships, fileActionResults);
superFileActionSummaryStats := TABLE
(
superFileActionResults,
{
syncAction,
UNSIGNED4 numFiles := COUNT(GROUP)
},
syncAction
);
superFileActionSummary := PROJECT
(
superFileActionSummaryStats,
TRANSFORM
(
ActionSummaryRec,
SELF.actionDescription := CASE
(
LEFT.syncAction,
SYNC_ACTION.ADD => 'Create new local superfile',
SYNC_ACTION.DELETE_SUPERFILE => 'Delete local superfile',
ERROR('Unable to determine superfile action')
),
SELF.fileCount := LEFT.numFiles
)
);
executionEngine := Std.Str.ToUpperCase(Std.System.Job.Platform());
RETURN MODULE
EXPORT STRING engine := IF(executionEngine = 'HTHOR', executionEngine, ERROR('This code must be run on hthor'));
EXPORT DATASET(FileInfoRec) remoteFiles := remoteResults.files;
EXPORT DATASET(SuperfileRelationshipRec) remoteSuperFileRelationships := remoteResults.superFileRelationships;
EXPORT DATASET(FileInfoRec) localFiles := localResults.files;
EXPORT DATASET(SuperfileRelationshipRec) localSuperFileRelationships := localResults.superFileRelationships;
EXPORT DATASET(FileActionRec) fileActions := fileActionResults;
EXPORT DATASET(ActionSummaryRec) fileActionSummary := fileActionSummary;
EXPORT DATASET(SuperFileActionRec) superFileActions := superFileActionResults;
EXPORT DATASET(ActionSummaryRec) superFileActionSummary := superFileActionSummary;
END;
END;
/**
* Using file analytics data provided by CollectFileInfo(), either
* execute the actions necessary to make the local system mirror the remote
* system or just output the commands that will be executed for review
* purposes.
*
* @param dali The IP address of the remote Dali
* @param patterns Set of filename patterns that will be used
* to gather files to analyze; OPTIONAL,
* defaults to ['*'] which indicates all files
* @param clusterMap DATASET(ClusterMapRec) containing remote
* and local cluster names; this is used to
* ensure that a file on a remote cluster
* is copied to the correct cluster on the
* local system (e.g. 'thor' versus 'mythor');
* if no mapping is provided, a remote file
* on a given cluster (e.g. 'thor') will be
* copied to a local cluster of the same
* name (e.g. 'thor); OPTIONAL, defaults to an
* empty dataset
* @param forceCompression If TRUE, all copied files will be created
* compressed; if TRUE then the destination
* file will be compressed only if the source
* file is compressed; OPTIONAL, defaults
* to FALSE
* @param disableContentCheck If TRUE, don't use a file's format or
* content CRC values to help determine
* if a file that already exists at the
* destination has been modified (the
* assumption will be that the file has NOT
* been modified); disabling this check can
* speed up the scanning operation, but source
* files that have been overwritten with new
* contents may not be copied; OPTIONAL,
* defaults to FALSE
* @param enableNoSplit If TRUE, files are copied to a single
* destination node rather than spread across
* all nodes of the cluster; OPTIONAL, defaults
* to FALSE
* @param isDryRun If TRUE, only information about the analysis
* and commands that would be executed are
* shown as results; if FALSE then the
* commands are also executed; OPTIONAL,
* defaults to TRUE for safety
* @param debugOutput If TRUE, emit datasets representing the
* internal state of the collection and
* analysis; OPTIONAL, defaults to FALSE
* @param asOfDate A date or datetime representing a cutoff;
* files modified prior to this value are
* ignored at both the remote and local
* clusters; use an empty string to denote
* no limit on date; if provided, the format
* must be either YYYY-MM-DD or
* YYYY-MM-DDTHH:MM:SS (note the T delimiter);
* OPTIONAL, defaults to an empty string
*
* @return An action that performs the analysis and, if isDryRun is TRUE,
* also performs the actions required to bring the data into sync
*
* @see CollectFileInfo
*/
EXPORT Go(STRING dali,
SET OF STRING patterns = DEFAULT_FILENAME_PATTERNS,
DATASET(ClusterMapRec) clusterMap = DATASET([], ClusterMapRec),
BOOLEAN forceCompression = FALSE,
BOOLEAN disableContentCheck = FALSE,
BOOLEAN enableNoSplit = FALSE,
BOOLEAN isDryRun = TRUE,
BOOLEAN debugOutput = FALSE,
STRING asOfDate = '') := FUNCTION
clusterMapDict := DICTIONARY(clusterMap, {remoteCluster => localCluster});
MappedCluster(STRING clusterName) := FUNCTION
newName := clusterMapDict[clusterName].localCluster;
RETURN IF(newName != '', newName, clusterName);
END;
ActionDescRec := RECORD
STRING cmd;
END;
actionCountLabel := IF(isDryRun, 'DryRun', '') + 'ActionCount';
actionLabel := IF(isDryRun, 'DryRun', '') + 'Actions';
// Make sure asOfDate is correctly formatted
validatedAsOfDate := IF(asOfDate = '' OR REGEXFIND('^\\d{4}-\\d{2}-\\d{2}(?:T\\d{2}:\\d{2}:\\d{2})?$', asOfDate), asOfDate, ERROR('asOfDate incorrectly formatted'));
info := CollectFileInfo(dali, patterns, disableContentCheck, validatedAsOfDate);
// Remove local superfile relations
removeLocalSuperFileRelations := GLOBAL(info.superFileActions(syncAction = SYNC_ACTION.DELETE_FILE), FEW);
removeLocalSuperFileRelationsDryRun := PROJECT
(
removeLocalSuperFileRelations,
TRANSFORM
(
ActionDescRec,
SELF.cmd := 'Std.File.RemoveSuperFile(' + QuotedAbsPath(LEFT.superFilePath) + ', ' + QuotedAbsPath(LEFT.subFilePath) + ');'
)
);
removeLocalSuperFileRelationsAction := PARALLEL
(
OUTPUT(removeLocalSuperFileRelationsDryRun, NAMED(actionLabel), ALL, EXTEND);
IF(~isDryRun, NOTHOR(APPLY(removeLocalSuperFileRelations, Std.File.RemoveSuperFile(AbsPath(superFilePath), AbsPath(subFilePath)))));
);
// Delete unneeded local superfiles
removeLocalUneededSuperFiles := GLOBAL(info.fileActions(syncAction = SYNC_ACTION.DELETE_SUPERFILE), FEW);
removeLocalUneededSuperFilesDryRun := PROJECT
(
removeLocalUneededSuperFiles,
TRANSFORM
(
ActionDescRec,
SELF.cmd := 'Std.File.DeleteSuperFile(' + QuotedAbsPath(LEFT.path) + ');'
)
);
removeLocalUneededSuperFilesAction := PARALLEL
(
OUTPUT(removeLocalUneededSuperFilesDryRun, NAMED(actionLabel), ALL, EXTEND);
IF(~isDryRun, NOTHOR(APPLY(removeLocalUneededSuperFiles, Std.File.DeleteSuperFile(AbsPath(path)))));
);
// Delete unneeded local files
removeLocalUneededFiles := GLOBAL(info.fileActions(~isSuperFile AND syncAction = SYNC_ACTION.DELETE_FILE), FEW);
removeLocalUneededFilesDryRun := PROJECT
(
removeLocalUneededFiles,
TRANSFORM
(
ActionDescRec,
SELF.cmd := 'Std.File.DeleteLogicalFile(' + QuotedAbsPath(LEFT.path) + ');'
)
);